]>
Commit | Line | Data |
---|---|---|
2e5d83bb PB |
1 | /* |
2 | * SCSI Device emulation | |
3 | * | |
4 | * Copyright (c) 2006 CodeSourcery. | |
5 | * Based on code by Fabrice Bellard | |
6 | * | |
7 | * Written by Paul Brook | |
ad3cea42 AT |
8 | * Modifications: |
9 | * 2009-Dec-12 Artyom Tarasenko : implemented stamdard inquiry for the case | |
10 | * when the allocation length of CDB is smaller | |
11 | * than 36. | |
12 | * 2009-Oct-13 Artyom Tarasenko : implemented the block descriptor in the | |
13 | * MODE SENSE response. | |
2e5d83bb | 14 | * |
8e31bf38 | 15 | * This code is licensed under the LGPL. |
a917d384 PB |
16 | * |
17 | * Note that this file only handles the SCSI architecture model and device | |
1d4db89c AZ |
18 | * commands. Emulation of interface/link layer protocols is handled by |
19 | * the host adapter emulator. | |
2e5d83bb PB |
20 | */ |
21 | ||
a4ab4792 | 22 | #include "qemu/osdep.h" |
7e462605 | 23 | #include "qemu/units.h" |
da34e65c | 24 | #include "qapi/error.h" |
1de7afc9 | 25 | #include "qemu/error-report.h" |
db725815 | 26 | #include "qemu/main-loop.h" |
0b8fa32f | 27 | #include "qemu/module.h" |
0d09e41a | 28 | #include "hw/scsi/scsi.h" |
ca77ee28 | 29 | #include "migration/qemu-file-types.h" |
d6454270 | 30 | #include "migration/vmstate.h" |
3d4a8bf0 | 31 | #include "hw/scsi/emulation.h" |
08e2c9f1 | 32 | #include "scsi/constants.h" |
9c17d615 | 33 | #include "sysemu/sysemu.h" |
4be74634 | 34 | #include "sysemu/block-backend.h" |
9c17d615 | 35 | #include "sysemu/blockdev.h" |
0d09e41a | 36 | #include "hw/block/block.h" |
9c17d615 | 37 | #include "sysemu/dma.h" |
f348b6d1 | 38 | #include "qemu/cutils.h" |
59ee9500 | 39 | #include "trace.h" |
22864256 | 40 | |
336a6915 PB |
41 | #ifdef __linux |
42 | #include <scsi/sg.h> | |
43 | #endif | |
44 | ||
7e462605 PMD |
45 | #define SCSI_WRITE_SAME_MAX (512 * KiB) |
46 | #define SCSI_DMA_BUF_SIZE (128 * KiB) | |
215e47b9 PB |
47 | #define SCSI_MAX_INQUIRY_LEN 256 |
48 | #define SCSI_MAX_MODE_LEN 256 | |
49 | ||
7e462605 PMD |
50 | #define DEFAULT_DISCARD_GRANULARITY (4 * KiB) |
51 | #define DEFAULT_MAX_UNMAP_SIZE (1 * GiB) | |
f8e1f533 | 52 | #define DEFAULT_MAX_IO_SIZE INT_MAX /* 2 GB - 1 block */ |
a917d384 | 53 | |
993935f3 PB |
54 | #define TYPE_SCSI_DISK_BASE "scsi-disk-base" |
55 | ||
fcaafb10 PB |
56 | #define SCSI_DISK_BASE(obj) \ |
57 | OBJECT_CHECK(SCSIDiskState, (obj), TYPE_SCSI_DISK_BASE) | |
58 | #define SCSI_DISK_BASE_CLASS(klass) \ | |
59 | OBJECT_CLASS_CHECK(SCSIDiskClass, (klass), TYPE_SCSI_DISK_BASE) | |
60 | #define SCSI_DISK_BASE_GET_CLASS(obj) \ | |
61 | OBJECT_GET_CLASS(SCSIDiskClass, (obj), TYPE_SCSI_DISK_BASE) | |
62 | ||
63 | typedef struct SCSIDiskClass { | |
64 | SCSIDeviceClass parent_class; | |
65 | DMAIOFunc *dma_readv; | |
66 | DMAIOFunc *dma_writev; | |
94f8ba11 | 67 | bool (*need_fua_emulation)(SCSICommand *cmd); |
d31347f5 | 68 | void (*update_sense)(SCSIRequest *r); |
fcaafb10 | 69 | } SCSIDiskClass; |
d52affa7 | 70 | |
4c41d2ef GH |
71 | typedef struct SCSIDiskReq { |
72 | SCSIRequest req; | |
a917d384 | 73 | /* Both sector and sector_count are in terms of qemu 512 byte blocks. */ |
e035b43d AL |
74 | uint64_t sector; |
75 | uint32_t sector_count; | |
7285477a | 76 | uint32_t buflen; |
a0e66a69 | 77 | bool started; |
94f8ba11 | 78 | bool need_fua_emulation; |
c87c0672 AL |
79 | struct iovec iov; |
80 | QEMUIOVector qiov; | |
a597e79c | 81 | BlockAcctCookie acct; |
8fdc7839 | 82 | unsigned char *status; |
4c41d2ef | 83 | } SCSIDiskReq; |
a917d384 | 84 | |
18e673b8 PH |
85 | #define SCSI_DISK_F_REMOVABLE 0 |
86 | #define SCSI_DISK_F_DPOFUA 1 | |
87 | #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS 2 | |
bfe3d7ac | 88 | |
fcaafb10 | 89 | typedef struct SCSIDiskState |
a917d384 | 90 | { |
d52affa7 | 91 | SCSIDevice qdev; |
bfe3d7ac | 92 | uint32_t features; |
8a9c16f6 | 93 | bool media_changed; |
3c2f7c12 | 94 | bool media_event; |
4480de19 | 95 | bool eject_request; |
64cc2284 | 96 | uint16_t port_index; |
8a1bd297 | 97 | uint64_t max_unmap_size; |
f8e1f533 | 98 | uint64_t max_io_size; |
213189ab | 99 | QEMUBH *bh; |
383b4d9b | 100 | char *version; |
a0fef654 | 101 | char *serial; |
353815aa DF |
102 | char *vendor; |
103 | char *product; | |
7471a649 | 104 | char *device_id; |
ece0d5e9 | 105 | bool tray_open; |
81b1008d | 106 | bool tray_locked; |
070f8009 DB |
107 | /* |
108 | * 0x0000 - rotation rate not reported | |
109 | * 0x0001 - non-rotating medium (SSD) | |
110 | * 0x0002-0x0400 - reserved | |
111 | * 0x0401-0xffe - rotations per minute | |
112 | * 0xffff - reserved | |
113 | */ | |
114 | uint16_t rotation_rate; | |
fcaafb10 | 115 | } SCSIDiskState; |
2e5d83bb | 116 | |
14b20748 | 117 | static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed); |
5dba48a8 | 118 | |
ad2d30f7 | 119 | static void scsi_free_request(SCSIRequest *req) |
4d611c9a | 120 | { |
ad2d30f7 PB |
121 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
122 | ||
db4c34c3 | 123 | qemu_vfree(r->iov.iov_base); |
4d611c9a PB |
124 | } |
125 | ||
b45ef674 PB |
126 | /* Helper function for command completion with sense. */ |
127 | static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense) | |
ed3a34a3 | 128 | { |
59ee9500 LV |
129 | trace_scsi_disk_check_condition(r->req.tag, sense.key, sense.asc, |
130 | sense.ascq); | |
b45ef674 PB |
131 | scsi_req_build_sense(&r->req, sense); |
132 | scsi_req_complete(&r->req, CHECK_CONDITION); | |
4d611c9a PB |
133 | } |
134 | ||
03c90063 | 135 | static void scsi_init_iovec(SCSIDiskReq *r, size_t size) |
103b40f5 | 136 | { |
7285477a PB |
137 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
138 | ||
139 | if (!r->iov.iov_base) { | |
43b978b9 | 140 | r->buflen = size; |
4be74634 | 141 | r->iov.iov_base = blk_blockalign(s->qdev.conf.blk, r->buflen); |
7285477a PB |
142 | } |
143 | r->iov.iov_len = MIN(r->sector_count * 512, r->buflen); | |
103b40f5 | 144 | qemu_iovec_init_external(&r->qiov, &r->iov, 1); |
103b40f5 PB |
145 | } |
146 | ||
43b978b9 PB |
147 | static void scsi_disk_save_request(QEMUFile *f, SCSIRequest *req) |
148 | { | |
149 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); | |
150 | ||
151 | qemu_put_be64s(f, &r->sector); | |
152 | qemu_put_be32s(f, &r->sector_count); | |
153 | qemu_put_be32s(f, &r->buflen); | |
18eef3bc GH |
154 | if (r->buflen) { |
155 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { | |
156 | qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len); | |
157 | } else if (!req->retry) { | |
158 | uint32_t len = r->iov.iov_len; | |
159 | qemu_put_be32s(f, &len); | |
160 | qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len); | |
161 | } | |
43b978b9 PB |
162 | } |
163 | } | |
164 | ||
165 | static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req) | |
166 | { | |
167 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); | |
168 | ||
169 | qemu_get_be64s(f, &r->sector); | |
170 | qemu_get_be32s(f, &r->sector_count); | |
171 | qemu_get_be32s(f, &r->buflen); | |
172 | if (r->buflen) { | |
173 | scsi_init_iovec(r, r->buflen); | |
174 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { | |
175 | qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len); | |
18eef3bc GH |
176 | } else if (!r->req.retry) { |
177 | uint32_t len; | |
178 | qemu_get_be32s(f, &len); | |
179 | r->iov.iov_len = len; | |
180 | assert(r->iov.iov_len <= r->buflen); | |
181 | qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len); | |
43b978b9 PB |
182 | } |
183 | } | |
184 | ||
185 | qemu_iovec_init_external(&r->qiov, &r->iov, 1); | |
186 | } | |
187 | ||
5b956f41 PB |
188 | static bool scsi_disk_req_check_error(SCSIDiskReq *r, int ret, bool acct_failed) |
189 | { | |
190 | if (r->req.io_canceled) { | |
191 | scsi_req_cancel_complete(&r->req); | |
192 | return true; | |
193 | } | |
194 | ||
14b20748 | 195 | if (ret < 0 || (r->status && *r->status)) { |
5b956f41 PB |
196 | return scsi_handle_rw_error(r, -ret, acct_failed); |
197 | } | |
198 | ||
199 | return false; | |
200 | } | |
201 | ||
c1b35247 | 202 | static void scsi_aio_complete(void *opaque, int ret) |
5d0d2467 PB |
203 | { |
204 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; | |
205 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
206 | ||
46e3f30e PB |
207 | assert(r->req.aiocb != NULL); |
208 | r->req.aiocb = NULL; | |
b9e413dd | 209 | aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); |
5b956f41 | 210 | if (scsi_disk_req_check_error(r, ret, true)) { |
0c92e0e6 PB |
211 | goto done; |
212 | } | |
5d0d2467 | 213 | |
d7628080 | 214 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
5d0d2467 PB |
215 | scsi_req_complete(&r->req, GOOD); |
216 | ||
217 | done: | |
b9e413dd | 218 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
3df9caf8 | 219 | scsi_req_unref(&r->req); |
5d0d2467 PB |
220 | } |
221 | ||
7e8c49c5 PB |
222 | static bool scsi_is_cmd_fua(SCSICommand *cmd) |
223 | { | |
224 | switch (cmd->buf[0]) { | |
225 | case READ_10: | |
226 | case READ_12: | |
227 | case READ_16: | |
228 | case WRITE_10: | |
229 | case WRITE_12: | |
230 | case WRITE_16: | |
231 | return (cmd->buf[1] & 8) != 0; | |
232 | ||
7f64f8e2 PB |
233 | case VERIFY_10: |
234 | case VERIFY_12: | |
235 | case VERIFY_16: | |
7e8c49c5 PB |
236 | case WRITE_VERIFY_10: |
237 | case WRITE_VERIFY_12: | |
238 | case WRITE_VERIFY_16: | |
239 | return true; | |
240 | ||
241 | case READ_6: | |
242 | case WRITE_6: | |
243 | default: | |
244 | return false; | |
245 | } | |
246 | } | |
247 | ||
248 | static void scsi_write_do_fua(SCSIDiskReq *r) | |
249 | { | |
250 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
251 | ||
5fd2b563 | 252 | assert(r->req.aiocb == NULL); |
5b956f41 | 253 | assert(!r->req.io_canceled); |
0c92e0e6 | 254 | |
94f8ba11 | 255 | if (r->need_fua_emulation) { |
4be74634 | 256 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
5366d0c8 | 257 | BLOCK_ACCT_FLUSH); |
4be74634 | 258 | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r); |
7e8c49c5 PB |
259 | return; |
260 | } | |
261 | ||
262 | scsi_req_complete(&r->req, GOOD); | |
3df9caf8 | 263 | scsi_req_unref(&r->req); |
7e8c49c5 PB |
264 | } |
265 | ||
5fd2b563 | 266 | static void scsi_dma_complete_noio(SCSIDiskReq *r, int ret) |
a917d384 | 267 | { |
5fd2b563 | 268 | assert(r->req.aiocb == NULL); |
5b956f41 | 269 | if (scsi_disk_req_check_error(r, ret, false)) { |
0c92e0e6 PB |
270 | goto done; |
271 | } | |
a597e79c | 272 | |
b77912a7 PB |
273 | r->sector += r->sector_count; |
274 | r->sector_count = 0; | |
7e8c49c5 PB |
275 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
276 | scsi_write_do_fua(r); | |
277 | return; | |
278 | } else { | |
279 | scsi_req_complete(&r->req, GOOD); | |
280 | } | |
c7bae6a7 PB |
281 | |
282 | done: | |
3df9caf8 | 283 | scsi_req_unref(&r->req); |
4d611c9a PB |
284 | } |
285 | ||
ef8489d4 PB |
286 | static void scsi_dma_complete(void *opaque, int ret) |
287 | { | |
288 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; | |
5fd2b563 | 289 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
ef8489d4 PB |
290 | |
291 | assert(r->req.aiocb != NULL); | |
5fd2b563 PB |
292 | r->req.aiocb = NULL; |
293 | ||
b9e413dd | 294 | aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); |
d7628080 AG |
295 | if (ret < 0) { |
296 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
297 | } else { | |
298 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
299 | } | |
5fd2b563 | 300 | scsi_dma_complete_noio(r, ret); |
b9e413dd | 301 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
ef8489d4 PB |
302 | } |
303 | ||
1505421a | 304 | static void scsi_read_complete_noio(SCSIDiskReq *r, int ret) |
0a4ac106 | 305 | { |
1505421a | 306 | uint32_t n; |
0a4ac106 | 307 | |
1505421a ZL |
308 | assert(r->req.aiocb == NULL); |
309 | if (scsi_disk_req_check_error(r, ret, false)) { | |
0c92e0e6 PB |
310 | goto done; |
311 | } | |
0a4ac106 | 312 | |
b77912a7 PB |
313 | n = r->qiov.size / 512; |
314 | r->sector += n; | |
315 | r->sector_count -= n; | |
316 | scsi_req_data(&r->req, r->qiov.size); | |
c7bae6a7 PB |
317 | |
318 | done: | |
3df9caf8 | 319 | scsi_req_unref(&r->req); |
1505421a ZL |
320 | } |
321 | ||
322 | static void scsi_read_complete(void *opaque, int ret) | |
323 | { | |
324 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; | |
325 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
326 | ||
327 | assert(r->req.aiocb != NULL); | |
328 | r->req.aiocb = NULL; | |
329 | ||
330 | aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); | |
331 | if (ret < 0) { | |
332 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
333 | } else { | |
334 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
335 | trace_scsi_disk_read_complete(r->req.tag, r->qiov.size); | |
336 | } | |
337 | scsi_read_complete_noio(r, ret); | |
b9e413dd | 338 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
0a4ac106 | 339 | } |
5dba48a8 | 340 | |
ac668426 | 341 | /* Actually issue a read to the block device. */ |
5fd2b563 | 342 | static void scsi_do_read(SCSIDiskReq *r, int ret) |
ac668426 | 343 | { |
ac668426 | 344 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
fcaafb10 | 345 | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
ac668426 | 346 | |
5fd2b563 | 347 | assert (r->req.aiocb == NULL); |
5b956f41 | 348 | if (scsi_disk_req_check_error(r, ret, false)) { |
0c92e0e6 PB |
349 | goto done; |
350 | } | |
ac668426 | 351 | |
31e8fd86 PB |
352 | /* The request is used as the AIO opaque value, so add a ref. */ |
353 | scsi_req_ref(&r->req); | |
354 | ||
ac668426 | 355 | if (r->req.sg) { |
4be74634 | 356 | dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_READ); |
ac668426 | 357 | r->req.resid -= r->req.sg->size; |
fcaafb10 PB |
358 | r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk), |
359 | r->req.sg, r->sector << BDRV_SECTOR_BITS, | |
99868af3 | 360 | BDRV_SECTOR_SIZE, |
fcaafb10 PB |
361 | sdc->dma_readv, r, scsi_dma_complete, r, |
362 | DMA_DIRECTION_FROM_DEVICE); | |
ac668426 | 363 | } else { |
03c90063 | 364 | scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); |
4be74634 | 365 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
03c90063 | 366 | r->qiov.size, BLOCK_ACCT_READ); |
890e48d7 | 367 | r->req.aiocb = sdc->dma_readv(r->sector << BDRV_SECTOR_BITS, &r->qiov, |
fcaafb10 | 368 | scsi_read_complete, r, r); |
ac668426 PB |
369 | } |
370 | ||
371 | done: | |
3df9caf8 | 372 | scsi_req_unref(&r->req); |
ac668426 PB |
373 | } |
374 | ||
5fd2b563 PB |
375 | static void scsi_do_read_cb(void *opaque, int ret) |
376 | { | |
377 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; | |
378 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
379 | ||
380 | assert (r->req.aiocb != NULL); | |
381 | r->req.aiocb = NULL; | |
382 | ||
b9e413dd | 383 | aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); |
d7628080 AG |
384 | if (ret < 0) { |
385 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
386 | } else { | |
387 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
388 | } | |
5fd2b563 | 389 | scsi_do_read(opaque, ret); |
b9e413dd | 390 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
5fd2b563 PB |
391 | } |
392 | ||
5c6c0e51 HR |
393 | /* Read more data from scsi device into buffer. */ |
394 | static void scsi_read_data(SCSIRequest *req) | |
2e5d83bb | 395 | { |
5c6c0e51 | 396 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
5dba48a8 | 397 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
ac668426 | 398 | bool first; |
2e5d83bb | 399 | |
59ee9500 | 400 | trace_scsi_disk_read_data_count(r->sector_count); |
a917d384 | 401 | if (r->sector_count == 0) { |
b45ef674 PB |
402 | /* This also clears the sense buffer for REQUEST SENSE. */ |
403 | scsi_req_complete(&r->req, GOOD); | |
a917d384 | 404 | return; |
2e5d83bb PB |
405 | } |
406 | ||
6fa2c95f SH |
407 | /* No data transfer may already be in progress */ |
408 | assert(r->req.aiocb == NULL); | |
409 | ||
c7bae6a7 PB |
410 | /* The request is used as the AIO opaque value, so add a ref. */ |
411 | scsi_req_ref(&r->req); | |
efb9ee02 | 412 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
59ee9500 | 413 | trace_scsi_disk_read_data_invalid(); |
1505421a | 414 | scsi_read_complete_noio(r, -EINVAL); |
efb9ee02 HR |
415 | return; |
416 | } | |
417 | ||
cd723b85 | 418 | if (!blk_is_available(req->dev->conf.blk)) { |
1505421a | 419 | scsi_read_complete_noio(r, -ENOMEDIUM); |
c7bae6a7 | 420 | return; |
a1aff5bf | 421 | } |
c7bae6a7 | 422 | |
ac668426 | 423 | first = !r->started; |
a0e66a69 | 424 | r->started = true; |
94f8ba11 | 425 | if (first && r->need_fua_emulation) { |
4be74634 | 426 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
5366d0c8 | 427 | BLOCK_ACCT_FLUSH); |
5fd2b563 | 428 | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_do_read_cb, r); |
5d0d2467 | 429 | } else { |
ac668426 | 430 | scsi_do_read(r, 0); |
5d0d2467 | 431 | } |
2e5d83bb PB |
432 | } |
433 | ||
c7bae6a7 | 434 | /* |
14b20748 FZ |
435 | * scsi_handle_rw_error has two return values. False means that the error |
436 | * must be ignored, true means that the error has been processed and the | |
c7bae6a7 PB |
437 | * caller should not do anything else for this request. Note that |
438 | * scsi_handle_rw_error always manages its reference counts, independent | |
439 | * of the return value. | |
440 | */ | |
14b20748 | 441 | static bool scsi_handle_rw_error(SCSIDiskReq *r, int error, bool acct_failed) |
5dba48a8 | 442 | { |
c85a7a00 | 443 | bool is_read = (r->req.cmd.mode == SCSI_XFER_FROM_DEV); |
4c41d2ef | 444 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
d31347f5 | 445 | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
4be74634 MA |
446 | BlockErrorAction action = blk_get_error_action(s->qdev.conf.blk, |
447 | is_read, error); | |
ea8a5d7f | 448 | |
a589569f | 449 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
d7628080 AG |
450 | if (acct_failed) { |
451 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
452 | } | |
efb9ee02 | 453 | switch (error) { |
14b20748 | 454 | case 0: |
e6aa5ba4 PB |
455 | /* A passthrough command has run and has produced sense data; check |
456 | * whether the error has to be handled by the guest or should rather | |
457 | * pause the host. | |
458 | */ | |
14b20748 | 459 | assert(r->status && *r->status); |
bdf9613b | 460 | if (scsi_sense_buf_is_guest_recoverable(r->req.sense, sizeof(r->req.sense))) { |
e6aa5ba4 | 461 | /* These errors are handled by guest. */ |
d31347f5 | 462 | sdc->update_sense(&r->req); |
e6aa5ba4 PB |
463 | scsi_req_complete(&r->req, *r->status); |
464 | return true; | |
465 | } | |
bdf9613b | 466 | error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense)); |
14b20748 | 467 | break; |
7e218df5 PB |
468 | case ENOMEDIUM: |
469 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); | |
470 | break; | |
efb9ee02 | 471 | case ENOMEM: |
b45ef674 | 472 | scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE)); |
efb9ee02 HR |
473 | break; |
474 | case EINVAL: | |
b45ef674 | 475 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
efb9ee02 | 476 | break; |
703dd81a PB |
477 | case ENOSPC: |
478 | scsi_check_condition(r, SENSE_CODE(SPACE_ALLOC_FAILED)); | |
479 | break; | |
efb9ee02 | 480 | default: |
b45ef674 | 481 | scsi_check_condition(r, SENSE_CODE(IO_ERROR)); |
efb9ee02 | 482 | break; |
a1f0cce2 | 483 | } |
ea8a5d7f | 484 | } |
14b20748 | 485 | |
4be74634 | 486 | blk_error_action(s->qdev.conf.blk, action, is_read, error); |
40dce4ee PB |
487 | if (action == BLOCK_ERROR_ACTION_IGNORE) { |
488 | scsi_req_complete(&r->req, 0); | |
489 | return true; | |
490 | } | |
491 | ||
a589569f | 492 | if (action == BLOCK_ERROR_ACTION_STOP) { |
3e1caa5f PB |
493 | scsi_req_retry(&r->req); |
494 | } | |
1c7f618f | 495 | return true; |
ea8a5d7f AL |
496 | } |
497 | ||
5fd2b563 | 498 | static void scsi_write_complete_noio(SCSIDiskReq *r, int ret) |
4d611c9a | 499 | { |
ea8a5d7f AL |
500 | uint32_t n; |
501 | ||
5fd2b563 | 502 | assert (r->req.aiocb == NULL); |
5b956f41 | 503 | if (scsi_disk_req_check_error(r, ret, false)) { |
0c92e0e6 PB |
504 | goto done; |
505 | } | |
a597e79c | 506 | |
103b40f5 | 507 | n = r->qiov.size / 512; |
ea8a5d7f AL |
508 | r->sector += n; |
509 | r->sector_count -= n; | |
a917d384 | 510 | if (r->sector_count == 0) { |
7e8c49c5 PB |
511 | scsi_write_do_fua(r); |
512 | return; | |
a917d384 | 513 | } else { |
43b978b9 | 514 | scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); |
59ee9500 | 515 | trace_scsi_disk_write_complete_noio(r->req.tag, r->qiov.size); |
103b40f5 | 516 | scsi_req_data(&r->req, r->qiov.size); |
4d611c9a | 517 | } |
c7bae6a7 PB |
518 | |
519 | done: | |
3df9caf8 | 520 | scsi_req_unref(&r->req); |
4d611c9a PB |
521 | } |
522 | ||
5fd2b563 PB |
523 | static void scsi_write_complete(void * opaque, int ret) |
524 | { | |
525 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; | |
526 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
527 | ||
528 | assert (r->req.aiocb != NULL); | |
529 | r->req.aiocb = NULL; | |
530 | ||
b9e413dd | 531 | aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); |
d7628080 AG |
532 | if (ret < 0) { |
533 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
534 | } else { | |
535 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); | |
536 | } | |
5fd2b563 | 537 | scsi_write_complete_noio(r, ret); |
b9e413dd | 538 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
5fd2b563 PB |
539 | } |
540 | ||
42741212 | 541 | static void scsi_write_data(SCSIRequest *req) |
ea8a5d7f | 542 | { |
5c6c0e51 | 543 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
4c41d2ef | 544 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
fcaafb10 | 545 | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
ea8a5d7f | 546 | |
6fa2c95f SH |
547 | /* No data transfer may already be in progress */ |
548 | assert(r->req.aiocb == NULL); | |
549 | ||
c7bae6a7 PB |
550 | /* The request is used as the AIO opaque value, so add a ref. */ |
551 | scsi_req_ref(&r->req); | |
efb9ee02 | 552 | if (r->req.cmd.mode != SCSI_XFER_TO_DEV) { |
59ee9500 | 553 | trace_scsi_disk_write_data_invalid(); |
5fd2b563 | 554 | scsi_write_complete_noio(r, -EINVAL); |
42741212 | 555 | return; |
efb9ee02 HR |
556 | } |
557 | ||
5d0d2467 PB |
558 | if (!r->req.sg && !r->qiov.size) { |
559 | /* Called for the first time. Ask the driver to send us more data. */ | |
a0e66a69 | 560 | r->started = true; |
5fd2b563 | 561 | scsi_write_complete_noio(r, 0); |
5d0d2467 PB |
562 | return; |
563 | } | |
cd723b85 | 564 | if (!blk_is_available(req->dev->conf.blk)) { |
5fd2b563 | 565 | scsi_write_complete_noio(r, -ENOMEDIUM); |
5d0d2467 PB |
566 | return; |
567 | } | |
568 | ||
7f64f8e2 PB |
569 | if (r->req.cmd.buf[0] == VERIFY_10 || r->req.cmd.buf[0] == VERIFY_12 || |
570 | r->req.cmd.buf[0] == VERIFY_16) { | |
571 | if (r->req.sg) { | |
ef8489d4 | 572 | scsi_dma_complete_noio(r, 0); |
7f64f8e2 | 573 | } else { |
5fd2b563 | 574 | scsi_write_complete_noio(r, 0); |
7f64f8e2 PB |
575 | } |
576 | return; | |
577 | } | |
578 | ||
5d0d2467 | 579 | if (r->req.sg) { |
4be74634 | 580 | dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_WRITE); |
5d0d2467 | 581 | r->req.resid -= r->req.sg->size; |
fcaafb10 PB |
582 | r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk), |
583 | r->req.sg, r->sector << BDRV_SECTOR_BITS, | |
99868af3 | 584 | BDRV_SECTOR_SIZE, |
fcaafb10 PB |
585 | sdc->dma_writev, r, scsi_dma_complete, r, |
586 | DMA_DIRECTION_TO_DEVICE); | |
5d0d2467 | 587 | } else { |
4be74634 | 588 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
03c90063 | 589 | r->qiov.size, BLOCK_ACCT_WRITE); |
fcaafb10 PB |
590 | r->req.aiocb = sdc->dma_writev(r->sector << BDRV_SECTOR_BITS, &r->qiov, |
591 | scsi_write_complete, r, r); | |
ea8a5d7f | 592 | } |
a917d384 | 593 | } |
2e5d83bb | 594 | |
a917d384 | 595 | /* Return a pointer to the data buffer. */ |
5c6c0e51 | 596 | static uint8_t *scsi_get_buf(SCSIRequest *req) |
a917d384 | 597 | { |
5c6c0e51 | 598 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
2e5d83bb | 599 | |
3f4cb3d3 | 600 | return (uint8_t *)r->iov.iov_base; |
2e5d83bb PB |
601 | } |
602 | ||
3d4a8bf0 | 603 | static int scsi_disk_emulate_vpd_page(SCSIRequest *req, uint8_t *outbuf) |
0b06c059 | 604 | { |
383b4d9b | 605 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
0a96ca24 DHB |
606 | uint8_t page_code = req->cmd.buf[2]; |
607 | int start, buflen = 0; | |
0b06c059 | 608 | |
0a96ca24 DHB |
609 | outbuf[buflen++] = s->qdev.type & 0x1f; |
610 | outbuf[buflen++] = page_code; | |
611 | outbuf[buflen++] = 0x00; | |
612 | outbuf[buflen++] = 0x00; | |
613 | start = buflen; | |
3e1c0c9a | 614 | |
0a96ca24 DHB |
615 | switch (page_code) { |
616 | case 0x00: /* Supported page codes, mandatory */ | |
617 | { | |
59ee9500 | 618 | trace_scsi_disk_emulate_vpd_page_00(req->cmd.xfer); |
0a96ca24 DHB |
619 | outbuf[buflen++] = 0x00; /* list of supported pages (this page) */ |
620 | if (s->serial) { | |
621 | outbuf[buflen++] = 0x80; /* unit serial number */ | |
622 | } | |
623 | outbuf[buflen++] = 0x83; /* device identification */ | |
624 | if (s->qdev.type == TYPE_DISK) { | |
625 | outbuf[buflen++] = 0xb0; /* block limits */ | |
626 | outbuf[buflen++] = 0xb1; /* block device characteristics */ | |
627 | outbuf[buflen++] = 0xb2; /* thin provisioning */ | |
628 | } | |
629 | break; | |
630 | } | |
631 | case 0x80: /* Device serial number, optional */ | |
632 | { | |
633 | int l; | |
0b06c059 | 634 | |
0a96ca24 | 635 | if (!s->serial) { |
59ee9500 | 636 | trace_scsi_disk_emulate_vpd_page_80_not_supported(); |
0a96ca24 | 637 | return -1; |
0b06c059 GH |
638 | } |
639 | ||
0a96ca24 DHB |
640 | l = strlen(s->serial); |
641 | if (l > 36) { | |
642 | l = 36; | |
643 | } | |
0b06c059 | 644 | |
59ee9500 | 645 | trace_scsi_disk_emulate_vpd_page_80(req->cmd.xfer); |
0a96ca24 DHB |
646 | memcpy(outbuf + buflen, s->serial, l); |
647 | buflen += l; | |
648 | break; | |
649 | } | |
64cc2284 | 650 | |
0a96ca24 DHB |
651 | case 0x83: /* Device identification page, mandatory */ |
652 | { | |
7471a649 | 653 | int id_len = s->device_id ? MIN(strlen(s->device_id), 255 - 8) : 0; |
64cc2284 | 654 | |
59ee9500 | 655 | trace_scsi_disk_emulate_vpd_page_83(req->cmd.xfer); |
0a96ca24 | 656 | |
a8f58afc KW |
657 | if (id_len) { |
658 | outbuf[buflen++] = 0x2; /* ASCII */ | |
659 | outbuf[buflen++] = 0; /* not officially assigned */ | |
660 | outbuf[buflen++] = 0; /* reserved */ | |
661 | outbuf[buflen++] = id_len; /* length of data following */ | |
7471a649 | 662 | memcpy(outbuf + buflen, s->device_id, id_len); |
a8f58afc KW |
663 | buflen += id_len; |
664 | } | |
0a96ca24 DHB |
665 | |
666 | if (s->qdev.wwn) { | |
667 | outbuf[buflen++] = 0x1; /* Binary */ | |
668 | outbuf[buflen++] = 0x3; /* NAA */ | |
669 | outbuf[buflen++] = 0; /* reserved */ | |
670 | outbuf[buflen++] = 8; | |
671 | stq_be_p(&outbuf[buflen], s->qdev.wwn); | |
672 | buflen += 8; | |
ea3bd56f | 673 | } |
0a96ca24 DHB |
674 | |
675 | if (s->qdev.port_wwn) { | |
676 | outbuf[buflen++] = 0x61; /* SAS / Binary */ | |
677 | outbuf[buflen++] = 0x93; /* PIV / Target port / NAA */ | |
678 | outbuf[buflen++] = 0; /* reserved */ | |
679 | outbuf[buflen++] = 8; | |
680 | stq_be_p(&outbuf[buflen], s->qdev.port_wwn); | |
681 | buflen += 8; | |
070f8009 | 682 | } |
0a96ca24 DHB |
683 | |
684 | if (s->port_index) { | |
685 | outbuf[buflen++] = 0x61; /* SAS / Binary */ | |
686 | ||
687 | /* PIV/Target port/relative target port */ | |
688 | outbuf[buflen++] = 0x94; | |
689 | ||
690 | outbuf[buflen++] = 0; /* reserved */ | |
691 | outbuf[buflen++] = 4; | |
692 | stw_be_p(&outbuf[buflen + 2], s->port_index); | |
693 | buflen += 4; | |
ee3659e3 | 694 | } |
0a96ca24 DHB |
695 | break; |
696 | } | |
697 | case 0xb0: /* block limits */ | |
698 | { | |
3d4a8bf0 | 699 | SCSIBlockLimits bl = {}; |
0a96ca24 DHB |
700 | |
701 | if (s->qdev.type == TYPE_ROM) { | |
59ee9500 | 702 | trace_scsi_disk_emulate_vpd_page_b0_not_supported(); |
0b06c059 GH |
703 | return -1; |
704 | } | |
3d4a8bf0 PB |
705 | bl.wsnz = 1; |
706 | bl.unmap_sectors = | |
707 | s->qdev.conf.discard_granularity / s->qdev.blocksize; | |
708 | bl.min_io_size = | |
709 | s->qdev.conf.min_io_size / s->qdev.blocksize; | |
710 | bl.opt_io_size = | |
711 | s->qdev.conf.opt_io_size / s->qdev.blocksize; | |
712 | bl.max_unmap_sectors = | |
713 | s->max_unmap_size / s->qdev.blocksize; | |
714 | bl.max_io_sectors = | |
715 | s->max_io_size / s->qdev.blocksize; | |
716 | /* 255 descriptors fit in 4 KiB with an 8-byte header */ | |
717 | bl.max_unmap_descr = 255; | |
718 | ||
0a96ca24 DHB |
719 | if (s->qdev.type == TYPE_DISK) { |
720 | int max_transfer_blk = blk_get_max_transfer(s->qdev.conf.blk); | |
721 | int max_io_sectors_blk = | |
722 | max_transfer_blk / s->qdev.blocksize; | |
723 | ||
3d4a8bf0 PB |
724 | bl.max_io_sectors = |
725 | MIN_NON_ZERO(max_io_sectors_blk, bl.max_io_sectors); | |
0a96ca24 | 726 | } |
3d4a8bf0 | 727 | buflen += scsi_emulate_block_limits(outbuf + buflen, &bl); |
0a96ca24 DHB |
728 | break; |
729 | } | |
730 | case 0xb1: /* block device characteristics */ | |
731 | { | |
740842c9 | 732 | buflen = 0x40; |
0a96ca24 DHB |
733 | outbuf[4] = (s->rotation_rate >> 8) & 0xff; |
734 | outbuf[5] = s->rotation_rate & 0xff; | |
740842c9 DHB |
735 | outbuf[6] = 0; /* PRODUCT TYPE */ |
736 | outbuf[7] = 0; /* WABEREQ | WACEREQ | NOMINAL FORM FACTOR */ | |
737 | outbuf[8] = 0; /* VBULS */ | |
0a96ca24 DHB |
738 | break; |
739 | } | |
740 | case 0xb2: /* thin provisioning */ | |
741 | { | |
742 | buflen = 8; | |
743 | outbuf[4] = 0; | |
744 | outbuf[5] = 0xe0; /* unmap & write_same 10/16 all supported */ | |
745 | outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1; | |
746 | outbuf[7] = 0; | |
747 | break; | |
748 | } | |
749 | default: | |
750 | return -1; | |
751 | } | |
752 | /* done with EVPD */ | |
753 | assert(buflen - start <= 255); | |
754 | outbuf[start - 1] = buflen - start; | |
755 | return buflen; | |
756 | } | |
757 | ||
758 | static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) | |
759 | { | |
760 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); | |
761 | int buflen = 0; | |
762 | ||
763 | if (req->cmd.buf[1] & 0x1) { | |
764 | /* Vital product data */ | |
765 | return scsi_disk_emulate_vpd_page(req, outbuf); | |
0b06c059 GH |
766 | } |
767 | ||
768 | /* Standard INQUIRY data */ | |
769 | if (req->cmd.buf[2] != 0) { | |
0b06c059 GH |
770 | return -1; |
771 | } | |
772 | ||
773 | /* PAGE CODE == 0 */ | |
0b06c059 | 774 | buflen = req->cmd.xfer; |
f01b5931 | 775 | if (buflen > SCSI_MAX_INQUIRY_LEN) { |
0b06c059 | 776 | buflen = SCSI_MAX_INQUIRY_LEN; |
f01b5931 | 777 | } |
0b06c059 | 778 | |
f37bd73b | 779 | outbuf[0] = s->qdev.type & 0x1f; |
bfe3d7ac | 780 | outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0; |
353815aa DF |
781 | |
782 | strpadcpy((char *) &outbuf[16], 16, s->product, ' '); | |
783 | strpadcpy((char *) &outbuf[8], 8, s->vendor, ' '); | |
784 | ||
314b1811 | 785 | memset(&outbuf[32], 0, 4); |
552fee93 | 786 | memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version))); |
99aba0c4 CH |
787 | /* |
788 | * We claim conformance to SPC-3, which is required for guests | |
789 | * to ask for modern features like READ CAPACITY(16) or the | |
790 | * block characteristics VPD page by default. Not all of SPC-3 | |
791 | * is actually implemented, but we're good enough. | |
792 | */ | |
2343be0d | 793 | outbuf[2] = s->qdev.default_scsi_version; |
1109c894 | 794 | outbuf[3] = 2 | 0x10; /* Format 2, HiSup */ |
ad3cea42 AT |
795 | |
796 | if (buflen > 36) { | |
797 | outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */ | |
798 | } else { | |
799 | /* If the allocation length of CDB is too small, | |
800 | the additional length is not adjusted */ | |
801 | outbuf[4] = 36 - 5; | |
802 | } | |
803 | ||
0b06c059 | 804 | /* Sync data transfer and TCQ. */ |
afd4030c | 805 | outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0); |
0b06c059 GH |
806 | return buflen; |
807 | } | |
808 | ||
430ee2f2 PB |
809 | static inline bool media_is_dvd(SCSIDiskState *s) |
810 | { | |
811 | uint64_t nb_sectors; | |
812 | if (s->qdev.type != TYPE_ROM) { | |
813 | return false; | |
814 | } | |
cd723b85 | 815 | if (!blk_is_available(s->qdev.conf.blk)) { |
7d99f4c1 MR |
816 | return false; |
817 | } | |
4be74634 | 818 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
430ee2f2 PB |
819 | return nb_sectors > CD_MAX_SECTORS; |
820 | } | |
821 | ||
ceb792ef PB |
822 | static inline bool media_is_cd(SCSIDiskState *s) |
823 | { | |
824 | uint64_t nb_sectors; | |
825 | if (s->qdev.type != TYPE_ROM) { | |
826 | return false; | |
827 | } | |
cd723b85 | 828 | if (!blk_is_available(s->qdev.conf.blk)) { |
7d99f4c1 MR |
829 | return false; |
830 | } | |
4be74634 | 831 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
ceb792ef PB |
832 | return nb_sectors <= CD_MAX_SECTORS; |
833 | } | |
834 | ||
1a4f0c3a PB |
835 | static int scsi_read_disc_information(SCSIDiskState *s, SCSIDiskReq *r, |
836 | uint8_t *outbuf) | |
837 | { | |
838 | uint8_t type = r->req.cmd.buf[1] & 7; | |
839 | ||
840 | if (s->qdev.type != TYPE_ROM) { | |
841 | return -1; | |
842 | } | |
843 | ||
844 | /* Types 1/2 are only defined for Blu-Ray. */ | |
845 | if (type != 0) { | |
846 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
847 | return -1; | |
848 | } | |
849 | ||
850 | memset(outbuf, 0, 34); | |
851 | outbuf[1] = 32; | |
852 | outbuf[2] = 0xe; /* last session complete, disc finalized */ | |
853 | outbuf[3] = 1; /* first track on disc */ | |
854 | outbuf[4] = 1; /* # of sessions */ | |
855 | outbuf[5] = 1; /* first track of last session */ | |
856 | outbuf[6] = 1; /* last track of last session */ | |
857 | outbuf[7] = 0x20; /* unrestricted use */ | |
858 | outbuf[8] = 0x00; /* CD-ROM or DVD-ROM */ | |
859 | /* 9-10-11: most significant byte corresponding bytes 4-5-6 */ | |
860 | /* 12-23: not meaningful for CD-ROM or DVD-ROM */ | |
861 | /* 24-31: disc bar code */ | |
862 | /* 32: disc application code */ | |
863 | /* 33: number of OPC tables */ | |
864 | ||
865 | return 34; | |
866 | } | |
867 | ||
b6c251ab PB |
868 | static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r, |
869 | uint8_t *outbuf) | |
870 | { | |
ceb792ef PB |
871 | static const int rds_caps_size[5] = { |
872 | [0] = 2048 + 4, | |
873 | [1] = 4 + 4, | |
874 | [3] = 188 + 4, | |
875 | [4] = 2048 + 4, | |
876 | }; | |
877 | ||
878 | uint8_t media = r->req.cmd.buf[1]; | |
879 | uint8_t layer = r->req.cmd.buf[6]; | |
880 | uint8_t format = r->req.cmd.buf[7]; | |
881 | int size = -1; | |
882 | ||
883 | if (s->qdev.type != TYPE_ROM) { | |
884 | return -1; | |
885 | } | |
886 | if (media != 0) { | |
887 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
888 | return -1; | |
889 | } | |
890 | ||
891 | if (format != 0xff) { | |
cd723b85 | 892 | if (!blk_is_available(s->qdev.conf.blk)) { |
ceb792ef PB |
893 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
894 | return -1; | |
895 | } | |
896 | if (media_is_cd(s)) { | |
897 | scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT)); | |
898 | return -1; | |
899 | } | |
900 | if (format >= ARRAY_SIZE(rds_caps_size)) { | |
901 | return -1; | |
902 | } | |
903 | size = rds_caps_size[format]; | |
904 | memset(outbuf, 0, size); | |
905 | } | |
906 | ||
907 | switch (format) { | |
908 | case 0x00: { | |
909 | /* Physical format information */ | |
910 | uint64_t nb_sectors; | |
911 | if (layer != 0) { | |
912 | goto fail; | |
913 | } | |
4be74634 | 914 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
ceb792ef PB |
915 | |
916 | outbuf[4] = 1; /* DVD-ROM, part version 1 */ | |
917 | outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */ | |
918 | outbuf[6] = 1; /* one layer, read-only (per MMC-2 spec) */ | |
919 | outbuf[7] = 0; /* default densities */ | |
920 | ||
921 | stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */ | |
922 | stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */ | |
923 | break; | |
924 | } | |
925 | ||
926 | case 0x01: /* DVD copyright information, all zeros */ | |
927 | break; | |
928 | ||
929 | case 0x03: /* BCA information - invalid field for no BCA info */ | |
930 | return -1; | |
931 | ||
932 | case 0x04: /* DVD disc manufacturing information, all zeros */ | |
933 | break; | |
934 | ||
935 | case 0xff: { /* List capabilities */ | |
936 | int i; | |
937 | size = 4; | |
938 | for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) { | |
939 | if (!rds_caps_size[i]) { | |
940 | continue; | |
941 | } | |
942 | outbuf[size] = i; | |
943 | outbuf[size + 1] = 0x40; /* Not writable, readable */ | |
944 | stw_be_p(&outbuf[size + 2], rds_caps_size[i]); | |
945 | size += 4; | |
946 | } | |
947 | break; | |
948 | } | |
949 | ||
950 | default: | |
951 | return -1; | |
952 | } | |
953 | ||
954 | /* Size of buffer, not including 2 byte size field */ | |
955 | stw_be_p(outbuf, size - 2); | |
956 | return size; | |
957 | ||
958 | fail: | |
b6c251ab PB |
959 | return -1; |
960 | } | |
961 | ||
3c2f7c12 | 962 | static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf) |
b6c251ab | 963 | { |
3c2f7c12 PB |
964 | uint8_t event_code, media_status; |
965 | ||
966 | media_status = 0; | |
967 | if (s->tray_open) { | |
968 | media_status = MS_TRAY_OPEN; | |
4be74634 | 969 | } else if (blk_is_inserted(s->qdev.conf.blk)) { |
3c2f7c12 PB |
970 | media_status = MS_MEDIA_PRESENT; |
971 | } | |
972 | ||
973 | /* Event notification descriptor */ | |
974 | event_code = MEC_NO_CHANGE; | |
4480de19 PB |
975 | if (media_status != MS_TRAY_OPEN) { |
976 | if (s->media_event) { | |
977 | event_code = MEC_NEW_MEDIA; | |
978 | s->media_event = false; | |
979 | } else if (s->eject_request) { | |
980 | event_code = MEC_EJECT_REQUESTED; | |
981 | s->eject_request = false; | |
982 | } | |
3c2f7c12 PB |
983 | } |
984 | ||
985 | outbuf[0] = event_code; | |
986 | outbuf[1] = media_status; | |
987 | ||
988 | /* These fields are reserved, just clear them. */ | |
989 | outbuf[2] = 0; | |
990 | outbuf[3] = 0; | |
991 | return 4; | |
992 | } | |
993 | ||
994 | static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r, | |
995 | uint8_t *outbuf) | |
996 | { | |
997 | int size; | |
998 | uint8_t *buf = r->req.cmd.buf; | |
999 | uint8_t notification_class_request = buf[4]; | |
1000 | if (s->qdev.type != TYPE_ROM) { | |
1001 | return -1; | |
1002 | } | |
1003 | if ((buf[1] & 1) == 0) { | |
1004 | /* asynchronous */ | |
1005 | return -1; | |
1006 | } | |
1007 | ||
1008 | size = 4; | |
1009 | outbuf[0] = outbuf[1] = 0; | |
1010 | outbuf[3] = 1 << GESN_MEDIA; /* supported events */ | |
1011 | if (notification_class_request & (1 << GESN_MEDIA)) { | |
1012 | outbuf[2] = GESN_MEDIA; | |
1013 | size += scsi_event_status_media(s, &outbuf[size]); | |
1014 | } else { | |
1015 | outbuf[2] = 0x80; | |
1016 | } | |
1017 | stw_be_p(outbuf, size - 4); | |
1018 | return size; | |
b6c251ab PB |
1019 | } |
1020 | ||
430ee2f2 | 1021 | static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf) |
b6c251ab | 1022 | { |
430ee2f2 PB |
1023 | int current; |
1024 | ||
b6c251ab PB |
1025 | if (s->qdev.type != TYPE_ROM) { |
1026 | return -1; | |
1027 | } | |
7d99f4c1 MR |
1028 | |
1029 | if (media_is_dvd(s)) { | |
1030 | current = MMC_PROFILE_DVD_ROM; | |
1031 | } else if (media_is_cd(s)) { | |
1032 | current = MMC_PROFILE_CD_ROM; | |
1033 | } else { | |
1034 | current = MMC_PROFILE_NONE; | |
1035 | } | |
1036 | ||
430ee2f2 PB |
1037 | memset(outbuf, 0, 40); |
1038 | stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */ | |
1039 | stw_be_p(&outbuf[6], current); | |
1040 | /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */ | |
1041 | outbuf[10] = 0x03; /* persistent, current */ | |
1042 | outbuf[11] = 8; /* two profiles */ | |
1043 | stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM); | |
1044 | outbuf[14] = (current == MMC_PROFILE_DVD_ROM); | |
1045 | stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM); | |
1046 | outbuf[18] = (current == MMC_PROFILE_CD_ROM); | |
1047 | /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */ | |
1048 | stw_be_p(&outbuf[20], 1); | |
1049 | outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */ | |
1050 | outbuf[23] = 8; | |
1051 | stl_be_p(&outbuf[24], 1); /* SCSI */ | |
1052 | outbuf[28] = 1; /* DBE = 1, mandatory */ | |
1053 | /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */ | |
1054 | stw_be_p(&outbuf[32], 3); | |
1055 | outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */ | |
1056 | outbuf[35] = 4; | |
1057 | outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */ | |
1058 | /* TODO: Random readable, CD read, DVD read, drive serial number, | |
1059 | power management */ | |
1060 | return 40; | |
b6c251ab PB |
1061 | } |
1062 | ||
1063 | static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf) | |
1064 | { | |
1065 | if (s->qdev.type != TYPE_ROM) { | |
1066 | return -1; | |
1067 | } | |
1068 | memset(outbuf, 0, 8); | |
1069 | outbuf[5] = 1; /* CD-ROM */ | |
1070 | return 8; | |
1071 | } | |
1072 | ||
cfc606da | 1073 | static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf, |
282ab04e | 1074 | int page_control) |
ebddfcbe | 1075 | { |
a8f4bbe2 PB |
1076 | static const int mode_sense_valid[0x3f] = { |
1077 | [MODE_PAGE_HD_GEOMETRY] = (1 << TYPE_DISK), | |
1078 | [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK), | |
1079 | [MODE_PAGE_CACHING] = (1 << TYPE_DISK) | (1 << TYPE_ROM), | |
a07c7dcd PB |
1080 | [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM), |
1081 | [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM), | |
a8f4bbe2 PB |
1082 | [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM), |
1083 | }; | |
ef405611 PB |
1084 | |
1085 | uint8_t *p = *p_outbuf + 2; | |
1086 | int length; | |
ebddfcbe | 1087 | |
a8f4bbe2 PB |
1088 | if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) { |
1089 | return -1; | |
1090 | } | |
1091 | ||
282ab04e BK |
1092 | /* |
1093 | * If Changeable Values are requested, a mask denoting those mode parameters | |
1094 | * that are changeable shall be returned. As we currently don't support | |
1095 | * parameter changes via MODE_SELECT all bits are returned set to zero. | |
1096 | * The buffer was already menset to zero by the caller of this function. | |
ef405611 PB |
1097 | * |
1098 | * The offsets here are off by two compared to the descriptions in the | |
1099 | * SCSI specs, because those include a 2-byte header. This is unfortunate, | |
1100 | * but it is done so that offsets are consistent within our implementation | |
1101 | * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both | |
1102 | * 2-byte and 4-byte headers. | |
282ab04e | 1103 | */ |
ebddfcbe | 1104 | switch (page) { |
67cc61e4 | 1105 | case MODE_PAGE_HD_GEOMETRY: |
ef405611 | 1106 | length = 0x16; |
282ab04e | 1107 | if (page_control == 1) { /* Changeable Values */ |
cfc606da | 1108 | break; |
282ab04e | 1109 | } |
ebddfcbe | 1110 | /* if a geometry hint is available, use it */ |
ef405611 PB |
1111 | p[0] = (s->qdev.conf.cyls >> 16) & 0xff; |
1112 | p[1] = (s->qdev.conf.cyls >> 8) & 0xff; | |
1113 | p[2] = s->qdev.conf.cyls & 0xff; | |
1114 | p[3] = s->qdev.conf.heads & 0xff; | |
ebddfcbe | 1115 | /* Write precomp start cylinder, disabled */ |
ef405611 PB |
1116 | p[4] = (s->qdev.conf.cyls >> 16) & 0xff; |
1117 | p[5] = (s->qdev.conf.cyls >> 8) & 0xff; | |
1118 | p[6] = s->qdev.conf.cyls & 0xff; | |
ebddfcbe | 1119 | /* Reduced current start cylinder, disabled */ |
ef405611 PB |
1120 | p[7] = (s->qdev.conf.cyls >> 16) & 0xff; |
1121 | p[8] = (s->qdev.conf.cyls >> 8) & 0xff; | |
1122 | p[9] = s->qdev.conf.cyls & 0xff; | |
ebddfcbe | 1123 | /* Device step rate [ns], 200ns */ |
ef405611 PB |
1124 | p[10] = 0; |
1125 | p[11] = 200; | |
ebddfcbe | 1126 | /* Landing zone cylinder */ |
ef405611 PB |
1127 | p[12] = 0xff; |
1128 | p[13] = 0xff; | |
ebddfcbe | 1129 | p[14] = 0xff; |
ebddfcbe | 1130 | /* Medium rotation rate [rpm], 5400 rpm */ |
ef405611 PB |
1131 | p[18] = (5400 >> 8) & 0xff; |
1132 | p[19] = 5400 & 0xff; | |
cfc606da | 1133 | break; |
ebddfcbe | 1134 | |
67cc61e4 | 1135 | case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY: |
ef405611 | 1136 | length = 0x1e; |
282ab04e | 1137 | if (page_control == 1) { /* Changeable Values */ |
cfc606da | 1138 | break; |
282ab04e | 1139 | } |
ebddfcbe | 1140 | /* Transfer rate [kbit/s], 5Mbit/s */ |
ef405611 PB |
1141 | p[0] = 5000 >> 8; |
1142 | p[1] = 5000 & 0xff; | |
ebddfcbe | 1143 | /* if a geometry hint is available, use it */ |
ef405611 PB |
1144 | p[2] = s->qdev.conf.heads & 0xff; |
1145 | p[3] = s->qdev.conf.secs & 0xff; | |
1146 | p[4] = s->qdev.blocksize >> 8; | |
1147 | p[6] = (s->qdev.conf.cyls >> 8) & 0xff; | |
1148 | p[7] = s->qdev.conf.cyls & 0xff; | |
1149 | /* Write precomp start cylinder, disabled */ | |
d252df48 MA |
1150 | p[8] = (s->qdev.conf.cyls >> 8) & 0xff; |
1151 | p[9] = s->qdev.conf.cyls & 0xff; | |
ef405611 | 1152 | /* Reduced current start cylinder, disabled */ |
d252df48 MA |
1153 | p[10] = (s->qdev.conf.cyls >> 8) & 0xff; |
1154 | p[11] = s->qdev.conf.cyls & 0xff; | |
ebddfcbe | 1155 | /* Device step rate [100us], 100us */ |
ef405611 PB |
1156 | p[12] = 0; |
1157 | p[13] = 1; | |
ebddfcbe | 1158 | /* Device step pulse width [us], 1us */ |
ef405611 | 1159 | p[14] = 1; |
ebddfcbe | 1160 | /* Device head settle delay [100us], 100us */ |
ef405611 PB |
1161 | p[15] = 0; |
1162 | p[16] = 1; | |
ebddfcbe | 1163 | /* Motor on delay [0.1s], 0.1s */ |
ef405611 | 1164 | p[17] = 1; |
ebddfcbe | 1165 | /* Motor off delay [0.1s], 0.1s */ |
ef405611 | 1166 | p[18] = 1; |
ebddfcbe | 1167 | /* Medium rotation rate [rpm], 5400 rpm */ |
ef405611 PB |
1168 | p[26] = (5400 >> 8) & 0xff; |
1169 | p[27] = 5400 & 0xff; | |
cfc606da | 1170 | break; |
ebddfcbe | 1171 | |
67cc61e4 | 1172 | case MODE_PAGE_CACHING: |
ef405611 | 1173 | length = 0x12; |
96c91bbf | 1174 | if (page_control == 1 || /* Changeable Values */ |
4be74634 | 1175 | blk_enable_write_cache(s->qdev.conf.blk)) { |
ef405611 | 1176 | p[0] = 4; /* WCE */ |
ebddfcbe | 1177 | } |
cfc606da | 1178 | break; |
ebddfcbe | 1179 | |
a07c7dcd | 1180 | case MODE_PAGE_R_W_ERROR: |
ef405611 | 1181 | length = 10; |
4f588b15 PB |
1182 | if (page_control == 1) { /* Changeable Values */ |
1183 | break; | |
1184 | } | |
ef405611 | 1185 | p[0] = 0x80; /* Automatic Write Reallocation Enabled */ |
a07c7dcd | 1186 | if (s->qdev.type == TYPE_ROM) { |
ef405611 | 1187 | p[1] = 0x20; /* Read Retry Count */ |
a07c7dcd PB |
1188 | } |
1189 | break; | |
1190 | ||
1191 | case MODE_PAGE_AUDIO_CTL: | |
ef405611 | 1192 | length = 14; |
a07c7dcd PB |
1193 | break; |
1194 | ||
67cc61e4 | 1195 | case MODE_PAGE_CAPABILITIES: |
ef405611 | 1196 | length = 0x14; |
282ab04e | 1197 | if (page_control == 1) { /* Changeable Values */ |
cfc606da | 1198 | break; |
282ab04e | 1199 | } |
a07c7dcd | 1200 | |
ef405611 PB |
1201 | p[0] = 0x3b; /* CD-R & CD-RW read */ |
1202 | p[1] = 0; /* Writing not supported */ | |
1203 | p[2] = 0x7f; /* Audio, composite, digital out, | |
ebddfcbe | 1204 | mode 2 form 1&2, multi session */ |
ef405611 | 1205 | p[3] = 0xff; /* CD DA, DA accurate, RW supported, |
ebddfcbe GH |
1206 | RW corrected, C2 errors, ISRC, |
1207 | UPC, Bar code */ | |
ef405611 | 1208 | p[4] = 0x2d | (s->tray_locked ? 2 : 0); |
ebddfcbe | 1209 | /* Locking supported, jumper present, eject, tray */ |
ef405611 | 1210 | p[5] = 0; /* no volume & mute control, no |
ebddfcbe | 1211 | changer */ |
ef405611 PB |
1212 | p[6] = (50 * 176) >> 8; /* 50x read speed */ |
1213 | p[7] = (50 * 176) & 0xff; | |
1214 | p[8] = 2 >> 8; /* Two volume levels */ | |
1215 | p[9] = 2 & 0xff; | |
1216 | p[10] = 2048 >> 8; /* 2M buffer */ | |
1217 | p[11] = 2048 & 0xff; | |
1218 | p[12] = (16 * 176) >> 8; /* 16x read speed current */ | |
1219 | p[13] = (16 * 176) & 0xff; | |
1220 | p[16] = (16 * 176) >> 8; /* 16x write speed */ | |
1221 | p[17] = (16 * 176) & 0xff; | |
1222 | p[18] = (16 * 176) >> 8; /* 16x write speed current */ | |
ebddfcbe | 1223 | p[19] = (16 * 176) & 0xff; |
cfc606da | 1224 | break; |
ebddfcbe GH |
1225 | |
1226 | default: | |
cfc606da | 1227 | return -1; |
ebddfcbe | 1228 | } |
cfc606da | 1229 | |
ef405611 PB |
1230 | assert(length < 256); |
1231 | (*p_outbuf)[0] = page; | |
1232 | (*p_outbuf)[1] = length; | |
1233 | *p_outbuf += length + 2; | |
1234 | return length + 2; | |
ebddfcbe GH |
1235 | } |
1236 | ||
cfc606da | 1237 | static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf) |
ebddfcbe | 1238 | { |
cfc606da | 1239 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
ebddfcbe | 1240 | uint64_t nb_sectors; |
e590ecbe PB |
1241 | bool dbd; |
1242 | int page, buflen, ret, page_control; | |
ebddfcbe | 1243 | uint8_t *p; |
ce512ee1 | 1244 | uint8_t dev_specific_param; |
ebddfcbe | 1245 | |
e590ecbe | 1246 | dbd = (r->req.cmd.buf[1] & 0x8) != 0; |
cfc606da PB |
1247 | page = r->req.cmd.buf[2] & 0x3f; |
1248 | page_control = (r->req.cmd.buf[2] & 0xc0) >> 6; | |
59ee9500 LV |
1249 | |
1250 | trace_scsi_disk_emulate_mode_sense((r->req.cmd.buf[0] == MODE_SENSE) ? 6 : | |
1251 | 10, page, r->req.cmd.xfer, page_control); | |
cfc606da | 1252 | memset(outbuf, 0, r->req.cmd.xfer); |
ebddfcbe GH |
1253 | p = outbuf; |
1254 | ||
e590ecbe | 1255 | if (s->qdev.type == TYPE_DISK) { |
da8365db | 1256 | dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0; |
4be74634 | 1257 | if (blk_is_read_only(s->qdev.conf.blk)) { |
e590ecbe PB |
1258 | dev_specific_param |= 0x80; /* Readonly. */ |
1259 | } | |
ce512ee1 | 1260 | } else { |
e590ecbe PB |
1261 | /* MMC prescribes that CD/DVD drives have no block descriptors, |
1262 | * and defines no device-specific parameter. */ | |
6a2de0f2 | 1263 | dev_specific_param = 0x00; |
e590ecbe | 1264 | dbd = true; |
ce512ee1 BK |
1265 | } |
1266 | ||
cfc606da | 1267 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
ce512ee1 BK |
1268 | p[1] = 0; /* Default media type. */ |
1269 | p[2] = dev_specific_param; | |
1270 | p[3] = 0; /* Block descriptor length. */ | |
1271 | p += 4; | |
1272 | } else { /* MODE_SENSE_10 */ | |
1273 | p[2] = 0; /* Default media type. */ | |
1274 | p[3] = dev_specific_param; | |
1275 | p[6] = p[7] = 0; /* Block descriptor length. */ | |
1276 | p += 8; | |
ebddfcbe | 1277 | } |
ebddfcbe | 1278 | |
4be74634 | 1279 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
e590ecbe | 1280 | if (!dbd && nb_sectors) { |
cfc606da | 1281 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
ce512ee1 BK |
1282 | outbuf[3] = 8; /* Block descriptor length */ |
1283 | } else { /* MODE_SENSE_10 */ | |
1284 | outbuf[7] = 8; /* Block descriptor length */ | |
1285 | } | |
69377307 | 1286 | nb_sectors /= (s->qdev.blocksize / 512); |
f01b5931 | 1287 | if (nb_sectors > 0xffffff) { |
2488b740 | 1288 | nb_sectors = 0; |
f01b5931 | 1289 | } |
ebddfcbe GH |
1290 | p[0] = 0; /* media density code */ |
1291 | p[1] = (nb_sectors >> 16) & 0xff; | |
1292 | p[2] = (nb_sectors >> 8) & 0xff; | |
1293 | p[3] = nb_sectors & 0xff; | |
1294 | p[4] = 0; /* reserved */ | |
1295 | p[5] = 0; /* bytes 5-7 are the sector size in bytes */ | |
69377307 | 1296 | p[6] = s->qdev.blocksize >> 8; |
ebddfcbe GH |
1297 | p[7] = 0; |
1298 | p += 8; | |
1299 | } | |
1300 | ||
cfc606da PB |
1301 | if (page_control == 3) { |
1302 | /* Saved Values */ | |
1303 | scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED)); | |
1304 | return -1; | |
282ab04e BK |
1305 | } |
1306 | ||
cfc606da PB |
1307 | if (page == 0x3f) { |
1308 | for (page = 0; page <= 0x3e; page++) { | |
1309 | mode_sense_page(s, page, &p, page_control); | |
1310 | } | |
1311 | } else { | |
1312 | ret = mode_sense_page(s, page, &p, page_control); | |
1313 | if (ret == -1) { | |
1314 | return -1; | |
1315 | } | |
ebddfcbe GH |
1316 | } |
1317 | ||
1318 | buflen = p - outbuf; | |
ce512ee1 BK |
1319 | /* |
1320 | * The mode data length field specifies the length in bytes of the | |
1321 | * following data that is available to be transferred. The mode data | |
1322 | * length does not include itself. | |
1323 | */ | |
cfc606da | 1324 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
ce512ee1 BK |
1325 | outbuf[0] = buflen - 1; |
1326 | } else { /* MODE_SENSE_10 */ | |
1327 | outbuf[0] = ((buflen - 2) >> 8) & 0xff; | |
1328 | outbuf[1] = (buflen - 2) & 0xff; | |
1329 | } | |
ebddfcbe GH |
1330 | return buflen; |
1331 | } | |
1332 | ||
02880f43 GH |
1333 | static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf) |
1334 | { | |
1335 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); | |
02880f43 GH |
1336 | int start_track, format, msf, toclen; |
1337 | uint64_t nb_sectors; | |
1338 | ||
1339 | msf = req->cmd.buf[1] & 2; | |
1340 | format = req->cmd.buf[2] & 0xf; | |
1341 | start_track = req->cmd.buf[6]; | |
4be74634 | 1342 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
59ee9500 | 1343 | trace_scsi_disk_emulate_read_toc(start_track, format, msf >> 1); |
69377307 | 1344 | nb_sectors /= s->qdev.blocksize / 512; |
02880f43 GH |
1345 | switch (format) { |
1346 | case 0: | |
1347 | toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track); | |
1348 | break; | |
1349 | case 1: | |
1350 | /* multi session : only a single session defined */ | |
1351 | toclen = 12; | |
1352 | memset(outbuf, 0, 12); | |
1353 | outbuf[1] = 0x0a; | |
1354 | outbuf[2] = 0x01; | |
1355 | outbuf[3] = 0x01; | |
1356 | break; | |
1357 | case 2: | |
1358 | toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track); | |
1359 | break; | |
1360 | default: | |
1361 | return -1; | |
1362 | } | |
02880f43 GH |
1363 | return toclen; |
1364 | } | |
1365 | ||
68bb01f3 | 1366 | static int scsi_disk_emulate_start_stop(SCSIDiskReq *r) |
bfd52647 MA |
1367 | { |
1368 | SCSIRequest *req = &r->req; | |
1369 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); | |
1370 | bool start = req->cmd.buf[4] & 1; | |
1371 | bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */ | |
ae5708b3 RS |
1372 | int pwrcnd = req->cmd.buf[4] & 0xf0; |
1373 | ||
1374 | if (pwrcnd) { | |
1375 | /* eject/load only happens for power condition == 0 */ | |
1376 | return 0; | |
1377 | } | |
bfd52647 | 1378 | |
b456a71c | 1379 | if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && loej) { |
68bb01f3 MA |
1380 | if (!start && !s->tray_open && s->tray_locked) { |
1381 | scsi_check_condition(r, | |
4be74634 | 1382 | blk_is_inserted(s->qdev.conf.blk) |
68bb01f3 MA |
1383 | ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED) |
1384 | : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED)); | |
1385 | return -1; | |
fdec4404 | 1386 | } |
d88b1819 LC |
1387 | |
1388 | if (s->tray_open != !start) { | |
4be74634 | 1389 | blk_eject(s->qdev.conf.blk, !start); |
d88b1819 LC |
1390 | s->tray_open = !start; |
1391 | } | |
bfd52647 | 1392 | } |
68bb01f3 | 1393 | return 0; |
bfd52647 MA |
1394 | } |
1395 | ||
314a3299 PB |
1396 | static void scsi_disk_emulate_read_data(SCSIRequest *req) |
1397 | { | |
1398 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); | |
1399 | int buflen = r->iov.iov_len; | |
1400 | ||
1401 | if (buflen) { | |
59ee9500 | 1402 | trace_scsi_disk_emulate_read_data(buflen); |
314a3299 PB |
1403 | r->iov.iov_len = 0; |
1404 | r->started = true; | |
1405 | scsi_req_data(&r->req, buflen); | |
1406 | return; | |
1407 | } | |
1408 | ||
1409 | /* This also clears the sense buffer for REQUEST SENSE. */ | |
1410 | scsi_req_complete(&r->req, GOOD); | |
1411 | } | |
1412 | ||
380feaff PB |
1413 | static int scsi_disk_check_mode_select(SCSIDiskState *s, int page, |
1414 | uint8_t *inbuf, int inlen) | |
1415 | { | |
1416 | uint8_t mode_current[SCSI_MAX_MODE_LEN]; | |
1417 | uint8_t mode_changeable[SCSI_MAX_MODE_LEN]; | |
1418 | uint8_t *p; | |
1419 | int len, expected_len, changeable_len, i; | |
1420 | ||
1421 | /* The input buffer does not include the page header, so it is | |
1422 | * off by 2 bytes. | |
1423 | */ | |
1424 | expected_len = inlen + 2; | |
1425 | if (expected_len > SCSI_MAX_MODE_LEN) { | |
1426 | return -1; | |
1427 | } | |
1428 | ||
1429 | p = mode_current; | |
1430 | memset(mode_current, 0, inlen + 2); | |
1431 | len = mode_sense_page(s, page, &p, 0); | |
1432 | if (len < 0 || len != expected_len) { | |
1433 | return -1; | |
1434 | } | |
1435 | ||
1436 | p = mode_changeable; | |
1437 | memset(mode_changeable, 0, inlen + 2); | |
1438 | changeable_len = mode_sense_page(s, page, &p, 1); | |
1439 | assert(changeable_len == len); | |
1440 | ||
1441 | /* Check that unchangeable bits are the same as what MODE SENSE | |
1442 | * would return. | |
1443 | */ | |
1444 | for (i = 2; i < len; i++) { | |
1445 | if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) != 0) { | |
1446 | return -1; | |
1447 | } | |
1448 | } | |
1449 | return 0; | |
1450 | } | |
1451 | ||
1452 | static void scsi_disk_apply_mode_select(SCSIDiskState *s, int page, uint8_t *p) | |
1453 | { | |
96c91bbf PB |
1454 | switch (page) { |
1455 | case MODE_PAGE_CACHING: | |
4be74634 | 1456 | blk_set_enable_write_cache(s->qdev.conf.blk, (p[0] & 4) != 0); |
96c91bbf PB |
1457 | break; |
1458 | ||
1459 | default: | |
1460 | break; | |
1461 | } | |
380feaff PB |
1462 | } |
1463 | ||
1464 | static int mode_select_pages(SCSIDiskReq *r, uint8_t *p, int len, bool change) | |
1465 | { | |
1466 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
1467 | ||
1468 | while (len > 0) { | |
1469 | int page, subpage, page_len; | |
1470 | ||
1471 | /* Parse both possible formats for the mode page headers. */ | |
1472 | page = p[0] & 0x3f; | |
1473 | if (p[0] & 0x40) { | |
1474 | if (len < 4) { | |
1475 | goto invalid_param_len; | |
1476 | } | |
1477 | subpage = p[1]; | |
1478 | page_len = lduw_be_p(&p[2]); | |
1479 | p += 4; | |
1480 | len -= 4; | |
1481 | } else { | |
1482 | if (len < 2) { | |
1483 | goto invalid_param_len; | |
1484 | } | |
1485 | subpage = 0; | |
1486 | page_len = p[1]; | |
1487 | p += 2; | |
1488 | len -= 2; | |
1489 | } | |
1490 | ||
1491 | if (subpage) { | |
1492 | goto invalid_param; | |
1493 | } | |
1494 | if (page_len > len) { | |
1495 | goto invalid_param_len; | |
1496 | } | |
1497 | ||
1498 | if (!change) { | |
1499 | if (scsi_disk_check_mode_select(s, page, p, page_len) < 0) { | |
1500 | goto invalid_param; | |
1501 | } | |
1502 | } else { | |
1503 | scsi_disk_apply_mode_select(s, page, p); | |
1504 | } | |
1505 | ||
1506 | p += page_len; | |
1507 | len -= page_len; | |
1508 | } | |
1509 | return 0; | |
1510 | ||
1511 | invalid_param: | |
1512 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); | |
1513 | return -1; | |
1514 | ||
1515 | invalid_param_len: | |
1516 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); | |
1517 | return -1; | |
1518 | } | |
1519 | ||
1520 | static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf) | |
1521 | { | |
accfeb2d | 1522 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
380feaff PB |
1523 | uint8_t *p = inbuf; |
1524 | int cmd = r->req.cmd.buf[0]; | |
1525 | int len = r->req.cmd.xfer; | |
1526 | int hdr_len = (cmd == MODE_SELECT ? 4 : 8); | |
1527 | int bd_len; | |
1528 | int pass; | |
1529 | ||
1530 | /* We only support PF=1, SP=0. */ | |
1531 | if ((r->req.cmd.buf[1] & 0x11) != 0x10) { | |
1532 | goto invalid_field; | |
1533 | } | |
1534 | ||
1535 | if (len < hdr_len) { | |
1536 | goto invalid_param_len; | |
1537 | } | |
1538 | ||
1539 | bd_len = (cmd == MODE_SELECT ? p[3] : lduw_be_p(&p[6])); | |
1540 | len -= hdr_len; | |
1541 | p += hdr_len; | |
1542 | if (len < bd_len) { | |
1543 | goto invalid_param_len; | |
1544 | } | |
1545 | if (bd_len != 0 && bd_len != 8) { | |
1546 | goto invalid_param; | |
1547 | } | |
1548 | ||
1549 | len -= bd_len; | |
1550 | p += bd_len; | |
1551 | ||
1552 | /* Ensure no change is made if there is an error! */ | |
1553 | for (pass = 0; pass < 2; pass++) { | |
1554 | if (mode_select_pages(r, p, len, pass == 1) < 0) { | |
1555 | assert(pass == 0); | |
1556 | return; | |
1557 | } | |
1558 | } | |
4be74634 | 1559 | if (!blk_enable_write_cache(s->qdev.conf.blk)) { |
accfeb2d PB |
1560 | /* The request is used as the AIO opaque value, so add a ref. */ |
1561 | scsi_req_ref(&r->req); | |
4be74634 | 1562 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
5366d0c8 | 1563 | BLOCK_ACCT_FLUSH); |
4be74634 | 1564 | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r); |
accfeb2d PB |
1565 | return; |
1566 | } | |
1567 | ||
380feaff PB |
1568 | scsi_req_complete(&r->req, GOOD); |
1569 | return; | |
1570 | ||
1571 | invalid_param: | |
1572 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); | |
1573 | return; | |
1574 | ||
1575 | invalid_param_len: | |
1576 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); | |
1577 | return; | |
1578 | ||
1579 | invalid_field: | |
1580 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
380feaff PB |
1581 | } |
1582 | ||
444bc908 PB |
1583 | static inline bool check_lba_range(SCSIDiskState *s, |
1584 | uint64_t sector_num, uint32_t nb_sectors) | |
1585 | { | |
1586 | /* | |
1587 | * The first line tests that no overflow happens when computing the last | |
1588 | * sector. The second line tests that the last accessed sector is in | |
1589 | * range. | |
12ca76fc PB |
1590 | * |
1591 | * Careful, the computations should not underflow for nb_sectors == 0, | |
1592 | * and a 0-block read to the first LBA beyond the end of device is | |
1593 | * valid. | |
444bc908 PB |
1594 | */ |
1595 | return (sector_num <= sector_num + nb_sectors && | |
12ca76fc | 1596 | sector_num + nb_sectors <= s->qdev.max_lba + 1); |
444bc908 PB |
1597 | } |
1598 | ||
5222aaf2 PB |
1599 | typedef struct UnmapCBData { |
1600 | SCSIDiskReq *r; | |
1601 | uint8_t *inbuf; | |
1602 | int count; | |
1603 | } UnmapCBData; | |
1604 | ||
5fd2b563 PB |
1605 | static void scsi_unmap_complete(void *opaque, int ret); |
1606 | ||
1607 | static void scsi_unmap_complete_noio(UnmapCBData *data, int ret) | |
5222aaf2 | 1608 | { |
5222aaf2 PB |
1609 | SCSIDiskReq *r = data->r; |
1610 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
1611 | uint64_t sector_num; | |
5bb0b62e | 1612 | uint32_t nb_sectors; |
5222aaf2 | 1613 | |
5fd2b563 | 1614 | assert(r->req.aiocb == NULL); |
5b956f41 | 1615 | if (scsi_disk_req_check_error(r, ret, false)) { |
d0242ead PB |
1616 | goto done; |
1617 | } | |
1618 | ||
d0242ead | 1619 | if (data->count > 0) { |
5222aaf2 PB |
1620 | sector_num = ldq_be_p(&data->inbuf[0]); |
1621 | nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL; | |
444bc908 | 1622 | if (!check_lba_range(s, sector_num, nb_sectors)) { |
5222aaf2 PB |
1623 | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
1624 | goto done; | |
1625 | } | |
1626 | ||
1c6c4bb7 EB |
1627 | r->req.aiocb = blk_aio_pdiscard(s->qdev.conf.blk, |
1628 | sector_num * s->qdev.blocksize, | |
1629 | nb_sectors * s->qdev.blocksize, | |
1630 | scsi_unmap_complete, data); | |
5222aaf2 PB |
1631 | data->count--; |
1632 | data->inbuf += 16; | |
1633 | return; | |
1634 | } | |
1635 | ||
d0242ead PB |
1636 | scsi_req_complete(&r->req, GOOD); |
1637 | ||
5222aaf2 | 1638 | done: |
3df9caf8 | 1639 | scsi_req_unref(&r->req); |
5222aaf2 PB |
1640 | g_free(data); |
1641 | } | |
1642 | ||
5fd2b563 PB |
1643 | static void scsi_unmap_complete(void *opaque, int ret) |
1644 | { | |
1645 | UnmapCBData *data = opaque; | |
1646 | SCSIDiskReq *r = data->r; | |
b9e413dd | 1647 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
5fd2b563 PB |
1648 | |
1649 | assert(r->req.aiocb != NULL); | |
1650 | r->req.aiocb = NULL; | |
1651 | ||
b9e413dd | 1652 | aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); |
5fd2b563 | 1653 | scsi_unmap_complete_noio(data, ret); |
b9e413dd | 1654 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
5fd2b563 PB |
1655 | } |
1656 | ||
5222aaf2 PB |
1657 | static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf) |
1658 | { | |
c5fd1fb0 | 1659 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
5222aaf2 PB |
1660 | uint8_t *p = inbuf; |
1661 | int len = r->req.cmd.xfer; | |
1662 | UnmapCBData *data; | |
1663 | ||
823bd739 PB |
1664 | /* Reject ANCHOR=1. */ |
1665 | if (r->req.cmd.buf[1] & 0x1) { | |
1666 | goto invalid_field; | |
1667 | } | |
1668 | ||
5222aaf2 PB |
1669 | if (len < 8) { |
1670 | goto invalid_param_len; | |
1671 | } | |
1672 | if (len < lduw_be_p(&p[0]) + 2) { | |
1673 | goto invalid_param_len; | |
1674 | } | |
1675 | if (len < lduw_be_p(&p[2]) + 8) { | |
1676 | goto invalid_param_len; | |
1677 | } | |
1678 | if (lduw_be_p(&p[2]) & 15) { | |
1679 | goto invalid_param_len; | |
1680 | } | |
1681 | ||
4be74634 | 1682 | if (blk_is_read_only(s->qdev.conf.blk)) { |
c5fd1fb0 PB |
1683 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
1684 | return; | |
1685 | } | |
1686 | ||
5222aaf2 PB |
1687 | data = g_new0(UnmapCBData, 1); |
1688 | data->r = r; | |
1689 | data->inbuf = &p[8]; | |
1690 | data->count = lduw_be_p(&p[2]) >> 4; | |
1691 | ||
1692 | /* The matching unref is in scsi_unmap_complete, before data is freed. */ | |
1693 | scsi_req_ref(&r->req); | |
5fd2b563 | 1694 | scsi_unmap_complete_noio(data, 0); |
5222aaf2 PB |
1695 | return; |
1696 | ||
1697 | invalid_param_len: | |
1698 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); | |
823bd739 PB |
1699 | return; |
1700 | ||
1701 | invalid_field: | |
1702 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
5222aaf2 PB |
1703 | } |
1704 | ||
84f94a9a PB |
1705 | typedef struct WriteSameCBData { |
1706 | SCSIDiskReq *r; | |
1707 | int64_t sector; | |
1708 | int nb_sectors; | |
1709 | QEMUIOVector qiov; | |
1710 | struct iovec iov; | |
1711 | } WriteSameCBData; | |
1712 | ||
1713 | static void scsi_write_same_complete(void *opaque, int ret) | |
1714 | { | |
1715 | WriteSameCBData *data = opaque; | |
1716 | SCSIDiskReq *r = data->r; | |
1717 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
1718 | ||
1719 | assert(r->req.aiocb != NULL); | |
1720 | r->req.aiocb = NULL; | |
b9e413dd | 1721 | aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); |
5b956f41 | 1722 | if (scsi_disk_req_check_error(r, ret, true)) { |
84f94a9a PB |
1723 | goto done; |
1724 | } | |
1725 | ||
d7628080 AG |
1726 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
1727 | ||
84f94a9a PB |
1728 | data->nb_sectors -= data->iov.iov_len / 512; |
1729 | data->sector += data->iov.iov_len / 512; | |
1730 | data->iov.iov_len = MIN(data->nb_sectors * 512, data->iov.iov_len); | |
1731 | if (data->iov.iov_len) { | |
4be74634 | 1732 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
5366d0c8 | 1733 | data->iov.iov_len, BLOCK_ACCT_WRITE); |
03c90063 EB |
1734 | /* Reinitialize qiov, to handle unaligned WRITE SAME request |
1735 | * where final qiov may need smaller size */ | |
a56537a1 | 1736 | qemu_iovec_init_external(&data->qiov, &data->iov, 1); |
03c90063 EB |
1737 | r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk, |
1738 | data->sector << BDRV_SECTOR_BITS, | |
1739 | &data->qiov, 0, | |
1740 | scsi_write_same_complete, data); | |
24355b79 | 1741 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
84f94a9a PB |
1742 | return; |
1743 | } | |
1744 | ||
1745 | scsi_req_complete(&r->req, GOOD); | |
1746 | ||
1747 | done: | |
3df9caf8 | 1748 | scsi_req_unref(&r->req); |
84f94a9a PB |
1749 | qemu_vfree(data->iov.iov_base); |
1750 | g_free(data); | |
b9e413dd | 1751 | aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); |
84f94a9a PB |
1752 | } |
1753 | ||
1754 | static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf) | |
1755 | { | |
1756 | SCSIRequest *req = &r->req; | |
1757 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); | |
1894df02 | 1758 | uint32_t nb_sectors = scsi_data_cdb_xfer(r->req.cmd.buf); |
84f94a9a PB |
1759 | WriteSameCBData *data; |
1760 | uint8_t *buf; | |
1761 | int i; | |
1762 | ||
1763 | /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */ | |
1764 | if (nb_sectors == 0 || (req->cmd.buf[1] & 0x16)) { | |
1765 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
1766 | return; | |
1767 | } | |
1768 | ||
4be74634 | 1769 | if (blk_is_read_only(s->qdev.conf.blk)) { |
84f94a9a PB |
1770 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
1771 | return; | |
1772 | } | |
1773 | if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) { | |
1774 | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); | |
1775 | return; | |
1776 | } | |
1777 | ||
4397a018 | 1778 | if ((req->cmd.buf[1] & 0x1) || buffer_is_zero(inbuf, s->qdev.blocksize)) { |
84f94a9a PB |
1779 | int flags = (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : 0; |
1780 | ||
1781 | /* The request is used as the AIO opaque value, so add a ref. */ | |
1782 | scsi_req_ref(&r->req); | |
4be74634 | 1783 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
5366d0c8 BC |
1784 | nb_sectors * s->qdev.blocksize, |
1785 | BLOCK_ACCT_WRITE); | |
d004bd52 | 1786 | r->req.aiocb = blk_aio_pwrite_zeroes(s->qdev.conf.blk, |
983a1600 EB |
1787 | r->req.cmd.lba * s->qdev.blocksize, |
1788 | nb_sectors * s->qdev.blocksize, | |
4be74634 | 1789 | flags, scsi_aio_complete, r); |
84f94a9a PB |
1790 | return; |
1791 | } | |
1792 | ||
1793 | data = g_new0(WriteSameCBData, 1); | |
1794 | data->r = r; | |
1795 | data->sector = r->req.cmd.lba * (s->qdev.blocksize / 512); | |
1796 | data->nb_sectors = nb_sectors * (s->qdev.blocksize / 512); | |
1797 | data->iov.iov_len = MIN(data->nb_sectors * 512, SCSI_WRITE_SAME_MAX); | |
4be74634 MA |
1798 | data->iov.iov_base = buf = blk_blockalign(s->qdev.conf.blk, |
1799 | data->iov.iov_len); | |
84f94a9a PB |
1800 | qemu_iovec_init_external(&data->qiov, &data->iov, 1); |
1801 | ||
1802 | for (i = 0; i < data->iov.iov_len; i += s->qdev.blocksize) { | |
1803 | memcpy(&buf[i], inbuf, s->qdev.blocksize); | |
1804 | } | |
1805 | ||
1806 | scsi_req_ref(&r->req); | |
4be74634 | 1807 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
5366d0c8 | 1808 | data->iov.iov_len, BLOCK_ACCT_WRITE); |
03c90063 EB |
1809 | r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk, |
1810 | data->sector << BDRV_SECTOR_BITS, | |
1811 | &data->qiov, 0, | |
1812 | scsi_write_same_complete, data); | |
84f94a9a PB |
1813 | } |
1814 | ||
314a3299 PB |
1815 | static void scsi_disk_emulate_write_data(SCSIRequest *req) |
1816 | { | |
af6d510d PB |
1817 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
1818 | ||
1819 | if (r->iov.iov_len) { | |
1820 | int buflen = r->iov.iov_len; | |
59ee9500 | 1821 | trace_scsi_disk_emulate_write_data(buflen); |
af6d510d PB |
1822 | r->iov.iov_len = 0; |
1823 | scsi_req_data(&r->req, buflen); | |
1824 | return; | |
1825 | } | |
1826 | ||
1827 | switch (req->cmd.buf[0]) { | |
1828 | case MODE_SELECT: | |
1829 | case MODE_SELECT_10: | |
1830 | /* This also clears the sense buffer for REQUEST SENSE. */ | |
380feaff | 1831 | scsi_disk_emulate_mode_select(r, r->iov.iov_base); |
af6d510d PB |
1832 | break; |
1833 | ||
5222aaf2 PB |
1834 | case UNMAP: |
1835 | scsi_disk_emulate_unmap(r, r->iov.iov_base); | |
1836 | break; | |
1837 | ||
d97e7730 PB |
1838 | case VERIFY_10: |
1839 | case VERIFY_12: | |
1840 | case VERIFY_16: | |
1841 | if (r->req.status == -1) { | |
1842 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
1843 | } | |
1844 | break; | |
1845 | ||
84f94a9a PB |
1846 | case WRITE_SAME_10: |
1847 | case WRITE_SAME_16: | |
1848 | scsi_disk_emulate_write_same(r, r->iov.iov_base); | |
1849 | break; | |
d97e7730 | 1850 | |
af6d510d PB |
1851 | default: |
1852 | abort(); | |
1853 | } | |
314a3299 PB |
1854 | } |
1855 | ||
b08d0ea0 | 1856 | static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf) |
aa5dbdc1 | 1857 | { |
b08d0ea0 | 1858 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
e7e25e32 | 1859 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
e7e25e32 | 1860 | uint64_t nb_sectors; |
7285477a | 1861 | uint8_t *outbuf; |
af6d510d | 1862 | int buflen; |
aa5dbdc1 | 1863 | |
b08d0ea0 PB |
1864 | switch (req->cmd.buf[0]) { |
1865 | case INQUIRY: | |
1866 | case MODE_SENSE: | |
1867 | case MODE_SENSE_10: | |
1868 | case RESERVE: | |
1869 | case RESERVE_10: | |
1870 | case RELEASE: | |
1871 | case RELEASE_10: | |
1872 | case START_STOP: | |
1873 | case ALLOW_MEDIUM_REMOVAL: | |
1874 | case GET_CONFIGURATION: | |
1875 | case GET_EVENT_STATUS_NOTIFICATION: | |
1876 | case MECHANISM_STATUS: | |
1877 | case REQUEST_SENSE: | |
1878 | break; | |
1879 | ||
1880 | default: | |
cd723b85 | 1881 | if (!blk_is_available(s->qdev.conf.blk)) { |
b08d0ea0 PB |
1882 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
1883 | return 0; | |
1884 | } | |
1885 | break; | |
1886 | } | |
1887 | ||
c8dcb531 PB |
1888 | /* |
1889 | * FIXME: we shouldn't return anything bigger than 4k, but the code | |
1890 | * requires the buffer to be as big as req->cmd.xfer in several | |
1891 | * places. So, do not allow CDBs with a very large ALLOCATION | |
1892 | * LENGTH. The real fix would be to modify scsi_read_data and | |
1893 | * dma_buf_read, so that they return data beyond the buflen | |
1894 | * as all zeros. | |
1895 | */ | |
1896 | if (req->cmd.xfer > 65536) { | |
1897 | goto illegal_request; | |
1898 | } | |
1899 | r->buflen = MAX(4096, req->cmd.xfer); | |
1900 | ||
7285477a | 1901 | if (!r->iov.iov_base) { |
4be74634 | 1902 | r->iov.iov_base = blk_blockalign(s->qdev.conf.blk, r->buflen); |
7285477a PB |
1903 | } |
1904 | ||
af6d510d | 1905 | buflen = req->cmd.xfer; |
7285477a | 1906 | outbuf = r->iov.iov_base; |
c8dcb531 | 1907 | memset(outbuf, 0, r->buflen); |
aa5dbdc1 GH |
1908 | switch (req->cmd.buf[0]) { |
1909 | case TEST_UNIT_READY: | |
cd723b85 | 1910 | assert(blk_is_available(s->qdev.conf.blk)); |
5f71d32f | 1911 | break; |
0b06c059 GH |
1912 | case INQUIRY: |
1913 | buflen = scsi_disk_emulate_inquiry(req, outbuf); | |
f01b5931 | 1914 | if (buflen < 0) { |
0b06c059 | 1915 | goto illegal_request; |
f01b5931 | 1916 | } |
5f71d32f | 1917 | break; |
ebddfcbe GH |
1918 | case MODE_SENSE: |
1919 | case MODE_SENSE_10: | |
cfc606da | 1920 | buflen = scsi_disk_emulate_mode_sense(r, outbuf); |
f01b5931 | 1921 | if (buflen < 0) { |
ebddfcbe | 1922 | goto illegal_request; |
f01b5931 | 1923 | } |
ebddfcbe | 1924 | break; |
02880f43 GH |
1925 | case READ_TOC: |
1926 | buflen = scsi_disk_emulate_read_toc(req, outbuf); | |
f01b5931 | 1927 | if (buflen < 0) { |
02880f43 | 1928 | goto illegal_request; |
f01b5931 | 1929 | } |
02880f43 | 1930 | break; |
3d53ba18 | 1931 | case RESERVE: |
f01b5931 | 1932 | if (req->cmd.buf[1] & 1) { |
3d53ba18 | 1933 | goto illegal_request; |
f01b5931 | 1934 | } |
3d53ba18 GH |
1935 | break; |
1936 | case RESERVE_10: | |
f01b5931 | 1937 | if (req->cmd.buf[1] & 3) { |
3d53ba18 | 1938 | goto illegal_request; |
f01b5931 | 1939 | } |
3d53ba18 GH |
1940 | break; |
1941 | case RELEASE: | |
f01b5931 | 1942 | if (req->cmd.buf[1] & 1) { |
3d53ba18 | 1943 | goto illegal_request; |
f01b5931 | 1944 | } |
3d53ba18 GH |
1945 | break; |
1946 | case RELEASE_10: | |
f01b5931 | 1947 | if (req->cmd.buf[1] & 3) { |
3d53ba18 | 1948 | goto illegal_request; |
f01b5931 | 1949 | } |
3d53ba18 | 1950 | break; |
8d3628ff | 1951 | case START_STOP: |
68bb01f3 | 1952 | if (scsi_disk_emulate_start_stop(r) < 0) { |
b08d0ea0 | 1953 | return 0; |
68bb01f3 | 1954 | } |
5f71d32f | 1955 | break; |
c68b9f34 | 1956 | case ALLOW_MEDIUM_REMOVAL: |
81b1008d | 1957 | s->tray_locked = req->cmd.buf[4] & 1; |
4be74634 | 1958 | blk_lock_medium(s->qdev.conf.blk, req->cmd.buf[4] & 1); |
5f71d32f | 1959 | break; |
5e30a07d | 1960 | case READ_CAPACITY_10: |
e7e25e32 | 1961 | /* The normal LEN field for this command is zero. */ |
5f71d32f | 1962 | memset(outbuf, 0, 8); |
4be74634 | 1963 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
f01b5931 | 1964 | if (!nb_sectors) { |
9bcaf4fe | 1965 | scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY)); |
0369f06f | 1966 | return 0; |
f01b5931 | 1967 | } |
7cec78b6 PB |
1968 | if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) { |
1969 | goto illegal_request; | |
1970 | } | |
69377307 | 1971 | nb_sectors /= s->qdev.blocksize / 512; |
e7e25e32 GH |
1972 | /* Returned value is the address of the last sector. */ |
1973 | nb_sectors--; | |
1974 | /* Remember the new size for read/write sanity checking. */ | |
7877903a | 1975 | s->qdev.max_lba = nb_sectors; |
e7e25e32 | 1976 | /* Clip to 2TB, instead of returning capacity modulo 2TB. */ |
f01b5931 | 1977 | if (nb_sectors > UINT32_MAX) { |
e7e25e32 | 1978 | nb_sectors = UINT32_MAX; |
f01b5931 | 1979 | } |
e7e25e32 GH |
1980 | outbuf[0] = (nb_sectors >> 24) & 0xff; |
1981 | outbuf[1] = (nb_sectors >> 16) & 0xff; | |
1982 | outbuf[2] = (nb_sectors >> 8) & 0xff; | |
1983 | outbuf[3] = nb_sectors & 0xff; | |
1984 | outbuf[4] = 0; | |
1985 | outbuf[5] = 0; | |
69377307 | 1986 | outbuf[6] = s->qdev.blocksize >> 8; |
e7e25e32 | 1987 | outbuf[7] = 0; |
5f71d32f | 1988 | break; |
f3b338ef PB |
1989 | case REQUEST_SENSE: |
1990 | /* Just return "NO SENSE". */ | |
37b6045c PB |
1991 | buflen = scsi_convert_sense(NULL, 0, outbuf, r->buflen, |
1992 | (req->cmd.buf[1] & 1) == 0); | |
c8dcb531 PB |
1993 | if (buflen < 0) { |
1994 | goto illegal_request; | |
1995 | } | |
f3b338ef | 1996 | break; |
b6c251ab PB |
1997 | case MECHANISM_STATUS: |
1998 | buflen = scsi_emulate_mechanism_status(s, outbuf); | |
1999 | if (buflen < 0) { | |
2000 | goto illegal_request; | |
2001 | } | |
2002 | break; | |
38215553 | 2003 | case GET_CONFIGURATION: |
430ee2f2 | 2004 | buflen = scsi_get_configuration(s, outbuf); |
b6c251ab PB |
2005 | if (buflen < 0) { |
2006 | goto illegal_request; | |
2007 | } | |
2008 | break; | |
2009 | case GET_EVENT_STATUS_NOTIFICATION: | |
2010 | buflen = scsi_get_event_status_notification(s, r, outbuf); | |
2011 | if (buflen < 0) { | |
2012 | goto illegal_request; | |
2013 | } | |
2014 | break; | |
1a4f0c3a PB |
2015 | case READ_DISC_INFORMATION: |
2016 | buflen = scsi_read_disc_information(s, r, outbuf); | |
2017 | if (buflen < 0) { | |
2018 | goto illegal_request; | |
2019 | } | |
2020 | break; | |
b6c251ab PB |
2021 | case READ_DVD_STRUCTURE: |
2022 | buflen = scsi_read_dvd_structure(s, r, outbuf); | |
2023 | if (buflen < 0) { | |
2024 | goto illegal_request; | |
2025 | } | |
38215553 | 2026 | break; |
f6515262 | 2027 | case SERVICE_ACTION_IN_16: |
5dd90e2a | 2028 | /* Service Action In subcommands. */ |
f6515262 | 2029 | if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) { |
59ee9500 | 2030 | trace_scsi_disk_emulate_command_SAI_16(); |
5dd90e2a | 2031 | memset(outbuf, 0, req->cmd.xfer); |
4be74634 | 2032 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
f01b5931 | 2033 | if (!nb_sectors) { |
9bcaf4fe | 2034 | scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY)); |
0369f06f | 2035 | return 0; |
f01b5931 | 2036 | } |
7cec78b6 PB |
2037 | if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) { |
2038 | goto illegal_request; | |
2039 | } | |
69377307 | 2040 | nb_sectors /= s->qdev.blocksize / 512; |
5dd90e2a GH |
2041 | /* Returned value is the address of the last sector. */ |
2042 | nb_sectors--; | |
2043 | /* Remember the new size for read/write sanity checking. */ | |
7877903a | 2044 | s->qdev.max_lba = nb_sectors; |
5dd90e2a GH |
2045 | outbuf[0] = (nb_sectors >> 56) & 0xff; |
2046 | outbuf[1] = (nb_sectors >> 48) & 0xff; | |
2047 | outbuf[2] = (nb_sectors >> 40) & 0xff; | |
2048 | outbuf[3] = (nb_sectors >> 32) & 0xff; | |
2049 | outbuf[4] = (nb_sectors >> 24) & 0xff; | |
2050 | outbuf[5] = (nb_sectors >> 16) & 0xff; | |
2051 | outbuf[6] = (nb_sectors >> 8) & 0xff; | |
2052 | outbuf[7] = nb_sectors & 0xff; | |
2053 | outbuf[8] = 0; | |
2054 | outbuf[9] = 0; | |
69377307 | 2055 | outbuf[10] = s->qdev.blocksize >> 8; |
5dd90e2a | 2056 | outbuf[11] = 0; |
ee3659e3 CH |
2057 | outbuf[12] = 0; |
2058 | outbuf[13] = get_physical_block_exp(&s->qdev.conf); | |
ea3bd56f CH |
2059 | |
2060 | /* set TPE bit if the format supports discard */ | |
2061 | if (s->qdev.conf.discard_granularity) { | |
2062 | outbuf[14] = 0x80; | |
2063 | } | |
2064 | ||
5dd90e2a | 2065 | /* Protection, exponent and lowest lba field left blank. */ |
5dd90e2a GH |
2066 | break; |
2067 | } | |
59ee9500 | 2068 | trace_scsi_disk_emulate_command_SAI_unsupported(); |
5dd90e2a | 2069 | goto illegal_request; |
101aa85f PB |
2070 | case SYNCHRONIZE_CACHE: |
2071 | /* The request is used as the AIO opaque value, so add a ref. */ | |
2072 | scsi_req_ref(&r->req); | |
4be74634 | 2073 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
5366d0c8 | 2074 | BLOCK_ACCT_FLUSH); |
4be74634 | 2075 | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r); |
101aa85f PB |
2076 | return 0; |
2077 | case SEEK_10: | |
59ee9500 | 2078 | trace_scsi_disk_emulate_command_SEEK_10(r->req.cmd.lba); |
101aa85f PB |
2079 | if (r->req.cmd.lba > s->qdev.max_lba) { |
2080 | goto illegal_lba; | |
2081 | } | |
2082 | break; | |
101aa85f | 2083 | case MODE_SELECT: |
59ee9500 | 2084 | trace_scsi_disk_emulate_command_MODE_SELECT(r->req.cmd.xfer); |
101aa85f PB |
2085 | break; |
2086 | case MODE_SELECT_10: | |
59ee9500 | 2087 | trace_scsi_disk_emulate_command_MODE_SELECT_10(r->req.cmd.xfer); |
101aa85f | 2088 | break; |
5222aaf2 | 2089 | case UNMAP: |
59ee9500 | 2090 | trace_scsi_disk_emulate_command_UNMAP(r->req.cmd.xfer); |
5222aaf2 | 2091 | break; |
d97e7730 PB |
2092 | case VERIFY_10: |
2093 | case VERIFY_12: | |
2094 | case VERIFY_16: | |
59ee9500 | 2095 | trace_scsi_disk_emulate_command_VERIFY((req->cmd.buf[1] >> 1) & 3); |
d97e7730 PB |
2096 | if (req->cmd.buf[1] & 6) { |
2097 | goto illegal_request; | |
2098 | } | |
2099 | break; | |
101aa85f | 2100 | case WRITE_SAME_10: |
101aa85f | 2101 | case WRITE_SAME_16: |
59ee9500 LV |
2102 | trace_scsi_disk_emulate_command_WRITE_SAME( |
2103 | req->cmd.buf[0] == WRITE_SAME_10 ? 10 : 16, r->req.cmd.xfer); | |
84f94a9a | 2104 | break; |
aa5dbdc1 | 2105 | default: |
59ee9500 LV |
2106 | trace_scsi_disk_emulate_command_UNKNOWN(buf[0], |
2107 | scsi_command_name(buf[0])); | |
b45ef674 | 2108 | scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE)); |
b08d0ea0 | 2109 | return 0; |
aa5dbdc1 | 2110 | } |
314a3299 | 2111 | assert(!r->req.aiocb); |
c8dcb531 | 2112 | r->iov.iov_len = MIN(r->buflen, req->cmd.xfer); |
b08d0ea0 PB |
2113 | if (r->iov.iov_len == 0) { |
2114 | scsi_req_complete(&r->req, GOOD); | |
2115 | } | |
af6d510d PB |
2116 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
2117 | assert(r->iov.iov_len == req->cmd.xfer); | |
2118 | return -r->iov.iov_len; | |
2119 | } else { | |
2120 | return r->iov.iov_len; | |
2121 | } | |
aa5dbdc1 | 2122 | |
aa5dbdc1 | 2123 | illegal_request: |
cfc606da PB |
2124 | if (r->req.status == -1) { |
2125 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
2126 | } | |
b08d0ea0 | 2127 | return 0; |
101aa85f PB |
2128 | |
2129 | illegal_lba: | |
2130 | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); | |
2131 | return 0; | |
aa5dbdc1 GH |
2132 | } |
2133 | ||
2e5d83bb PB |
2134 | /* Execute a scsi command. Returns the length of the data expected by the |
2135 | command. This will be Positive for data transfers from the device | |
2136 | (eg. disk reads), negative for transfers to the device (eg. disk writes), | |
2137 | and zero if the command does not transfer any data. */ | |
2138 | ||
b08d0ea0 | 2139 | static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf) |
2e5d83bb | 2140 | { |
5c6c0e51 HR |
2141 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
2142 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); | |
94f8ba11 | 2143 | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
e93176d5 | 2144 | uint32_t len; |
a917d384 | 2145 | uint8_t command; |
a917d384 PB |
2146 | |
2147 | command = buf[0]; | |
aa5dbdc1 | 2148 | |
cd723b85 | 2149 | if (!blk_is_available(s->qdev.conf.blk)) { |
b08d0ea0 PB |
2150 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
2151 | return 0; | |
9bcaf4fe PB |
2152 | } |
2153 | ||
1894df02 | 2154 | len = scsi_data_cdb_xfer(r->req.cmd.buf); |
a917d384 | 2155 | switch (command) { |
ebf46023 GH |
2156 | case READ_6: |
2157 | case READ_10: | |
bd536cf3 GH |
2158 | case READ_12: |
2159 | case READ_16: | |
59ee9500 | 2160 | trace_scsi_disk_dma_command_READ(r->req.cmd.lba, len); |
2343be0d PB |
2161 | /* Protection information is not supported. For SCSI versions 2 and |
2162 | * older (as determined by snooping the guest's INQUIRY commands), | |
2163 | * there is no RD/WR/VRPROTECT, so skip this check in these versions. | |
2164 | */ | |
2165 | if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) { | |
96bdbbab RS |
2166 | goto illegal_request; |
2167 | } | |
444bc908 | 2168 | if (!check_lba_range(s, r->req.cmd.lba, len)) { |
274fb0e1 | 2169 | goto illegal_lba; |
f01b5931 | 2170 | } |
69377307 PB |
2171 | r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512); |
2172 | r->sector_count = len * (s->qdev.blocksize / 512); | |
2e5d83bb | 2173 | break; |
ebf46023 GH |
2174 | case WRITE_6: |
2175 | case WRITE_10: | |
bd536cf3 GH |
2176 | case WRITE_12: |
2177 | case WRITE_16: | |
5e30a07d | 2178 | case WRITE_VERIFY_10: |
ebef0bbb BK |
2179 | case WRITE_VERIFY_12: |
2180 | case WRITE_VERIFY_16: | |
4be74634 | 2181 | if (blk_is_read_only(s->qdev.conf.blk)) { |
6a8a685c RS |
2182 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
2183 | return 0; | |
2184 | } | |
59ee9500 | 2185 | trace_scsi_disk_dma_command_WRITE( |
2dd791b6 HR |
2186 | (command & 0xe) == 0xe ? "And Verify " : "", |
2187 | r->req.cmd.lba, len); | |
4f04560b | 2188 | /* fall through */ |
166dbda7 PB |
2189 | case VERIFY_10: |
2190 | case VERIFY_12: | |
2191 | case VERIFY_16: | |
2192 | /* We get here only for BYTCHK == 0x01 and only for scsi-block. | |
2193 | * As far as DMA is concerned, we can treat it the same as a write; | |
2194 | * scsi_block_do_sgio will send VERIFY commands. | |
2195 | */ | |
2343be0d | 2196 | if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) { |
96bdbbab RS |
2197 | goto illegal_request; |
2198 | } | |
444bc908 | 2199 | if (!check_lba_range(s, r->req.cmd.lba, len)) { |
274fb0e1 | 2200 | goto illegal_lba; |
f01b5931 | 2201 | } |
69377307 PB |
2202 | r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512); |
2203 | r->sector_count = len * (s->qdev.blocksize / 512); | |
2e5d83bb | 2204 | break; |
101aa85f | 2205 | default: |
b08d0ea0 | 2206 | abort(); |
96bdbbab RS |
2207 | illegal_request: |
2208 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); | |
2209 | return 0; | |
274fb0e1 | 2210 | illegal_lba: |
b45ef674 | 2211 | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
274fb0e1 | 2212 | return 0; |
2e5d83bb | 2213 | } |
94f8ba11 | 2214 | r->need_fua_emulation = sdc->need_fua_emulation(&r->req.cmd); |
b08d0ea0 | 2215 | if (r->sector_count == 0) { |
b45ef674 | 2216 | scsi_req_complete(&r->req, GOOD); |
a917d384 | 2217 | } |
b08d0ea0 | 2218 | assert(r->iov.iov_len == 0); |
efb9ee02 | 2219 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
b08d0ea0 | 2220 | return -r->sector_count * 512; |
a917d384 | 2221 | } else { |
b08d0ea0 | 2222 | return r->sector_count * 512; |
2e5d83bb | 2223 | } |
2e5d83bb PB |
2224 | } |
2225 | ||
e9447f35 JK |
2226 | static void scsi_disk_reset(DeviceState *dev) |
2227 | { | |
2228 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev); | |
2229 | uint64_t nb_sectors; | |
2230 | ||
c7b48872 | 2231 | scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET)); |
e9447f35 | 2232 | |
4be74634 | 2233 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
69377307 | 2234 | nb_sectors /= s->qdev.blocksize / 512; |
e9447f35 JK |
2235 | if (nb_sectors) { |
2236 | nb_sectors--; | |
2237 | } | |
7877903a | 2238 | s->qdev.max_lba = nb_sectors; |
7721c7f7 PH |
2239 | /* reset tray statuses */ |
2240 | s->tray_locked = 0; | |
2241 | s->tray_open = 0; | |
2343be0d PB |
2242 | |
2243 | s->qdev.scsi_version = s->qdev.default_scsi_version; | |
e9447f35 JK |
2244 | } |
2245 | ||
aaebacef PB |
2246 | static void scsi_disk_resize_cb(void *opaque) |
2247 | { | |
2248 | SCSIDiskState *s = opaque; | |
2249 | ||
2250 | /* SPC lists this sense code as available only for | |
2251 | * direct-access devices. | |
2252 | */ | |
2253 | if (s->qdev.type == TYPE_DISK) { | |
53200fad | 2254 | scsi_device_report_change(&s->qdev, SENSE_CODE(CAPACITY_CHANGED)); |
aaebacef PB |
2255 | } |
2256 | } | |
2257 | ||
39829a01 | 2258 | static void scsi_cd_change_media_cb(void *opaque, bool load, Error **errp) |
2c6942fa | 2259 | { |
8a9c16f6 PB |
2260 | SCSIDiskState *s = opaque; |
2261 | ||
2262 | /* | |
2263 | * When a CD gets changed, we have to report an ejected state and | |
2264 | * then a loaded state to guests so that they detect tray | |
2265 | * open/close and media change events. Guests that do not use | |
2266 | * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close | |
2267 | * states rely on this behavior. | |
2268 | * | |
2269 | * media_changed governs the state machine used for unit attention | |
2270 | * report. media_event is used by GET EVENT STATUS NOTIFICATION. | |
2271 | */ | |
2272 | s->media_changed = load; | |
2273 | s->tray_open = !load; | |
e48e84ea | 2274 | scsi_device_set_ua(&s->qdev, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM)); |
3c2f7c12 | 2275 | s->media_event = true; |
4480de19 PB |
2276 | s->eject_request = false; |
2277 | } | |
2278 | ||
2279 | static void scsi_cd_eject_request_cb(void *opaque, bool force) | |
2280 | { | |
2281 | SCSIDiskState *s = opaque; | |
2282 | ||
2283 | s->eject_request = true; | |
2284 | if (force) { | |
2285 | s->tray_locked = false; | |
2286 | } | |
2c6942fa MA |
2287 | } |
2288 | ||
e4def80b MA |
2289 | static bool scsi_cd_is_tray_open(void *opaque) |
2290 | { | |
2291 | return ((SCSIDiskState *)opaque)->tray_open; | |
2292 | } | |
2293 | ||
f107639a MA |
2294 | static bool scsi_cd_is_medium_locked(void *opaque) |
2295 | { | |
2296 | return ((SCSIDiskState *)opaque)->tray_locked; | |
2297 | } | |
2298 | ||
aaebacef | 2299 | static const BlockDevOps scsi_disk_removable_block_ops = { |
2c6942fa | 2300 | .change_media_cb = scsi_cd_change_media_cb, |
4480de19 | 2301 | .eject_request_cb = scsi_cd_eject_request_cb, |
e4def80b | 2302 | .is_tray_open = scsi_cd_is_tray_open, |
f107639a | 2303 | .is_medium_locked = scsi_cd_is_medium_locked, |
aaebacef PB |
2304 | |
2305 | .resize_cb = scsi_disk_resize_cb, | |
2306 | }; | |
2307 | ||
2308 | static const BlockDevOps scsi_disk_block_ops = { | |
2309 | .resize_cb = scsi_disk_resize_cb, | |
f107639a MA |
2310 | }; |
2311 | ||
8a9c16f6 PB |
2312 | static void scsi_disk_unit_attention_reported(SCSIDevice *dev) |
2313 | { | |
2314 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); | |
2315 | if (s->media_changed) { | |
2316 | s->media_changed = false; | |
e48e84ea | 2317 | scsi_device_set_ua(&s->qdev, SENSE_CODE(MEDIUM_CHANGED)); |
8a9c16f6 PB |
2318 | } |
2319 | } | |
2320 | ||
a818a4b6 | 2321 | static void scsi_realize(SCSIDevice *dev, Error **errp) |
2e5d83bb | 2322 | { |
d52affa7 | 2323 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
7cef3d12 | 2324 | bool read_only; |
2e5d83bb | 2325 | |
4be74634 | 2326 | if (!s->qdev.conf.blk) { |
a818a4b6 FZ |
2327 | error_setg(errp, "drive property not set"); |
2328 | return; | |
d52affa7 GH |
2329 | } |
2330 | ||
bfe3d7ac | 2331 | if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) && |
4be74634 | 2332 | !blk_is_inserted(s->qdev.conf.blk)) { |
a818a4b6 FZ |
2333 | error_setg(errp, "Device needs media, but drive is empty"); |
2334 | return; | |
98f28ad7 MA |
2335 | } |
2336 | ||
0eb28a42 | 2337 | blkconf_blocksizes(&s->qdev.conf); |
3da023b5 MK |
2338 | |
2339 | if (s->qdev.conf.logical_block_size > | |
2340 | s->qdev.conf.physical_block_size) { | |
2341 | error_setg(errp, | |
2342 | "logical_block_size > physical_block_size not supported"); | |
2343 | return; | |
2344 | } | |
2345 | ||
4f71fb43 KW |
2346 | if (blk_get_aio_context(s->qdev.conf.blk) != qemu_get_aio_context() && |
2347 | !s->qdev.hba_supports_iothread) | |
2348 | { | |
2349 | error_setg(errp, "HBA does not support iothreads"); | |
2350 | return; | |
2351 | } | |
2352 | ||
5ff5efb4 | 2353 | if (dev->type == TYPE_DISK) { |
ceff3e1f | 2354 | if (!blkconf_geometry(&dev->conf, NULL, 65535, 255, 255, errp)) { |
a818a4b6 | 2355 | return; |
5ff5efb4 | 2356 | } |
b7eb0c9f | 2357 | } |
7cef3d12 KW |
2358 | |
2359 | read_only = blk_is_read_only(s->qdev.conf.blk); | |
2360 | if (dev->type == TYPE_ROM) { | |
2361 | read_only = true; | |
2362 | } | |
2363 | ||
2364 | if (!blkconf_apply_backend_options(&dev->conf, read_only, | |
ceff3e1f | 2365 | dev->type == TYPE_DISK, errp)) { |
a17c17a2 KW |
2366 | return; |
2367 | } | |
a0fef654 | 2368 | |
215e47b9 PB |
2369 | if (s->qdev.conf.discard_granularity == -1) { |
2370 | s->qdev.conf.discard_granularity = | |
2371 | MAX(s->qdev.conf.logical_block_size, DEFAULT_DISCARD_GRANULARITY); | |
2372 | } | |
2373 | ||
552fee93 | 2374 | if (!s->version) { |
35c2c8dc | 2375 | s->version = g_strdup(qemu_hw_version()); |
552fee93 | 2376 | } |
353815aa DF |
2377 | if (!s->vendor) { |
2378 | s->vendor = g_strdup("QEMU"); | |
2379 | } | |
7471a649 KW |
2380 | if (!s->device_id) { |
2381 | if (s->serial) { | |
2382 | s->device_id = g_strdup_printf("%.20s", s->serial); | |
2383 | } else { | |
2384 | const char *str = blk_name(s->qdev.conf.blk); | |
2385 | if (str && *str) { | |
2386 | s->device_id = g_strdup(str); | |
2387 | } | |
2388 | } | |
2389 | } | |
552fee93 | 2390 | |
4be74634 | 2391 | if (blk_is_sg(s->qdev.conf.blk)) { |
a818a4b6 FZ |
2392 | error_setg(errp, "unwanted /dev/sg*"); |
2393 | return; | |
32bb404a MA |
2394 | } |
2395 | ||
18e673b8 PH |
2396 | if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && |
2397 | !(s->features & (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS))) { | |
4be74634 | 2398 | blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_removable_block_ops, s); |
aaebacef | 2399 | } else { |
4be74634 | 2400 | blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_block_ops, s); |
2e5d83bb | 2401 | } |
4be74634 | 2402 | blk_set_guest_block_size(s->qdev.conf.blk, s->qdev.blocksize); |
8cfacf07 | 2403 | |
4be74634 | 2404 | blk_iostatus_enable(s->qdev.conf.blk); |
d52affa7 GH |
2405 | } |
2406 | ||
a818a4b6 | 2407 | static void scsi_hd_realize(SCSIDevice *dev, Error **errp) |
b443ae67 | 2408 | { |
e39be482 | 2409 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
3ff35ba3 | 2410 | AioContext *ctx = NULL; |
df1d4c34 ET |
2411 | /* can happen for devices without drive. The error message for missing |
2412 | * backend will be issued in scsi_realize | |
2413 | */ | |
2414 | if (s->qdev.conf.blk) { | |
3ff35ba3 AG |
2415 | ctx = blk_get_aio_context(s->qdev.conf.blk); |
2416 | aio_context_acquire(ctx); | |
df1d4c34 ET |
2417 | blkconf_blocksizes(&s->qdev.conf); |
2418 | } | |
e39be482 PB |
2419 | s->qdev.blocksize = s->qdev.conf.logical_block_size; |
2420 | s->qdev.type = TYPE_DISK; | |
353815aa DF |
2421 | if (!s->product) { |
2422 | s->product = g_strdup("QEMU HARDDISK"); | |
2423 | } | |
a818a4b6 | 2424 | scsi_realize(&s->qdev, errp); |
3ff35ba3 AG |
2425 | if (ctx) { |
2426 | aio_context_release(ctx); | |
2427 | } | |
b443ae67 MA |
2428 | } |
2429 | ||
a818a4b6 | 2430 | static void scsi_cd_realize(SCSIDevice *dev, Error **errp) |
b443ae67 | 2431 | { |
e39be482 | 2432 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
3ff35ba3 | 2433 | AioContext *ctx; |
83b4fe0e | 2434 | int ret; |
9ef6e505 KW |
2435 | |
2436 | if (!dev->conf.blk) { | |
83b4fe0e KW |
2437 | /* Anonymous BlockBackend for an empty drive. As we put it into |
2438 | * dev->conf, qdev takes care of detaching on unplug. */ | |
d861ab3a | 2439 | dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
83b4fe0e KW |
2440 | ret = blk_attach_dev(dev->conf.blk, &dev->qdev); |
2441 | assert(ret == 0); | |
9ef6e505 KW |
2442 | } |
2443 | ||
3ff35ba3 AG |
2444 | ctx = blk_get_aio_context(dev->conf.blk); |
2445 | aio_context_acquire(ctx); | |
e39be482 PB |
2446 | s->qdev.blocksize = 2048; |
2447 | s->qdev.type = TYPE_ROM; | |
bfe3d7ac | 2448 | s->features |= 1 << SCSI_DISK_F_REMOVABLE; |
353815aa DF |
2449 | if (!s->product) { |
2450 | s->product = g_strdup("QEMU CD-ROM"); | |
2451 | } | |
a818a4b6 | 2452 | scsi_realize(&s->qdev, errp); |
3ff35ba3 | 2453 | aio_context_release(ctx); |
b443ae67 MA |
2454 | } |
2455 | ||
a818a4b6 | 2456 | static void scsi_disk_realize(SCSIDevice *dev, Error **errp) |
b443ae67 | 2457 | { |
95b5edcd | 2458 | DriveInfo *dinfo; |
a818a4b6 | 2459 | Error *local_err = NULL; |
b443ae67 | 2460 | |
4be74634 | 2461 | if (!dev->conf.blk) { |
a818a4b6 FZ |
2462 | scsi_realize(dev, &local_err); |
2463 | assert(local_err); | |
2464 | error_propagate(errp, local_err); | |
2465 | return; | |
b443ae67 MA |
2466 | } |
2467 | ||
4be74634 | 2468 | dinfo = blk_legacy_dinfo(dev->conf.blk); |
26f8b3a8 | 2469 | if (dinfo && dinfo->media_cd) { |
a818a4b6 | 2470 | scsi_cd_realize(dev, errp); |
e39be482 | 2471 | } else { |
a818a4b6 | 2472 | scsi_hd_realize(dev, errp); |
e39be482 | 2473 | } |
b443ae67 MA |
2474 | } |
2475 | ||
b08d0ea0 | 2476 | static const SCSIReqOps scsi_disk_emulate_reqops = { |
8dbd4574 | 2477 | .size = sizeof(SCSIDiskReq), |
12010e7b | 2478 | .free_req = scsi_free_request, |
b08d0ea0 | 2479 | .send_command = scsi_disk_emulate_command, |
314a3299 PB |
2480 | .read_data = scsi_disk_emulate_read_data, |
2481 | .write_data = scsi_disk_emulate_write_data, | |
b08d0ea0 PB |
2482 | .get_buf = scsi_get_buf, |
2483 | }; | |
2484 | ||
2485 | static const SCSIReqOps scsi_disk_dma_reqops = { | |
2486 | .size = sizeof(SCSIDiskReq), | |
2487 | .free_req = scsi_free_request, | |
2488 | .send_command = scsi_disk_dma_command, | |
12010e7b PB |
2489 | .read_data = scsi_read_data, |
2490 | .write_data = scsi_write_data, | |
12010e7b | 2491 | .get_buf = scsi_get_buf, |
43b978b9 PB |
2492 | .load_request = scsi_disk_load_request, |
2493 | .save_request = scsi_disk_save_request, | |
8dbd4574 PB |
2494 | }; |
2495 | ||
b08d0ea0 PB |
2496 | static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = { |
2497 | [TEST_UNIT_READY] = &scsi_disk_emulate_reqops, | |
2498 | [INQUIRY] = &scsi_disk_emulate_reqops, | |
2499 | [MODE_SENSE] = &scsi_disk_emulate_reqops, | |
2500 | [MODE_SENSE_10] = &scsi_disk_emulate_reqops, | |
2501 | [START_STOP] = &scsi_disk_emulate_reqops, | |
2502 | [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops, | |
2503 | [READ_CAPACITY_10] = &scsi_disk_emulate_reqops, | |
2504 | [READ_TOC] = &scsi_disk_emulate_reqops, | |
2505 | [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops, | |
2506 | [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops, | |
2507 | [GET_CONFIGURATION] = &scsi_disk_emulate_reqops, | |
2508 | [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops, | |
2509 | [MECHANISM_STATUS] = &scsi_disk_emulate_reqops, | |
2510 | [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops, | |
2511 | [REQUEST_SENSE] = &scsi_disk_emulate_reqops, | |
2512 | [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops, | |
2513 | [SEEK_10] = &scsi_disk_emulate_reqops, | |
b08d0ea0 PB |
2514 | [MODE_SELECT] = &scsi_disk_emulate_reqops, |
2515 | [MODE_SELECT_10] = &scsi_disk_emulate_reqops, | |
5222aaf2 | 2516 | [UNMAP] = &scsi_disk_emulate_reqops, |
b08d0ea0 PB |
2517 | [WRITE_SAME_10] = &scsi_disk_emulate_reqops, |
2518 | [WRITE_SAME_16] = &scsi_disk_emulate_reqops, | |
d97e7730 PB |
2519 | [VERIFY_10] = &scsi_disk_emulate_reqops, |
2520 | [VERIFY_12] = &scsi_disk_emulate_reqops, | |
2521 | [VERIFY_16] = &scsi_disk_emulate_reqops, | |
b08d0ea0 PB |
2522 | |
2523 | [READ_6] = &scsi_disk_dma_reqops, | |
2524 | [READ_10] = &scsi_disk_dma_reqops, | |
2525 | [READ_12] = &scsi_disk_dma_reqops, | |
2526 | [READ_16] = &scsi_disk_dma_reqops, | |
b08d0ea0 PB |
2527 | [WRITE_6] = &scsi_disk_dma_reqops, |
2528 | [WRITE_10] = &scsi_disk_dma_reqops, | |
2529 | [WRITE_12] = &scsi_disk_dma_reqops, | |
2530 | [WRITE_16] = &scsi_disk_dma_reqops, | |
2531 | [WRITE_VERIFY_10] = &scsi_disk_dma_reqops, | |
2532 | [WRITE_VERIFY_12] = &scsi_disk_dma_reqops, | |
2533 | [WRITE_VERIFY_16] = &scsi_disk_dma_reqops, | |
2534 | }; | |
2535 | ||
59ee9500 LV |
2536 | static void scsi_disk_new_request_dump(uint32_t lun, uint32_t tag, uint8_t *buf) |
2537 | { | |
2538 | int i; | |
2539 | int len = scsi_cdb_length(buf); | |
2540 | char *line_buffer, *p; | |
2541 | ||
2542 | line_buffer = g_malloc(len * 5 + 1); | |
2543 | ||
2544 | for (i = 0, p = line_buffer; i < len; i++) { | |
2545 | p += sprintf(p, " 0x%02x", buf[i]); | |
2546 | } | |
2547 | trace_scsi_disk_new_request(lun, tag, line_buffer); | |
2548 | ||
2549 | g_free(line_buffer); | |
2550 | } | |
2551 | ||
63db0f0e PB |
2552 | static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, |
2553 | uint8_t *buf, void *hba_private) | |
8dbd4574 PB |
2554 | { |
2555 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); | |
2556 | SCSIRequest *req; | |
b08d0ea0 PB |
2557 | const SCSIReqOps *ops; |
2558 | uint8_t command; | |
8dbd4574 | 2559 | |
79fb50bb PB |
2560 | command = buf[0]; |
2561 | ops = scsi_disk_reqops_dispatch[command]; | |
2562 | if (!ops) { | |
2563 | ops = &scsi_disk_emulate_reqops; | |
2564 | } | |
2565 | req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private); | |
2566 | ||
59ee9500 LV |
2567 | if (trace_event_get_state_backends(TRACE_SCSI_DISK_NEW_REQUEST)) { |
2568 | scsi_disk_new_request_dump(lun, tag, buf); | |
b08d0ea0 | 2569 | } |
b08d0ea0 | 2570 | |
8dbd4574 PB |
2571 | return req; |
2572 | } | |
2573 | ||
336a6915 PB |
2574 | #ifdef __linux__ |
2575 | static int get_device_type(SCSIDiskState *s) | |
2576 | { | |
336a6915 PB |
2577 | uint8_t cmd[16]; |
2578 | uint8_t buf[36]; | |
336a6915 PB |
2579 | int ret; |
2580 | ||
2581 | memset(cmd, 0, sizeof(cmd)); | |
2582 | memset(buf, 0, sizeof(buf)); | |
2583 | cmd[0] = INQUIRY; | |
2584 | cmd[4] = sizeof(buf); | |
2585 | ||
a0c7e35b DHB |
2586 | ret = scsi_SG_IO_FROM_DEV(s->qdev.conf.blk, cmd, sizeof(cmd), |
2587 | buf, sizeof(buf)); | |
2588 | if (ret < 0) { | |
336a6915 PB |
2589 | return -1; |
2590 | } | |
2591 | s->qdev.type = buf[0]; | |
bfe3d7ac PB |
2592 | if (buf[1] & 0x80) { |
2593 | s->features |= 1 << SCSI_DISK_F_REMOVABLE; | |
2594 | } | |
336a6915 PB |
2595 | return 0; |
2596 | } | |
2597 | ||
a818a4b6 | 2598 | static void scsi_block_realize(SCSIDevice *dev, Error **errp) |
336a6915 PB |
2599 | { |
2600 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); | |
3ff35ba3 | 2601 | AioContext *ctx; |
336a6915 PB |
2602 | int sg_version; |
2603 | int rc; | |
2604 | ||
4be74634 | 2605 | if (!s->qdev.conf.blk) { |
a818a4b6 FZ |
2606 | error_setg(errp, "drive property not set"); |
2607 | return; | |
336a6915 PB |
2608 | } |
2609 | ||
51f43d57 FZ |
2610 | if (s->rotation_rate) { |
2611 | error_report_once("rotation_rate is specified for scsi-block but is " | |
2612 | "not implemented. This option is deprecated and will " | |
2613 | "be removed in a future version"); | |
2614 | } | |
2615 | ||
3ff35ba3 AG |
2616 | ctx = blk_get_aio_context(s->qdev.conf.blk); |
2617 | aio_context_acquire(ctx); | |
2618 | ||
336a6915 | 2619 | /* check we are using a driver managing SG_IO (version 3 and after) */ |
4be74634 | 2620 | rc = blk_ioctl(s->qdev.conf.blk, SG_GET_VERSION_NUM, &sg_version); |
4bbeb8b1 | 2621 | if (rc < 0) { |
09c2c6ff PB |
2622 | error_setg_errno(errp, -rc, "cannot get SG_IO version number"); |
2623 | if (rc != -EPERM) { | |
2624 | error_append_hint(errp, "Is this a SCSI device?\n"); | |
2625 | } | |
3ff35ba3 | 2626 | goto out; |
4bbeb8b1 FZ |
2627 | } |
2628 | if (sg_version < 30000) { | |
a818a4b6 | 2629 | error_setg(errp, "scsi generic interface too old"); |
3ff35ba3 | 2630 | goto out; |
336a6915 PB |
2631 | } |
2632 | ||
2633 | /* get device type from INQUIRY data */ | |
2634 | rc = get_device_type(s); | |
2635 | if (rc < 0) { | |
a818a4b6 | 2636 | error_setg(errp, "INQUIRY failed"); |
3ff35ba3 | 2637 | goto out; |
336a6915 PB |
2638 | } |
2639 | ||
2640 | /* Make a guess for the block size, we'll fix it when the guest sends. | |
2641 | * READ CAPACITY. If they don't, they likely would assume these sizes | |
2642 | * anyway. (TODO: check in /sys). | |
2643 | */ | |
2644 | if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) { | |
2645 | s->qdev.blocksize = 2048; | |
2646 | } else { | |
2647 | s->qdev.blocksize = 512; | |
2648 | } | |
18e673b8 PH |
2649 | |
2650 | /* Makes the scsi-block device not removable by using HMP and QMP eject | |
2651 | * command. | |
2652 | */ | |
2653 | s->features |= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS); | |
2654 | ||
a818a4b6 | 2655 | scsi_realize(&s->qdev, errp); |
a71c775b | 2656 | scsi_generic_read_device_inquiry(&s->qdev); |
3ff35ba3 AG |
2657 | |
2658 | out: | |
2659 | aio_context_release(ctx); | |
336a6915 PB |
2660 | } |
2661 | ||
8fdc7839 PB |
2662 | typedef struct SCSIBlockReq { |
2663 | SCSIDiskReq req; | |
2664 | sg_io_hdr_t io_header; | |
2665 | ||
2666 | /* Selected bytes of the original CDB, copied into our own CDB. */ | |
2667 | uint8_t cmd, cdb1, group_number; | |
2668 | ||
2669 | /* CDB passed to SG_IO. */ | |
2670 | uint8_t cdb[16]; | |
2671 | } SCSIBlockReq; | |
2672 | ||
2673 | static BlockAIOCB *scsi_block_do_sgio(SCSIBlockReq *req, | |
2674 | int64_t offset, QEMUIOVector *iov, | |
2675 | int direction, | |
2676 | BlockCompletionFunc *cb, void *opaque) | |
2677 | { | |
2678 | sg_io_hdr_t *io_header = &req->io_header; | |
2679 | SCSIDiskReq *r = &req->req; | |
2680 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
2681 | int nb_logical_blocks; | |
2682 | uint64_t lba; | |
2683 | BlockAIOCB *aiocb; | |
2684 | ||
2685 | /* This is not supported yet. It can only happen if the guest does | |
2686 | * reads and writes that are not aligned to one logical sectors | |
2687 | * _and_ cover multiple MemoryRegions. | |
2688 | */ | |
2689 | assert(offset % s->qdev.blocksize == 0); | |
2690 | assert(iov->size % s->qdev.blocksize == 0); | |
2691 | ||
2692 | io_header->interface_id = 'S'; | |
2693 | ||
2694 | /* The data transfer comes from the QEMUIOVector. */ | |
2695 | io_header->dxfer_direction = direction; | |
2696 | io_header->dxfer_len = iov->size; | |
2697 | io_header->dxferp = (void *)iov->iov; | |
2698 | io_header->iovec_count = iov->niov; | |
2699 | assert(io_header->iovec_count == iov->niov); /* no overflow! */ | |
2700 | ||
2701 | /* Build a new CDB with the LBA and length patched in, in case | |
2702 | * DMA helpers split the transfer in multiple segments. Do not | |
2703 | * build a CDB smaller than what the guest wanted, and only build | |
2704 | * a larger one if strictly necessary. | |
2705 | */ | |
2706 | io_header->cmdp = req->cdb; | |
2707 | lba = offset / s->qdev.blocksize; | |
2708 | nb_logical_blocks = io_header->dxfer_len / s->qdev.blocksize; | |
2709 | ||
2710 | if ((req->cmd >> 5) == 0 && lba <= 0x1ffff) { | |
2711 | /* 6-byte CDB */ | |
2712 | stl_be_p(&req->cdb[0], lba | (req->cmd << 24)); | |
2713 | req->cdb[4] = nb_logical_blocks; | |
2714 | req->cdb[5] = 0; | |
2715 | io_header->cmd_len = 6; | |
2716 | } else if ((req->cmd >> 5) <= 1 && lba <= 0xffffffffULL) { | |
2717 | /* 10-byte CDB */ | |
2718 | req->cdb[0] = (req->cmd & 0x1f) | 0x20; | |
2719 | req->cdb[1] = req->cdb1; | |
2720 | stl_be_p(&req->cdb[2], lba); | |
2721 | req->cdb[6] = req->group_number; | |
2722 | stw_be_p(&req->cdb[7], nb_logical_blocks); | |
2723 | req->cdb[9] = 0; | |
2724 | io_header->cmd_len = 10; | |
2725 | } else if ((req->cmd >> 5) != 4 && lba <= 0xffffffffULL) { | |
2726 | /* 12-byte CDB */ | |
2727 | req->cdb[0] = (req->cmd & 0x1f) | 0xA0; | |
2728 | req->cdb[1] = req->cdb1; | |
2729 | stl_be_p(&req->cdb[2], lba); | |
2730 | stl_be_p(&req->cdb[6], nb_logical_blocks); | |
2731 | req->cdb[10] = req->group_number; | |
2732 | req->cdb[11] = 0; | |
2733 | io_header->cmd_len = 12; | |
2734 | } else { | |
2735 | /* 16-byte CDB */ | |
2736 | req->cdb[0] = (req->cmd & 0x1f) | 0x80; | |
2737 | req->cdb[1] = req->cdb1; | |
2738 | stq_be_p(&req->cdb[2], lba); | |
2739 | stl_be_p(&req->cdb[10], nb_logical_blocks); | |
2740 | req->cdb[14] = req->group_number; | |
2741 | req->cdb[15] = 0; | |
2742 | io_header->cmd_len = 16; | |
2743 | } | |
2744 | ||
2745 | /* The rest is as in scsi-generic.c. */ | |
2746 | io_header->mx_sb_len = sizeof(r->req.sense); | |
2747 | io_header->sbp = r->req.sense; | |
2748 | io_header->timeout = UINT_MAX; | |
2749 | io_header->usr_ptr = r; | |
2750 | io_header->flags |= SG_FLAG_DIRECT_IO; | |
2751 | ||
2752 | aiocb = blk_aio_ioctl(s->qdev.conf.blk, SG_IO, io_header, cb, opaque); | |
2753 | assert(aiocb != NULL); | |
2754 | return aiocb; | |
2755 | } | |
2756 | ||
2757 | static bool scsi_block_no_fua(SCSICommand *cmd) | |
2758 | { | |
2759 | return false; | |
2760 | } | |
2761 | ||
2762 | static BlockAIOCB *scsi_block_dma_readv(int64_t offset, | |
2763 | QEMUIOVector *iov, | |
2764 | BlockCompletionFunc *cb, void *cb_opaque, | |
2765 | void *opaque) | |
2766 | { | |
2767 | SCSIBlockReq *r = opaque; | |
2768 | return scsi_block_do_sgio(r, offset, iov, | |
2769 | SG_DXFER_FROM_DEV, cb, cb_opaque); | |
2770 | } | |
2771 | ||
2772 | static BlockAIOCB *scsi_block_dma_writev(int64_t offset, | |
2773 | QEMUIOVector *iov, | |
2774 | BlockCompletionFunc *cb, void *cb_opaque, | |
2775 | void *opaque) | |
2776 | { | |
2777 | SCSIBlockReq *r = opaque; | |
2778 | return scsi_block_do_sgio(r, offset, iov, | |
2779 | SG_DXFER_TO_DEV, cb, cb_opaque); | |
2780 | } | |
2781 | ||
592c3b28 | 2782 | static bool scsi_block_is_passthrough(SCSIDiskState *s, uint8_t *buf) |
336a6915 | 2783 | { |
336a6915 | 2784 | switch (buf[0]) { |
8fdc7839 PB |
2785 | case VERIFY_10: |
2786 | case VERIFY_12: | |
2787 | case VERIFY_16: | |
2788 | /* Check if BYTCHK == 0x01 (data-out buffer contains data | |
2789 | * for the number of logical blocks specified in the length | |
2790 | * field). For other modes, do not use scatter/gather operation. | |
2791 | */ | |
1f8af0d1 | 2792 | if ((buf[1] & 6) == 2) { |
8fdc7839 PB |
2793 | return false; |
2794 | } | |
2795 | break; | |
2796 | ||
336a6915 PB |
2797 | case READ_6: |
2798 | case READ_10: | |
2799 | case READ_12: | |
2800 | case READ_16: | |
2801 | case WRITE_6: | |
2802 | case WRITE_10: | |
2803 | case WRITE_12: | |
2804 | case WRITE_16: | |
2805 | case WRITE_VERIFY_10: | |
2806 | case WRITE_VERIFY_12: | |
2807 | case WRITE_VERIFY_16: | |
8fdc7839 | 2808 | /* MMC writing cannot be done via DMA helpers, because it sometimes |
33ebad12 | 2809 | * involves writing beyond the maximum LBA or to negative LBA (lead-in). |
166dbda7 | 2810 | * We might use scsi_block_dma_reqops as long as no writing commands are |
33ebad12 PB |
2811 | * seen, but performance usually isn't paramount on optical media. So, |
2812 | * just make scsi-block operate the same as scsi-generic for them. | |
2813 | */ | |
b08d0ea0 | 2814 | if (s->qdev.type != TYPE_ROM) { |
592c3b28 | 2815 | return false; |
b08d0ea0 | 2816 | } |
592c3b28 PB |
2817 | break; |
2818 | ||
2819 | default: | |
2820 | break; | |
336a6915 PB |
2821 | } |
2822 | ||
592c3b28 PB |
2823 | return true; |
2824 | } | |
2825 | ||
2826 | ||
8fdc7839 PB |
2827 | static int32_t scsi_block_dma_command(SCSIRequest *req, uint8_t *buf) |
2828 | { | |
2829 | SCSIBlockReq *r = (SCSIBlockReq *)req; | |
2343be0d PB |
2830 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
2831 | ||
8fdc7839 PB |
2832 | r->cmd = req->cmd.buf[0]; |
2833 | switch (r->cmd >> 5) { | |
2834 | case 0: | |
2835 | /* 6-byte CDB. */ | |
2836 | r->cdb1 = r->group_number = 0; | |
2837 | break; | |
2838 | case 1: | |
2839 | /* 10-byte CDB. */ | |
2840 | r->cdb1 = req->cmd.buf[1]; | |
2841 | r->group_number = req->cmd.buf[6]; | |
ed45cae3 | 2842 | break; |
8fdc7839 PB |
2843 | case 4: |
2844 | /* 12-byte CDB. */ | |
2845 | r->cdb1 = req->cmd.buf[1]; | |
2846 | r->group_number = req->cmd.buf[10]; | |
2847 | break; | |
2848 | case 5: | |
2849 | /* 16-byte CDB. */ | |
2850 | r->cdb1 = req->cmd.buf[1]; | |
2851 | r->group_number = req->cmd.buf[14]; | |
2852 | break; | |
2853 | default: | |
2854 | abort(); | |
2855 | } | |
2856 | ||
2343be0d PB |
2857 | /* Protection information is not supported. For SCSI versions 2 and |
2858 | * older (as determined by snooping the guest's INQUIRY commands), | |
2859 | * there is no RD/WR/VRPROTECT, so skip this check in these versions. | |
2860 | */ | |
2861 | if (s->qdev.scsi_version > 2 && (req->cmd.buf[1] & 0xe0)) { | |
8fdc7839 PB |
2862 | scsi_check_condition(&r->req, SENSE_CODE(INVALID_FIELD)); |
2863 | return 0; | |
2864 | } | |
2865 | ||
2866 | r->req.status = &r->io_header.status; | |
2867 | return scsi_disk_dma_command(req, buf); | |
2868 | } | |
2869 | ||
2870 | static const SCSIReqOps scsi_block_dma_reqops = { | |
2871 | .size = sizeof(SCSIBlockReq), | |
2872 | .free_req = scsi_free_request, | |
2873 | .send_command = scsi_block_dma_command, | |
2874 | .read_data = scsi_read_data, | |
2875 | .write_data = scsi_write_data, | |
2876 | .get_buf = scsi_get_buf, | |
2877 | .load_request = scsi_disk_load_request, | |
2878 | .save_request = scsi_disk_save_request, | |
2879 | }; | |
2880 | ||
592c3b28 PB |
2881 | static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag, |
2882 | uint32_t lun, uint8_t *buf, | |
2883 | void *hba_private) | |
2884 | { | |
2885 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); | |
2886 | ||
2887 | if (scsi_block_is_passthrough(s, buf)) { | |
2888 | return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun, | |
2889 | hba_private); | |
2890 | } else { | |
8fdc7839 | 2891 | return scsi_req_alloc(&scsi_block_dma_reqops, &s->qdev, tag, lun, |
592c3b28 PB |
2892 | hba_private); |
2893 | } | |
336a6915 | 2894 | } |
3e7e180a PB |
2895 | |
2896 | static int scsi_block_parse_cdb(SCSIDevice *d, SCSICommand *cmd, | |
2897 | uint8_t *buf, void *hba_private) | |
2898 | { | |
2899 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); | |
2900 | ||
2901 | if (scsi_block_is_passthrough(s, buf)) { | |
2902 | return scsi_bus_parse_cdb(&s->qdev, cmd, buf, hba_private); | |
2903 | } else { | |
2904 | return scsi_req_parse_cdb(&s->qdev, cmd, buf); | |
2905 | } | |
2906 | } | |
2907 | ||
d31347f5 SK |
2908 | static void scsi_block_update_sense(SCSIRequest *req) |
2909 | { | |
2910 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); | |
2911 | SCSIBlockReq *br = DO_UPCAST(SCSIBlockReq, req, r); | |
2912 | r->req.sense_len = MIN(br->io_header.sb_len_wr, sizeof(r->req.sense)); | |
2913 | } | |
336a6915 PB |
2914 | #endif |
2915 | ||
fcaafb10 PB |
2916 | static |
2917 | BlockAIOCB *scsi_dma_readv(int64_t offset, QEMUIOVector *iov, | |
2918 | BlockCompletionFunc *cb, void *cb_opaque, | |
2919 | void *opaque) | |
2920 | { | |
2921 | SCSIDiskReq *r = opaque; | |
2922 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
2923 | return blk_aio_preadv(s->qdev.conf.blk, offset, iov, 0, cb, cb_opaque); | |
2924 | } | |
2925 | ||
2926 | static | |
2927 | BlockAIOCB *scsi_dma_writev(int64_t offset, QEMUIOVector *iov, | |
2928 | BlockCompletionFunc *cb, void *cb_opaque, | |
2929 | void *opaque) | |
2930 | { | |
2931 | SCSIDiskReq *r = opaque; | |
2932 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); | |
2933 | return blk_aio_pwritev(s->qdev.conf.blk, offset, iov, 0, cb, cb_opaque); | |
2934 | } | |
2935 | ||
993935f3 PB |
2936 | static void scsi_disk_base_class_initfn(ObjectClass *klass, void *data) |
2937 | { | |
2938 | DeviceClass *dc = DEVICE_CLASS(klass); | |
fcaafb10 | 2939 | SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass); |
993935f3 PB |
2940 | |
2941 | dc->fw_name = "disk"; | |
2942 | dc->reset = scsi_disk_reset; | |
fcaafb10 PB |
2943 | sdc->dma_readv = scsi_dma_readv; |
2944 | sdc->dma_writev = scsi_dma_writev; | |
94f8ba11 | 2945 | sdc->need_fua_emulation = scsi_is_cmd_fua; |
993935f3 PB |
2946 | } |
2947 | ||
2948 | static const TypeInfo scsi_disk_base_info = { | |
2949 | .name = TYPE_SCSI_DISK_BASE, | |
2950 | .parent = TYPE_SCSI_DEVICE, | |
2951 | .class_init = scsi_disk_base_class_initfn, | |
2952 | .instance_size = sizeof(SCSIDiskState), | |
fcaafb10 | 2953 | .class_size = sizeof(SCSIDiskClass), |
6214a11a | 2954 | .abstract = true, |
993935f3 PB |
2955 | }; |
2956 | ||
4f71fb43 KW |
2957 | #define DEFINE_SCSI_DISK_PROPERTIES() \ |
2958 | DEFINE_PROP_DRIVE_IOTHREAD("drive", SCSIDiskState, qdev.conf.blk), \ | |
2959 | DEFINE_BLOCK_PROPERTIES_BASE(SCSIDiskState, qdev.conf), \ | |
2960 | DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \ | |
2961 | DEFINE_PROP_STRING("ver", SCSIDiskState, version), \ | |
2962 | DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \ | |
2963 | DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \ | |
2964 | DEFINE_PROP_STRING("product", SCSIDiskState, product), \ | |
7471a649 KW |
2965 | DEFINE_PROP_STRING("device_id", SCSIDiskState, device_id) |
2966 | ||
b443ae67 | 2967 | |
39bffca2 AL |
2968 | static Property scsi_hd_properties[] = { |
2969 | DEFINE_SCSI_DISK_PROPERTIES(), | |
bfe3d7ac PB |
2970 | DEFINE_PROP_BIT("removable", SCSIDiskState, features, |
2971 | SCSI_DISK_F_REMOVABLE, false), | |
da8365db PB |
2972 | DEFINE_PROP_BIT("dpofua", SCSIDiskState, features, |
2973 | SCSI_DISK_F_DPOFUA, false), | |
2ecab408 PB |
2974 | DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0), |
2975 | DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0), | |
64cc2284 | 2976 | DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0), |
8a1bd297 PB |
2977 | DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size, |
2978 | DEFAULT_MAX_UNMAP_SIZE), | |
f8e1f533 PB |
2979 | DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size, |
2980 | DEFAULT_MAX_IO_SIZE), | |
070f8009 | 2981 | DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0), |
2343be0d PB |
2982 | DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, |
2983 | 5), | |
d252df48 | 2984 | DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf), |
39bffca2 AL |
2985 | DEFINE_PROP_END_OF_LIST(), |
2986 | }; | |
2987 | ||
43b978b9 PB |
2988 | static const VMStateDescription vmstate_scsi_disk_state = { |
2989 | .name = "scsi-disk", | |
2990 | .version_id = 1, | |
2991 | .minimum_version_id = 1, | |
43b978b9 PB |
2992 | .fields = (VMStateField[]) { |
2993 | VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState), | |
2994 | VMSTATE_BOOL(media_changed, SCSIDiskState), | |
2995 | VMSTATE_BOOL(media_event, SCSIDiskState), | |
2996 | VMSTATE_BOOL(eject_request, SCSIDiskState), | |
2997 | VMSTATE_BOOL(tray_open, SCSIDiskState), | |
2998 | VMSTATE_BOOL(tray_locked, SCSIDiskState), | |
2999 | VMSTATE_END_OF_LIST() | |
3000 | } | |
3001 | }; | |
3002 | ||
b9eea3e6 AL |
3003 | static void scsi_hd_class_initfn(ObjectClass *klass, void *data) |
3004 | { | |
39bffca2 | 3005 | DeviceClass *dc = DEVICE_CLASS(klass); |
b9eea3e6 AL |
3006 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
3007 | ||
a818a4b6 | 3008 | sc->realize = scsi_hd_realize; |
b9eea3e6 AL |
3009 | sc->alloc_req = scsi_new_request; |
3010 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; | |
39bffca2 | 3011 | dc->desc = "virtual SCSI disk"; |
39bffca2 | 3012 | dc->props = scsi_hd_properties; |
43b978b9 | 3013 | dc->vmsd = &vmstate_scsi_disk_state; |
b9eea3e6 AL |
3014 | } |
3015 | ||
8c43a6f0 | 3016 | static const TypeInfo scsi_hd_info = { |
39bffca2 | 3017 | .name = "scsi-hd", |
993935f3 | 3018 | .parent = TYPE_SCSI_DISK_BASE, |
39bffca2 AL |
3019 | .class_init = scsi_hd_class_initfn, |
3020 | }; | |
3021 | ||
3022 | static Property scsi_cd_properties[] = { | |
3023 | DEFINE_SCSI_DISK_PROPERTIES(), | |
2ecab408 PB |
3024 | DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0), |
3025 | DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0), | |
64cc2284 | 3026 | DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0), |
f8e1f533 PB |
3027 | DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size, |
3028 | DEFAULT_MAX_IO_SIZE), | |
2343be0d PB |
3029 | DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, |
3030 | 5), | |
39bffca2 | 3031 | DEFINE_PROP_END_OF_LIST(), |
b9eea3e6 AL |
3032 | }; |
3033 | ||
3034 | static void scsi_cd_class_initfn(ObjectClass *klass, void *data) | |
3035 | { | |
39bffca2 | 3036 | DeviceClass *dc = DEVICE_CLASS(klass); |
b9eea3e6 AL |
3037 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
3038 | ||
a818a4b6 | 3039 | sc->realize = scsi_cd_realize; |
b9eea3e6 AL |
3040 | sc->alloc_req = scsi_new_request; |
3041 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; | |
39bffca2 | 3042 | dc->desc = "virtual SCSI CD-ROM"; |
39bffca2 | 3043 | dc->props = scsi_cd_properties; |
43b978b9 | 3044 | dc->vmsd = &vmstate_scsi_disk_state; |
b9eea3e6 AL |
3045 | } |
3046 | ||
8c43a6f0 | 3047 | static const TypeInfo scsi_cd_info = { |
39bffca2 | 3048 | .name = "scsi-cd", |
993935f3 | 3049 | .parent = TYPE_SCSI_DISK_BASE, |
39bffca2 | 3050 | .class_init = scsi_cd_class_initfn, |
b9eea3e6 AL |
3051 | }; |
3052 | ||
336a6915 | 3053 | #ifdef __linux__ |
39bffca2 | 3054 | static Property scsi_block_properties[] = { |
14b20748 | 3055 | DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \ |
4be74634 | 3056 | DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.blk), |
07488549 | 3057 | DEFINE_PROP_BOOL("share-rw", SCSIDiskState, qdev.conf.share_rw, false), |
070f8009 | 3058 | DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0), |
0a96ca24 DHB |
3059 | DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size, |
3060 | DEFAULT_MAX_UNMAP_SIZE), | |
3061 | DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size, | |
3062 | DEFAULT_MAX_IO_SIZE), | |
2343be0d | 3063 | DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, |
29e560f0 | 3064 | -1), |
39bffca2 AL |
3065 | DEFINE_PROP_END_OF_LIST(), |
3066 | }; | |
3067 | ||
b9eea3e6 AL |
3068 | static void scsi_block_class_initfn(ObjectClass *klass, void *data) |
3069 | { | |
39bffca2 | 3070 | DeviceClass *dc = DEVICE_CLASS(klass); |
b9eea3e6 | 3071 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
8fdc7839 | 3072 | SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass); |
b9eea3e6 | 3073 | |
a818a4b6 | 3074 | sc->realize = scsi_block_realize; |
b9eea3e6 | 3075 | sc->alloc_req = scsi_block_new_request; |
3e7e180a | 3076 | sc->parse_cdb = scsi_block_parse_cdb; |
8fdc7839 PB |
3077 | sdc->dma_readv = scsi_block_dma_readv; |
3078 | sdc->dma_writev = scsi_block_dma_writev; | |
d31347f5 | 3079 | sdc->update_sense = scsi_block_update_sense; |
8fdc7839 | 3080 | sdc->need_fua_emulation = scsi_block_no_fua; |
39bffca2 | 3081 | dc->desc = "SCSI block device passthrough"; |
39bffca2 | 3082 | dc->props = scsi_block_properties; |
43b978b9 | 3083 | dc->vmsd = &vmstate_scsi_disk_state; |
b9eea3e6 AL |
3084 | } |
3085 | ||
8c43a6f0 | 3086 | static const TypeInfo scsi_block_info = { |
39bffca2 | 3087 | .name = "scsi-block", |
993935f3 | 3088 | .parent = TYPE_SCSI_DISK_BASE, |
39bffca2 | 3089 | .class_init = scsi_block_class_initfn, |
b9eea3e6 | 3090 | }; |
336a6915 | 3091 | #endif |
b9eea3e6 | 3092 | |
39bffca2 AL |
3093 | static Property scsi_disk_properties[] = { |
3094 | DEFINE_SCSI_DISK_PROPERTIES(), | |
bfe3d7ac PB |
3095 | DEFINE_PROP_BIT("removable", SCSIDiskState, features, |
3096 | SCSI_DISK_F_REMOVABLE, false), | |
da8365db PB |
3097 | DEFINE_PROP_BIT("dpofua", SCSIDiskState, features, |
3098 | SCSI_DISK_F_DPOFUA, false), | |
2ecab408 PB |
3099 | DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0), |
3100 | DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0), | |
64cc2284 | 3101 | DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0), |
8a1bd297 PB |
3102 | DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size, |
3103 | DEFAULT_MAX_UNMAP_SIZE), | |
f8e1f533 PB |
3104 | DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size, |
3105 | DEFAULT_MAX_IO_SIZE), | |
2343be0d PB |
3106 | DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, |
3107 | 5), | |
39bffca2 AL |
3108 | DEFINE_PROP_END_OF_LIST(), |
3109 | }; | |
3110 | ||
b9eea3e6 AL |
3111 | static void scsi_disk_class_initfn(ObjectClass *klass, void *data) |
3112 | { | |
39bffca2 | 3113 | DeviceClass *dc = DEVICE_CLASS(klass); |
b9eea3e6 AL |
3114 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
3115 | ||
a818a4b6 | 3116 | sc->realize = scsi_disk_realize; |
b9eea3e6 AL |
3117 | sc->alloc_req = scsi_new_request; |
3118 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; | |
39bffca2 AL |
3119 | dc->fw_name = "disk"; |
3120 | dc->desc = "virtual SCSI disk or CD-ROM (legacy)"; | |
3121 | dc->reset = scsi_disk_reset; | |
3122 | dc->props = scsi_disk_properties; | |
43b978b9 | 3123 | dc->vmsd = &vmstate_scsi_disk_state; |
b9eea3e6 AL |
3124 | } |
3125 | ||
8c43a6f0 | 3126 | static const TypeInfo scsi_disk_info = { |
39bffca2 | 3127 | .name = "scsi-disk", |
993935f3 | 3128 | .parent = TYPE_SCSI_DISK_BASE, |
39bffca2 | 3129 | .class_init = scsi_disk_class_initfn, |
d52affa7 GH |
3130 | }; |
3131 | ||
83f7d43a | 3132 | static void scsi_disk_register_types(void) |
d52affa7 | 3133 | { |
993935f3 | 3134 | type_register_static(&scsi_disk_base_info); |
39bffca2 AL |
3135 | type_register_static(&scsi_hd_info); |
3136 | type_register_static(&scsi_cd_info); | |
b9eea3e6 | 3137 | #ifdef __linux__ |
39bffca2 | 3138 | type_register_static(&scsi_block_info); |
b9eea3e6 | 3139 | #endif |
39bffca2 | 3140 | type_register_static(&scsi_disk_info); |
8ccc2ace | 3141 | } |
83f7d43a AF |
3142 | |
3143 | type_init(scsi_disk_register_types) |