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