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