]>
Commit | Line | Data |
---|---|---|
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 | ||
019adbd3 | 16 | #include "standard-headers/linux/virtio_ids.h" |
0d09e41a | 17 | #include "hw/virtio/virtio-scsi.h" |
b4a42f81 | 18 | #include "qemu/error-report.h" |
36b15c79 | 19 | #include "qemu/iov.h" |
4be74634 | 20 | #include "sysemu/block-backend.h" |
0d09e41a PB |
21 | #include <hw/scsi/scsi.h> |
22 | #include <block/scsi.h> | |
23 | #include <hw/virtio/virtio-bus.h> | |
8c085dbe | 24 | #include "hw/virtio/virtio-access.h" |
dfb37cf7 | 25 | #include "migration/migration.h" |
973abc7f | 26 | |
2ccdcd8d PB |
27 | static inline int virtio_scsi_get_lun(uint8_t *lun) |
28 | { | |
29 | return ((lun[2] << 8) | lun[3]) & 0x3FFF; | |
30 | } | |
31 | ||
32 | static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun) | |
33 | { | |
34 | if (lun[0] != 1) { | |
35 | return NULL; | |
36 | } | |
37 | if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) { | |
38 | return NULL; | |
39 | } | |
40 | return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); | |
41 | } | |
42 | ||
c505333d | 43 | VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq) |
36b15c79 PB |
44 | { |
45 | VirtIOSCSIReq *req; | |
faf1e1fb FZ |
46 | VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s; |
47 | const size_t zero_skip = offsetof(VirtIOSCSIReq, elem) | |
48 | + sizeof(VirtQueueElement); | |
36b15c79 | 49 | |
faf1e1fb | 50 | req = g_slice_alloc(sizeof(*req) + vs->cdb_size); |
36b15c79 PB |
51 | req->vq = vq; |
52 | req->dev = s; | |
36b15c79 | 53 | qemu_sglist_init(&req->qsgl, DEVICE(s), 8, &address_space_memory); |
3eff1f46 | 54 | qemu_iovec_init(&req->resp_iov, 1); |
faf1e1fb | 55 | memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip); |
36b15c79 PB |
56 | return req; |
57 | } | |
58 | ||
aa8e8f83 | 59 | void virtio_scsi_free_req(VirtIOSCSIReq *req) |
36b15c79 | 60 | { |
faf1e1fb FZ |
61 | VirtIOSCSICommon *vs = (VirtIOSCSICommon *)req->dev; |
62 | ||
3eff1f46 | 63 | qemu_iovec_destroy(&req->resp_iov); |
36b15c79 | 64 | qemu_sglist_destroy(&req->qsgl); |
faf1e1fb | 65 | g_slice_free1(sizeof(*req) + vs->cdb_size, req); |
36b15c79 PB |
66 | } |
67 | ||
326799c0 SH |
68 | static void virtio_scsi_complete_req(VirtIOSCSIReq *req) |
69 | { | |
70 | VirtIOSCSI *s = req->dev; | |
71 | VirtQueue *vq = req->vq; | |
0ac8e139 | 72 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
3eff1f46 PB |
73 | |
74 | qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size); | |
63c7e542 FZ |
75 | if (req->vring) { |
76 | assert(req->vq == NULL); | |
77 | virtio_scsi_vring_push_notify(req); | |
78 | } else { | |
79 | virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size); | |
80 | virtio_notify(vdev, vq); | |
81 | } | |
82 | ||
326799c0 SH |
83 | if (req->sreq) { |
84 | req->sreq->hba_private = NULL; | |
85 | scsi_req_unref(req->sreq); | |
86 | } | |
36b15c79 | 87 | virtio_scsi_free_req(req); |
326799c0 SH |
88 | } |
89 | ||
90 | static void virtio_scsi_bad_req(void) | |
91 | { | |
92 | error_report("wrong size for virtio-scsi headers"); | |
93 | exit(1); | |
94 | } | |
95 | ||
57fbae6e PB |
96 | static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov, |
97 | hwaddr *addr, int num, size_t skip) | |
326799c0 | 98 | { |
f487b677 | 99 | QEMUSGList *qsgl = &req->qsgl; |
57fbae6e PB |
100 | size_t copied = 0; |
101 | ||
102 | while (num) { | |
103 | if (skip >= iov->iov_len) { | |
104 | skip -= iov->iov_len; | |
105 | } else { | |
106 | qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip); | |
107 | copied += iov->iov_len - skip; | |
108 | skip = 0; | |
109 | } | |
110 | iov++; | |
111 | addr++; | |
112 | num--; | |
326799c0 | 113 | } |
57fbae6e PB |
114 | |
115 | assert(skip == 0); | |
116 | return copied; | |
326799c0 SH |
117 | } |
118 | ||
36b15c79 PB |
119 | static int virtio_scsi_parse_req(VirtIOSCSIReq *req, |
120 | unsigned req_size, unsigned resp_size) | |
326799c0 | 121 | { |
55783a55 | 122 | VirtIODevice *vdev = (VirtIODevice *) req->dev; |
3eff1f46 | 123 | size_t in_size, out_size; |
36b15c79 | 124 | |
3eff1f46 PB |
125 | if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, |
126 | &req->req, req_size) < req_size) { | |
36b15c79 PB |
127 | return -EINVAL; |
128 | } | |
36b15c79 | 129 | |
3eff1f46 PB |
130 | if (qemu_iovec_concat_iov(&req->resp_iov, |
131 | req->elem.in_sg, req->elem.in_num, 0, | |
132 | resp_size) < resp_size) { | |
36b15c79 PB |
133 | return -EINVAL; |
134 | } | |
55783a55 | 135 | |
489c7901 | 136 | req->resp_size = resp_size; |
326799c0 | 137 | |
55783a55 PB |
138 | /* Old BIOSes left some padding by mistake after the req_size/resp_size. |
139 | * As a workaround, always consider the first buffer as the virtio-scsi | |
140 | * request/response, making the payload start at the second element | |
141 | * of the iovec. | |
142 | * | |
143 | * The actual length of the response header, stored in req->resp_size, | |
144 | * does not change. | |
145 | * | |
146 | * TODO: always disable this workaround for virtio 1.0 devices. | |
147 | */ | |
ef546f12 | 148 | if (!virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) { |
55783a55 PB |
149 | req_size = req->elem.out_sg[0].iov_len; |
150 | resp_size = req->elem.in_sg[0].iov_len; | |
151 | } | |
152 | ||
3eff1f46 PB |
153 | out_size = qemu_sgl_concat(req, req->elem.out_sg, |
154 | &req->elem.out_addr[0], req->elem.out_num, | |
155 | req_size); | |
156 | in_size = qemu_sgl_concat(req, req->elem.in_sg, | |
157 | &req->elem.in_addr[0], req->elem.in_num, | |
158 | resp_size); | |
159 | ||
160 | if (out_size && in_size) { | |
161 | return -ENOTSUP; | |
162 | } | |
163 | ||
164 | if (out_size) { | |
165 | req->mode = SCSI_XFER_TO_DEV; | |
166 | } else if (in_size) { | |
167 | req->mode = SCSI_XFER_FROM_DEV; | |
326799c0 | 168 | } |
36b15c79 PB |
169 | |
170 | return 0; | |
326799c0 SH |
171 | } |
172 | ||
173 | static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq) | |
174 | { | |
36b15c79 | 175 | VirtIOSCSIReq *req = virtio_scsi_init_req(s, vq); |
326799c0 | 176 | if (!virtqueue_pop(vq, &req->elem)) { |
36b15c79 | 177 | virtio_scsi_free_req(req); |
326799c0 SH |
178 | return NULL; |
179 | } | |
326799c0 SH |
180 | return req; |
181 | } | |
182 | ||
5db1764c PB |
183 | static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq) |
184 | { | |
185 | VirtIOSCSIReq *req = sreq->hba_private; | |
292c8e50 | 186 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev); |
d2ad7dd4 | 187 | uint32_t n = virtio_queue_get_id(req->vq) - 2; |
5db1764c | 188 | |
292c8e50 | 189 | assert(n < vs->conf.num_queues); |
fcf104a7 | 190 | qemu_put_be32s(f, &n); |
5db1764c PB |
191 | qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); |
192 | } | |
193 | ||
194 | static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) | |
195 | { | |
196 | SCSIBus *bus = sreq->bus; | |
197 | VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); | |
292c8e50 | 198 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); |
5db1764c | 199 | VirtIOSCSIReq *req; |
fcf104a7 | 200 | uint32_t n; |
5db1764c | 201 | |
fcf104a7 | 202 | qemu_get_be32s(f, &n); |
292c8e50 | 203 | assert(n < vs->conf.num_queues); |
36b15c79 | 204 | req = virtio_scsi_init_req(s, vs->cmd_vqs[n]); |
5db1764c | 205 | qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); |
3c3ce981 MT |
206 | /* TODO: add a way for SCSIBusInfo's load_request to fail, |
207 | * and fail migration instead of asserting here. | |
208 | * When we do, we might be able to re-enable NDEBUG below. | |
209 | */ | |
210 | #ifdef NDEBUG | |
211 | #error building with NDEBUG is not supported | |
212 | #endif | |
213 | assert(req->elem.in_num <= ARRAY_SIZE(req->elem.in_sg)); | |
214 | assert(req->elem.out_num <= ARRAY_SIZE(req->elem.out_sg)); | |
36b15c79 PB |
215 | |
216 | if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, | |
217 | sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) { | |
218 | error_report("invalid SCSI request migration data"); | |
219 | exit(1); | |
220 | } | |
5db1764c PB |
221 | |
222 | scsi_req_ref(sreq); | |
223 | req->sreq = sreq; | |
224 | if (req->sreq->cmd.mode != SCSI_XFER_NONE) { | |
3eff1f46 | 225 | assert(req->sreq->cmd.mode == req->mode); |
5db1764c PB |
226 | } |
227 | return req; | |
228 | } | |
229 | ||
49e7e31a FZ |
230 | typedef struct { |
231 | Notifier notifier; | |
232 | VirtIOSCSIReq *tmf_req; | |
233 | } VirtIOSCSICancelNotifier; | |
234 | ||
235 | static void virtio_scsi_cancel_notify(Notifier *notifier, void *data) | |
236 | { | |
237 | VirtIOSCSICancelNotifier *n = container_of(notifier, | |
238 | VirtIOSCSICancelNotifier, | |
239 | notifier); | |
240 | ||
241 | if (--n->tmf_req->remaining == 0) { | |
242 | virtio_scsi_complete_req(n->tmf_req); | |
243 | } | |
244 | g_slice_free(VirtIOSCSICancelNotifier, n); | |
245 | } | |
246 | ||
247 | /* Return 0 if the request is ready to be completed and return to guest; | |
248 | * -EINPROGRESS if the request is submitted and will be completed later, in the | |
249 | * case of async cancellation. */ | |
250 | static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) | |
326799c0 | 251 | { |
3eff1f46 | 252 | SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun); |
06114d72 | 253 | SCSIRequest *r, *next; |
0866aca1 | 254 | BusChild *kid; |
06114d72 | 255 | int target; |
49e7e31a | 256 | int ret = 0; |
06114d72 | 257 | |
05430559 FZ |
258 | if (s->dataplane_started) { |
259 | assert(blk_get_aio_context(d->conf.blk) == s->ctx); | |
63c7e542 | 260 | } |
06114d72 | 261 | /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */ |
3eff1f46 | 262 | req->resp.tmf.response = VIRTIO_SCSI_S_OK; |
06114d72 | 263 | |
8c085dbe | 264 | virtio_tswap32s(VIRTIO_DEVICE(s), &req->req.tmf.subtype); |
3eff1f46 | 265 | switch (req->req.tmf.subtype) { |
06114d72 PB |
266 | case VIRTIO_SCSI_T_TMF_ABORT_TASK: |
267 | case VIRTIO_SCSI_T_TMF_QUERY_TASK: | |
268 | if (!d) { | |
269 | goto fail; | |
270 | } | |
3eff1f46 | 271 | if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { |
06114d72 PB |
272 | goto incorrect_lun; |
273 | } | |
274 | QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { | |
4dd7c82c | 275 | VirtIOSCSIReq *cmd_req = r->hba_private; |
3eff1f46 | 276 | if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) { |
06114d72 PB |
277 | break; |
278 | } | |
279 | } | |
4dd7c82c PB |
280 | if (r) { |
281 | /* | |
282 | * Assert that the request has not been completed yet, we | |
283 | * check for it in the loop above. | |
284 | */ | |
285 | assert(r->hba_private); | |
3eff1f46 | 286 | if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) { |
06114d72 PB |
287 | /* "If the specified command is present in the task set, then |
288 | * return a service response set to FUNCTION SUCCEEDED". | |
289 | */ | |
3eff1f46 | 290 | req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; |
06114d72 | 291 | } else { |
49e7e31a FZ |
292 | VirtIOSCSICancelNotifier *notifier; |
293 | ||
294 | req->remaining = 1; | |
295 | notifier = g_slice_new(VirtIOSCSICancelNotifier); | |
296 | notifier->tmf_req = req; | |
297 | notifier->notifier.notify = virtio_scsi_cancel_notify; | |
298 | scsi_req_cancel_async(r, ¬ifier->notifier); | |
299 | ret = -EINPROGRESS; | |
06114d72 PB |
300 | } |
301 | } | |
302 | break; | |
303 | ||
304 | case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET: | |
305 | if (!d) { | |
306 | goto fail; | |
307 | } | |
3eff1f46 | 308 | if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { |
06114d72 PB |
309 | goto incorrect_lun; |
310 | } | |
311 | s->resetting++; | |
312 | qdev_reset_all(&d->qdev); | |
313 | s->resetting--; | |
314 | break; | |
315 | ||
316 | case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET: | |
317 | case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET: | |
318 | case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET: | |
319 | if (!d) { | |
320 | goto fail; | |
321 | } | |
3eff1f46 | 322 | if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { |
06114d72 PB |
323 | goto incorrect_lun; |
324 | } | |
49e7e31a FZ |
325 | |
326 | /* Add 1 to "remaining" until virtio_scsi_do_tmf returns. | |
327 | * This way, if the bus starts calling back to the notifiers | |
328 | * even before we finish the loop, virtio_scsi_cancel_notify | |
329 | * will not complete the TMF too early. | |
330 | */ | |
331 | req->remaining = 1; | |
06114d72 PB |
332 | QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { |
333 | if (r->hba_private) { | |
3eff1f46 | 334 | if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) { |
06114d72 PB |
335 | /* "If there is any command present in the task set, then |
336 | * return a service response set to FUNCTION SUCCEEDED". | |
337 | */ | |
3eff1f46 | 338 | req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; |
06114d72 PB |
339 | break; |
340 | } else { | |
49e7e31a FZ |
341 | VirtIOSCSICancelNotifier *notifier; |
342 | ||
343 | req->remaining++; | |
344 | notifier = g_slice_new(VirtIOSCSICancelNotifier); | |
345 | notifier->notifier.notify = virtio_scsi_cancel_notify; | |
346 | notifier->tmf_req = req; | |
347 | scsi_req_cancel_async(r, ¬ifier->notifier); | |
06114d72 PB |
348 | } |
349 | } | |
350 | } | |
49e7e31a FZ |
351 | if (--req->remaining > 0) { |
352 | ret = -EINPROGRESS; | |
353 | } | |
06114d72 PB |
354 | break; |
355 | ||
356 | case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: | |
3eff1f46 | 357 | target = req->req.tmf.lun[1]; |
06114d72 | 358 | s->resetting++; |
0866aca1 AL |
359 | QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) { |
360 | d = DO_UPCAST(SCSIDevice, qdev, kid->child); | |
06114d72 PB |
361 | if (d->channel == 0 && d->id == target) { |
362 | qdev_reset_all(&d->qdev); | |
363 | } | |
364 | } | |
365 | s->resetting--; | |
366 | break; | |
367 | ||
368 | case VIRTIO_SCSI_T_TMF_CLEAR_ACA: | |
369 | default: | |
3eff1f46 | 370 | req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED; |
06114d72 | 371 | break; |
326799c0 SH |
372 | } |
373 | ||
49e7e31a | 374 | return ret; |
06114d72 PB |
375 | |
376 | incorrect_lun: | |
3eff1f46 | 377 | req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN; |
49e7e31a | 378 | return ret; |
06114d72 PB |
379 | |
380 | fail: | |
3eff1f46 | 381 | req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET; |
49e7e31a | 382 | return ret; |
326799c0 SH |
383 | } |
384 | ||
dc56b7c4 | 385 | void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req) |
973abc7f | 386 | { |
dc56b7c4 | 387 | VirtIODevice *vdev = (VirtIODevice *)s; |
024d9adc | 388 | uint32_t type; |
49e7e31a | 389 | int r = 0; |
326799c0 | 390 | |
dc56b7c4 FZ |
391 | if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, |
392 | &type, sizeof(type)) < sizeof(type)) { | |
393 | virtio_scsi_bad_req(); | |
394 | return; | |
395 | } | |
36b15c79 | 396 | |
024d9adc BW |
397 | virtio_tswap32s(vdev, &type); |
398 | if (type == VIRTIO_SCSI_T_TMF) { | |
dc56b7c4 FZ |
399 | if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq), |
400 | sizeof(VirtIOSCSICtrlTMFResp)) < 0) { | |
06114d72 | 401 | virtio_scsi_bad_req(); |
dc56b7c4 | 402 | } else { |
49e7e31a | 403 | r = virtio_scsi_do_tmf(s, req); |
b0b4ea17 | 404 | } |
06114d72 | 405 | |
024d9adc BW |
406 | } else if (type == VIRTIO_SCSI_T_AN_QUERY || |
407 | type == VIRTIO_SCSI_T_AN_SUBSCRIBE) { | |
dc56b7c4 FZ |
408 | if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq), |
409 | sizeof(VirtIOSCSICtrlANResp)) < 0) { | |
410 | virtio_scsi_bad_req(); | |
411 | } else { | |
412 | req->resp.an.event_actual = 0; | |
413 | req->resp.an.response = VIRTIO_SCSI_S_OK; | |
06114d72 | 414 | } |
dc56b7c4 | 415 | } |
49e7e31a FZ |
416 | if (r == 0) { |
417 | virtio_scsi_complete_req(req); | |
418 | } else { | |
419 | assert(r == -EINPROGRESS); | |
420 | } | |
dc56b7c4 FZ |
421 | } |
422 | ||
423 | static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) | |
424 | { | |
425 | VirtIOSCSI *s = (VirtIOSCSI *)vdev; | |
426 | VirtIOSCSIReq *req; | |
427 | ||
dfb37cf7 | 428 | if (s->ctx && !s->dataplane_disabled) { |
63c7e542 FZ |
429 | virtio_scsi_dataplane_start(s); |
430 | return; | |
431 | } | |
dc56b7c4 FZ |
432 | while ((req = virtio_scsi_pop_req(s, vq))) { |
433 | virtio_scsi_handle_ctrl_req(s, req); | |
326799c0 SH |
434 | } |
435 | } | |
436 | ||
a3de269c PB |
437 | static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req) |
438 | { | |
3eff1f46 PB |
439 | /* Sense data is not in req->resp and is copied separately |
440 | * in virtio_scsi_command_complete. | |
441 | */ | |
442 | req->resp_size = sizeof(VirtIOSCSICmdResp); | |
a3de269c PB |
443 | virtio_scsi_complete_req(req); |
444 | } | |
445 | ||
2ccdcd8d PB |
446 | static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status, |
447 | size_t resid) | |
448 | { | |
449 | VirtIOSCSIReq *req = r->hba_private; | |
489c7901 | 450 | uint8_t sense[SCSI_SENSE_BUF_SIZE]; |
474ee55a | 451 | uint32_t sense_len; |
8c085dbe | 452 | VirtIODevice *vdev = VIRTIO_DEVICE(req->dev); |
2ccdcd8d | 453 | |
e9c0f0f5 EF |
454 | if (r->io_canceled) { |
455 | return; | |
456 | } | |
457 | ||
3eff1f46 PB |
458 | req->resp.cmd.response = VIRTIO_SCSI_S_OK; |
459 | req->resp.cmd.status = status; | |
460 | if (req->resp.cmd.status == GOOD) { | |
8c085dbe | 461 | req->resp.cmd.resid = virtio_tswap32(vdev, resid); |
2ccdcd8d | 462 | } else { |
3eff1f46 | 463 | req->resp.cmd.resid = 0; |
489c7901 | 464 | sense_len = scsi_req_get_sense(r, sense, sizeof(sense)); |
3eff1f46 PB |
465 | sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd)); |
466 | qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd), | |
b7890c40 | 467 | sense, sense_len); |
8c085dbe | 468 | req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len); |
2ccdcd8d | 469 | } |
a3de269c | 470 | virtio_scsi_complete_cmd_req(req); |
2ccdcd8d PB |
471 | } |
472 | ||
33cbb2c5 PB |
473 | static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, |
474 | uint8_t *buf, void *hba_private) | |
475 | { | |
476 | VirtIOSCSIReq *req = hba_private; | |
477 | ||
478 | if (cmd->len == 0) { | |
03325525 | 479 | cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE); |
33cbb2c5 PB |
480 | memcpy(cmd->buf, buf, cmd->len); |
481 | } | |
482 | ||
483 | /* Extract the direction and mode directly from the request, for | |
484 | * host device passthrough. | |
485 | */ | |
486 | cmd->xfer = req->qsgl.size; | |
9df7bfdd | 487 | cmd->mode = req->mode; |
33cbb2c5 PB |
488 | return 0; |
489 | } | |
490 | ||
2ccdcd8d PB |
491 | static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r) |
492 | { | |
493 | VirtIOSCSIReq *req = r->hba_private; | |
494 | ||
495 | return &req->qsgl; | |
496 | } | |
497 | ||
498 | static void virtio_scsi_request_cancelled(SCSIRequest *r) | |
499 | { | |
500 | VirtIOSCSIReq *req = r->hba_private; | |
501 | ||
502 | if (!req) { | |
503 | return; | |
504 | } | |
06114d72 | 505 | if (req->dev->resetting) { |
3eff1f46 | 506 | req->resp.cmd.response = VIRTIO_SCSI_S_RESET; |
06114d72 | 507 | } else { |
3eff1f46 | 508 | req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED; |
06114d72 | 509 | } |
a3de269c | 510 | virtio_scsi_complete_cmd_req(req); |
2ccdcd8d PB |
511 | } |
512 | ||
513 | static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) | |
326799c0 | 514 | { |
3eff1f46 | 515 | req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE; |
a3de269c | 516 | virtio_scsi_complete_cmd_req(req); |
973abc7f SH |
517 | } |
518 | ||
359eea71 | 519 | bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req) |
bf359a44 FZ |
520 | { |
521 | VirtIOSCSICommon *vs = &s->parent_obj; | |
bf359a44 FZ |
522 | SCSIDevice *d; |
523 | int rc; | |
524 | ||
525 | rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, | |
526 | sizeof(VirtIOSCSICmdResp) + vs->sense_size); | |
527 | if (rc < 0) { | |
528 | if (rc == -ENOTSUP) { | |
529 | virtio_scsi_fail_cmd_req(req); | |
530 | } else { | |
531 | virtio_scsi_bad_req(); | |
532 | } | |
359eea71 | 533 | return false; |
bf359a44 FZ |
534 | } |
535 | ||
536 | d = virtio_scsi_device_find(s, req->req.cmd.lun); | |
537 | if (!d) { | |
538 | req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; | |
539 | virtio_scsi_complete_cmd_req(req); | |
359eea71 | 540 | return false; |
bf359a44 | 541 | } |
05430559 FZ |
542 | if (s->dataplane_started) { |
543 | assert(blk_get_aio_context(d->conf.blk) == s->ctx); | |
63c7e542 | 544 | } |
bf359a44 FZ |
545 | req->sreq = scsi_req_new(d, req->req.cmd.tag, |
546 | virtio_scsi_get_lun(req->req.cmd.lun), | |
bb72463b | 547 | req->req.cmd.cdb, req); |
bf359a44 FZ |
548 | |
549 | if (req->sreq->cmd.mode != SCSI_XFER_NONE | |
550 | && (req->sreq->cmd.mode != req->mode || | |
551 | req->sreq->cmd.xfer > req->qsgl.size)) { | |
552 | req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN; | |
553 | virtio_scsi_complete_cmd_req(req); | |
359eea71 | 554 | return false; |
bf359a44 | 555 | } |
5170f40b | 556 | scsi_req_ref(req->sreq); |
4be74634 | 557 | blk_io_plug(d->conf.blk); |
359eea71 FZ |
558 | return true; |
559 | } | |
bf359a44 | 560 | |
359eea71 FZ |
561 | void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req) |
562 | { | |
35e4e96c PB |
563 | SCSIRequest *sreq = req->sreq; |
564 | if (scsi_req_enqueue(sreq)) { | |
565 | scsi_req_continue(sreq); | |
bf359a44 | 566 | } |
4be74634 | 567 | blk_io_unplug(sreq->dev->conf.blk); |
35e4e96c | 568 | scsi_req_unref(sreq); |
bf359a44 FZ |
569 | } |
570 | ||
973abc7f SH |
571 | static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq) |
572 | { | |
292c8e50 | 573 | /* use non-QOM casts in the data path */ |
326799c0 | 574 | VirtIOSCSI *s = (VirtIOSCSI *)vdev; |
1880ad4f FZ |
575 | VirtIOSCSIReq *req, *next; |
576 | QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs); | |
326799c0 | 577 | |
dfb37cf7 | 578 | if (s->ctx && !s->dataplane_disabled) { |
63c7e542 FZ |
579 | virtio_scsi_dataplane_start(s); |
580 | return; | |
581 | } | |
326799c0 | 582 | while ((req = virtio_scsi_pop_req(s, vq))) { |
359eea71 | 583 | if (virtio_scsi_handle_cmd_req_prepare(s, req)) { |
1880ad4f | 584 | QTAILQ_INSERT_TAIL(&reqs, req, next); |
359eea71 | 585 | } |
326799c0 | 586 | } |
1880ad4f FZ |
587 | |
588 | QTAILQ_FOREACH_SAFE(req, &reqs, next, next) { | |
589 | virtio_scsi_handle_cmd_req_submit(s, req); | |
590 | } | |
973abc7f SH |
591 | } |
592 | ||
593 | static void virtio_scsi_get_config(VirtIODevice *vdev, | |
594 | uint8_t *config) | |
595 | { | |
596 | VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; | |
292c8e50 | 597 | VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev); |
973abc7f | 598 | |
8c085dbe RR |
599 | virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues); |
600 | virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2); | |
601 | virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors); | |
602 | virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun); | |
603 | virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent)); | |
604 | virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size); | |
605 | virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size); | |
606 | virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL); | |
607 | virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET); | |
608 | virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN); | |
973abc7f SH |
609 | } |
610 | ||
611 | static void virtio_scsi_set_config(VirtIODevice *vdev, | |
612 | const uint8_t *config) | |
613 | { | |
614 | VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; | |
292c8e50 | 615 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); |
973abc7f | 616 | |
8c085dbe RR |
617 | if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 || |
618 | (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) { | |
973abc7f SH |
619 | error_report("bad data written to virtio-scsi configuration space"); |
620 | exit(1); | |
621 | } | |
622 | ||
8c085dbe RR |
623 | vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size); |
624 | vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size); | |
973abc7f SH |
625 | } |
626 | ||
627 | static uint32_t virtio_scsi_get_features(VirtIODevice *vdev, | |
628 | uint32_t requested_features) | |
629 | { | |
630 | return requested_features; | |
631 | } | |
632 | ||
633 | static void virtio_scsi_reset(VirtIODevice *vdev) | |
634 | { | |
292c8e50 PB |
635 | VirtIOSCSI *s = VIRTIO_SCSI(vdev); |
636 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); | |
973abc7f | 637 | |
63c7e542 FZ |
638 | if (s->ctx) { |
639 | virtio_scsi_dataplane_stop(s); | |
640 | } | |
47a150a4 PB |
641 | s->resetting++; |
642 | qbus_reset_all(&s->bus.qbus); | |
643 | s->resetting--; | |
644 | ||
03325525 MT |
645 | vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; |
646 | vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; | |
2baa1beb | 647 | s->events_dropped = false; |
973abc7f SH |
648 | } |
649 | ||
5db1764c PB |
650 | /* The device does not have anything to save beyond the virtio data. |
651 | * Request data is saved with callbacks from SCSI devices. | |
652 | */ | |
653 | static void virtio_scsi_save(QEMUFile *f, void *opaque) | |
654 | { | |
0ac8e139 FK |
655 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); |
656 | virtio_save(vdev, f); | |
5db1764c PB |
657 | } |
658 | ||
659 | static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id) | |
660 | { | |
0ac8e139 | 661 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); |
2a633c46 OW |
662 | int ret; |
663 | ||
1b5fc0de | 664 | ret = virtio_load(vdev, f, version_id); |
2a633c46 OW |
665 | if (ret) { |
666 | return ret; | |
667 | } | |
5db1764c PB |
668 | return 0; |
669 | } | |
670 | ||
20e6dca1 FZ |
671 | void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, |
672 | uint32_t event, uint32_t reason) | |
b6866fee | 673 | { |
292c8e50 | 674 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); |
91e7fcca | 675 | VirtIOSCSIReq *req; |
b6866fee | 676 | VirtIOSCSIEvent *evt; |
0ac8e139 | 677 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
b6866fee | 678 | |
0ac8e139 | 679 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
cd41a671 PB |
680 | return; |
681 | } | |
682 | ||
63c7e542 FZ |
683 | if (s->dataplane_started) { |
684 | assert(s->ctx); | |
685 | aio_context_acquire(s->ctx); | |
686 | } | |
687 | ||
688 | if (s->dataplane_started) { | |
689 | req = virtio_scsi_pop_req_vring(s, s->event_vring); | |
690 | } else { | |
691 | req = virtio_scsi_pop_req(s, vs->event_vq); | |
692 | } | |
64f64855 PB |
693 | if (!req) { |
694 | s->events_dropped = true; | |
63c7e542 | 695 | goto out; |
64f64855 | 696 | } |
b6866fee | 697 | |
64f64855 PB |
698 | if (s->events_dropped) { |
699 | event |= VIRTIO_SCSI_T_EVENTS_MISSED; | |
700 | s->events_dropped = false; | |
701 | } | |
702 | ||
dfecbb95 | 703 | if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) { |
64f64855 PB |
704 | virtio_scsi_bad_req(); |
705 | } | |
706 | ||
3eff1f46 | 707 | evt = &req->resp.event; |
64f64855 | 708 | memset(evt, 0, sizeof(VirtIOSCSIEvent)); |
424baff5 CLG |
709 | evt->event = virtio_tswap32(vdev, event); |
710 | evt->reason = virtio_tswap32(vdev, reason); | |
64f64855 | 711 | if (!dev) { |
49fb65c7 | 712 | assert(event == VIRTIO_SCSI_T_EVENTS_MISSED); |
64f64855 | 713 | } else { |
b6866fee CM |
714 | evt->lun[0] = 1; |
715 | evt->lun[1] = dev->id; | |
716 | ||
717 | /* Linux wants us to keep the same encoding we use for REPORT LUNS. */ | |
718 | if (dev->lun >= 256) { | |
719 | evt->lun[2] = (dev->lun >> 8) | 0x40; | |
720 | } | |
721 | evt->lun[3] = dev->lun & 0xFF; | |
64f64855 PB |
722 | } |
723 | virtio_scsi_complete_req(req); | |
63c7e542 FZ |
724 | out: |
725 | if (s->dataplane_started) { | |
726 | aio_context_release(s->ctx); | |
727 | } | |
64f64855 PB |
728 | } |
729 | ||
730 | static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq) | |
731 | { | |
0ac8e139 | 732 | VirtIOSCSI *s = VIRTIO_SCSI(vdev); |
64f64855 | 733 | |
dfb37cf7 | 734 | if (s->ctx && !s->dataplane_disabled) { |
63c7e542 FZ |
735 | virtio_scsi_dataplane_start(s); |
736 | return; | |
737 | } | |
64f64855 PB |
738 | if (s->events_dropped) { |
739 | virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); | |
b6866fee CM |
740 | } |
741 | } | |
742 | ||
feda01e4 PB |
743 | static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense) |
744 | { | |
745 | VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); | |
0ac8e139 | 746 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
feda01e4 | 747 | |
ef546f12 | 748 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) && |
feda01e4 PB |
749 | dev->type != TYPE_ROM) { |
750 | virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE, | |
751 | sense.asc | (sense.ascq << 8)); | |
752 | } | |
753 | } | |
754 | ||
02206e52 IM |
755 | static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev, |
756 | Error **errp) | |
b6866fee | 757 | { |
02206e52 | 758 | VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); |
3e2b2a9c FZ |
759 | VirtIOSCSI *s = VIRTIO_SCSI(vdev); |
760 | SCSIDevice *sd = SCSI_DEVICE(dev); | |
761 | ||
762 | if (s->ctx && !s->dataplane_disabled) { | |
763 | if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) { | |
764 | return; | |
765 | } | |
766 | blk_op_block_all(sd->conf.blk, s->blocker); | |
05430559 FZ |
767 | aio_context_acquire(s->ctx); |
768 | blk_set_aio_context(sd->conf.blk, s->ctx); | |
769 | aio_context_release(s->ctx); | |
3e2b2a9c | 770 | } |
b6866fee | 771 | |
ef546f12 | 772 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { |
3e2b2a9c | 773 | virtio_scsi_push_event(s, sd, |
02206e52 | 774 | VIRTIO_SCSI_T_TRANSPORT_RESET, |
b6866fee CM |
775 | VIRTIO_SCSI_EVT_RESET_RESCAN); |
776 | } | |
777 | } | |
778 | ||
02206e52 IM |
779 | static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev, |
780 | Error **errp) | |
b6866fee | 781 | { |
02206e52 | 782 | VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); |
3e2b2a9c FZ |
783 | VirtIOSCSI *s = VIRTIO_SCSI(vdev); |
784 | SCSIDevice *sd = SCSI_DEVICE(dev); | |
b6866fee | 785 | |
ef546f12 | 786 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { |
3e2b2a9c | 787 | virtio_scsi_push_event(s, sd, |
02206e52 | 788 | VIRTIO_SCSI_T_TRANSPORT_RESET, |
b6866fee CM |
789 | VIRTIO_SCSI_EVT_RESET_REMOVED); |
790 | } | |
3e2b2a9c FZ |
791 | |
792 | if (s->ctx) { | |
793 | blk_op_unblock_all(sd->conf.blk, s->blocker); | |
794 | } | |
02206e52 | 795 | qdev_simple_device_unplug_cb(hotplug_dev, dev, errp); |
b6866fee CM |
796 | } |
797 | ||
2ccdcd8d PB |
798 | static struct SCSIBusInfo virtio_scsi_scsi_info = { |
799 | .tcq = true, | |
800 | .max_channel = VIRTIO_SCSI_MAX_CHANNEL, | |
801 | .max_target = VIRTIO_SCSI_MAX_TARGET, | |
802 | .max_lun = VIRTIO_SCSI_MAX_LUN, | |
803 | ||
804 | .complete = virtio_scsi_command_complete, | |
805 | .cancel = virtio_scsi_request_cancelled, | |
feda01e4 | 806 | .change = virtio_scsi_change, |
33cbb2c5 | 807 | .parse_cdb = virtio_scsi_parse_cdb, |
2ccdcd8d | 808 | .get_sg_list = virtio_scsi_get_sg_list, |
5db1764c PB |
809 | .save_request = virtio_scsi_save_request, |
810 | .load_request = virtio_scsi_load_request, | |
2ccdcd8d PB |
811 | }; |
812 | ||
91d670fb ML |
813 | void virtio_scsi_common_realize(DeviceState *dev, Error **errp, |
814 | HandleOutput ctrl, HandleOutput evt, | |
815 | HandleOutput cmd) | |
973abc7f | 816 | { |
71a6520b AF |
817 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
818 | VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); | |
d2ad7dd4 | 819 | int i; |
973abc7f | 820 | |
7598f0f3 | 821 | virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, |
763684be | 822 | sizeof(VirtIOSCSIConfig)); |
3ab1dfdd | 823 | |
0ba1f531 FZ |
824 | if (s->conf.num_queues == 0 || |
825 | s->conf.num_queues > VIRTIO_PCI_QUEUE_MAX - 2) { | |
826 | error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " | |
c9f65528 | 827 | "must be a positive integer less than %d.", |
0ba1f531 | 828 | s->conf.num_queues, VIRTIO_PCI_QUEUE_MAX - 2); |
93bd49af | 829 | virtio_cleanup(vdev); |
c9f65528 FZ |
830 | return; |
831 | } | |
3c55fe2a | 832 | s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues); |
03325525 MT |
833 | s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; |
834 | s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; | |
3ab1dfdd | 835 | |
0ac8e139 | 836 | s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, |
91d670fb | 837 | ctrl); |
0ac8e139 | 838 | s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, |
91d670fb | 839 | evt); |
394e2e4c | 840 | for (i = 0; i < s->conf.num_queues; i++) { |
0ac8e139 | 841 | s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, |
91d670fb | 842 | cmd); |
d2ad7dd4 | 843 | } |
9786b592 FZ |
844 | |
845 | if (s->conf.iothread) { | |
846 | virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread); | |
847 | } | |
292c8e50 PB |
848 | } |
849 | ||
dfb37cf7 FZ |
850 | /* Disable dataplane thread during live migration since it does not |
851 | * update the dirty memory bitmap yet. | |
852 | */ | |
853 | static void virtio_scsi_migration_state_changed(Notifier *notifier, void *data) | |
854 | { | |
855 | VirtIOSCSI *s = container_of(notifier, VirtIOSCSI, | |
856 | migration_state_notifier); | |
857 | MigrationState *mig = data; | |
858 | ||
859 | if (migration_in_setup(mig)) { | |
860 | if (!s->dataplane_started) { | |
861 | return; | |
862 | } | |
863 | virtio_scsi_dataplane_stop(s); | |
864 | s->dataplane_disabled = true; | |
865 | } else if (migration_has_finished(mig) || | |
866 | migration_has_failed(mig)) { | |
867 | if (s->dataplane_started) { | |
868 | return; | |
869 | } | |
4be74634 | 870 | blk_drain_all(); /* complete in-flight non-dataplane requests */ |
dfb37cf7 FZ |
871 | s->dataplane_disabled = false; |
872 | } | |
873 | } | |
874 | ||
71a6520b | 875 | static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) |
292c8e50 | 876 | { |
71a6520b | 877 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
7598f0f3 | 878 | VirtIOSCSI *s = VIRTIO_SCSI(dev); |
292c8e50 | 879 | static int virtio_scsi_id; |
caad4eb3 | 880 | Error *err = NULL; |
292c8e50 | 881 | |
91d670fb ML |
882 | virtio_scsi_common_realize(dev, &err, virtio_scsi_handle_ctrl, |
883 | virtio_scsi_handle_event, | |
884 | virtio_scsi_handle_cmd); | |
71a6520b AF |
885 | if (err != NULL) { |
886 | error_propagate(errp, err); | |
887 | return; | |
292c8e50 PB |
888 | } |
889 | ||
7598f0f3 | 890 | scsi_bus_new(&s->bus, sizeof(s->bus), dev, |
b1187b51 | 891 | &virtio_scsi_scsi_info, vdev->bus_name); |
02206e52 IM |
892 | /* override default SCSI bus hotplug-handler, with virtio-scsi's one */ |
893 | qbus_set_hotplug_handler(BUS(&s->bus), dev, &error_abort); | |
6f32a6b4 | 894 | |
7598f0f3 | 895 | if (!dev->hotplugged) { |
caad4eb3 AF |
896 | scsi_bus_legacy_handle_cmdline(&s->bus, &err); |
897 | if (err != NULL) { | |
71a6520b AF |
898 | error_propagate(errp, err); |
899 | return; | |
caad4eb3 | 900 | } |
2ccdcd8d PB |
901 | } |
902 | ||
7598f0f3 | 903 | register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1, |
5db1764c | 904 | virtio_scsi_save, virtio_scsi_load, s); |
dfb37cf7 FZ |
905 | s->migration_state_notifier.notify = virtio_scsi_migration_state_changed; |
906 | add_migration_state_change_notifier(&s->migration_state_notifier); | |
f6758f7d HR |
907 | |
908 | error_setg(&s->blocker, "block device is in use by data plane"); | |
3ab1dfdd FK |
909 | } |
910 | ||
19d339f1 FZ |
911 | static void virtio_scsi_instance_init(Object *obj) |
912 | { | |
913 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(obj); | |
914 | ||
915 | object_property_add_link(obj, "iothread", TYPE_IOTHREAD, | |
916 | (Object **)&vs->conf.iothread, | |
917 | qdev_prop_allow_set_link_before_realize, | |
918 | OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort); | |
919 | } | |
920 | ||
306ec6c3 | 921 | void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp) |
292c8e50 | 922 | { |
306ec6c3 AF |
923 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
924 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); | |
292c8e50 PB |
925 | |
926 | g_free(vs->cmd_vqs); | |
6a1a8cc7 | 927 | virtio_cleanup(vdev); |
292c8e50 PB |
928 | } |
929 | ||
306ec6c3 | 930 | static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp) |
3ab1dfdd | 931 | { |
306ec6c3 AF |
932 | VirtIOSCSI *s = VIRTIO_SCSI(dev); |
933 | ||
f6758f7d HR |
934 | error_free(s->blocker); |
935 | ||
306ec6c3 | 936 | unregister_savevm(dev, "virtio-scsi", s); |
dfb37cf7 | 937 | remove_migration_state_change_notifier(&s->migration_state_notifier); |
3ab1dfdd | 938 | |
306ec6c3 | 939 | virtio_scsi_common_unrealize(dev, errp); |
3ab1dfdd FK |
940 | } |
941 | ||
942 | static Property virtio_scsi_properties[] = { | |
292c8e50 | 943 | DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSI, parent_obj.conf), |
3ab1dfdd FK |
944 | DEFINE_PROP_END_OF_LIST(), |
945 | }; | |
946 | ||
292c8e50 PB |
947 | static void virtio_scsi_common_class_init(ObjectClass *klass, void *data) |
948 | { | |
949 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
125ee0ed | 950 | DeviceClass *dc = DEVICE_CLASS(klass); |
292c8e50 PB |
951 | |
952 | vdc->get_config = virtio_scsi_get_config; | |
125ee0ed | 953 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
292c8e50 PB |
954 | } |
955 | ||
3ab1dfdd FK |
956 | static void virtio_scsi_class_init(ObjectClass *klass, void *data) |
957 | { | |
958 | DeviceClass *dc = DEVICE_CLASS(klass); | |
959 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
02206e52 | 960 | HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); |
71a6520b | 961 | |
3ab1dfdd | 962 | dc->props = virtio_scsi_properties; |
125ee0ed | 963 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
71a6520b | 964 | vdc->realize = virtio_scsi_device_realize; |
306ec6c3 | 965 | vdc->unrealize = virtio_scsi_device_unrealize; |
3ab1dfdd FK |
966 | vdc->set_config = virtio_scsi_set_config; |
967 | vdc->get_features = virtio_scsi_get_features; | |
968 | vdc->reset = virtio_scsi_reset; | |
02206e52 IM |
969 | hc->plug = virtio_scsi_hotplug; |
970 | hc->unplug = virtio_scsi_hotunplug; | |
3ab1dfdd FK |
971 | } |
972 | ||
292c8e50 PB |
973 | static const TypeInfo virtio_scsi_common_info = { |
974 | .name = TYPE_VIRTIO_SCSI_COMMON, | |
975 | .parent = TYPE_VIRTIO_DEVICE, | |
976 | .instance_size = sizeof(VirtIOSCSICommon), | |
a27292b5 | 977 | .abstract = true, |
292c8e50 PB |
978 | .class_init = virtio_scsi_common_class_init, |
979 | }; | |
980 | ||
3ab1dfdd FK |
981 | static const TypeInfo virtio_scsi_info = { |
982 | .name = TYPE_VIRTIO_SCSI, | |
292c8e50 | 983 | .parent = TYPE_VIRTIO_SCSI_COMMON, |
3ab1dfdd | 984 | .instance_size = sizeof(VirtIOSCSI), |
19d339f1 | 985 | .instance_init = virtio_scsi_instance_init, |
3ab1dfdd | 986 | .class_init = virtio_scsi_class_init, |
02206e52 IM |
987 | .interfaces = (InterfaceInfo[]) { |
988 | { TYPE_HOTPLUG_HANDLER }, | |
989 | { } | |
990 | } | |
3ab1dfdd FK |
991 | }; |
992 | ||
993 | static void virtio_register_types(void) | |
994 | { | |
292c8e50 | 995 | type_register_static(&virtio_scsi_common_info); |
3ab1dfdd FK |
996 | type_register_static(&virtio_scsi_info); |
997 | } | |
998 | ||
999 | type_init(virtio_register_types) |