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