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