]> Git Repo - qemu.git/blame - hw/scsi-disk.c
scsi-disk: adjust offsets in MODE SENSE by 2
[qemu.git] / hw / scsi-disk.c
CommitLineData
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, ...) \
26do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
2e5d83bb 27#else
001faf32 28#define DPRINTF(fmt, ...) do {} while(0)
2e5d83bb
PB
29#endif
30
87ecb68b 31#include "qemu-common.h"
2f792016 32#include "qemu-error.h"
43b443b6 33#include "scsi.h"
0d65e1f8 34#include "scsi-defs.h"
666daa68 35#include "sysemu.h"
2446333c 36#include "blockdev.h"
9db1c0f7 37#include "hw/block-common.h"
5d0d2467 38#include "dma.h"
22864256 39
336a6915
PB
40#ifdef __linux
41#include <scsi/sg.h>
42#endif
43
f0f72ffe 44#define SCSI_DMA_BUF_SIZE 131072
57575058 45#define SCSI_MAX_INQUIRY_LEN 256
a917d384 46
d52affa7
GH
47typedef struct SCSIDiskState SCSIDiskState;
48
4c41d2ef
GH
49typedef struct SCSIDiskReq {
50 SCSIRequest req;
a917d384 51 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
e035b43d
AL
52 uint64_t sector;
53 uint32_t sector_count;
7285477a 54 uint32_t buflen;
a0e66a69 55 bool started;
c87c0672
AL
56 struct iovec iov;
57 QEMUIOVector qiov;
a597e79c 58 BlockAcctCookie acct;
4c41d2ef 59} SCSIDiskReq;
a917d384 60
bfe3d7ac 61#define SCSI_DISK_F_REMOVABLE 0
da8365db 62#define SCSI_DISK_F_DPOFUA 1
bfe3d7ac 63
d52affa7 64struct SCSIDiskState
a917d384 65{
d52affa7 66 SCSIDevice qdev;
bfe3d7ac 67 uint32_t features;
8a9c16f6 68 bool media_changed;
3c2f7c12 69 bool media_event;
4480de19 70 bool eject_request;
27395add 71 uint64_t wwn;
213189ab 72 QEMUBH *bh;
383b4d9b 73 char *version;
a0fef654 74 char *serial;
353815aa
DF
75 char *vendor;
76 char *product;
ece0d5e9 77 bool tray_open;
81b1008d 78 bool tray_locked;
2e5d83bb
PB
79};
80
71544d30 81static int scsi_handle_rw_error(SCSIDiskReq *r, int error);
5dba48a8 82
ad2d30f7 83static void scsi_free_request(SCSIRequest *req)
4d611c9a 84{
ad2d30f7
PB
85 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
86
7285477a
PB
87 if (r->iov.iov_base) {
88 qemu_vfree(r->iov.iov_base);
89 }
4d611c9a
PB
90}
91
b45ef674
PB
92/* Helper function for command completion with sense. */
93static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
ed3a34a3 94{
02fa69b6
BS
95 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
96 r->req.tag, sense.key, sense.asc, sense.ascq);
b45ef674
PB
97 scsi_req_build_sense(&r->req, sense);
98 scsi_req_complete(&r->req, CHECK_CONDITION);
4d611c9a
PB
99}
100
101/* Cancel a pending data transfer. */
5c6c0e51 102static void scsi_cancel_io(SCSIRequest *req)
4d611c9a 103{
5c6c0e51
HR
104 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
105
106 DPRINTF("Cancel tag=0x%x\n", req->tag);
107 if (r->req.aiocb) {
108 bdrv_aio_cancel(r->req.aiocb);
c7bae6a7
PB
109
110 /* This reference was left in by scsi_*_data. We take ownership of
111 * it the moment scsi_req_cancel is called, independent of whether
112 * bdrv_aio_cancel completes the request or not. */
113 scsi_req_unref(&r->req);
a917d384 114 }
5c6c0e51 115 r->req.aiocb = NULL;
a917d384
PB
116}
117
43b978b9 118static uint32_t scsi_init_iovec(SCSIDiskReq *r, size_t size)
103b40f5 119{
7285477a
PB
120 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
121
122 if (!r->iov.iov_base) {
43b978b9 123 r->buflen = size;
44740c38 124 r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
7285477a
PB
125 }
126 r->iov.iov_len = MIN(r->sector_count * 512, r->buflen);
103b40f5
PB
127 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
128 return r->qiov.size / 512;
129}
130
43b978b9
PB
131static void scsi_disk_save_request(QEMUFile *f, SCSIRequest *req)
132{
133 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
134
135 qemu_put_be64s(f, &r->sector);
136 qemu_put_be32s(f, &r->sector_count);
137 qemu_put_be32s(f, &r->buflen);
18eef3bc
GH
138 if (r->buflen) {
139 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
140 qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len);
141 } else if (!req->retry) {
142 uint32_t len = r->iov.iov_len;
143 qemu_put_be32s(f, &len);
144 qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len);
145 }
43b978b9
PB
146 }
147}
148
149static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req)
150{
151 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
152
153 qemu_get_be64s(f, &r->sector);
154 qemu_get_be32s(f, &r->sector_count);
155 qemu_get_be32s(f, &r->buflen);
156 if (r->buflen) {
157 scsi_init_iovec(r, r->buflen);
158 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
159 qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len);
18eef3bc
GH
160 } else if (!r->req.retry) {
161 uint32_t len;
162 qemu_get_be32s(f, &len);
163 r->iov.iov_len = len;
164 assert(r->iov.iov_len <= r->buflen);
165 qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len);
43b978b9
PB
166 }
167 }
168
169 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
170}
171
c1b35247 172static void scsi_aio_complete(void *opaque, int ret)
5d0d2467
PB
173{
174 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
175 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
176
177 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
178
80624c93 179 if (ret < 0) {
5d0d2467
PB
180 if (scsi_handle_rw_error(r, -ret)) {
181 goto done;
182 }
183 }
184
5d0d2467
PB
185 scsi_req_complete(&r->req, GOOD);
186
187done:
b8aba8d7
PB
188 if (!r->req.io_canceled) {
189 scsi_req_unref(&r->req);
190 }
5d0d2467
PB
191}
192
7e8c49c5
PB
193static bool scsi_is_cmd_fua(SCSICommand *cmd)
194{
195 switch (cmd->buf[0]) {
196 case READ_10:
197 case READ_12:
198 case READ_16:
199 case WRITE_10:
200 case WRITE_12:
201 case WRITE_16:
202 return (cmd->buf[1] & 8) != 0;
203
7f64f8e2
PB
204 case VERIFY_10:
205 case VERIFY_12:
206 case VERIFY_16:
7e8c49c5
PB
207 case WRITE_VERIFY_10:
208 case WRITE_VERIFY_12:
209 case WRITE_VERIFY_16:
210 return true;
211
212 case READ_6:
213 case WRITE_6:
214 default:
215 return false;
216 }
217}
218
219static void scsi_write_do_fua(SCSIDiskReq *r)
220{
221 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
222
223 if (scsi_is_cmd_fua(&r->req.cmd)) {
224 bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH);
c1b35247 225 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r);
7e8c49c5
PB
226 return;
227 }
228
229 scsi_req_complete(&r->req, GOOD);
230 if (!r->req.io_canceled) {
231 scsi_req_unref(&r->req);
232 }
233}
234
b77912a7 235static void scsi_dma_complete(void *opaque, int ret)
a917d384 236{
4c41d2ef 237 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
a597e79c 238 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
a917d384 239
7f64f8e2
PB
240 if (r->req.aiocb != NULL) {
241 r->req.aiocb = NULL;
242 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
243 }
a597e79c 244
80624c93 245 if (ret < 0) {
71544d30 246 if (scsi_handle_rw_error(r, -ret)) {
c7bae6a7 247 goto done;
5dba48a8 248 }
4d611c9a 249 }
5dba48a8 250
b77912a7
PB
251 r->sector += r->sector_count;
252 r->sector_count = 0;
7e8c49c5
PB
253 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
254 scsi_write_do_fua(r);
255 return;
256 } else {
257 scsi_req_complete(&r->req, GOOD);
258 }
c7bae6a7
PB
259
260done:
261 if (!r->req.io_canceled) {
262 scsi_req_unref(&r->req);
263 }
4d611c9a
PB
264}
265
b77912a7 266static void scsi_read_complete(void * opaque, int ret)
0a4ac106
PB
267{
268 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
269 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
b77912a7 270 int n;
0a4ac106 271
b77912a7
PB
272 if (r->req.aiocb != NULL) {
273 r->req.aiocb = NULL;
274 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
275 }
0a4ac106
PB
276
277 if (ret < 0) {
71544d30 278 if (scsi_handle_rw_error(r, -ret)) {
c7bae6a7 279 goto done;
0a4ac106
PB
280 }
281 }
282
b77912a7
PB
283 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->qiov.size);
284
285 n = r->qiov.size / 512;
286 r->sector += n;
287 r->sector_count -= n;
288 scsi_req_data(&r->req, r->qiov.size);
c7bae6a7
PB
289
290done:
291 if (!r->req.io_canceled) {
292 scsi_req_unref(&r->req);
293 }
0a4ac106 294}
5dba48a8 295
ac668426
PB
296/* Actually issue a read to the block device. */
297static void scsi_do_read(void *opaque, int ret)
298{
299 SCSIDiskReq *r = opaque;
300 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
301 uint32_t n;
302
303 if (r->req.aiocb != NULL) {
304 r->req.aiocb = NULL;
305 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
306 }
307
308 if (ret < 0) {
309 if (scsi_handle_rw_error(r, -ret)) {
310 goto done;
311 }
312 }
313
31e8fd86
PB
314 if (r->req.io_canceled) {
315 return;
316 }
317
318 /* The request is used as the AIO opaque value, so add a ref. */
319 scsi_req_ref(&r->req);
320
ac668426
PB
321 if (r->req.sg) {
322 dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BDRV_ACCT_READ);
323 r->req.resid -= r->req.sg->size;
324 r->req.aiocb = dma_bdrv_read(s->qdev.conf.bs, r->req.sg, r->sector,
325 scsi_dma_complete, r);
326 } else {
327 n = scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
328 bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
329 r->req.aiocb = bdrv_aio_readv(s->qdev.conf.bs, r->sector, &r->qiov, n,
330 scsi_read_complete, r);
331 }
332
333done:
334 if (!r->req.io_canceled) {
335 scsi_req_unref(&r->req);
336 }
337}
338
5c6c0e51
HR
339/* Read more data from scsi device into buffer. */
340static void scsi_read_data(SCSIRequest *req)
2e5d83bb 341{
5c6c0e51 342 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
5dba48a8 343 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
ac668426 344 bool first;
2e5d83bb 345
a917d384
PB
346 DPRINTF("Read sector_count=%d\n", r->sector_count);
347 if (r->sector_count == 0) {
b45ef674
PB
348 /* This also clears the sense buffer for REQUEST SENSE. */
349 scsi_req_complete(&r->req, GOOD);
a917d384 350 return;
2e5d83bb
PB
351 }
352
6fa2c95f
SH
353 /* No data transfer may already be in progress */
354 assert(r->req.aiocb == NULL);
355
c7bae6a7
PB
356 /* The request is used as the AIO opaque value, so add a ref. */
357 scsi_req_ref(&r->req);
efb9ee02
HR
358 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
359 DPRINTF("Data transfer direction invalid\n");
360 scsi_read_complete(r, -EINVAL);
361 return;
362 }
363
a1aff5bf
MA
364 if (s->tray_open) {
365 scsi_read_complete(r, -ENOMEDIUM);
c7bae6a7 366 return;
a1aff5bf 367 }
c7bae6a7 368
ac668426 369 first = !r->started;
a0e66a69 370 r->started = true;
ac668426
PB
371 if (first && scsi_is_cmd_fua(&r->req.cmd)) {
372 bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH);
373 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_do_read, r);
5d0d2467 374 } else {
ac668426 375 scsi_do_read(r, 0);
5d0d2467 376 }
2e5d83bb
PB
377}
378
c7bae6a7
PB
379/*
380 * scsi_handle_rw_error has two return values. 0 means that the error
381 * must be ignored, 1 means that the error has been processed and the
382 * caller should not do anything else for this request. Note that
383 * scsi_handle_rw_error always manages its reference counts, independent
384 * of the return value.
385 */
71544d30 386static int scsi_handle_rw_error(SCSIDiskReq *r, int error)
5dba48a8 387{
71544d30 388 int is_read = (r->req.cmd.xfer == SCSI_XFER_FROM_DEV);
4c41d2ef 389 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
44740c38 390 BlockErrorAction action = bdrv_get_on_error(s->qdev.conf.bs, is_read);
ea8a5d7f 391
380f640f 392 if (action == BLOCK_ERR_IGNORE) {
329c0a48 393 bdrv_emit_qmp_error_event(s->qdev.conf.bs, BDRV_ACTION_IGNORE, is_read);
ea8a5d7f 394 return 0;
380f640f 395 }
ea8a5d7f
AL
396
397 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
398 || action == BLOCK_ERR_STOP_ANY) {
5dba48a8 399
329c0a48 400 bdrv_emit_qmp_error_event(s->qdev.conf.bs, BDRV_ACTION_STOP, is_read);
0461d5a6 401 vm_stop(RUN_STATE_IO_ERROR);
44740c38 402 bdrv_iostatus_set_err(s->qdev.conf.bs, error);
71544d30 403 scsi_req_retry(&r->req);
ea8a5d7f 404 } else {
efb9ee02 405 switch (error) {
7e218df5
PB
406 case ENOMEDIUM:
407 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
408 break;
efb9ee02 409 case ENOMEM:
b45ef674 410 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
efb9ee02
HR
411 break;
412 case EINVAL:
b45ef674 413 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
efb9ee02
HR
414 break;
415 default:
b45ef674 416 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
efb9ee02 417 break;
a1f0cce2 418 }
329c0a48 419 bdrv_emit_qmp_error_event(s->qdev.conf.bs, BDRV_ACTION_REPORT, is_read);
ea8a5d7f 420 }
ea8a5d7f
AL
421 return 1;
422}
423
4d611c9a
PB
424static void scsi_write_complete(void * opaque, int ret)
425{
4c41d2ef 426 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
a597e79c 427 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
ea8a5d7f
AL
428 uint32_t n;
429
8e321cc6
PB
430 if (r->req.aiocb != NULL) {
431 r->req.aiocb = NULL;
44740c38 432 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
8e321cc6 433 }
a597e79c 434
80624c93 435 if (ret < 0) {
71544d30 436 if (scsi_handle_rw_error(r, -ret)) {
c7bae6a7 437 goto done;
5dba48a8 438 }
4d611c9a
PB
439 }
440
103b40f5 441 n = r->qiov.size / 512;
ea8a5d7f
AL
442 r->sector += n;
443 r->sector_count -= n;
a917d384 444 if (r->sector_count == 0) {
7e8c49c5
PB
445 scsi_write_do_fua(r);
446 return;
a917d384 447 } else {
43b978b9 448 scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
103b40f5
PB
449 DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, r->qiov.size);
450 scsi_req_data(&r->req, r->qiov.size);
4d611c9a 451 }
c7bae6a7
PB
452
453done:
454 if (!r->req.io_canceled) {
455 scsi_req_unref(&r->req);
456 }
4d611c9a
PB
457}
458
42741212 459static void scsi_write_data(SCSIRequest *req)
ea8a5d7f 460{
5c6c0e51 461 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
4c41d2ef 462 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
ea8a5d7f
AL
463 uint32_t n;
464
6fa2c95f
SH
465 /* No data transfer may already be in progress */
466 assert(r->req.aiocb == NULL);
467
c7bae6a7
PB
468 /* The request is used as the AIO opaque value, so add a ref. */
469 scsi_req_ref(&r->req);
efb9ee02
HR
470 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
471 DPRINTF("Data transfer direction invalid\n");
472 scsi_write_complete(r, -EINVAL);
42741212 473 return;
efb9ee02
HR
474 }
475
5d0d2467
PB
476 if (!r->req.sg && !r->qiov.size) {
477 /* Called for the first time. Ask the driver to send us more data. */
a0e66a69 478 r->started = true;
5d0d2467
PB
479 scsi_write_complete(r, 0);
480 return;
481 }
482 if (s->tray_open) {
483 scsi_write_complete(r, -ENOMEDIUM);
484 return;
485 }
486
7f64f8e2
PB
487 if (r->req.cmd.buf[0] == VERIFY_10 || r->req.cmd.buf[0] == VERIFY_12 ||
488 r->req.cmd.buf[0] == VERIFY_16) {
489 if (r->req.sg) {
490 scsi_dma_complete(r, 0);
491 } else {
492 scsi_write_complete(r, 0);
493 }
494 return;
495 }
496
5d0d2467
PB
497 if (r->req.sg) {
498 dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BDRV_ACCT_WRITE);
499 r->req.resid -= r->req.sg->size;
500 r->req.aiocb = dma_bdrv_write(s->qdev.conf.bs, r->req.sg, r->sector,
501 scsi_dma_complete, r);
502 } else {
503 n = r->qiov.size / 512;
44740c38
PB
504 bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE);
505 r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, r->sector, &r->qiov, n,
103b40f5 506 scsi_write_complete, r);
ea8a5d7f 507 }
a917d384 508}
2e5d83bb 509
a917d384 510/* Return a pointer to the data buffer. */
5c6c0e51 511static uint8_t *scsi_get_buf(SCSIRequest *req)
a917d384 512{
5c6c0e51 513 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
2e5d83bb 514
3f4cb3d3 515 return (uint8_t *)r->iov.iov_base;
2e5d83bb
PB
516}
517
0b06c059
GH
518static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
519{
383b4d9b 520 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
0b06c059 521 int buflen = 0;
82579390 522 int start;
0b06c059 523
0b06c059
GH
524 if (req->cmd.buf[1] & 0x1) {
525 /* Vital product data */
526 uint8_t page_code = req->cmd.buf[2];
0b06c059 527
e39be482 528 outbuf[buflen++] = s->qdev.type & 0x1f;
0b06c059
GH
529 outbuf[buflen++] = page_code ; // this page
530 outbuf[buflen++] = 0x00;
82579390
PB
531 outbuf[buflen++] = 0x00;
532 start = buflen;
0b06c059
GH
533
534 switch (page_code) {
535 case 0x00: /* Supported page codes, mandatory */
39d98982 536 {
0b06c059
GH
537 DPRINTF("Inquiry EVPD[Supported pages] "
538 "buffer size %zd\n", req->cmd.xfer);
0b06c059 539 outbuf[buflen++] = 0x00; // list of supported pages (this page)
f01b5931 540 if (s->serial) {
3e1c0c9a 541 outbuf[buflen++] = 0x80; // unit serial number
f01b5931 542 }
0b06c059 543 outbuf[buflen++] = 0x83; // device identification
f37bd73b 544 if (s->qdev.type == TYPE_DISK) {
ea3bd56f
CH
545 outbuf[buflen++] = 0xb0; // block limits
546 outbuf[buflen++] = 0xb2; // thin provisioning
39d98982 547 }
0b06c059 548 break;
39d98982 549 }
0b06c059
GH
550 case 0x80: /* Device serial number, optional */
551 {
3e1c0c9a 552 int l;
0b06c059 553
3e1c0c9a
HR
554 if (!s->serial) {
555 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
556 return -1;
557 }
558
559 l = strlen(s->serial);
f01b5931 560 if (l > 20) {
0b06c059 561 l = 20;
f01b5931 562 }
0b06c059
GH
563
564 DPRINTF("Inquiry EVPD[Serial number] "
565 "buffer size %zd\n", req->cmd.xfer);
a0fef654 566 memcpy(outbuf+buflen, s->serial, l);
0b06c059
GH
567 buflen += l;
568 break;
569 }
570
571 case 0x83: /* Device identification page, mandatory */
572 {
fd930791
PB
573 const char *str = s->serial ?: bdrv_get_device_name(s->qdev.conf.bs);
574 int max_len = s->serial ? 20 : 255 - 8;
575 int id_len = strlen(str);
0b06c059 576
f01b5931 577 if (id_len > max_len) {
0b06c059 578 id_len = max_len;
f01b5931 579 }
0b06c059
GH
580 DPRINTF("Inquiry EVPD[Device identification] "
581 "buffer size %zd\n", req->cmd.xfer);
582
0b06c059
GH
583 outbuf[buflen++] = 0x2; // ASCII
584 outbuf[buflen++] = 0; // not officially assigned
585 outbuf[buflen++] = 0; // reserved
586 outbuf[buflen++] = id_len; // length of data following
fd930791 587 memcpy(outbuf+buflen, str, id_len);
0b06c059 588 buflen += id_len;
27395add
PB
589
590 if (s->wwn) {
591 outbuf[buflen++] = 0x1; // Binary
592 outbuf[buflen++] = 0x3; // NAA
593 outbuf[buflen++] = 0; // reserved
594 outbuf[buflen++] = 8;
595 stq_be_p(&outbuf[buflen], s->wwn);
596 buflen += 8;
597 }
0b06c059
GH
598 break;
599 }
ea3bd56f 600 case 0xb0: /* block limits */
ee3659e3 601 {
ea3bd56f
CH
602 unsigned int unmap_sectors =
603 s->qdev.conf.discard_granularity / s->qdev.blocksize;
8cfacf07
CH
604 unsigned int min_io_size =
605 s->qdev.conf.min_io_size / s->qdev.blocksize;
606 unsigned int opt_io_size =
607 s->qdev.conf.opt_io_size / s->qdev.blocksize;
ee3659e3 608
f37bd73b 609 if (s->qdev.type == TYPE_ROM) {
39d98982
HR
610 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
611 page_code);
612 return -1;
613 }
ee3659e3 614 /* required VPD size with unmap support */
82579390 615 buflen = 0x40;
ee3659e3
CH
616 memset(outbuf + 4, 0, buflen - 4);
617
618 /* optimal transfer length granularity */
619 outbuf[6] = (min_io_size >> 8) & 0xff;
620 outbuf[7] = min_io_size & 0xff;
621
622 /* optimal transfer length */
623 outbuf[12] = (opt_io_size >> 24) & 0xff;
624 outbuf[13] = (opt_io_size >> 16) & 0xff;
625 outbuf[14] = (opt_io_size >> 8) & 0xff;
626 outbuf[15] = opt_io_size & 0xff;
ea3bd56f
CH
627
628 /* optimal unmap granularity */
629 outbuf[28] = (unmap_sectors >> 24) & 0xff;
630 outbuf[29] = (unmap_sectors >> 16) & 0xff;
631 outbuf[30] = (unmap_sectors >> 8) & 0xff;
632 outbuf[31] = unmap_sectors & 0xff;
633 break;
634 }
635 case 0xb2: /* thin provisioning */
636 {
82579390 637 buflen = 8;
ea3bd56f 638 outbuf[4] = 0;
c9e4d828 639 outbuf[5] = 0x60; /* write_same 10/16 supported */
f644a290 640 outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1;
ea3bd56f 641 outbuf[7] = 0;
ee3659e3
CH
642 break;
643 }
0b06c059 644 default:
0b06c059
GH
645 return -1;
646 }
647 /* done with EVPD */
82579390
PB
648 assert(buflen - start <= 255);
649 outbuf[start - 1] = buflen - start;
0b06c059
GH
650 return buflen;
651 }
652
653 /* Standard INQUIRY data */
654 if (req->cmd.buf[2] != 0) {
0b06c059
GH
655 return -1;
656 }
657
658 /* PAGE CODE == 0 */
0b06c059 659 buflen = req->cmd.xfer;
f01b5931 660 if (buflen > SCSI_MAX_INQUIRY_LEN) {
0b06c059 661 buflen = SCSI_MAX_INQUIRY_LEN;
f01b5931 662 }
0b06c059
GH
663 memset(outbuf, 0, buflen);
664
f37bd73b 665 outbuf[0] = s->qdev.type & 0x1f;
bfe3d7ac 666 outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0;
353815aa
DF
667
668 strpadcpy((char *) &outbuf[16], 16, s->product, ' ');
669 strpadcpy((char *) &outbuf[8], 8, s->vendor, ' ');
670
314b1811 671 memset(&outbuf[32], 0, 4);
552fee93 672 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
99aba0c4
CH
673 /*
674 * We claim conformance to SPC-3, which is required for guests
675 * to ask for modern features like READ CAPACITY(16) or the
676 * block characteristics VPD page by default. Not all of SPC-3
677 * is actually implemented, but we're good enough.
678 */
ee3659e3 679 outbuf[2] = 5;
0b06c059 680 outbuf[3] = 2; /* Format 2 */
ad3cea42
AT
681
682 if (buflen > 36) {
683 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
684 } else {
685 /* If the allocation length of CDB is too small,
686 the additional length is not adjusted */
687 outbuf[4] = 36 - 5;
688 }
689
0b06c059 690 /* Sync data transfer and TCQ. */
afd4030c 691 outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0);
0b06c059
GH
692 return buflen;
693}
694
430ee2f2
PB
695static inline bool media_is_dvd(SCSIDiskState *s)
696{
697 uint64_t nb_sectors;
698 if (s->qdev.type != TYPE_ROM) {
699 return false;
700 }
44740c38 701 if (!bdrv_is_inserted(s->qdev.conf.bs)) {
430ee2f2
PB
702 return false;
703 }
44740c38 704 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
430ee2f2
PB
705 return nb_sectors > CD_MAX_SECTORS;
706}
707
ceb792ef
PB
708static inline bool media_is_cd(SCSIDiskState *s)
709{
710 uint64_t nb_sectors;
711 if (s->qdev.type != TYPE_ROM) {
712 return false;
713 }
44740c38 714 if (!bdrv_is_inserted(s->qdev.conf.bs)) {
ceb792ef
PB
715 return false;
716 }
44740c38 717 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
ceb792ef
PB
718 return nb_sectors <= CD_MAX_SECTORS;
719}
720
1a4f0c3a
PB
721static int scsi_read_disc_information(SCSIDiskState *s, SCSIDiskReq *r,
722 uint8_t *outbuf)
723{
724 uint8_t type = r->req.cmd.buf[1] & 7;
725
726 if (s->qdev.type != TYPE_ROM) {
727 return -1;
728 }
729
730 /* Types 1/2 are only defined for Blu-Ray. */
731 if (type != 0) {
732 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
733 return -1;
734 }
735
736 memset(outbuf, 0, 34);
737 outbuf[1] = 32;
738 outbuf[2] = 0xe; /* last session complete, disc finalized */
739 outbuf[3] = 1; /* first track on disc */
740 outbuf[4] = 1; /* # of sessions */
741 outbuf[5] = 1; /* first track of last session */
742 outbuf[6] = 1; /* last track of last session */
743 outbuf[7] = 0x20; /* unrestricted use */
744 outbuf[8] = 0x00; /* CD-ROM or DVD-ROM */
745 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
746 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
747 /* 24-31: disc bar code */
748 /* 32: disc application code */
749 /* 33: number of OPC tables */
750
751 return 34;
752}
753
b6c251ab
PB
754static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r,
755 uint8_t *outbuf)
756{
ceb792ef
PB
757 static const int rds_caps_size[5] = {
758 [0] = 2048 + 4,
759 [1] = 4 + 4,
760 [3] = 188 + 4,
761 [4] = 2048 + 4,
762 };
763
764 uint8_t media = r->req.cmd.buf[1];
765 uint8_t layer = r->req.cmd.buf[6];
766 uint8_t format = r->req.cmd.buf[7];
767 int size = -1;
768
769 if (s->qdev.type != TYPE_ROM) {
770 return -1;
771 }
772 if (media != 0) {
773 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
774 return -1;
775 }
776
777 if (format != 0xff) {
44740c38 778 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
ceb792ef
PB
779 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
780 return -1;
781 }
782 if (media_is_cd(s)) {
783 scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT));
784 return -1;
785 }
786 if (format >= ARRAY_SIZE(rds_caps_size)) {
787 return -1;
788 }
789 size = rds_caps_size[format];
790 memset(outbuf, 0, size);
791 }
792
793 switch (format) {
794 case 0x00: {
795 /* Physical format information */
796 uint64_t nb_sectors;
797 if (layer != 0) {
798 goto fail;
799 }
44740c38 800 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
ceb792ef
PB
801
802 outbuf[4] = 1; /* DVD-ROM, part version 1 */
803 outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
804 outbuf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
805 outbuf[7] = 0; /* default densities */
806
807 stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */
808 stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */
809 break;
810 }
811
812 case 0x01: /* DVD copyright information, all zeros */
813 break;
814
815 case 0x03: /* BCA information - invalid field for no BCA info */
816 return -1;
817
818 case 0x04: /* DVD disc manufacturing information, all zeros */
819 break;
820
821 case 0xff: { /* List capabilities */
822 int i;
823 size = 4;
824 for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) {
825 if (!rds_caps_size[i]) {
826 continue;
827 }
828 outbuf[size] = i;
829 outbuf[size + 1] = 0x40; /* Not writable, readable */
830 stw_be_p(&outbuf[size + 2], rds_caps_size[i]);
831 size += 4;
832 }
833 break;
834 }
835
836 default:
837 return -1;
838 }
839
840 /* Size of buffer, not including 2 byte size field */
841 stw_be_p(outbuf, size - 2);
842 return size;
843
844fail:
b6c251ab
PB
845 return -1;
846}
847
3c2f7c12 848static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf)
b6c251ab 849{
3c2f7c12
PB
850 uint8_t event_code, media_status;
851
852 media_status = 0;
853 if (s->tray_open) {
854 media_status = MS_TRAY_OPEN;
44740c38 855 } else if (bdrv_is_inserted(s->qdev.conf.bs)) {
3c2f7c12
PB
856 media_status = MS_MEDIA_PRESENT;
857 }
858
859 /* Event notification descriptor */
860 event_code = MEC_NO_CHANGE;
4480de19
PB
861 if (media_status != MS_TRAY_OPEN) {
862 if (s->media_event) {
863 event_code = MEC_NEW_MEDIA;
864 s->media_event = false;
865 } else if (s->eject_request) {
866 event_code = MEC_EJECT_REQUESTED;
867 s->eject_request = false;
868 }
3c2f7c12
PB
869 }
870
871 outbuf[0] = event_code;
872 outbuf[1] = media_status;
873
874 /* These fields are reserved, just clear them. */
875 outbuf[2] = 0;
876 outbuf[3] = 0;
877 return 4;
878}
879
880static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r,
881 uint8_t *outbuf)
882{
883 int size;
884 uint8_t *buf = r->req.cmd.buf;
885 uint8_t notification_class_request = buf[4];
886 if (s->qdev.type != TYPE_ROM) {
887 return -1;
888 }
889 if ((buf[1] & 1) == 0) {
890 /* asynchronous */
891 return -1;
892 }
893
894 size = 4;
895 outbuf[0] = outbuf[1] = 0;
896 outbuf[3] = 1 << GESN_MEDIA; /* supported events */
897 if (notification_class_request & (1 << GESN_MEDIA)) {
898 outbuf[2] = GESN_MEDIA;
899 size += scsi_event_status_media(s, &outbuf[size]);
900 } else {
901 outbuf[2] = 0x80;
902 }
903 stw_be_p(outbuf, size - 4);
904 return size;
b6c251ab
PB
905}
906
430ee2f2 907static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf)
b6c251ab 908{
430ee2f2
PB
909 int current;
910
b6c251ab
PB
911 if (s->qdev.type != TYPE_ROM) {
912 return -1;
913 }
430ee2f2
PB
914 current = media_is_dvd(s) ? MMC_PROFILE_DVD_ROM : MMC_PROFILE_CD_ROM;
915 memset(outbuf, 0, 40);
916 stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */
917 stw_be_p(&outbuf[6], current);
918 /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
919 outbuf[10] = 0x03; /* persistent, current */
920 outbuf[11] = 8; /* two profiles */
921 stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM);
922 outbuf[14] = (current == MMC_PROFILE_DVD_ROM);
923 stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM);
924 outbuf[18] = (current == MMC_PROFILE_CD_ROM);
925 /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
926 stw_be_p(&outbuf[20], 1);
927 outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */
928 outbuf[23] = 8;
929 stl_be_p(&outbuf[24], 1); /* SCSI */
930 outbuf[28] = 1; /* DBE = 1, mandatory */
931 /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
932 stw_be_p(&outbuf[32], 3);
933 outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */
934 outbuf[35] = 4;
935 outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
936 /* TODO: Random readable, CD read, DVD read, drive serial number,
937 power management */
938 return 40;
b6c251ab
PB
939}
940
941static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf)
942{
943 if (s->qdev.type != TYPE_ROM) {
944 return -1;
945 }
946 memset(outbuf, 0, 8);
947 outbuf[5] = 1; /* CD-ROM */
948 return 8;
949}
950
cfc606da 951static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
282ab04e 952 int page_control)
ebddfcbe 953{
a8f4bbe2
PB
954 static const int mode_sense_valid[0x3f] = {
955 [MODE_PAGE_HD_GEOMETRY] = (1 << TYPE_DISK),
956 [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK),
957 [MODE_PAGE_CACHING] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
a07c7dcd
PB
958 [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
959 [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM),
a8f4bbe2
PB
960 [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM),
961 };
ef405611
PB
962
963 uint8_t *p = *p_outbuf + 2;
964 int length;
ebddfcbe 965
a8f4bbe2
PB
966 if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) {
967 return -1;
968 }
969
282ab04e
BK
970 /*
971 * If Changeable Values are requested, a mask denoting those mode parameters
972 * that are changeable shall be returned. As we currently don't support
973 * parameter changes via MODE_SELECT all bits are returned set to zero.
974 * The buffer was already menset to zero by the caller of this function.
ef405611
PB
975 *
976 * The offsets here are off by two compared to the descriptions in the
977 * SCSI specs, because those include a 2-byte header. This is unfortunate,
978 * but it is done so that offsets are consistent within our implementation
979 * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both
980 * 2-byte and 4-byte headers.
282ab04e 981 */
ebddfcbe 982 switch (page) {
67cc61e4 983 case MODE_PAGE_HD_GEOMETRY:
ef405611 984 length = 0x16;
282ab04e 985 if (page_control == 1) { /* Changeable Values */
cfc606da 986 break;
282ab04e 987 }
ebddfcbe 988 /* if a geometry hint is available, use it */
ef405611
PB
989 p[0] = (s->qdev.conf.cyls >> 16) & 0xff;
990 p[1] = (s->qdev.conf.cyls >> 8) & 0xff;
991 p[2] = s->qdev.conf.cyls & 0xff;
992 p[3] = s->qdev.conf.heads & 0xff;
ebddfcbe 993 /* Write precomp start cylinder, disabled */
ef405611
PB
994 p[4] = (s->qdev.conf.cyls >> 16) & 0xff;
995 p[5] = (s->qdev.conf.cyls >> 8) & 0xff;
996 p[6] = s->qdev.conf.cyls & 0xff;
ebddfcbe 997 /* Reduced current start cylinder, disabled */
ef405611
PB
998 p[7] = (s->qdev.conf.cyls >> 16) & 0xff;
999 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1000 p[9] = s->qdev.conf.cyls & 0xff;
ebddfcbe 1001 /* Device step rate [ns], 200ns */
ef405611
PB
1002 p[10] = 0;
1003 p[11] = 200;
ebddfcbe 1004 /* Landing zone cylinder */
ef405611
PB
1005 p[12] = 0xff;
1006 p[13] = 0xff;
ebddfcbe 1007 p[14] = 0xff;
ebddfcbe 1008 /* Medium rotation rate [rpm], 5400 rpm */
ef405611
PB
1009 p[18] = (5400 >> 8) & 0xff;
1010 p[19] = 5400 & 0xff;
cfc606da 1011 break;
ebddfcbe 1012
67cc61e4 1013 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY:
ef405611 1014 length = 0x1e;
282ab04e 1015 if (page_control == 1) { /* Changeable Values */
cfc606da 1016 break;
282ab04e 1017 }
ebddfcbe 1018 /* Transfer rate [kbit/s], 5Mbit/s */
ef405611
PB
1019 p[0] = 5000 >> 8;
1020 p[1] = 5000 & 0xff;
ebddfcbe 1021 /* if a geometry hint is available, use it */
ef405611
PB
1022 p[2] = s->qdev.conf.heads & 0xff;
1023 p[3] = s->qdev.conf.secs & 0xff;
1024 p[4] = s->qdev.blocksize >> 8;
1025 p[6] = (s->qdev.conf.cyls >> 8) & 0xff;
1026 p[7] = s->qdev.conf.cyls & 0xff;
1027 /* Write precomp start cylinder, disabled */
d252df48
MA
1028 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1029 p[9] = s->qdev.conf.cyls & 0xff;
ef405611 1030 /* Reduced current start cylinder, disabled */
d252df48
MA
1031 p[10] = (s->qdev.conf.cyls >> 8) & 0xff;
1032 p[11] = s->qdev.conf.cyls & 0xff;
ebddfcbe 1033 /* Device step rate [100us], 100us */
ef405611
PB
1034 p[12] = 0;
1035 p[13] = 1;
ebddfcbe 1036 /* Device step pulse width [us], 1us */
ef405611 1037 p[14] = 1;
ebddfcbe 1038 /* Device head settle delay [100us], 100us */
ef405611
PB
1039 p[15] = 0;
1040 p[16] = 1;
ebddfcbe 1041 /* Motor on delay [0.1s], 0.1s */
ef405611 1042 p[17] = 1;
ebddfcbe 1043 /* Motor off delay [0.1s], 0.1s */
ef405611 1044 p[18] = 1;
ebddfcbe 1045 /* Medium rotation rate [rpm], 5400 rpm */
ef405611
PB
1046 p[26] = (5400 >> 8) & 0xff;
1047 p[27] = 5400 & 0xff;
cfc606da 1048 break;
ebddfcbe 1049
67cc61e4 1050 case MODE_PAGE_CACHING:
ef405611 1051 length = 0x12;
282ab04e 1052 if (page_control == 1) { /* Changeable Values */
cfc606da 1053 break;
282ab04e 1054 }
44740c38 1055 if (bdrv_enable_write_cache(s->qdev.conf.bs)) {
ef405611 1056 p[0] = 4; /* WCE */
ebddfcbe 1057 }
cfc606da 1058 break;
ebddfcbe 1059
a07c7dcd 1060 case MODE_PAGE_R_W_ERROR:
ef405611
PB
1061 length = 10;
1062 p[0] = 0x80; /* Automatic Write Reallocation Enabled */
a07c7dcd 1063 if (s->qdev.type == TYPE_ROM) {
ef405611 1064 p[1] = 0x20; /* Read Retry Count */
a07c7dcd
PB
1065 }
1066 break;
1067
1068 case MODE_PAGE_AUDIO_CTL:
ef405611 1069 length = 14;
a07c7dcd
PB
1070 break;
1071
67cc61e4 1072 case MODE_PAGE_CAPABILITIES:
ef405611 1073 length = 0x14;
282ab04e 1074 if (page_control == 1) { /* Changeable Values */
cfc606da 1075 break;
282ab04e 1076 }
a07c7dcd 1077
ef405611
PB
1078 p[0] = 0x3b; /* CD-R & CD-RW read */
1079 p[1] = 0; /* Writing not supported */
1080 p[2] = 0x7f; /* Audio, composite, digital out,
ebddfcbe 1081 mode 2 form 1&2, multi session */
ef405611 1082 p[3] = 0xff; /* CD DA, DA accurate, RW supported,
ebddfcbe
GH
1083 RW corrected, C2 errors, ISRC,
1084 UPC, Bar code */
ef405611 1085 p[4] = 0x2d | (s->tray_locked ? 2 : 0);
ebddfcbe 1086 /* Locking supported, jumper present, eject, tray */
ef405611 1087 p[5] = 0; /* no volume & mute control, no
ebddfcbe 1088 changer */
ef405611
PB
1089 p[6] = (50 * 176) >> 8; /* 50x read speed */
1090 p[7] = (50 * 176) & 0xff;
1091 p[8] = 2 >> 8; /* Two volume levels */
1092 p[9] = 2 & 0xff;
1093 p[10] = 2048 >> 8; /* 2M buffer */
1094 p[11] = 2048 & 0xff;
1095 p[12] = (16 * 176) >> 8; /* 16x read speed current */
1096 p[13] = (16 * 176) & 0xff;
1097 p[16] = (16 * 176) >> 8; /* 16x write speed */
1098 p[17] = (16 * 176) & 0xff;
1099 p[18] = (16 * 176) >> 8; /* 16x write speed current */
ebddfcbe 1100 p[19] = (16 * 176) & 0xff;
cfc606da 1101 break;
ebddfcbe
GH
1102
1103 default:
cfc606da 1104 return -1;
ebddfcbe 1105 }
cfc606da 1106
ef405611
PB
1107 assert(length < 256);
1108 (*p_outbuf)[0] = page;
1109 (*p_outbuf)[1] = length;
1110 *p_outbuf += length + 2;
1111 return length + 2;
ebddfcbe
GH
1112}
1113
cfc606da 1114static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
ebddfcbe 1115{
cfc606da 1116 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
ebddfcbe 1117 uint64_t nb_sectors;
e590ecbe
PB
1118 bool dbd;
1119 int page, buflen, ret, page_control;
ebddfcbe 1120 uint8_t *p;
ce512ee1 1121 uint8_t dev_specific_param;
ebddfcbe 1122
e590ecbe 1123 dbd = (r->req.cmd.buf[1] & 0x8) != 0;
cfc606da
PB
1124 page = r->req.cmd.buf[2] & 0x3f;
1125 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
aa2b1e89 1126 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
cfc606da
PB
1127 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
1128 memset(outbuf, 0, r->req.cmd.xfer);
ebddfcbe
GH
1129 p = outbuf;
1130
e590ecbe 1131 if (s->qdev.type == TYPE_DISK) {
da8365db 1132 dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0;
e590ecbe
PB
1133 if (bdrv_is_read_only(s->qdev.conf.bs)) {
1134 dev_specific_param |= 0x80; /* Readonly. */
1135 }
ce512ee1 1136 } else {
e590ecbe
PB
1137 /* MMC prescribes that CD/DVD drives have no block descriptors,
1138 * and defines no device-specific parameter. */
6a2de0f2 1139 dev_specific_param = 0x00;
e590ecbe 1140 dbd = true;
ce512ee1
BK
1141 }
1142
cfc606da 1143 if (r->req.cmd.buf[0] == MODE_SENSE) {
ce512ee1
BK
1144 p[1] = 0; /* Default media type. */
1145 p[2] = dev_specific_param;
1146 p[3] = 0; /* Block descriptor length. */
1147 p += 4;
1148 } else { /* MODE_SENSE_10 */
1149 p[2] = 0; /* Default media type. */
1150 p[3] = dev_specific_param;
1151 p[6] = p[7] = 0; /* Block descriptor length. */
1152 p += 8;
ebddfcbe 1153 }
ebddfcbe 1154
44740c38 1155 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
e590ecbe 1156 if (!dbd && nb_sectors) {
cfc606da 1157 if (r->req.cmd.buf[0] == MODE_SENSE) {
ce512ee1
BK
1158 outbuf[3] = 8; /* Block descriptor length */
1159 } else { /* MODE_SENSE_10 */
1160 outbuf[7] = 8; /* Block descriptor length */
1161 }
69377307 1162 nb_sectors /= (s->qdev.blocksize / 512);
f01b5931 1163 if (nb_sectors > 0xffffff) {
2488b740 1164 nb_sectors = 0;
f01b5931 1165 }
ebddfcbe
GH
1166 p[0] = 0; /* media density code */
1167 p[1] = (nb_sectors >> 16) & 0xff;
1168 p[2] = (nb_sectors >> 8) & 0xff;
1169 p[3] = nb_sectors & 0xff;
1170 p[4] = 0; /* reserved */
1171 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
69377307 1172 p[6] = s->qdev.blocksize >> 8;
ebddfcbe
GH
1173 p[7] = 0;
1174 p += 8;
1175 }
1176
cfc606da
PB
1177 if (page_control == 3) {
1178 /* Saved Values */
1179 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
1180 return -1;
282ab04e
BK
1181 }
1182
cfc606da
PB
1183 if (page == 0x3f) {
1184 for (page = 0; page <= 0x3e; page++) {
1185 mode_sense_page(s, page, &p, page_control);
1186 }
1187 } else {
1188 ret = mode_sense_page(s, page, &p, page_control);
1189 if (ret == -1) {
1190 return -1;
1191 }
ebddfcbe
GH
1192 }
1193
1194 buflen = p - outbuf;
ce512ee1
BK
1195 /*
1196 * The mode data length field specifies the length in bytes of the
1197 * following data that is available to be transferred. The mode data
1198 * length does not include itself.
1199 */
cfc606da 1200 if (r->req.cmd.buf[0] == MODE_SENSE) {
ce512ee1
BK
1201 outbuf[0] = buflen - 1;
1202 } else { /* MODE_SENSE_10 */
1203 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
1204 outbuf[1] = (buflen - 2) & 0xff;
1205 }
ebddfcbe
GH
1206 return buflen;
1207}
1208
02880f43
GH
1209static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
1210{
1211 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
02880f43
GH
1212 int start_track, format, msf, toclen;
1213 uint64_t nb_sectors;
1214
1215 msf = req->cmd.buf[1] & 2;
1216 format = req->cmd.buf[2] & 0xf;
1217 start_track = req->cmd.buf[6];
44740c38 1218 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
02880f43 1219 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
69377307 1220 nb_sectors /= s->qdev.blocksize / 512;
02880f43
GH
1221 switch (format) {
1222 case 0:
1223 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
1224 break;
1225 case 1:
1226 /* multi session : only a single session defined */
1227 toclen = 12;
1228 memset(outbuf, 0, 12);
1229 outbuf[1] = 0x0a;
1230 outbuf[2] = 0x01;
1231 outbuf[3] = 0x01;
1232 break;
1233 case 2:
1234 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
1235 break;
1236 default:
1237 return -1;
1238 }
02880f43
GH
1239 return toclen;
1240}
1241
68bb01f3 1242static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
bfd52647
MA
1243{
1244 SCSIRequest *req = &r->req;
1245 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1246 bool start = req->cmd.buf[4] & 1;
1247 bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
1248
1249 if (s->qdev.type == TYPE_ROM && loej) {
68bb01f3
MA
1250 if (!start && !s->tray_open && s->tray_locked) {
1251 scsi_check_condition(r,
44740c38 1252 bdrv_is_inserted(s->qdev.conf.bs)
68bb01f3
MA
1253 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED)
1254 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED));
1255 return -1;
fdec4404 1256 }
d88b1819
LC
1257
1258 if (s->tray_open != !start) {
1259 bdrv_eject(s->qdev.conf.bs, !start);
1260 s->tray_open = !start;
1261 }
bfd52647 1262 }
68bb01f3 1263 return 0;
bfd52647
MA
1264}
1265
314a3299
PB
1266static void scsi_disk_emulate_read_data(SCSIRequest *req)
1267{
1268 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1269 int buflen = r->iov.iov_len;
1270
1271 if (buflen) {
1272 DPRINTF("Read buf_len=%zd\n", buflen);
1273 r->iov.iov_len = 0;
1274 r->started = true;
1275 scsi_req_data(&r->req, buflen);
1276 return;
1277 }
1278
1279 /* This also clears the sense buffer for REQUEST SENSE. */
1280 scsi_req_complete(&r->req, GOOD);
1281}
1282
1283static void scsi_disk_emulate_write_data(SCSIRequest *req)
1284{
af6d510d
PB
1285 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1286
1287 if (r->iov.iov_len) {
1288 int buflen = r->iov.iov_len;
1289 DPRINTF("Write buf_len=%zd\n", buflen);
1290 r->iov.iov_len = 0;
1291 scsi_req_data(&r->req, buflen);
1292 return;
1293 }
1294
1295 switch (req->cmd.buf[0]) {
1296 case MODE_SELECT:
1297 case MODE_SELECT_10:
1298 /* This also clears the sense buffer for REQUEST SENSE. */
1299 scsi_req_complete(&r->req, GOOD);
1300 break;
1301
1302 default:
1303 abort();
1304 }
314a3299
PB
1305}
1306
b08d0ea0 1307static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf)
aa5dbdc1 1308{
b08d0ea0 1309 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
e7e25e32 1310 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
e7e25e32 1311 uint64_t nb_sectors;
7285477a 1312 uint8_t *outbuf;
af6d510d 1313 int buflen;
aa5dbdc1 1314
b08d0ea0
PB
1315 switch (req->cmd.buf[0]) {
1316 case INQUIRY:
1317 case MODE_SENSE:
1318 case MODE_SENSE_10:
1319 case RESERVE:
1320 case RESERVE_10:
1321 case RELEASE:
1322 case RELEASE_10:
1323 case START_STOP:
1324 case ALLOW_MEDIUM_REMOVAL:
1325 case GET_CONFIGURATION:
1326 case GET_EVENT_STATUS_NOTIFICATION:
1327 case MECHANISM_STATUS:
1328 case REQUEST_SENSE:
1329 break;
1330
1331 default:
1332 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
1333 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1334 return 0;
1335 }
1336 break;
1337 }
1338
7285477a
PB
1339 if (!r->iov.iov_base) {
1340 /*
1341 * FIXME: we shouldn't return anything bigger than 4k, but the code
1342 * requires the buffer to be as big as req->cmd.xfer in several
1343 * places. So, do not allow CDBs with a very large ALLOCATION
1344 * LENGTH. The real fix would be to modify scsi_read_data and
1345 * dma_buf_read, so that they return data beyond the buflen
1346 * as all zeros.
1347 */
1348 if (req->cmd.xfer > 65536) {
1349 goto illegal_request;
1350 }
1351 r->buflen = MAX(4096, req->cmd.xfer);
44740c38 1352 r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
7285477a
PB
1353 }
1354
af6d510d 1355 buflen = req->cmd.xfer;
7285477a 1356 outbuf = r->iov.iov_base;
aa5dbdc1
GH
1357 switch (req->cmd.buf[0]) {
1358 case TEST_UNIT_READY:
9bcaf4fe 1359 assert(!s->tray_open && bdrv_is_inserted(s->qdev.conf.bs));
5f71d32f 1360 break;
0b06c059
GH
1361 case INQUIRY:
1362 buflen = scsi_disk_emulate_inquiry(req, outbuf);
f01b5931 1363 if (buflen < 0) {
0b06c059 1364 goto illegal_request;
f01b5931 1365 }
5f71d32f 1366 break;
ebddfcbe
GH
1367 case MODE_SENSE:
1368 case MODE_SENSE_10:
cfc606da 1369 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
f01b5931 1370 if (buflen < 0) {
ebddfcbe 1371 goto illegal_request;
f01b5931 1372 }
ebddfcbe 1373 break;
02880f43
GH
1374 case READ_TOC:
1375 buflen = scsi_disk_emulate_read_toc(req, outbuf);
f01b5931 1376 if (buflen < 0) {
02880f43 1377 goto illegal_request;
f01b5931 1378 }
02880f43 1379 break;
3d53ba18 1380 case RESERVE:
f01b5931 1381 if (req->cmd.buf[1] & 1) {
3d53ba18 1382 goto illegal_request;
f01b5931 1383 }
3d53ba18
GH
1384 break;
1385 case RESERVE_10:
f01b5931 1386 if (req->cmd.buf[1] & 3) {
3d53ba18 1387 goto illegal_request;
f01b5931 1388 }
3d53ba18
GH
1389 break;
1390 case RELEASE:
f01b5931 1391 if (req->cmd.buf[1] & 1) {
3d53ba18 1392 goto illegal_request;
f01b5931 1393 }
3d53ba18
GH
1394 break;
1395 case RELEASE_10:
f01b5931 1396 if (req->cmd.buf[1] & 3) {
3d53ba18 1397 goto illegal_request;
f01b5931 1398 }
3d53ba18 1399 break;
8d3628ff 1400 case START_STOP:
68bb01f3 1401 if (scsi_disk_emulate_start_stop(r) < 0) {
b08d0ea0 1402 return 0;
68bb01f3 1403 }
5f71d32f 1404 break;
c68b9f34 1405 case ALLOW_MEDIUM_REMOVAL:
81b1008d 1406 s->tray_locked = req->cmd.buf[4] & 1;
44740c38 1407 bdrv_lock_medium(s->qdev.conf.bs, req->cmd.buf[4] & 1);
5f71d32f 1408 break;
5e30a07d 1409 case READ_CAPACITY_10:
e7e25e32 1410 /* The normal LEN field for this command is zero. */
5f71d32f 1411 memset(outbuf, 0, 8);
44740c38 1412 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
f01b5931 1413 if (!nb_sectors) {
9bcaf4fe
PB
1414 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
1415 return -1;
f01b5931 1416 }
7cec78b6
PB
1417 if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) {
1418 goto illegal_request;
1419 }
69377307 1420 nb_sectors /= s->qdev.blocksize / 512;
e7e25e32
GH
1421 /* Returned value is the address of the last sector. */
1422 nb_sectors--;
1423 /* Remember the new size for read/write sanity checking. */
7877903a 1424 s->qdev.max_lba = nb_sectors;
e7e25e32 1425 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
f01b5931 1426 if (nb_sectors > UINT32_MAX) {
e7e25e32 1427 nb_sectors = UINT32_MAX;
f01b5931 1428 }
e7e25e32
GH
1429 outbuf[0] = (nb_sectors >> 24) & 0xff;
1430 outbuf[1] = (nb_sectors >> 16) & 0xff;
1431 outbuf[2] = (nb_sectors >> 8) & 0xff;
1432 outbuf[3] = nb_sectors & 0xff;
1433 outbuf[4] = 0;
1434 outbuf[5] = 0;
69377307 1435 outbuf[6] = s->qdev.blocksize >> 8;
e7e25e32
GH
1436 outbuf[7] = 0;
1437 buflen = 8;
5f71d32f 1438 break;
f3b338ef
PB
1439 case REQUEST_SENSE:
1440 /* Just return "NO SENSE". */
1441 buflen = scsi_build_sense(NULL, 0, outbuf, r->buflen,
1442 (req->cmd.buf[1] & 1) == 0);
1443 break;
b6c251ab
PB
1444 case MECHANISM_STATUS:
1445 buflen = scsi_emulate_mechanism_status(s, outbuf);
1446 if (buflen < 0) {
1447 goto illegal_request;
1448 }
1449 break;
38215553 1450 case GET_CONFIGURATION:
430ee2f2 1451 buflen = scsi_get_configuration(s, outbuf);
b6c251ab
PB
1452 if (buflen < 0) {
1453 goto illegal_request;
1454 }
1455 break;
1456 case GET_EVENT_STATUS_NOTIFICATION:
1457 buflen = scsi_get_event_status_notification(s, r, outbuf);
1458 if (buflen < 0) {
1459 goto illegal_request;
1460 }
1461 break;
1a4f0c3a
PB
1462 case READ_DISC_INFORMATION:
1463 buflen = scsi_read_disc_information(s, r, outbuf);
1464 if (buflen < 0) {
1465 goto illegal_request;
1466 }
1467 break;
b6c251ab
PB
1468 case READ_DVD_STRUCTURE:
1469 buflen = scsi_read_dvd_structure(s, r, outbuf);
1470 if (buflen < 0) {
1471 goto illegal_request;
1472 }
38215553 1473 break;
f6515262 1474 case SERVICE_ACTION_IN_16:
5dd90e2a 1475 /* Service Action In subcommands. */
f6515262 1476 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
5dd90e2a
GH
1477 DPRINTF("SAI READ CAPACITY(16)\n");
1478 memset(outbuf, 0, req->cmd.xfer);
44740c38 1479 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
f01b5931 1480 if (!nb_sectors) {
9bcaf4fe
PB
1481 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
1482 return -1;
f01b5931 1483 }
7cec78b6
PB
1484 if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) {
1485 goto illegal_request;
1486 }
69377307 1487 nb_sectors /= s->qdev.blocksize / 512;
5dd90e2a
GH
1488 /* Returned value is the address of the last sector. */
1489 nb_sectors--;
1490 /* Remember the new size for read/write sanity checking. */
7877903a 1491 s->qdev.max_lba = nb_sectors;
5dd90e2a
GH
1492 outbuf[0] = (nb_sectors >> 56) & 0xff;
1493 outbuf[1] = (nb_sectors >> 48) & 0xff;
1494 outbuf[2] = (nb_sectors >> 40) & 0xff;
1495 outbuf[3] = (nb_sectors >> 32) & 0xff;
1496 outbuf[4] = (nb_sectors >> 24) & 0xff;
1497 outbuf[5] = (nb_sectors >> 16) & 0xff;
1498 outbuf[6] = (nb_sectors >> 8) & 0xff;
1499 outbuf[7] = nb_sectors & 0xff;
1500 outbuf[8] = 0;
1501 outbuf[9] = 0;
69377307 1502 outbuf[10] = s->qdev.blocksize >> 8;
5dd90e2a 1503 outbuf[11] = 0;
ee3659e3
CH
1504 outbuf[12] = 0;
1505 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
ea3bd56f
CH
1506
1507 /* set TPE bit if the format supports discard */
1508 if (s->qdev.conf.discard_granularity) {
1509 outbuf[14] = 0x80;
1510 }
1511
5dd90e2a
GH
1512 /* Protection, exponent and lowest lba field left blank. */
1513 buflen = req->cmd.xfer;
1514 break;
1515 }
1516 DPRINTF("Unsupported Service Action In\n");
1517 goto illegal_request;
101aa85f
PB
1518 case SYNCHRONIZE_CACHE:
1519 /* The request is used as the AIO opaque value, so add a ref. */
1520 scsi_req_ref(&r->req);
1521 bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH);
1522 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r);
1523 return 0;
1524 case SEEK_10:
1525 DPRINTF("Seek(10) (sector %" PRId64 ")\n", r->req.cmd.lba);
1526 if (r->req.cmd.lba > s->qdev.max_lba) {
1527 goto illegal_lba;
1528 }
1529 break;
101aa85f
PB
1530 case MODE_SELECT:
1531 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1532 /* We don't support mode parameter changes.
1533 Allow the mode parameter header + block descriptors only. */
1534 if (r->req.cmd.xfer > 12) {
1535 goto illegal_request;
1536 }
1537 break;
1538 case MODE_SELECT_10:
1539 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1540 /* We don't support mode parameter changes.
1541 Allow the mode parameter header + block descriptors only. */
1542 if (r->req.cmd.xfer > 16) {
1543 goto illegal_request;
1544 }
1545 break;
101aa85f
PB
1546 case WRITE_SAME_10:
1547 nb_sectors = lduw_be_p(&req->cmd.buf[7]);
1548 goto write_same;
1549 case WRITE_SAME_16:
1550 nb_sectors = ldl_be_p(&req->cmd.buf[10]) & 0xffffffffULL;
1551 write_same:
1552 if (r->req.cmd.lba > s->qdev.max_lba) {
1553 goto illegal_lba;
1554 }
1555
1556 /*
1557 * We only support WRITE SAME with the unmap bit set for now.
1558 */
1559 if (!(req->cmd.buf[1] & 0x8)) {
1560 goto illegal_request;
1561 }
1562
1563 /* The request is used as the AIO opaque value, so add a ref. */
1564 scsi_req_ref(&r->req);
1565 r->req.aiocb = bdrv_aio_discard(s->qdev.conf.bs,
1566 r->req.cmd.lba * (s->qdev.blocksize / 512),
1567 nb_sectors * (s->qdev.blocksize / 512),
1568 scsi_aio_complete, r);
1569 return 0;
aa5dbdc1 1570 default:
b08d0ea0 1571 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
b45ef674 1572 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
b08d0ea0 1573 return 0;
aa5dbdc1 1574 }
314a3299 1575 assert(!r->req.aiocb);
b08d0ea0 1576 r->iov.iov_len = MIN(buflen, req->cmd.xfer);
b08d0ea0
PB
1577 if (r->iov.iov_len == 0) {
1578 scsi_req_complete(&r->req, GOOD);
1579 }
af6d510d
PB
1580 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1581 assert(r->iov.iov_len == req->cmd.xfer);
1582 return -r->iov.iov_len;
1583 } else {
1584 return r->iov.iov_len;
1585 }
aa5dbdc1 1586
aa5dbdc1 1587illegal_request:
cfc606da
PB
1588 if (r->req.status == -1) {
1589 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1590 }
b08d0ea0 1591 return 0;
101aa85f
PB
1592
1593illegal_lba:
1594 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1595 return 0;
aa5dbdc1
GH
1596}
1597
2e5d83bb
PB
1598/* Execute a scsi command. Returns the length of the data expected by the
1599 command. This will be Positive for data transfers from the device
1600 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1601 and zero if the command does not transfer any data. */
1602
b08d0ea0 1603static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf)
2e5d83bb 1604{
5c6c0e51
HR
1605 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1606 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
ad2d30f7 1607 int32_t len;
a917d384 1608 uint8_t command;
a917d384
PB
1609
1610 command = buf[0];
aa5dbdc1 1611
b08d0ea0
PB
1612 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
1613 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1614 return 0;
9bcaf4fe
PB
1615 }
1616
a917d384 1617 switch (command) {
ebf46023
GH
1618 case READ_6:
1619 case READ_10:
bd536cf3
GH
1620 case READ_12:
1621 case READ_16:
5c6c0e51 1622 len = r->req.cmd.xfer / s->qdev.blocksize;
2dd791b6 1623 DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
7877903a 1624 if (r->req.cmd.lba > s->qdev.max_lba) {
274fb0e1 1625 goto illegal_lba;
f01b5931 1626 }
69377307
PB
1627 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1628 r->sector_count = len * (s->qdev.blocksize / 512);
2e5d83bb 1629 break;
7f64f8e2
PB
1630 case VERIFY_10:
1631 case VERIFY_12:
1632 case VERIFY_16:
ebf46023
GH
1633 case WRITE_6:
1634 case WRITE_10:
bd536cf3
GH
1635 case WRITE_12:
1636 case WRITE_16:
5e30a07d 1637 case WRITE_VERIFY_10:
ebef0bbb
BK
1638 case WRITE_VERIFY_12:
1639 case WRITE_VERIFY_16:
5c6c0e51 1640 len = r->req.cmd.xfer / s->qdev.blocksize;
ebef0bbb 1641 DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
2dd791b6
HR
1642 (command & 0xe) == 0xe ? "And Verify " : "",
1643 r->req.cmd.lba, len);
7877903a 1644 if (r->req.cmd.lba > s->qdev.max_lba) {
274fb0e1 1645 goto illegal_lba;
f01b5931 1646 }
69377307
PB
1647 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1648 r->sector_count = len * (s->qdev.blocksize / 512);
2e5d83bb 1649 break;
101aa85f 1650 default:
b08d0ea0 1651 abort();
274fb0e1 1652 illegal_lba:
b45ef674 1653 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
274fb0e1 1654 return 0;
2e5d83bb 1655 }
b08d0ea0 1656 if (r->sector_count == 0) {
b45ef674 1657 scsi_req_complete(&r->req, GOOD);
a917d384 1658 }
b08d0ea0 1659 assert(r->iov.iov_len == 0);
efb9ee02 1660 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
b08d0ea0 1661 return -r->sector_count * 512;
a917d384 1662 } else {
b08d0ea0 1663 return r->sector_count * 512;
2e5d83bb 1664 }
2e5d83bb
PB
1665}
1666
e9447f35
JK
1667static void scsi_disk_reset(DeviceState *dev)
1668{
1669 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1670 uint64_t nb_sectors;
1671
c7b48872 1672 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
e9447f35 1673
44740c38 1674 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
69377307 1675 nb_sectors /= s->qdev.blocksize / 512;
e9447f35
JK
1676 if (nb_sectors) {
1677 nb_sectors--;
1678 }
7877903a 1679 s->qdev.max_lba = nb_sectors;
e9447f35
JK
1680}
1681
1682static void scsi_destroy(SCSIDevice *dev)
1683{
1684 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1685
c7b48872 1686 scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
f8b6cc00 1687 blockdev_mark_auto_del(s->qdev.conf.bs);
56a14938
GH
1688}
1689
7d4b4ba5 1690static void scsi_cd_change_media_cb(void *opaque, bool load)
2c6942fa 1691{
8a9c16f6
PB
1692 SCSIDiskState *s = opaque;
1693
1694 /*
1695 * When a CD gets changed, we have to report an ejected state and
1696 * then a loaded state to guests so that they detect tray
1697 * open/close and media change events. Guests that do not use
1698 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1699 * states rely on this behavior.
1700 *
1701 * media_changed governs the state machine used for unit attention
1702 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
1703 */
1704 s->media_changed = load;
1705 s->tray_open = !load;
1706 s->qdev.unit_attention = SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM);
3c2f7c12 1707 s->media_event = true;
4480de19
PB
1708 s->eject_request = false;
1709}
1710
1711static void scsi_cd_eject_request_cb(void *opaque, bool force)
1712{
1713 SCSIDiskState *s = opaque;
1714
1715 s->eject_request = true;
1716 if (force) {
1717 s->tray_locked = false;
1718 }
2c6942fa
MA
1719}
1720
e4def80b
MA
1721static bool scsi_cd_is_tray_open(void *opaque)
1722{
1723 return ((SCSIDiskState *)opaque)->tray_open;
1724}
1725
f107639a
MA
1726static bool scsi_cd_is_medium_locked(void *opaque)
1727{
1728 return ((SCSIDiskState *)opaque)->tray_locked;
1729}
1730
1731static const BlockDevOps scsi_cd_block_ops = {
2c6942fa 1732 .change_media_cb = scsi_cd_change_media_cb,
4480de19 1733 .eject_request_cb = scsi_cd_eject_request_cb,
e4def80b 1734 .is_tray_open = scsi_cd_is_tray_open,
f107639a
MA
1735 .is_medium_locked = scsi_cd_is_medium_locked,
1736};
1737
8a9c16f6
PB
1738static void scsi_disk_unit_attention_reported(SCSIDevice *dev)
1739{
1740 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1741 if (s->media_changed) {
1742 s->media_changed = false;
1743 s->qdev.unit_attention = SENSE_CODE(MEDIUM_CHANGED);
1744 }
1745}
1746
e39be482 1747static int scsi_initfn(SCSIDevice *dev)
2e5d83bb 1748{
d52affa7 1749 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2e5d83bb 1750
f8b6cc00 1751 if (!s->qdev.conf.bs) {
6a84cb1f 1752 error_report("drive property not set");
d52affa7
GH
1753 return -1;
1754 }
1755
bfe3d7ac
PB
1756 if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
1757 !bdrv_is_inserted(s->qdev.conf.bs)) {
98f28ad7
MA
1758 error_report("Device needs media, but drive is empty");
1759 return -1;
1760 }
1761
911525db 1762 blkconf_serial(&s->qdev.conf, &s->serial);
b7eb0c9f
MA
1763 if (blkconf_geometry(&dev->conf, NULL, 65535, 255, 255) < 0) {
1764 return -1;
1765 }
a0fef654 1766
552fee93 1767 if (!s->version) {
93bfef4c 1768 s->version = g_strdup(qemu_get_version());
552fee93 1769 }
353815aa
DF
1770 if (!s->vendor) {
1771 s->vendor = g_strdup("QEMU");
1772 }
552fee93 1773
44740c38 1774 if (bdrv_is_sg(s->qdev.conf.bs)) {
6a84cb1f 1775 error_report("unwanted /dev/sg*");
32bb404a
MA
1776 return -1;
1777 }
1778
bfe3d7ac 1779 if (s->features & (1 << SCSI_DISK_F_REMOVABLE)) {
44740c38 1780 bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_cd_block_ops, s);
2e5d83bb 1781 }
44740c38 1782 bdrv_set_buffer_alignment(s->qdev.conf.bs, s->qdev.blocksize);
8cfacf07 1783
44740c38 1784 bdrv_iostatus_enable(s->qdev.conf.bs);
7082826e 1785 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, NULL);
d52affa7
GH
1786 return 0;
1787}
1788
b443ae67
MA
1789static int scsi_hd_initfn(SCSIDevice *dev)
1790{
e39be482
PB
1791 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1792 s->qdev.blocksize = s->qdev.conf.logical_block_size;
1793 s->qdev.type = TYPE_DISK;
353815aa
DF
1794 if (!s->product) {
1795 s->product = g_strdup("QEMU HARDDISK");
1796 }
e39be482 1797 return scsi_initfn(&s->qdev);
b443ae67
MA
1798}
1799
1800static int scsi_cd_initfn(SCSIDevice *dev)
1801{
e39be482
PB
1802 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1803 s->qdev.blocksize = 2048;
1804 s->qdev.type = TYPE_ROM;
bfe3d7ac 1805 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
353815aa
DF
1806 if (!s->product) {
1807 s->product = g_strdup("QEMU CD-ROM");
1808 }
e39be482 1809 return scsi_initfn(&s->qdev);
b443ae67
MA
1810}
1811
1812static int scsi_disk_initfn(SCSIDevice *dev)
1813{
95b5edcd 1814 DriveInfo *dinfo;
b443ae67
MA
1815
1816 if (!dev->conf.bs) {
e39be482 1817 return scsi_initfn(dev); /* ... and die there */
b443ae67
MA
1818 }
1819
e39be482
PB
1820 dinfo = drive_get_by_blockdev(dev->conf.bs);
1821 if (dinfo->media_cd) {
1822 return scsi_cd_initfn(dev);
1823 } else {
1824 return scsi_hd_initfn(dev);
1825 }
b443ae67
MA
1826}
1827
b08d0ea0 1828static const SCSIReqOps scsi_disk_emulate_reqops = {
8dbd4574 1829 .size = sizeof(SCSIDiskReq),
12010e7b 1830 .free_req = scsi_free_request,
b08d0ea0 1831 .send_command = scsi_disk_emulate_command,
314a3299
PB
1832 .read_data = scsi_disk_emulate_read_data,
1833 .write_data = scsi_disk_emulate_write_data,
b08d0ea0
PB
1834 .get_buf = scsi_get_buf,
1835};
1836
1837static const SCSIReqOps scsi_disk_dma_reqops = {
1838 .size = sizeof(SCSIDiskReq),
1839 .free_req = scsi_free_request,
1840 .send_command = scsi_disk_dma_command,
12010e7b
PB
1841 .read_data = scsi_read_data,
1842 .write_data = scsi_write_data,
1843 .cancel_io = scsi_cancel_io,
1844 .get_buf = scsi_get_buf,
43b978b9
PB
1845 .load_request = scsi_disk_load_request,
1846 .save_request = scsi_disk_save_request,
8dbd4574
PB
1847};
1848
b08d0ea0
PB
1849static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = {
1850 [TEST_UNIT_READY] = &scsi_disk_emulate_reqops,
1851 [INQUIRY] = &scsi_disk_emulate_reqops,
1852 [MODE_SENSE] = &scsi_disk_emulate_reqops,
1853 [MODE_SENSE_10] = &scsi_disk_emulate_reqops,
1854 [START_STOP] = &scsi_disk_emulate_reqops,
1855 [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops,
1856 [READ_CAPACITY_10] = &scsi_disk_emulate_reqops,
1857 [READ_TOC] = &scsi_disk_emulate_reqops,
1858 [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops,
1859 [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops,
1860 [GET_CONFIGURATION] = &scsi_disk_emulate_reqops,
1861 [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops,
1862 [MECHANISM_STATUS] = &scsi_disk_emulate_reqops,
1863 [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops,
1864 [REQUEST_SENSE] = &scsi_disk_emulate_reqops,
1865 [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops,
1866 [SEEK_10] = &scsi_disk_emulate_reqops,
b08d0ea0
PB
1867 [MODE_SELECT] = &scsi_disk_emulate_reqops,
1868 [MODE_SELECT_10] = &scsi_disk_emulate_reqops,
b08d0ea0
PB
1869 [WRITE_SAME_10] = &scsi_disk_emulate_reqops,
1870 [WRITE_SAME_16] = &scsi_disk_emulate_reqops,
1871
1872 [READ_6] = &scsi_disk_dma_reqops,
1873 [READ_10] = &scsi_disk_dma_reqops,
1874 [READ_12] = &scsi_disk_dma_reqops,
1875 [READ_16] = &scsi_disk_dma_reqops,
1876 [VERIFY_10] = &scsi_disk_dma_reqops,
1877 [VERIFY_12] = &scsi_disk_dma_reqops,
1878 [VERIFY_16] = &scsi_disk_dma_reqops,
1879 [WRITE_6] = &scsi_disk_dma_reqops,
1880 [WRITE_10] = &scsi_disk_dma_reqops,
1881 [WRITE_12] = &scsi_disk_dma_reqops,
1882 [WRITE_16] = &scsi_disk_dma_reqops,
1883 [WRITE_VERIFY_10] = &scsi_disk_dma_reqops,
1884 [WRITE_VERIFY_12] = &scsi_disk_dma_reqops,
1885 [WRITE_VERIFY_16] = &scsi_disk_dma_reqops,
1886};
1887
63db0f0e
PB
1888static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
1889 uint8_t *buf, void *hba_private)
8dbd4574
PB
1890{
1891 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1892 SCSIRequest *req;
b08d0ea0
PB
1893 const SCSIReqOps *ops;
1894 uint8_t command;
8dbd4574 1895
b08d0ea0
PB
1896#ifdef DEBUG_SCSI
1897 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, buf[0]);
1898 {
1899 int i;
1900 for (i = 1; i < r->req.cmd.len; i++) {
1901 printf(" 0x%02x", buf[i]);
1902 }
1903 printf("\n");
1904 }
1905#endif
1906
1907 command = buf[0];
1908 ops = scsi_disk_reqops_dispatch[command];
1909 if (!ops) {
1910 ops = &scsi_disk_emulate_reqops;
1911 }
1912 req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private);
8dbd4574
PB
1913 return req;
1914}
1915
336a6915
PB
1916#ifdef __linux__
1917static int get_device_type(SCSIDiskState *s)
1918{
1919 BlockDriverState *bdrv = s->qdev.conf.bs;
1920 uint8_t cmd[16];
1921 uint8_t buf[36];
1922 uint8_t sensebuf[8];
1923 sg_io_hdr_t io_header;
1924 int ret;
1925
1926 memset(cmd, 0, sizeof(cmd));
1927 memset(buf, 0, sizeof(buf));
1928 cmd[0] = INQUIRY;
1929 cmd[4] = sizeof(buf);
1930
1931 memset(&io_header, 0, sizeof(io_header));
1932 io_header.interface_id = 'S';
1933 io_header.dxfer_direction = SG_DXFER_FROM_DEV;
1934 io_header.dxfer_len = sizeof(buf);
1935 io_header.dxferp = buf;
1936 io_header.cmdp = cmd;
1937 io_header.cmd_len = sizeof(cmd);
1938 io_header.mx_sb_len = sizeof(sensebuf);
1939 io_header.sbp = sensebuf;
1940 io_header.timeout = 6000; /* XXX */
1941
1942 ret = bdrv_ioctl(bdrv, SG_IO, &io_header);
1943 if (ret < 0 || io_header.driver_status || io_header.host_status) {
1944 return -1;
1945 }
1946 s->qdev.type = buf[0];
bfe3d7ac
PB
1947 if (buf[1] & 0x80) {
1948 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
1949 }
336a6915
PB
1950 return 0;
1951}
1952
1953static int scsi_block_initfn(SCSIDevice *dev)
1954{
1955 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1956 int sg_version;
1957 int rc;
1958
1959 if (!s->qdev.conf.bs) {
1960 error_report("scsi-block: drive property not set");
1961 return -1;
1962 }
1963
1964 /* check we are using a driver managing SG_IO (version 3 and after) */
1965 if (bdrv_ioctl(s->qdev.conf.bs, SG_GET_VERSION_NUM, &sg_version) < 0 ||
1966 sg_version < 30000) {
1967 error_report("scsi-block: scsi generic interface too old");
1968 return -1;
1969 }
1970
1971 /* get device type from INQUIRY data */
1972 rc = get_device_type(s);
1973 if (rc < 0) {
1974 error_report("scsi-block: INQUIRY failed");
1975 return -1;
1976 }
1977
1978 /* Make a guess for the block size, we'll fix it when the guest sends.
1979 * READ CAPACITY. If they don't, they likely would assume these sizes
1980 * anyway. (TODO: check in /sys).
1981 */
1982 if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) {
1983 s->qdev.blocksize = 2048;
1984 } else {
1985 s->qdev.blocksize = 512;
1986 }
1987 return scsi_initfn(&s->qdev);
1988}
1989
1990static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag,
1991 uint32_t lun, uint8_t *buf,
1992 void *hba_private)
1993{
1994 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1995
1996 switch (buf[0]) {
1997 case READ_6:
1998 case READ_10:
1999 case READ_12:
2000 case READ_16:
7f64f8e2
PB
2001 case VERIFY_10:
2002 case VERIFY_12:
2003 case VERIFY_16:
336a6915
PB
2004 case WRITE_6:
2005 case WRITE_10:
2006 case WRITE_12:
2007 case WRITE_16:
2008 case WRITE_VERIFY_10:
2009 case WRITE_VERIFY_12:
2010 case WRITE_VERIFY_16:
eaccf49e
PB
2011 /* If we are not using O_DIRECT, we might read stale data from the
2012 * host cache if writes were made using other commands than these
2013 * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without
2014 * O_DIRECT everything must go through SG_IO.
2015 */
137745c5 2016 if (bdrv_get_flags(s->qdev.conf.bs) & BDRV_O_NOCACHE) {
eaccf49e
PB
2017 break;
2018 }
2019
33ebad12
PB
2020 /* MMC writing cannot be done via pread/pwrite, because it sometimes
2021 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2022 * And once you do these writes, reading from the block device is
2023 * unreliable, too. It is even possible that reads deliver random data
2024 * from the host page cache (this is probably a Linux bug).
2025 *
b08d0ea0 2026 * We might use scsi_disk_dma_reqops as long as no writing commands are
33ebad12
PB
2027 * seen, but performance usually isn't paramount on optical media. So,
2028 * just make scsi-block operate the same as scsi-generic for them.
2029 */
b08d0ea0
PB
2030 if (s->qdev.type != TYPE_ROM) {
2031 return scsi_req_alloc(&scsi_disk_dma_reqops, &s->qdev, tag, lun,
2032 hba_private);
2033 }
336a6915
PB
2034 }
2035
2036 return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun,
2037 hba_private);
2038}
2039#endif
2040
353815aa
DF
2041#define DEFINE_SCSI_DISK_PROPERTIES() \
2042 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2043 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2044 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2045 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2046 DEFINE_PROP_STRING("product", SCSIDiskState, product)
b443ae67 2047
39bffca2
AL
2048static Property scsi_hd_properties[] = {
2049 DEFINE_SCSI_DISK_PROPERTIES(),
bfe3d7ac
PB
2050 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2051 SCSI_DISK_F_REMOVABLE, false),
da8365db
PB
2052 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2053 SCSI_DISK_F_DPOFUA, false),
27395add 2054 DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0),
d252df48 2055 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
39bffca2
AL
2056 DEFINE_PROP_END_OF_LIST(),
2057};
2058
43b978b9
PB
2059static const VMStateDescription vmstate_scsi_disk_state = {
2060 .name = "scsi-disk",
2061 .version_id = 1,
2062 .minimum_version_id = 1,
2063 .minimum_version_id_old = 1,
2064 .fields = (VMStateField[]) {
2065 VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState),
2066 VMSTATE_BOOL(media_changed, SCSIDiskState),
2067 VMSTATE_BOOL(media_event, SCSIDiskState),
2068 VMSTATE_BOOL(eject_request, SCSIDiskState),
2069 VMSTATE_BOOL(tray_open, SCSIDiskState),
2070 VMSTATE_BOOL(tray_locked, SCSIDiskState),
2071 VMSTATE_END_OF_LIST()
2072 }
2073};
2074
b9eea3e6
AL
2075static void scsi_hd_class_initfn(ObjectClass *klass, void *data)
2076{
39bffca2 2077 DeviceClass *dc = DEVICE_CLASS(klass);
b9eea3e6
AL
2078 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2079
2080 sc->init = scsi_hd_initfn;
2081 sc->destroy = scsi_destroy;
2082 sc->alloc_req = scsi_new_request;
2083 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
39bffca2
AL
2084 dc->fw_name = "disk";
2085 dc->desc = "virtual SCSI disk";
2086 dc->reset = scsi_disk_reset;
2087 dc->props = scsi_hd_properties;
43b978b9 2088 dc->vmsd = &vmstate_scsi_disk_state;
b9eea3e6
AL
2089}
2090
39bffca2
AL
2091static TypeInfo scsi_hd_info = {
2092 .name = "scsi-hd",
2093 .parent = TYPE_SCSI_DEVICE,
2094 .instance_size = sizeof(SCSIDiskState),
2095 .class_init = scsi_hd_class_initfn,
2096};
2097
2098static Property scsi_cd_properties[] = {
2099 DEFINE_SCSI_DISK_PROPERTIES(),
27395add 2100 DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0),
39bffca2 2101 DEFINE_PROP_END_OF_LIST(),
b9eea3e6
AL
2102};
2103
2104static void scsi_cd_class_initfn(ObjectClass *klass, void *data)
2105{
39bffca2 2106 DeviceClass *dc = DEVICE_CLASS(klass);
b9eea3e6
AL
2107 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2108
2109 sc->init = scsi_cd_initfn;
2110 sc->destroy = scsi_destroy;
2111 sc->alloc_req = scsi_new_request;
2112 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
39bffca2
AL
2113 dc->fw_name = "disk";
2114 dc->desc = "virtual SCSI CD-ROM";
2115 dc->reset = scsi_disk_reset;
2116 dc->props = scsi_cd_properties;
43b978b9 2117 dc->vmsd = &vmstate_scsi_disk_state;
b9eea3e6
AL
2118}
2119
39bffca2
AL
2120static TypeInfo scsi_cd_info = {
2121 .name = "scsi-cd",
2122 .parent = TYPE_SCSI_DEVICE,
2123 .instance_size = sizeof(SCSIDiskState),
2124 .class_init = scsi_cd_class_initfn,
b9eea3e6
AL
2125};
2126
336a6915 2127#ifdef __linux__
39bffca2 2128static Property scsi_block_properties[] = {
03847837 2129 DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.bs),
39bffca2
AL
2130 DEFINE_PROP_END_OF_LIST(),
2131};
2132
b9eea3e6
AL
2133static void scsi_block_class_initfn(ObjectClass *klass, void *data)
2134{
39bffca2 2135 DeviceClass *dc = DEVICE_CLASS(klass);
b9eea3e6
AL
2136 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2137
2138 sc->init = scsi_block_initfn;
2139 sc->destroy = scsi_destroy;
2140 sc->alloc_req = scsi_block_new_request;
39bffca2
AL
2141 dc->fw_name = "disk";
2142 dc->desc = "SCSI block device passthrough";
2143 dc->reset = scsi_disk_reset;
2144 dc->props = scsi_block_properties;
43b978b9 2145 dc->vmsd = &vmstate_scsi_disk_state;
b9eea3e6
AL
2146}
2147
39bffca2
AL
2148static TypeInfo scsi_block_info = {
2149 .name = "scsi-block",
2150 .parent = TYPE_SCSI_DEVICE,
2151 .instance_size = sizeof(SCSIDiskState),
2152 .class_init = scsi_block_class_initfn,
b9eea3e6 2153};
336a6915 2154#endif
b9eea3e6 2155
39bffca2
AL
2156static Property scsi_disk_properties[] = {
2157 DEFINE_SCSI_DISK_PROPERTIES(),
bfe3d7ac
PB
2158 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2159 SCSI_DISK_F_REMOVABLE, false),
da8365db
PB
2160 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2161 SCSI_DISK_F_DPOFUA, false),
27395add 2162 DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0),
39bffca2
AL
2163 DEFINE_PROP_END_OF_LIST(),
2164};
2165
b9eea3e6
AL
2166static void scsi_disk_class_initfn(ObjectClass *klass, void *data)
2167{
39bffca2 2168 DeviceClass *dc = DEVICE_CLASS(klass);
b9eea3e6
AL
2169 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2170
2171 sc->init = scsi_disk_initfn;
2172 sc->destroy = scsi_destroy;
2173 sc->alloc_req = scsi_new_request;
2174 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
39bffca2
AL
2175 dc->fw_name = "disk";
2176 dc->desc = "virtual SCSI disk or CD-ROM (legacy)";
2177 dc->reset = scsi_disk_reset;
2178 dc->props = scsi_disk_properties;
43b978b9 2179 dc->vmsd = &vmstate_scsi_disk_state;
b9eea3e6
AL
2180}
2181
39bffca2
AL
2182static TypeInfo scsi_disk_info = {
2183 .name = "scsi-disk",
2184 .parent = TYPE_SCSI_DEVICE,
2185 .instance_size = sizeof(SCSIDiskState),
2186 .class_init = scsi_disk_class_initfn,
d52affa7
GH
2187};
2188
83f7d43a 2189static void scsi_disk_register_types(void)
d52affa7 2190{
39bffca2
AL
2191 type_register_static(&scsi_hd_info);
2192 type_register_static(&scsi_cd_info);
b9eea3e6 2193#ifdef __linux__
39bffca2 2194 type_register_static(&scsi_block_info);
b9eea3e6 2195#endif
39bffca2 2196 type_register_static(&scsi_disk_info);
8ccc2ace 2197}
83f7d43a
AF
2198
2199type_init(scsi_disk_register_types)
This page took 1.079741 seconds and 4 git commands to generate.