]> git.proxmox.com Git - qemu.git/blob - hw/scsi-disk.c
c295326e991c2e91997f226c30e9f81d9e0ccd25
[qemu.git] / hw / scsi-disk.c
1 /*
2 * SCSI Device emulation
3 *
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
6 *
7 * Written by Paul Brook
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.
14 *
15 * This code is licensed under the LGPL.
16 *
17 * Note that this file only handles the SCSI architecture model and device
18 * commands. Emulation of interface/link layer protocols is handled by
19 * the host adapter emulator.
20 */
21
22 //#define DEBUG_SCSI
23
24 #ifdef DEBUG_SCSI
25 #define DPRINTF(fmt, ...) \
26 do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
27 #else
28 #define DPRINTF(fmt, ...) do {} while(0)
29 #endif
30
31 #include "qemu-common.h"
32 #include "qemu-error.h"
33 #include "scsi.h"
34 #include "scsi-defs.h"
35 #include "sysemu.h"
36 #include "blockdev.h"
37 #include "hw/block-common.h"
38 #include "dma.h"
39
40 #ifdef __linux
41 #include <scsi/sg.h>
42 #endif
43
44 #define SCSI_DMA_BUF_SIZE 131072
45 #define SCSI_MAX_INQUIRY_LEN 256
46 #define SCSI_MAX_MODE_LEN 256
47
48 typedef struct SCSIDiskState SCSIDiskState;
49
50 typedef struct SCSIDiskReq {
51 SCSIRequest req;
52 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
53 uint64_t sector;
54 uint32_t sector_count;
55 uint32_t buflen;
56 bool started;
57 struct iovec iov;
58 QEMUIOVector qiov;
59 BlockAcctCookie acct;
60 } SCSIDiskReq;
61
62 #define SCSI_DISK_F_REMOVABLE 0
63 #define SCSI_DISK_F_DPOFUA 1
64
65 struct SCSIDiskState
66 {
67 SCSIDevice qdev;
68 uint32_t features;
69 bool media_changed;
70 bool media_event;
71 bool eject_request;
72 uint64_t wwn;
73 QEMUBH *bh;
74 char *version;
75 char *serial;
76 char *vendor;
77 char *product;
78 bool tray_open;
79 bool tray_locked;
80 };
81
82 static int scsi_handle_rw_error(SCSIDiskReq *r, int error);
83
84 static void scsi_free_request(SCSIRequest *req)
85 {
86 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
87
88 if (r->iov.iov_base) {
89 qemu_vfree(r->iov.iov_base);
90 }
91 }
92
93 /* Helper function for command completion with sense. */
94 static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
95 {
96 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
97 r->req.tag, sense.key, sense.asc, sense.ascq);
98 scsi_req_build_sense(&r->req, sense);
99 scsi_req_complete(&r->req, CHECK_CONDITION);
100 }
101
102 /* Cancel a pending data transfer. */
103 static void scsi_cancel_io(SCSIRequest *req)
104 {
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);
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);
115 }
116 r->req.aiocb = NULL;
117 }
118
119 static uint32_t scsi_init_iovec(SCSIDiskReq *r, size_t size)
120 {
121 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
122
123 if (!r->iov.iov_base) {
124 r->buflen = size;
125 r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
126 }
127 r->iov.iov_len = MIN(r->sector_count * 512, r->buflen);
128 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
129 return r->qiov.size / 512;
130 }
131
132 static 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);
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 }
147 }
148 }
149
150 static 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);
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);
167 }
168 }
169
170 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
171 }
172
173 static void scsi_aio_complete(void *opaque, int ret)
174 {
175 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
176 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
177
178 assert(r->req.aiocb != NULL);
179 r->req.aiocb = NULL;
180 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
181
182 if (ret < 0) {
183 if (scsi_handle_rw_error(r, -ret)) {
184 goto done;
185 }
186 }
187
188 scsi_req_complete(&r->req, GOOD);
189
190 done:
191 if (!r->req.io_canceled) {
192 scsi_req_unref(&r->req);
193 }
194 }
195
196 static bool scsi_is_cmd_fua(SCSICommand *cmd)
197 {
198 switch (cmd->buf[0]) {
199 case READ_10:
200 case READ_12:
201 case READ_16:
202 case WRITE_10:
203 case WRITE_12:
204 case WRITE_16:
205 return (cmd->buf[1] & 8) != 0;
206
207 case VERIFY_10:
208 case VERIFY_12:
209 case VERIFY_16:
210 case WRITE_VERIFY_10:
211 case WRITE_VERIFY_12:
212 case WRITE_VERIFY_16:
213 return true;
214
215 case READ_6:
216 case WRITE_6:
217 default:
218 return false;
219 }
220 }
221
222 static void scsi_write_do_fua(SCSIDiskReq *r)
223 {
224 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
225
226 if (scsi_is_cmd_fua(&r->req.cmd)) {
227 bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH);
228 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r);
229 return;
230 }
231
232 scsi_req_complete(&r->req, GOOD);
233 if (!r->req.io_canceled) {
234 scsi_req_unref(&r->req);
235 }
236 }
237
238 static void scsi_dma_complete(void *opaque, int ret)
239 {
240 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
241 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
242
243 assert(r->req.aiocb != NULL);
244 r->req.aiocb = NULL;
245 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
246
247 if (ret < 0) {
248 if (scsi_handle_rw_error(r, -ret)) {
249 goto done;
250 }
251 }
252
253 r->sector += r->sector_count;
254 r->sector_count = 0;
255 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
256 scsi_write_do_fua(r);
257 return;
258 } else {
259 scsi_req_complete(&r->req, GOOD);
260 }
261
262 done:
263 if (!r->req.io_canceled) {
264 scsi_req_unref(&r->req);
265 }
266 }
267
268 static void scsi_read_complete(void * opaque, int ret)
269 {
270 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
271 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
272 int n;
273
274 assert(r->req.aiocb != NULL);
275 r->req.aiocb = NULL;
276 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
277
278 if (ret < 0) {
279 if (scsi_handle_rw_error(r, -ret)) {
280 goto done;
281 }
282 }
283
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);
290
291 done:
292 if (!r->req.io_canceled) {
293 scsi_req_unref(&r->req);
294 }
295 }
296
297 /* Actually issue a read to the block device. */
298 static 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
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
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
334 done:
335 if (!r->req.io_canceled) {
336 scsi_req_unref(&r->req);
337 }
338 }
339
340 /* Read more data from scsi device into buffer. */
341 static void scsi_read_data(SCSIRequest *req)
342 {
343 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
344 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
345 bool first;
346
347 DPRINTF("Read sector_count=%d\n", r->sector_count);
348 if (r->sector_count == 0) {
349 /* This also clears the sense buffer for REQUEST SENSE. */
350 scsi_req_complete(&r->req, GOOD);
351 return;
352 }
353
354 /* No data transfer may already be in progress */
355 assert(r->req.aiocb == NULL);
356
357 /* The request is used as the AIO opaque value, so add a ref. */
358 scsi_req_ref(&r->req);
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
365 if (s->tray_open) {
366 scsi_read_complete(r, -ENOMEDIUM);
367 return;
368 }
369
370 first = !r->started;
371 r->started = true;
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);
375 } else {
376 scsi_do_read(r, 0);
377 }
378 }
379
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 */
387 static int scsi_handle_rw_error(SCSIDiskReq *r, int error)
388 {
389 int is_read = (r->req.cmd.xfer == SCSI_XFER_FROM_DEV);
390 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
391 BlockdevOnError action = bdrv_get_on_error(s->qdev.conf.bs, is_read);
392
393 if (action == BLOCKDEV_ON_ERROR_IGNORE) {
394 bdrv_emit_qmp_error_event(s->qdev.conf.bs, BDRV_ACTION_IGNORE, is_read);
395 return 0;
396 }
397
398 if ((error == ENOSPC && action == BLOCKDEV_ON_ERROR_ENOSPC)
399 || action == BLOCKDEV_ON_ERROR_STOP) {
400
401 bdrv_emit_qmp_error_event(s->qdev.conf.bs, BDRV_ACTION_STOP, is_read);
402 vm_stop(RUN_STATE_IO_ERROR);
403 bdrv_iostatus_set_err(s->qdev.conf.bs, error);
404 scsi_req_retry(&r->req);
405 } else {
406 switch (error) {
407 case ENOMEDIUM:
408 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
409 break;
410 case ENOMEM:
411 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
412 break;
413 case EINVAL:
414 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
415 break;
416 default:
417 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
418 break;
419 }
420 bdrv_emit_qmp_error_event(s->qdev.conf.bs, BDRV_ACTION_REPORT, is_read);
421 }
422 return 1;
423 }
424
425 static void scsi_write_complete(void * opaque, int ret)
426 {
427 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
428 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
429 uint32_t n;
430
431 if (r->req.aiocb != NULL) {
432 r->req.aiocb = NULL;
433 bdrv_acct_done(s->qdev.conf.bs, &r->acct);
434 }
435
436 if (ret < 0) {
437 if (scsi_handle_rw_error(r, -ret)) {
438 goto done;
439 }
440 }
441
442 n = r->qiov.size / 512;
443 r->sector += n;
444 r->sector_count -= n;
445 if (r->sector_count == 0) {
446 scsi_write_do_fua(r);
447 return;
448 } else {
449 scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
450 DPRINTF("Write complete tag=0x%x more=%zd\n", r->req.tag, r->qiov.size);
451 scsi_req_data(&r->req, r->qiov.size);
452 }
453
454 done:
455 if (!r->req.io_canceled) {
456 scsi_req_unref(&r->req);
457 }
458 }
459
460 static void scsi_write_data(SCSIRequest *req)
461 {
462 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
463 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
464 uint32_t n;
465
466 /* No data transfer may already be in progress */
467 assert(r->req.aiocb == NULL);
468
469 /* The request is used as the AIO opaque value, so add a ref. */
470 scsi_req_ref(&r->req);
471 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
472 DPRINTF("Data transfer direction invalid\n");
473 scsi_write_complete(r, -EINVAL);
474 return;
475 }
476
477 if (!r->req.sg && !r->qiov.size) {
478 /* Called for the first time. Ask the driver to send us more data. */
479 r->started = true;
480 scsi_write_complete(r, 0);
481 return;
482 }
483 if (s->tray_open) {
484 scsi_write_complete(r, -ENOMEDIUM);
485 return;
486 }
487
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
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;
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,
507 scsi_write_complete, r);
508 }
509 }
510
511 /* Return a pointer to the data buffer. */
512 static uint8_t *scsi_get_buf(SCSIRequest *req)
513 {
514 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
515
516 return (uint8_t *)r->iov.iov_base;
517 }
518
519 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
520 {
521 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
522 int buflen = 0;
523 int start;
524
525 if (req->cmd.buf[1] & 0x1) {
526 /* Vital product data */
527 uint8_t page_code = req->cmd.buf[2];
528
529 outbuf[buflen++] = s->qdev.type & 0x1f;
530 outbuf[buflen++] = page_code ; // this page
531 outbuf[buflen++] = 0x00;
532 outbuf[buflen++] = 0x00;
533 start = buflen;
534
535 switch (page_code) {
536 case 0x00: /* Supported page codes, mandatory */
537 {
538 DPRINTF("Inquiry EVPD[Supported pages] "
539 "buffer size %zd\n", req->cmd.xfer);
540 outbuf[buflen++] = 0x00; // list of supported pages (this page)
541 if (s->serial) {
542 outbuf[buflen++] = 0x80; // unit serial number
543 }
544 outbuf[buflen++] = 0x83; // device identification
545 if (s->qdev.type == TYPE_DISK) {
546 outbuf[buflen++] = 0xb0; // block limits
547 outbuf[buflen++] = 0xb2; // thin provisioning
548 }
549 break;
550 }
551 case 0x80: /* Device serial number, optional */
552 {
553 int l;
554
555 if (!s->serial) {
556 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
557 return -1;
558 }
559
560 l = strlen(s->serial);
561 if (l > 20) {
562 l = 20;
563 }
564
565 DPRINTF("Inquiry EVPD[Serial number] "
566 "buffer size %zd\n", req->cmd.xfer);
567 memcpy(outbuf+buflen, s->serial, l);
568 buflen += l;
569 break;
570 }
571
572 case 0x83: /* Device identification page, mandatory */
573 {
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);
577
578 if (id_len > max_len) {
579 id_len = max_len;
580 }
581 DPRINTF("Inquiry EVPD[Device identification] "
582 "buffer size %zd\n", req->cmd.xfer);
583
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
588 memcpy(outbuf+buflen, str, id_len);
589 buflen += id_len;
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 }
599 break;
600 }
601 case 0xb0: /* block limits */
602 {
603 unsigned int unmap_sectors =
604 s->qdev.conf.discard_granularity / s->qdev.blocksize;
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;
609
610 if (s->qdev.type == TYPE_ROM) {
611 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
612 page_code);
613 return -1;
614 }
615 /* required VPD size with unmap support */
616 buflen = 0x40;
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;
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 {
638 buflen = 8;
639 outbuf[4] = 0;
640 outbuf[5] = 0xe0; /* unmap & write_same 10/16 all supported */
641 outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1;
642 outbuf[7] = 0;
643 break;
644 }
645 default:
646 return -1;
647 }
648 /* done with EVPD */
649 assert(buflen - start <= 255);
650 outbuf[start - 1] = buflen - start;
651 return buflen;
652 }
653
654 /* Standard INQUIRY data */
655 if (req->cmd.buf[2] != 0) {
656 return -1;
657 }
658
659 /* PAGE CODE == 0 */
660 buflen = req->cmd.xfer;
661 if (buflen > SCSI_MAX_INQUIRY_LEN) {
662 buflen = SCSI_MAX_INQUIRY_LEN;
663 }
664 memset(outbuf, 0, buflen);
665
666 outbuf[0] = s->qdev.type & 0x1f;
667 outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0;
668
669 strpadcpy((char *) &outbuf[16], 16, s->product, ' ');
670 strpadcpy((char *) &outbuf[8], 8, s->vendor, ' ');
671
672 memset(&outbuf[32], 0, 4);
673 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
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 */
680 outbuf[2] = 5;
681 outbuf[3] = 2 | 0x10; /* Format 2, HiSup */
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
691 /* Sync data transfer and TCQ. */
692 outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0);
693 return buflen;
694 }
695
696 static inline bool media_is_dvd(SCSIDiskState *s)
697 {
698 uint64_t nb_sectors;
699 if (s->qdev.type != TYPE_ROM) {
700 return false;
701 }
702 if (!bdrv_is_inserted(s->qdev.conf.bs)) {
703 return false;
704 }
705 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
706 return nb_sectors > CD_MAX_SECTORS;
707 }
708
709 static inline bool media_is_cd(SCSIDiskState *s)
710 {
711 uint64_t nb_sectors;
712 if (s->qdev.type != TYPE_ROM) {
713 return false;
714 }
715 if (!bdrv_is_inserted(s->qdev.conf.bs)) {
716 return false;
717 }
718 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
719 return nb_sectors <= CD_MAX_SECTORS;
720 }
721
722 static 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
755 static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r,
756 uint8_t *outbuf)
757 {
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) {
779 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
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 }
801 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
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
845 fail:
846 return -1;
847 }
848
849 static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf)
850 {
851 uint8_t event_code, media_status;
852
853 media_status = 0;
854 if (s->tray_open) {
855 media_status = MS_TRAY_OPEN;
856 } else if (bdrv_is_inserted(s->qdev.conf.bs)) {
857 media_status = MS_MEDIA_PRESENT;
858 }
859
860 /* Event notification descriptor */
861 event_code = MEC_NO_CHANGE;
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 }
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
881 static 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;
906 }
907
908 static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf)
909 {
910 int current;
911
912 if (s->qdev.type != TYPE_ROM) {
913 return -1;
914 }
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;
940 }
941
942 static 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
952 static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
953 int page_control)
954 {
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),
959 [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM),
960 [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM),
961 [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM),
962 };
963
964 uint8_t *p = *p_outbuf + 2;
965 int length;
966
967 if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) {
968 return -1;
969 }
970
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.
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.
982 */
983 switch (page) {
984 case MODE_PAGE_HD_GEOMETRY:
985 length = 0x16;
986 if (page_control == 1) { /* Changeable Values */
987 break;
988 }
989 /* if a geometry hint is available, use it */
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;
994 /* Write precomp start cylinder, disabled */
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;
998 /* Reduced current start cylinder, disabled */
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;
1002 /* Device step rate [ns], 200ns */
1003 p[10] = 0;
1004 p[11] = 200;
1005 /* Landing zone cylinder */
1006 p[12] = 0xff;
1007 p[13] = 0xff;
1008 p[14] = 0xff;
1009 /* Medium rotation rate [rpm], 5400 rpm */
1010 p[18] = (5400 >> 8) & 0xff;
1011 p[19] = 5400 & 0xff;
1012 break;
1013
1014 case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY:
1015 length = 0x1e;
1016 if (page_control == 1) { /* Changeable Values */
1017 break;
1018 }
1019 /* Transfer rate [kbit/s], 5Mbit/s */
1020 p[0] = 5000 >> 8;
1021 p[1] = 5000 & 0xff;
1022 /* if a geometry hint is available, use it */
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 */
1029 p[8] = (s->qdev.conf.cyls >> 8) & 0xff;
1030 p[9] = s->qdev.conf.cyls & 0xff;
1031 /* Reduced current start cylinder, disabled */
1032 p[10] = (s->qdev.conf.cyls >> 8) & 0xff;
1033 p[11] = s->qdev.conf.cyls & 0xff;
1034 /* Device step rate [100us], 100us */
1035 p[12] = 0;
1036 p[13] = 1;
1037 /* Device step pulse width [us], 1us */
1038 p[14] = 1;
1039 /* Device head settle delay [100us], 100us */
1040 p[15] = 0;
1041 p[16] = 1;
1042 /* Motor on delay [0.1s], 0.1s */
1043 p[17] = 1;
1044 /* Motor off delay [0.1s], 0.1s */
1045 p[18] = 1;
1046 /* Medium rotation rate [rpm], 5400 rpm */
1047 p[26] = (5400 >> 8) & 0xff;
1048 p[27] = 5400 & 0xff;
1049 break;
1050
1051 case MODE_PAGE_CACHING:
1052 length = 0x12;
1053 if (page_control == 1 || /* Changeable Values */
1054 bdrv_enable_write_cache(s->qdev.conf.bs)) {
1055 p[0] = 4; /* WCE */
1056 }
1057 break;
1058
1059 case MODE_PAGE_R_W_ERROR:
1060 length = 10;
1061 if (page_control == 1) { /* Changeable Values */
1062 break;
1063 }
1064 p[0] = 0x80; /* Automatic Write Reallocation Enabled */
1065 if (s->qdev.type == TYPE_ROM) {
1066 p[1] = 0x20; /* Read Retry Count */
1067 }
1068 break;
1069
1070 case MODE_PAGE_AUDIO_CTL:
1071 length = 14;
1072 break;
1073
1074 case MODE_PAGE_CAPABILITIES:
1075 length = 0x14;
1076 if (page_control == 1) { /* Changeable Values */
1077 break;
1078 }
1079
1080 p[0] = 0x3b; /* CD-R & CD-RW read */
1081 p[1] = 0; /* Writing not supported */
1082 p[2] = 0x7f; /* Audio, composite, digital out,
1083 mode 2 form 1&2, multi session */
1084 p[3] = 0xff; /* CD DA, DA accurate, RW supported,
1085 RW corrected, C2 errors, ISRC,
1086 UPC, Bar code */
1087 p[4] = 0x2d | (s->tray_locked ? 2 : 0);
1088 /* Locking supported, jumper present, eject, tray */
1089 p[5] = 0; /* no volume & mute control, no
1090 changer */
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 */
1102 p[19] = (16 * 176) & 0xff;
1103 break;
1104
1105 default:
1106 return -1;
1107 }
1108
1109 assert(length < 256);
1110 (*p_outbuf)[0] = page;
1111 (*p_outbuf)[1] = length;
1112 *p_outbuf += length + 2;
1113 return length + 2;
1114 }
1115
1116 static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
1117 {
1118 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1119 uint64_t nb_sectors;
1120 bool dbd;
1121 int page, buflen, ret, page_control;
1122 uint8_t *p;
1123 uint8_t dev_specific_param;
1124
1125 dbd = (r->req.cmd.buf[1] & 0x8) != 0;
1126 page = r->req.cmd.buf[2] & 0x3f;
1127 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
1128 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
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);
1131 p = outbuf;
1132
1133 if (s->qdev.type == TYPE_DISK) {
1134 dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0;
1135 if (bdrv_is_read_only(s->qdev.conf.bs)) {
1136 dev_specific_param |= 0x80; /* Readonly. */
1137 }
1138 } else {
1139 /* MMC prescribes that CD/DVD drives have no block descriptors,
1140 * and defines no device-specific parameter. */
1141 dev_specific_param = 0x00;
1142 dbd = true;
1143 }
1144
1145 if (r->req.cmd.buf[0] == MODE_SENSE) {
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;
1155 }
1156
1157 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1158 if (!dbd && nb_sectors) {
1159 if (r->req.cmd.buf[0] == MODE_SENSE) {
1160 outbuf[3] = 8; /* Block descriptor length */
1161 } else { /* MODE_SENSE_10 */
1162 outbuf[7] = 8; /* Block descriptor length */
1163 }
1164 nb_sectors /= (s->qdev.blocksize / 512);
1165 if (nb_sectors > 0xffffff) {
1166 nb_sectors = 0;
1167 }
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 */
1174 p[6] = s->qdev.blocksize >> 8;
1175 p[7] = 0;
1176 p += 8;
1177 }
1178
1179 if (page_control == 3) {
1180 /* Saved Values */
1181 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
1182 return -1;
1183 }
1184
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 }
1194 }
1195
1196 buflen = p - outbuf;
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 */
1202 if (r->req.cmd.buf[0] == MODE_SENSE) {
1203 outbuf[0] = buflen - 1;
1204 } else { /* MODE_SENSE_10 */
1205 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
1206 outbuf[1] = (buflen - 2) & 0xff;
1207 }
1208 return buflen;
1209 }
1210
1211 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
1212 {
1213 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
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];
1220 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1221 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
1222 nb_sectors /= s->qdev.blocksize / 512;
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 }
1241 return toclen;
1242 }
1243
1244 static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
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 int pwrcnd = req->cmd.buf[4] & 0xf0;
1251
1252 if (pwrcnd) {
1253 /* eject/load only happens for power condition == 0 */
1254 return 0;
1255 }
1256
1257 if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && loej) {
1258 if (!start && !s->tray_open && s->tray_locked) {
1259 scsi_check_condition(r,
1260 bdrv_is_inserted(s->qdev.conf.bs)
1261 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED)
1262 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED));
1263 return -1;
1264 }
1265
1266 if (s->tray_open != !start) {
1267 bdrv_eject(s->qdev.conf.bs, !start);
1268 s->tray_open = !start;
1269 }
1270 }
1271 return 0;
1272 }
1273
1274 static void scsi_disk_emulate_read_data(SCSIRequest *req)
1275 {
1276 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1277 int buflen = r->iov.iov_len;
1278
1279 if (buflen) {
1280 DPRINTF("Read buf_len=%d\n", buflen);
1281 r->iov.iov_len = 0;
1282 r->started = true;
1283 scsi_req_data(&r->req, buflen);
1284 return;
1285 }
1286
1287 /* This also clears the sense buffer for REQUEST SENSE. */
1288 scsi_req_complete(&r->req, GOOD);
1289 }
1290
1291 static int scsi_disk_check_mode_select(SCSIDiskState *s, int page,
1292 uint8_t *inbuf, int inlen)
1293 {
1294 uint8_t mode_current[SCSI_MAX_MODE_LEN];
1295 uint8_t mode_changeable[SCSI_MAX_MODE_LEN];
1296 uint8_t *p;
1297 int len, expected_len, changeable_len, i;
1298
1299 /* The input buffer does not include the page header, so it is
1300 * off by 2 bytes.
1301 */
1302 expected_len = inlen + 2;
1303 if (expected_len > SCSI_MAX_MODE_LEN) {
1304 return -1;
1305 }
1306
1307 p = mode_current;
1308 memset(mode_current, 0, inlen + 2);
1309 len = mode_sense_page(s, page, &p, 0);
1310 if (len < 0 || len != expected_len) {
1311 return -1;
1312 }
1313
1314 p = mode_changeable;
1315 memset(mode_changeable, 0, inlen + 2);
1316 changeable_len = mode_sense_page(s, page, &p, 1);
1317 assert(changeable_len == len);
1318
1319 /* Check that unchangeable bits are the same as what MODE SENSE
1320 * would return.
1321 */
1322 for (i = 2; i < len; i++) {
1323 if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) != 0) {
1324 return -1;
1325 }
1326 }
1327 return 0;
1328 }
1329
1330 static void scsi_disk_apply_mode_select(SCSIDiskState *s, int page, uint8_t *p)
1331 {
1332 switch (page) {
1333 case MODE_PAGE_CACHING:
1334 bdrv_set_enable_write_cache(s->qdev.conf.bs, (p[0] & 4) != 0);
1335 break;
1336
1337 default:
1338 break;
1339 }
1340 }
1341
1342 static int mode_select_pages(SCSIDiskReq *r, uint8_t *p, int len, bool change)
1343 {
1344 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1345
1346 while (len > 0) {
1347 int page, subpage, page_len;
1348
1349 /* Parse both possible formats for the mode page headers. */
1350 page = p[0] & 0x3f;
1351 if (p[0] & 0x40) {
1352 if (len < 4) {
1353 goto invalid_param_len;
1354 }
1355 subpage = p[1];
1356 page_len = lduw_be_p(&p[2]);
1357 p += 4;
1358 len -= 4;
1359 } else {
1360 if (len < 2) {
1361 goto invalid_param_len;
1362 }
1363 subpage = 0;
1364 page_len = p[1];
1365 p += 2;
1366 len -= 2;
1367 }
1368
1369 if (subpage) {
1370 goto invalid_param;
1371 }
1372 if (page_len > len) {
1373 goto invalid_param_len;
1374 }
1375
1376 if (!change) {
1377 if (scsi_disk_check_mode_select(s, page, p, page_len) < 0) {
1378 goto invalid_param;
1379 }
1380 } else {
1381 scsi_disk_apply_mode_select(s, page, p);
1382 }
1383
1384 p += page_len;
1385 len -= page_len;
1386 }
1387 return 0;
1388
1389 invalid_param:
1390 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1391 return -1;
1392
1393 invalid_param_len:
1394 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1395 return -1;
1396 }
1397
1398 static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf)
1399 {
1400 uint8_t *p = inbuf;
1401 int cmd = r->req.cmd.buf[0];
1402 int len = r->req.cmd.xfer;
1403 int hdr_len = (cmd == MODE_SELECT ? 4 : 8);
1404 int bd_len;
1405 int pass;
1406
1407 /* We only support PF=1, SP=0. */
1408 if ((r->req.cmd.buf[1] & 0x11) != 0x10) {
1409 goto invalid_field;
1410 }
1411
1412 if (len < hdr_len) {
1413 goto invalid_param_len;
1414 }
1415
1416 bd_len = (cmd == MODE_SELECT ? p[3] : lduw_be_p(&p[6]));
1417 len -= hdr_len;
1418 p += hdr_len;
1419 if (len < bd_len) {
1420 goto invalid_param_len;
1421 }
1422 if (bd_len != 0 && bd_len != 8) {
1423 goto invalid_param;
1424 }
1425
1426 len -= bd_len;
1427 p += bd_len;
1428
1429 /* Ensure no change is made if there is an error! */
1430 for (pass = 0; pass < 2; pass++) {
1431 if (mode_select_pages(r, p, len, pass == 1) < 0) {
1432 assert(pass == 0);
1433 return;
1434 }
1435 }
1436 scsi_req_complete(&r->req, GOOD);
1437 return;
1438
1439 invalid_param:
1440 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM));
1441 return;
1442
1443 invalid_param_len:
1444 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1445 return;
1446
1447 invalid_field:
1448 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1449 return;
1450 }
1451
1452 static inline bool check_lba_range(SCSIDiskState *s,
1453 uint64_t sector_num, uint32_t nb_sectors)
1454 {
1455 /*
1456 * The first line tests that no overflow happens when computing the last
1457 * sector. The second line tests that the last accessed sector is in
1458 * range.
1459 *
1460 * Careful, the computations should not underflow for nb_sectors == 0,
1461 * and a 0-block read to the first LBA beyond the end of device is
1462 * valid.
1463 */
1464 return (sector_num <= sector_num + nb_sectors &&
1465 sector_num + nb_sectors <= s->qdev.max_lba + 1);
1466 }
1467
1468 typedef struct UnmapCBData {
1469 SCSIDiskReq *r;
1470 uint8_t *inbuf;
1471 int count;
1472 } UnmapCBData;
1473
1474 static void scsi_unmap_complete(void *opaque, int ret)
1475 {
1476 UnmapCBData *data = opaque;
1477 SCSIDiskReq *r = data->r;
1478 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
1479 uint64_t sector_num;
1480 uint32_t nb_sectors;
1481
1482 r->req.aiocb = NULL;
1483 if (ret < 0) {
1484 if (scsi_handle_rw_error(r, -ret)) {
1485 goto done;
1486 }
1487 }
1488
1489 if (data->count > 0 && !r->req.io_canceled) {
1490 sector_num = ldq_be_p(&data->inbuf[0]);
1491 nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
1492 if (!check_lba_range(s, sector_num, nb_sectors)) {
1493 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1494 goto done;
1495 }
1496
1497 r->req.aiocb = bdrv_aio_discard(s->qdev.conf.bs,
1498 sector_num * (s->qdev.blocksize / 512),
1499 nb_sectors * (s->qdev.blocksize / 512),
1500 scsi_unmap_complete, data);
1501 data->count--;
1502 data->inbuf += 16;
1503 return;
1504 }
1505
1506 done:
1507 if (data->count == 0) {
1508 scsi_req_complete(&r->req, GOOD);
1509 }
1510 if (!r->req.io_canceled) {
1511 scsi_req_unref(&r->req);
1512 }
1513 g_free(data);
1514 }
1515
1516 static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
1517 {
1518 uint8_t *p = inbuf;
1519 int len = r->req.cmd.xfer;
1520 UnmapCBData *data;
1521
1522 if (len < 8) {
1523 goto invalid_param_len;
1524 }
1525 if (len < lduw_be_p(&p[0]) + 2) {
1526 goto invalid_param_len;
1527 }
1528 if (len < lduw_be_p(&p[2]) + 8) {
1529 goto invalid_param_len;
1530 }
1531 if (lduw_be_p(&p[2]) & 15) {
1532 goto invalid_param_len;
1533 }
1534
1535 data = g_new0(UnmapCBData, 1);
1536 data->r = r;
1537 data->inbuf = &p[8];
1538 data->count = lduw_be_p(&p[2]) >> 4;
1539
1540 /* The matching unref is in scsi_unmap_complete, before data is freed. */
1541 scsi_req_ref(&r->req);
1542 scsi_unmap_complete(data, 0);
1543 return;
1544
1545 invalid_param_len:
1546 scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
1547 return;
1548 }
1549
1550 static void scsi_disk_emulate_write_data(SCSIRequest *req)
1551 {
1552 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1553
1554 if (r->iov.iov_len) {
1555 int buflen = r->iov.iov_len;
1556 DPRINTF("Write buf_len=%d\n", buflen);
1557 r->iov.iov_len = 0;
1558 scsi_req_data(&r->req, buflen);
1559 return;
1560 }
1561
1562 switch (req->cmd.buf[0]) {
1563 case MODE_SELECT:
1564 case MODE_SELECT_10:
1565 /* This also clears the sense buffer for REQUEST SENSE. */
1566 scsi_disk_emulate_mode_select(r, r->iov.iov_base);
1567 break;
1568
1569 case UNMAP:
1570 scsi_disk_emulate_unmap(r, r->iov.iov_base);
1571 break;
1572
1573 default:
1574 abort();
1575 }
1576 }
1577
1578 static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf)
1579 {
1580 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1581 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1582 uint64_t nb_sectors;
1583 uint8_t *outbuf;
1584 int buflen;
1585
1586 switch (req->cmd.buf[0]) {
1587 case INQUIRY:
1588 case MODE_SENSE:
1589 case MODE_SENSE_10:
1590 case RESERVE:
1591 case RESERVE_10:
1592 case RELEASE:
1593 case RELEASE_10:
1594 case START_STOP:
1595 case ALLOW_MEDIUM_REMOVAL:
1596 case GET_CONFIGURATION:
1597 case GET_EVENT_STATUS_NOTIFICATION:
1598 case MECHANISM_STATUS:
1599 case REQUEST_SENSE:
1600 break;
1601
1602 default:
1603 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
1604 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1605 return 0;
1606 }
1607 break;
1608 }
1609
1610 if (!r->iov.iov_base) {
1611 /*
1612 * FIXME: we shouldn't return anything bigger than 4k, but the code
1613 * requires the buffer to be as big as req->cmd.xfer in several
1614 * places. So, do not allow CDBs with a very large ALLOCATION
1615 * LENGTH. The real fix would be to modify scsi_read_data and
1616 * dma_buf_read, so that they return data beyond the buflen
1617 * as all zeros.
1618 */
1619 if (req->cmd.xfer > 65536) {
1620 goto illegal_request;
1621 }
1622 r->buflen = MAX(4096, req->cmd.xfer);
1623 r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
1624 }
1625
1626 buflen = req->cmd.xfer;
1627 outbuf = r->iov.iov_base;
1628 switch (req->cmd.buf[0]) {
1629 case TEST_UNIT_READY:
1630 assert(!s->tray_open && bdrv_is_inserted(s->qdev.conf.bs));
1631 break;
1632 case INQUIRY:
1633 buflen = scsi_disk_emulate_inquiry(req, outbuf);
1634 if (buflen < 0) {
1635 goto illegal_request;
1636 }
1637 break;
1638 case MODE_SENSE:
1639 case MODE_SENSE_10:
1640 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
1641 if (buflen < 0) {
1642 goto illegal_request;
1643 }
1644 break;
1645 case READ_TOC:
1646 buflen = scsi_disk_emulate_read_toc(req, outbuf);
1647 if (buflen < 0) {
1648 goto illegal_request;
1649 }
1650 break;
1651 case RESERVE:
1652 if (req->cmd.buf[1] & 1) {
1653 goto illegal_request;
1654 }
1655 break;
1656 case RESERVE_10:
1657 if (req->cmd.buf[1] & 3) {
1658 goto illegal_request;
1659 }
1660 break;
1661 case RELEASE:
1662 if (req->cmd.buf[1] & 1) {
1663 goto illegal_request;
1664 }
1665 break;
1666 case RELEASE_10:
1667 if (req->cmd.buf[1] & 3) {
1668 goto illegal_request;
1669 }
1670 break;
1671 case START_STOP:
1672 if (scsi_disk_emulate_start_stop(r) < 0) {
1673 return 0;
1674 }
1675 break;
1676 case ALLOW_MEDIUM_REMOVAL:
1677 s->tray_locked = req->cmd.buf[4] & 1;
1678 bdrv_lock_medium(s->qdev.conf.bs, req->cmd.buf[4] & 1);
1679 break;
1680 case READ_CAPACITY_10:
1681 /* The normal LEN field for this command is zero. */
1682 memset(outbuf, 0, 8);
1683 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1684 if (!nb_sectors) {
1685 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
1686 return -1;
1687 }
1688 if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) {
1689 goto illegal_request;
1690 }
1691 nb_sectors /= s->qdev.blocksize / 512;
1692 /* Returned value is the address of the last sector. */
1693 nb_sectors--;
1694 /* Remember the new size for read/write sanity checking. */
1695 s->qdev.max_lba = nb_sectors;
1696 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1697 if (nb_sectors > UINT32_MAX) {
1698 nb_sectors = UINT32_MAX;
1699 }
1700 outbuf[0] = (nb_sectors >> 24) & 0xff;
1701 outbuf[1] = (nb_sectors >> 16) & 0xff;
1702 outbuf[2] = (nb_sectors >> 8) & 0xff;
1703 outbuf[3] = nb_sectors & 0xff;
1704 outbuf[4] = 0;
1705 outbuf[5] = 0;
1706 outbuf[6] = s->qdev.blocksize >> 8;
1707 outbuf[7] = 0;
1708 buflen = 8;
1709 break;
1710 case REQUEST_SENSE:
1711 /* Just return "NO SENSE". */
1712 buflen = scsi_build_sense(NULL, 0, outbuf, r->buflen,
1713 (req->cmd.buf[1] & 1) == 0);
1714 break;
1715 case MECHANISM_STATUS:
1716 buflen = scsi_emulate_mechanism_status(s, outbuf);
1717 if (buflen < 0) {
1718 goto illegal_request;
1719 }
1720 break;
1721 case GET_CONFIGURATION:
1722 buflen = scsi_get_configuration(s, outbuf);
1723 if (buflen < 0) {
1724 goto illegal_request;
1725 }
1726 break;
1727 case GET_EVENT_STATUS_NOTIFICATION:
1728 buflen = scsi_get_event_status_notification(s, r, outbuf);
1729 if (buflen < 0) {
1730 goto illegal_request;
1731 }
1732 break;
1733 case READ_DISC_INFORMATION:
1734 buflen = scsi_read_disc_information(s, r, outbuf);
1735 if (buflen < 0) {
1736 goto illegal_request;
1737 }
1738 break;
1739 case READ_DVD_STRUCTURE:
1740 buflen = scsi_read_dvd_structure(s, r, outbuf);
1741 if (buflen < 0) {
1742 goto illegal_request;
1743 }
1744 break;
1745 case SERVICE_ACTION_IN_16:
1746 /* Service Action In subcommands. */
1747 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
1748 DPRINTF("SAI READ CAPACITY(16)\n");
1749 memset(outbuf, 0, req->cmd.xfer);
1750 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1751 if (!nb_sectors) {
1752 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
1753 return -1;
1754 }
1755 if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) {
1756 goto illegal_request;
1757 }
1758 nb_sectors /= s->qdev.blocksize / 512;
1759 /* Returned value is the address of the last sector. */
1760 nb_sectors--;
1761 /* Remember the new size for read/write sanity checking. */
1762 s->qdev.max_lba = nb_sectors;
1763 outbuf[0] = (nb_sectors >> 56) & 0xff;
1764 outbuf[1] = (nb_sectors >> 48) & 0xff;
1765 outbuf[2] = (nb_sectors >> 40) & 0xff;
1766 outbuf[3] = (nb_sectors >> 32) & 0xff;
1767 outbuf[4] = (nb_sectors >> 24) & 0xff;
1768 outbuf[5] = (nb_sectors >> 16) & 0xff;
1769 outbuf[6] = (nb_sectors >> 8) & 0xff;
1770 outbuf[7] = nb_sectors & 0xff;
1771 outbuf[8] = 0;
1772 outbuf[9] = 0;
1773 outbuf[10] = s->qdev.blocksize >> 8;
1774 outbuf[11] = 0;
1775 outbuf[12] = 0;
1776 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
1777
1778 /* set TPE bit if the format supports discard */
1779 if (s->qdev.conf.discard_granularity) {
1780 outbuf[14] = 0x80;
1781 }
1782
1783 /* Protection, exponent and lowest lba field left blank. */
1784 buflen = req->cmd.xfer;
1785 break;
1786 }
1787 DPRINTF("Unsupported Service Action In\n");
1788 goto illegal_request;
1789 case SYNCHRONIZE_CACHE:
1790 /* The request is used as the AIO opaque value, so add a ref. */
1791 scsi_req_ref(&r->req);
1792 bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH);
1793 r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r);
1794 return 0;
1795 case SEEK_10:
1796 DPRINTF("Seek(10) (sector %" PRId64 ")\n", r->req.cmd.lba);
1797 if (r->req.cmd.lba > s->qdev.max_lba) {
1798 goto illegal_lba;
1799 }
1800 break;
1801 case MODE_SELECT:
1802 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1803 break;
1804 case MODE_SELECT_10:
1805 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1806 break;
1807 case UNMAP:
1808 DPRINTF("Unmap (len %lu)\n", (long)r->req.cmd.xfer);
1809 break;
1810 case WRITE_SAME_10:
1811 case WRITE_SAME_16:
1812 nb_sectors = scsi_data_cdb_length(r->req.cmd.buf);
1813 if (bdrv_is_read_only(s->qdev.conf.bs)) {
1814 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1815 return 0;
1816 }
1817 if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) {
1818 goto illegal_lba;
1819 }
1820
1821 /*
1822 * We only support WRITE SAME with the unmap bit set for now.
1823 */
1824 if (!(req->cmd.buf[1] & 0x8)) {
1825 goto illegal_request;
1826 }
1827
1828 /* The request is used as the AIO opaque value, so add a ref. */
1829 scsi_req_ref(&r->req);
1830 r->req.aiocb = bdrv_aio_discard(s->qdev.conf.bs,
1831 r->req.cmd.lba * (s->qdev.blocksize / 512),
1832 nb_sectors * (s->qdev.blocksize / 512),
1833 scsi_aio_complete, r);
1834 return 0;
1835 default:
1836 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1837 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
1838 return 0;
1839 }
1840 assert(!r->req.aiocb);
1841 r->iov.iov_len = MIN(buflen, req->cmd.xfer);
1842 if (r->iov.iov_len == 0) {
1843 scsi_req_complete(&r->req, GOOD);
1844 }
1845 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1846 assert(r->iov.iov_len == req->cmd.xfer);
1847 return -r->iov.iov_len;
1848 } else {
1849 return r->iov.iov_len;
1850 }
1851
1852 illegal_request:
1853 if (r->req.status == -1) {
1854 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1855 }
1856 return 0;
1857
1858 illegal_lba:
1859 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1860 return 0;
1861 }
1862
1863 /* Execute a scsi command. Returns the length of the data expected by the
1864 command. This will be Positive for data transfers from the device
1865 (eg. disk reads), negative for transfers to the device (eg. disk writes),
1866 and zero if the command does not transfer any data. */
1867
1868 static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf)
1869 {
1870 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1871 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1872 uint32_t len;
1873 uint8_t command;
1874
1875 command = buf[0];
1876
1877 if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
1878 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1879 return 0;
1880 }
1881
1882 len = scsi_data_cdb_length(r->req.cmd.buf);
1883 switch (command) {
1884 case READ_6:
1885 case READ_10:
1886 case READ_12:
1887 case READ_16:
1888 DPRINTF("Read (sector %" PRId64 ", count %u)\n", r->req.cmd.lba, len);
1889 if (r->req.cmd.buf[1] & 0xe0) {
1890 goto illegal_request;
1891 }
1892 if (!check_lba_range(s, r->req.cmd.lba, len)) {
1893 goto illegal_lba;
1894 }
1895 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1896 r->sector_count = len * (s->qdev.blocksize / 512);
1897 break;
1898 case WRITE_6:
1899 case WRITE_10:
1900 case WRITE_12:
1901 case WRITE_16:
1902 case WRITE_VERIFY_10:
1903 case WRITE_VERIFY_12:
1904 case WRITE_VERIFY_16:
1905 if (bdrv_is_read_only(s->qdev.conf.bs)) {
1906 scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
1907 return 0;
1908 }
1909 /* fallthrough */
1910 case VERIFY_10:
1911 case VERIFY_12:
1912 case VERIFY_16:
1913 DPRINTF("Write %s(sector %" PRId64 ", count %u)\n",
1914 (command & 0xe) == 0xe ? "And Verify " : "",
1915 r->req.cmd.lba, len);
1916 if (r->req.cmd.buf[1] & 0xe0) {
1917 goto illegal_request;
1918 }
1919 if (!check_lba_range(s, r->req.cmd.lba, len)) {
1920 goto illegal_lba;
1921 }
1922 r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1923 r->sector_count = len * (s->qdev.blocksize / 512);
1924 break;
1925 default:
1926 abort();
1927 illegal_request:
1928 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1929 return 0;
1930 illegal_lba:
1931 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1932 return 0;
1933 }
1934 if (r->sector_count == 0) {
1935 scsi_req_complete(&r->req, GOOD);
1936 }
1937 assert(r->iov.iov_len == 0);
1938 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1939 return -r->sector_count * 512;
1940 } else {
1941 return r->sector_count * 512;
1942 }
1943 }
1944
1945 static void scsi_disk_reset(DeviceState *dev)
1946 {
1947 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1948 uint64_t nb_sectors;
1949
1950 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
1951
1952 bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1953 nb_sectors /= s->qdev.blocksize / 512;
1954 if (nb_sectors) {
1955 nb_sectors--;
1956 }
1957 s->qdev.max_lba = nb_sectors;
1958 }
1959
1960 static void scsi_destroy(SCSIDevice *dev)
1961 {
1962 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1963
1964 scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
1965 blockdev_mark_auto_del(s->qdev.conf.bs);
1966 }
1967
1968 static void scsi_disk_resize_cb(void *opaque)
1969 {
1970 SCSIDiskState *s = opaque;
1971
1972 /* SPC lists this sense code as available only for
1973 * direct-access devices.
1974 */
1975 if (s->qdev.type == TYPE_DISK) {
1976 scsi_device_set_ua(&s->qdev, SENSE_CODE(CAPACITY_CHANGED));
1977 scsi_device_report_change(&s->qdev, SENSE_CODE(CAPACITY_CHANGED));
1978 }
1979 }
1980
1981 static void scsi_cd_change_media_cb(void *opaque, bool load)
1982 {
1983 SCSIDiskState *s = opaque;
1984
1985 /*
1986 * When a CD gets changed, we have to report an ejected state and
1987 * then a loaded state to guests so that they detect tray
1988 * open/close and media change events. Guests that do not use
1989 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1990 * states rely on this behavior.
1991 *
1992 * media_changed governs the state machine used for unit attention
1993 * report. media_event is used by GET EVENT STATUS NOTIFICATION.
1994 */
1995 s->media_changed = load;
1996 s->tray_open = !load;
1997 scsi_device_set_ua(&s->qdev, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM));
1998 s->media_event = true;
1999 s->eject_request = false;
2000 }
2001
2002 static void scsi_cd_eject_request_cb(void *opaque, bool force)
2003 {
2004 SCSIDiskState *s = opaque;
2005
2006 s->eject_request = true;
2007 if (force) {
2008 s->tray_locked = false;
2009 }
2010 }
2011
2012 static bool scsi_cd_is_tray_open(void *opaque)
2013 {
2014 return ((SCSIDiskState *)opaque)->tray_open;
2015 }
2016
2017 static bool scsi_cd_is_medium_locked(void *opaque)
2018 {
2019 return ((SCSIDiskState *)opaque)->tray_locked;
2020 }
2021
2022 static const BlockDevOps scsi_disk_removable_block_ops = {
2023 .change_media_cb = scsi_cd_change_media_cb,
2024 .eject_request_cb = scsi_cd_eject_request_cb,
2025 .is_tray_open = scsi_cd_is_tray_open,
2026 .is_medium_locked = scsi_cd_is_medium_locked,
2027
2028 .resize_cb = scsi_disk_resize_cb,
2029 };
2030
2031 static const BlockDevOps scsi_disk_block_ops = {
2032 .resize_cb = scsi_disk_resize_cb,
2033 };
2034
2035 static void scsi_disk_unit_attention_reported(SCSIDevice *dev)
2036 {
2037 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2038 if (s->media_changed) {
2039 s->media_changed = false;
2040 scsi_device_set_ua(&s->qdev, SENSE_CODE(MEDIUM_CHANGED));
2041 }
2042 }
2043
2044 static int scsi_initfn(SCSIDevice *dev)
2045 {
2046 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2047
2048 if (!s->qdev.conf.bs) {
2049 error_report("drive property not set");
2050 return -1;
2051 }
2052
2053 if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) &&
2054 !bdrv_is_inserted(s->qdev.conf.bs)) {
2055 error_report("Device needs media, but drive is empty");
2056 return -1;
2057 }
2058
2059 blkconf_serial(&s->qdev.conf, &s->serial);
2060 if (dev->type == TYPE_DISK
2061 && blkconf_geometry(&dev->conf, NULL, 65535, 255, 255) < 0) {
2062 return -1;
2063 }
2064
2065 if (!s->version) {
2066 s->version = g_strdup(qemu_get_version());
2067 }
2068 if (!s->vendor) {
2069 s->vendor = g_strdup("QEMU");
2070 }
2071
2072 if (bdrv_is_sg(s->qdev.conf.bs)) {
2073 error_report("unwanted /dev/sg*");
2074 return -1;
2075 }
2076
2077 if (s->features & (1 << SCSI_DISK_F_REMOVABLE)) {
2078 bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_disk_removable_block_ops, s);
2079 } else {
2080 bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_disk_block_ops, s);
2081 }
2082 bdrv_set_buffer_alignment(s->qdev.conf.bs, s->qdev.blocksize);
2083
2084 bdrv_iostatus_enable(s->qdev.conf.bs);
2085 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, NULL);
2086 return 0;
2087 }
2088
2089 static int scsi_hd_initfn(SCSIDevice *dev)
2090 {
2091 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2092 s->qdev.blocksize = s->qdev.conf.logical_block_size;
2093 s->qdev.type = TYPE_DISK;
2094 if (!s->product) {
2095 s->product = g_strdup("QEMU HARDDISK");
2096 }
2097 return scsi_initfn(&s->qdev);
2098 }
2099
2100 static int scsi_cd_initfn(SCSIDevice *dev)
2101 {
2102 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2103 s->qdev.blocksize = 2048;
2104 s->qdev.type = TYPE_ROM;
2105 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2106 if (!s->product) {
2107 s->product = g_strdup("QEMU CD-ROM");
2108 }
2109 return scsi_initfn(&s->qdev);
2110 }
2111
2112 static int scsi_disk_initfn(SCSIDevice *dev)
2113 {
2114 DriveInfo *dinfo;
2115
2116 if (!dev->conf.bs) {
2117 return scsi_initfn(dev); /* ... and die there */
2118 }
2119
2120 dinfo = drive_get_by_blockdev(dev->conf.bs);
2121 if (dinfo->media_cd) {
2122 return scsi_cd_initfn(dev);
2123 } else {
2124 return scsi_hd_initfn(dev);
2125 }
2126 }
2127
2128 static const SCSIReqOps scsi_disk_emulate_reqops = {
2129 .size = sizeof(SCSIDiskReq),
2130 .free_req = scsi_free_request,
2131 .send_command = scsi_disk_emulate_command,
2132 .read_data = scsi_disk_emulate_read_data,
2133 .write_data = scsi_disk_emulate_write_data,
2134 .get_buf = scsi_get_buf,
2135 };
2136
2137 static const SCSIReqOps scsi_disk_dma_reqops = {
2138 .size = sizeof(SCSIDiskReq),
2139 .free_req = scsi_free_request,
2140 .send_command = scsi_disk_dma_command,
2141 .read_data = scsi_read_data,
2142 .write_data = scsi_write_data,
2143 .cancel_io = scsi_cancel_io,
2144 .get_buf = scsi_get_buf,
2145 .load_request = scsi_disk_load_request,
2146 .save_request = scsi_disk_save_request,
2147 };
2148
2149 static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = {
2150 [TEST_UNIT_READY] = &scsi_disk_emulate_reqops,
2151 [INQUIRY] = &scsi_disk_emulate_reqops,
2152 [MODE_SENSE] = &scsi_disk_emulate_reqops,
2153 [MODE_SENSE_10] = &scsi_disk_emulate_reqops,
2154 [START_STOP] = &scsi_disk_emulate_reqops,
2155 [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops,
2156 [READ_CAPACITY_10] = &scsi_disk_emulate_reqops,
2157 [READ_TOC] = &scsi_disk_emulate_reqops,
2158 [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops,
2159 [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops,
2160 [GET_CONFIGURATION] = &scsi_disk_emulate_reqops,
2161 [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops,
2162 [MECHANISM_STATUS] = &scsi_disk_emulate_reqops,
2163 [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops,
2164 [REQUEST_SENSE] = &scsi_disk_emulate_reqops,
2165 [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops,
2166 [SEEK_10] = &scsi_disk_emulate_reqops,
2167 [MODE_SELECT] = &scsi_disk_emulate_reqops,
2168 [MODE_SELECT_10] = &scsi_disk_emulate_reqops,
2169 [UNMAP] = &scsi_disk_emulate_reqops,
2170 [WRITE_SAME_10] = &scsi_disk_emulate_reqops,
2171 [WRITE_SAME_16] = &scsi_disk_emulate_reqops,
2172
2173 [READ_6] = &scsi_disk_dma_reqops,
2174 [READ_10] = &scsi_disk_dma_reqops,
2175 [READ_12] = &scsi_disk_dma_reqops,
2176 [READ_16] = &scsi_disk_dma_reqops,
2177 [VERIFY_10] = &scsi_disk_dma_reqops,
2178 [VERIFY_12] = &scsi_disk_dma_reqops,
2179 [VERIFY_16] = &scsi_disk_dma_reqops,
2180 [WRITE_6] = &scsi_disk_dma_reqops,
2181 [WRITE_10] = &scsi_disk_dma_reqops,
2182 [WRITE_12] = &scsi_disk_dma_reqops,
2183 [WRITE_16] = &scsi_disk_dma_reqops,
2184 [WRITE_VERIFY_10] = &scsi_disk_dma_reqops,
2185 [WRITE_VERIFY_12] = &scsi_disk_dma_reqops,
2186 [WRITE_VERIFY_16] = &scsi_disk_dma_reqops,
2187 };
2188
2189 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
2190 uint8_t *buf, void *hba_private)
2191 {
2192 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2193 SCSIRequest *req;
2194 const SCSIReqOps *ops;
2195 uint8_t command;
2196
2197 command = buf[0];
2198 ops = scsi_disk_reqops_dispatch[command];
2199 if (!ops) {
2200 ops = &scsi_disk_emulate_reqops;
2201 }
2202 req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private);
2203
2204 #ifdef DEBUG_SCSI
2205 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
2206 {
2207 int i;
2208 for (i = 1; i < req->cmd.len; i++) {
2209 printf(" 0x%02x", buf[i]);
2210 }
2211 printf("\n");
2212 }
2213 #endif
2214
2215 return req;
2216 }
2217
2218 #ifdef __linux__
2219 static int get_device_type(SCSIDiskState *s)
2220 {
2221 BlockDriverState *bdrv = s->qdev.conf.bs;
2222 uint8_t cmd[16];
2223 uint8_t buf[36];
2224 uint8_t sensebuf[8];
2225 sg_io_hdr_t io_header;
2226 int ret;
2227
2228 memset(cmd, 0, sizeof(cmd));
2229 memset(buf, 0, sizeof(buf));
2230 cmd[0] = INQUIRY;
2231 cmd[4] = sizeof(buf);
2232
2233 memset(&io_header, 0, sizeof(io_header));
2234 io_header.interface_id = 'S';
2235 io_header.dxfer_direction = SG_DXFER_FROM_DEV;
2236 io_header.dxfer_len = sizeof(buf);
2237 io_header.dxferp = buf;
2238 io_header.cmdp = cmd;
2239 io_header.cmd_len = sizeof(cmd);
2240 io_header.mx_sb_len = sizeof(sensebuf);
2241 io_header.sbp = sensebuf;
2242 io_header.timeout = 6000; /* XXX */
2243
2244 ret = bdrv_ioctl(bdrv, SG_IO, &io_header);
2245 if (ret < 0 || io_header.driver_status || io_header.host_status) {
2246 return -1;
2247 }
2248 s->qdev.type = buf[0];
2249 if (buf[1] & 0x80) {
2250 s->features |= 1 << SCSI_DISK_F_REMOVABLE;
2251 }
2252 return 0;
2253 }
2254
2255 static int scsi_block_initfn(SCSIDevice *dev)
2256 {
2257 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
2258 int sg_version;
2259 int rc;
2260
2261 if (!s->qdev.conf.bs) {
2262 error_report("scsi-block: drive property not set");
2263 return -1;
2264 }
2265
2266 /* check we are using a driver managing SG_IO (version 3 and after) */
2267 if (bdrv_ioctl(s->qdev.conf.bs, SG_GET_VERSION_NUM, &sg_version) < 0 ||
2268 sg_version < 30000) {
2269 error_report("scsi-block: scsi generic interface too old");
2270 return -1;
2271 }
2272
2273 /* get device type from INQUIRY data */
2274 rc = get_device_type(s);
2275 if (rc < 0) {
2276 error_report("scsi-block: INQUIRY failed");
2277 return -1;
2278 }
2279
2280 /* Make a guess for the block size, we'll fix it when the guest sends.
2281 * READ CAPACITY. If they don't, they likely would assume these sizes
2282 * anyway. (TODO: check in /sys).
2283 */
2284 if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) {
2285 s->qdev.blocksize = 2048;
2286 } else {
2287 s->qdev.blocksize = 512;
2288 }
2289 return scsi_initfn(&s->qdev);
2290 }
2291
2292 static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag,
2293 uint32_t lun, uint8_t *buf,
2294 void *hba_private)
2295 {
2296 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
2297
2298 switch (buf[0]) {
2299 case READ_6:
2300 case READ_10:
2301 case READ_12:
2302 case READ_16:
2303 case VERIFY_10:
2304 case VERIFY_12:
2305 case VERIFY_16:
2306 case WRITE_6:
2307 case WRITE_10:
2308 case WRITE_12:
2309 case WRITE_16:
2310 case WRITE_VERIFY_10:
2311 case WRITE_VERIFY_12:
2312 case WRITE_VERIFY_16:
2313 /* If we are not using O_DIRECT, we might read stale data from the
2314 * host cache if writes were made using other commands than these
2315 * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without
2316 * O_DIRECT everything must go through SG_IO.
2317 */
2318 if (bdrv_get_flags(s->qdev.conf.bs) & BDRV_O_NOCACHE) {
2319 break;
2320 }
2321
2322 /* MMC writing cannot be done via pread/pwrite, because it sometimes
2323 * involves writing beyond the maximum LBA or to negative LBA (lead-in).
2324 * And once you do these writes, reading from the block device is
2325 * unreliable, too. It is even possible that reads deliver random data
2326 * from the host page cache (this is probably a Linux bug).
2327 *
2328 * We might use scsi_disk_dma_reqops as long as no writing commands are
2329 * seen, but performance usually isn't paramount on optical media. So,
2330 * just make scsi-block operate the same as scsi-generic for them.
2331 */
2332 if (s->qdev.type != TYPE_ROM) {
2333 return scsi_req_alloc(&scsi_disk_dma_reqops, &s->qdev, tag, lun,
2334 hba_private);
2335 }
2336 }
2337
2338 return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun,
2339 hba_private);
2340 }
2341 #endif
2342
2343 #define DEFINE_SCSI_DISK_PROPERTIES() \
2344 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
2345 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
2346 DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \
2347 DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \
2348 DEFINE_PROP_STRING("product", SCSIDiskState, product)
2349
2350 static Property scsi_hd_properties[] = {
2351 DEFINE_SCSI_DISK_PROPERTIES(),
2352 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2353 SCSI_DISK_F_REMOVABLE, false),
2354 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2355 SCSI_DISK_F_DPOFUA, false),
2356 DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0),
2357 DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
2358 DEFINE_PROP_END_OF_LIST(),
2359 };
2360
2361 static const VMStateDescription vmstate_scsi_disk_state = {
2362 .name = "scsi-disk",
2363 .version_id = 1,
2364 .minimum_version_id = 1,
2365 .minimum_version_id_old = 1,
2366 .fields = (VMStateField[]) {
2367 VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState),
2368 VMSTATE_BOOL(media_changed, SCSIDiskState),
2369 VMSTATE_BOOL(media_event, SCSIDiskState),
2370 VMSTATE_BOOL(eject_request, SCSIDiskState),
2371 VMSTATE_BOOL(tray_open, SCSIDiskState),
2372 VMSTATE_BOOL(tray_locked, SCSIDiskState),
2373 VMSTATE_END_OF_LIST()
2374 }
2375 };
2376
2377 static void scsi_hd_class_initfn(ObjectClass *klass, void *data)
2378 {
2379 DeviceClass *dc = DEVICE_CLASS(klass);
2380 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2381
2382 sc->init = scsi_hd_initfn;
2383 sc->destroy = scsi_destroy;
2384 sc->alloc_req = scsi_new_request;
2385 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2386 dc->fw_name = "disk";
2387 dc->desc = "virtual SCSI disk";
2388 dc->reset = scsi_disk_reset;
2389 dc->props = scsi_hd_properties;
2390 dc->vmsd = &vmstate_scsi_disk_state;
2391 }
2392
2393 static TypeInfo scsi_hd_info = {
2394 .name = "scsi-hd",
2395 .parent = TYPE_SCSI_DEVICE,
2396 .instance_size = sizeof(SCSIDiskState),
2397 .class_init = scsi_hd_class_initfn,
2398 };
2399
2400 static Property scsi_cd_properties[] = {
2401 DEFINE_SCSI_DISK_PROPERTIES(),
2402 DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0),
2403 DEFINE_PROP_END_OF_LIST(),
2404 };
2405
2406 static void scsi_cd_class_initfn(ObjectClass *klass, void *data)
2407 {
2408 DeviceClass *dc = DEVICE_CLASS(klass);
2409 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2410
2411 sc->init = scsi_cd_initfn;
2412 sc->destroy = scsi_destroy;
2413 sc->alloc_req = scsi_new_request;
2414 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2415 dc->fw_name = "disk";
2416 dc->desc = "virtual SCSI CD-ROM";
2417 dc->reset = scsi_disk_reset;
2418 dc->props = scsi_cd_properties;
2419 dc->vmsd = &vmstate_scsi_disk_state;
2420 }
2421
2422 static TypeInfo scsi_cd_info = {
2423 .name = "scsi-cd",
2424 .parent = TYPE_SCSI_DEVICE,
2425 .instance_size = sizeof(SCSIDiskState),
2426 .class_init = scsi_cd_class_initfn,
2427 };
2428
2429 #ifdef __linux__
2430 static Property scsi_block_properties[] = {
2431 DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.bs),
2432 DEFINE_PROP_INT32("bootindex", SCSIDiskState, qdev.conf.bootindex, -1),
2433 DEFINE_PROP_END_OF_LIST(),
2434 };
2435
2436 static void scsi_block_class_initfn(ObjectClass *klass, void *data)
2437 {
2438 DeviceClass *dc = DEVICE_CLASS(klass);
2439 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2440
2441 sc->init = scsi_block_initfn;
2442 sc->destroy = scsi_destroy;
2443 sc->alloc_req = scsi_block_new_request;
2444 dc->fw_name = "disk";
2445 dc->desc = "SCSI block device passthrough";
2446 dc->reset = scsi_disk_reset;
2447 dc->props = scsi_block_properties;
2448 dc->vmsd = &vmstate_scsi_disk_state;
2449 }
2450
2451 static TypeInfo scsi_block_info = {
2452 .name = "scsi-block",
2453 .parent = TYPE_SCSI_DEVICE,
2454 .instance_size = sizeof(SCSIDiskState),
2455 .class_init = scsi_block_class_initfn,
2456 };
2457 #endif
2458
2459 static Property scsi_disk_properties[] = {
2460 DEFINE_SCSI_DISK_PROPERTIES(),
2461 DEFINE_PROP_BIT("removable", SCSIDiskState, features,
2462 SCSI_DISK_F_REMOVABLE, false),
2463 DEFINE_PROP_BIT("dpofua", SCSIDiskState, features,
2464 SCSI_DISK_F_DPOFUA, false),
2465 DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0),
2466 DEFINE_PROP_END_OF_LIST(),
2467 };
2468
2469 static void scsi_disk_class_initfn(ObjectClass *klass, void *data)
2470 {
2471 DeviceClass *dc = DEVICE_CLASS(klass);
2472 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);
2473
2474 sc->init = scsi_disk_initfn;
2475 sc->destroy = scsi_destroy;
2476 sc->alloc_req = scsi_new_request;
2477 sc->unit_attention_reported = scsi_disk_unit_attention_reported;
2478 dc->fw_name = "disk";
2479 dc->desc = "virtual SCSI disk or CD-ROM (legacy)";
2480 dc->reset = scsi_disk_reset;
2481 dc->props = scsi_disk_properties;
2482 dc->vmsd = &vmstate_scsi_disk_state;
2483 }
2484
2485 static TypeInfo scsi_disk_info = {
2486 .name = "scsi-disk",
2487 .parent = TYPE_SCSI_DEVICE,
2488 .instance_size = sizeof(SCSIDiskState),
2489 .class_init = scsi_disk_class_initfn,
2490 };
2491
2492 static void scsi_disk_register_types(void)
2493 {
2494 type_register_static(&scsi_hd_info);
2495 type_register_static(&scsi_cd_info);
2496 #ifdef __linux__
2497 type_register_static(&scsi_block_info);
2498 #endif
2499 type_register_static(&scsi_disk_info);
2500 }
2501
2502 type_init(scsi_disk_register_types)