]> git.proxmox.com Git - mirror_qemu.git/blob - hw/scsi-disk.c
scsi-disk: Factor out scsi_disk_emulate_start_stop()
[mirror_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
41 #define SCSI_DMA_BUF_SIZE 131072
42 #define SCSI_MAX_INQUIRY_LEN 256
43
44 #define SCSI_REQ_STATUS_RETRY 0x01
45 #define SCSI_REQ_STATUS_RETRY_TYPE_MASK 0x06
46 #define SCSI_REQ_STATUS_RETRY_READ 0x00
47 #define SCSI_REQ_STATUS_RETRY_WRITE 0x02
48 #define SCSI_REQ_STATUS_RETRY_FLUSH 0x04
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 struct iovec iov;
58 QEMUIOVector qiov;
59 uint32_t status;
60 BlockAcctCookie acct;
61 } SCSIDiskReq;
62
63 struct SCSIDiskState
64 {
65 SCSIDevice qdev;
66 BlockDriverState *bs;
67 /* The qemu block layer uses a fixed 512 byte sector size.
68 This is the number of 512 byte blocks in a single scsi sector. */
69 int cluster_size;
70 uint32_t removable;
71 uint64_t max_lba;
72 QEMUBH *bh;
73 char *version;
74 char *serial;
75 };
76
77 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
78 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf);
79
80 static void scsi_free_request(SCSIRequest *req)
81 {
82 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
83
84 qemu_vfree(r->iov.iov_base);
85 }
86
87 /* Helper function for command completion with sense. */
88 static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
89 {
90 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
91 r->req.tag, sense.key, sense.asc, sense.ascq);
92 scsi_req_build_sense(&r->req, sense);
93 scsi_req_complete(&r->req, CHECK_CONDITION);
94 }
95
96 /* Cancel a pending data transfer. */
97 static void scsi_cancel_io(SCSIRequest *req)
98 {
99 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
100
101 DPRINTF("Cancel tag=0x%x\n", req->tag);
102 if (r->req.aiocb) {
103 bdrv_aio_cancel(r->req.aiocb);
104 }
105 r->req.aiocb = NULL;
106 }
107
108 static void scsi_read_complete(void * opaque, int ret)
109 {
110 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
111 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
112 int n;
113
114 if (r->req.aiocb != NULL) {
115 r->req.aiocb = NULL;
116 bdrv_acct_done(s->bs, &r->acct);
117 }
118
119 if (ret) {
120 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_READ)) {
121 return;
122 }
123 }
124
125 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->iov.iov_len);
126
127 n = r->iov.iov_len / 512;
128 r->sector += n;
129 r->sector_count -= n;
130 scsi_req_data(&r->req, r->iov.iov_len);
131 }
132
133 static void scsi_flush_complete(void * opaque, int ret)
134 {
135 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
136 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
137
138 if (r->req.aiocb != NULL) {
139 r->req.aiocb = NULL;
140 bdrv_acct_done(s->bs, &r->acct);
141 }
142
143 if (ret < 0) {
144 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_FLUSH)) {
145 return;
146 }
147 }
148
149 scsi_req_complete(&r->req, GOOD);
150 }
151
152 /* Read more data from scsi device into buffer. */
153 static void scsi_read_data(SCSIRequest *req)
154 {
155 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
156 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
157 uint32_t n;
158
159 if (r->sector_count == (uint32_t)-1) {
160 DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
161 r->sector_count = 0;
162 scsi_req_data(&r->req, r->iov.iov_len);
163 return;
164 }
165 DPRINTF("Read sector_count=%d\n", r->sector_count);
166 if (r->sector_count == 0) {
167 /* This also clears the sense buffer for REQUEST SENSE. */
168 scsi_req_complete(&r->req, GOOD);
169 return;
170 }
171
172 /* No data transfer may already be in progress */
173 assert(r->req.aiocb == NULL);
174
175 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
176 DPRINTF("Data transfer direction invalid\n");
177 scsi_read_complete(r, -EINVAL);
178 return;
179 }
180
181 n = r->sector_count;
182 if (n > SCSI_DMA_BUF_SIZE / 512)
183 n = SCSI_DMA_BUF_SIZE / 512;
184
185 r->iov.iov_len = n * 512;
186 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
187
188 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
189 r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
190 scsi_read_complete, r);
191 if (r->req.aiocb == NULL) {
192 scsi_read_complete(r, -EIO);
193 }
194 }
195
196 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
197 {
198 int is_read = (type == SCSI_REQ_STATUS_RETRY_READ);
199 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
200 BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
201
202 if (action == BLOCK_ERR_IGNORE) {
203 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
204 return 0;
205 }
206
207 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
208 || action == BLOCK_ERR_STOP_ANY) {
209
210 type &= SCSI_REQ_STATUS_RETRY_TYPE_MASK;
211 r->status |= SCSI_REQ_STATUS_RETRY | type;
212
213 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
214 vm_stop(VMSTOP_DISKFULL);
215 } else {
216 switch (error) {
217 case ENOMEM:
218 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
219 break;
220 case EINVAL:
221 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
222 break;
223 default:
224 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
225 break;
226 }
227 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
228 }
229 return 1;
230 }
231
232 static void scsi_write_complete(void * opaque, int ret)
233 {
234 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
235 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
236 uint32_t len;
237 uint32_t n;
238
239 if (r->req.aiocb != NULL) {
240 r->req.aiocb = NULL;
241 bdrv_acct_done(s->bs, &r->acct);
242 }
243
244 if (ret) {
245 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
246 return;
247 }
248 }
249
250 n = r->iov.iov_len / 512;
251 r->sector += n;
252 r->sector_count -= n;
253 if (r->sector_count == 0) {
254 scsi_req_complete(&r->req, GOOD);
255 } else {
256 len = r->sector_count * 512;
257 if (len > SCSI_DMA_BUF_SIZE) {
258 len = SCSI_DMA_BUF_SIZE;
259 }
260 r->iov.iov_len = len;
261 DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
262 scsi_req_data(&r->req, len);
263 }
264 }
265
266 static void scsi_write_data(SCSIRequest *req)
267 {
268 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
269 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
270 uint32_t n;
271
272 /* No data transfer may already be in progress */
273 assert(r->req.aiocb == NULL);
274
275 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
276 DPRINTF("Data transfer direction invalid\n");
277 scsi_write_complete(r, -EINVAL);
278 return;
279 }
280
281 n = r->iov.iov_len / 512;
282 if (n) {
283 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
284
285 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE);
286 r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
287 scsi_write_complete, r);
288 if (r->req.aiocb == NULL) {
289 scsi_write_complete(r, -ENOMEM);
290 }
291 } else {
292 /* Invoke completion routine to fetch data from host. */
293 scsi_write_complete(r, 0);
294 }
295 }
296
297 static void scsi_dma_restart_bh(void *opaque)
298 {
299 SCSIDiskState *s = opaque;
300 SCSIRequest *req;
301 SCSIDiskReq *r;
302
303 qemu_bh_delete(s->bh);
304 s->bh = NULL;
305
306 QTAILQ_FOREACH(req, &s->qdev.requests, next) {
307 r = DO_UPCAST(SCSIDiskReq, req, req);
308 if (r->status & SCSI_REQ_STATUS_RETRY) {
309 int status = r->status;
310 int ret;
311
312 r->status &=
313 ~(SCSI_REQ_STATUS_RETRY | SCSI_REQ_STATUS_RETRY_TYPE_MASK);
314
315 switch (status & SCSI_REQ_STATUS_RETRY_TYPE_MASK) {
316 case SCSI_REQ_STATUS_RETRY_READ:
317 scsi_read_data(&r->req);
318 break;
319 case SCSI_REQ_STATUS_RETRY_WRITE:
320 scsi_write_data(&r->req);
321 break;
322 case SCSI_REQ_STATUS_RETRY_FLUSH:
323 ret = scsi_disk_emulate_command(r, r->iov.iov_base);
324 if (ret == 0) {
325 scsi_req_complete(&r->req, GOOD);
326 }
327 }
328 }
329 }
330 }
331
332 static void scsi_dma_restart_cb(void *opaque, int running, int reason)
333 {
334 SCSIDiskState *s = opaque;
335
336 if (!running)
337 return;
338
339 if (!s->bh) {
340 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
341 qemu_bh_schedule(s->bh);
342 }
343 }
344
345 /* Return a pointer to the data buffer. */
346 static uint8_t *scsi_get_buf(SCSIRequest *req)
347 {
348 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
349
350 return (uint8_t *)r->iov.iov_base;
351 }
352
353 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
354 {
355 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
356 int buflen = 0;
357
358 if (req->cmd.buf[1] & 0x2) {
359 /* Command support data - optional, not implemented */
360 BADF("optional INQUIRY command support request not implemented\n");
361 return -1;
362 }
363
364 if (req->cmd.buf[1] & 0x1) {
365 /* Vital product data */
366 uint8_t page_code = req->cmd.buf[2];
367 if (req->cmd.xfer < 4) {
368 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
369 "less than 4\n", page_code, req->cmd.xfer);
370 return -1;
371 }
372
373 if (s->qdev.type == TYPE_ROM) {
374 outbuf[buflen++] = 5;
375 } else {
376 outbuf[buflen++] = 0;
377 }
378 outbuf[buflen++] = page_code ; // this page
379 outbuf[buflen++] = 0x00;
380
381 switch (page_code) {
382 case 0x00: /* Supported page codes, mandatory */
383 {
384 int pages;
385 DPRINTF("Inquiry EVPD[Supported pages] "
386 "buffer size %zd\n", req->cmd.xfer);
387 pages = buflen++;
388 outbuf[buflen++] = 0x00; // list of supported pages (this page)
389 if (s->serial)
390 outbuf[buflen++] = 0x80; // unit serial number
391 outbuf[buflen++] = 0x83; // device identification
392 if (s->qdev.type == TYPE_DISK) {
393 outbuf[buflen++] = 0xb0; // block limits
394 outbuf[buflen++] = 0xb2; // thin provisioning
395 }
396 outbuf[pages] = buflen - pages - 1; // number of pages
397 break;
398 }
399 case 0x80: /* Device serial number, optional */
400 {
401 int l;
402
403 if (!s->serial) {
404 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
405 return -1;
406 }
407
408 l = strlen(s->serial);
409 if (l > req->cmd.xfer)
410 l = req->cmd.xfer;
411 if (l > 20)
412 l = 20;
413
414 DPRINTF("Inquiry EVPD[Serial number] "
415 "buffer size %zd\n", req->cmd.xfer);
416 outbuf[buflen++] = l;
417 memcpy(outbuf+buflen, s->serial, l);
418 buflen += l;
419 break;
420 }
421
422 case 0x83: /* Device identification page, mandatory */
423 {
424 int max_len = 255 - 8;
425 int id_len = strlen(bdrv_get_device_name(s->bs));
426
427 if (id_len > max_len)
428 id_len = max_len;
429 DPRINTF("Inquiry EVPD[Device identification] "
430 "buffer size %zd\n", req->cmd.xfer);
431
432 outbuf[buflen++] = 4 + id_len;
433 outbuf[buflen++] = 0x2; // ASCII
434 outbuf[buflen++] = 0; // not officially assigned
435 outbuf[buflen++] = 0; // reserved
436 outbuf[buflen++] = id_len; // length of data following
437
438 memcpy(outbuf+buflen, bdrv_get_device_name(s->bs), id_len);
439 buflen += id_len;
440 break;
441 }
442 case 0xb0: /* block limits */
443 {
444 unsigned int unmap_sectors =
445 s->qdev.conf.discard_granularity / s->qdev.blocksize;
446 unsigned int min_io_size =
447 s->qdev.conf.min_io_size / s->qdev.blocksize;
448 unsigned int opt_io_size =
449 s->qdev.conf.opt_io_size / s->qdev.blocksize;
450
451 if (s->qdev.type == TYPE_ROM) {
452 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
453 page_code);
454 return -1;
455 }
456 /* required VPD size with unmap support */
457 outbuf[3] = buflen = 0x3c;
458
459 memset(outbuf + 4, 0, buflen - 4);
460
461 /* optimal transfer length granularity */
462 outbuf[6] = (min_io_size >> 8) & 0xff;
463 outbuf[7] = min_io_size & 0xff;
464
465 /* optimal transfer length */
466 outbuf[12] = (opt_io_size >> 24) & 0xff;
467 outbuf[13] = (opt_io_size >> 16) & 0xff;
468 outbuf[14] = (opt_io_size >> 8) & 0xff;
469 outbuf[15] = opt_io_size & 0xff;
470
471 /* optimal unmap granularity */
472 outbuf[28] = (unmap_sectors >> 24) & 0xff;
473 outbuf[29] = (unmap_sectors >> 16) & 0xff;
474 outbuf[30] = (unmap_sectors >> 8) & 0xff;
475 outbuf[31] = unmap_sectors & 0xff;
476 break;
477 }
478 case 0xb2: /* thin provisioning */
479 {
480 outbuf[3] = buflen = 8;
481 outbuf[4] = 0;
482 outbuf[5] = 0x40; /* write same with unmap supported */
483 outbuf[6] = 0;
484 outbuf[7] = 0;
485 break;
486 }
487 default:
488 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
489 "buffer size %zd\n", page_code, req->cmd.xfer);
490 return -1;
491 }
492 /* done with EVPD */
493 return buflen;
494 }
495
496 /* Standard INQUIRY data */
497 if (req->cmd.buf[2] != 0) {
498 BADF("Error: Inquiry (STANDARD) page or code "
499 "is non-zero [%02X]\n", req->cmd.buf[2]);
500 return -1;
501 }
502
503 /* PAGE CODE == 0 */
504 if (req->cmd.xfer < 5) {
505 BADF("Error: Inquiry (STANDARD) buffer size %zd "
506 "is less than 5\n", req->cmd.xfer);
507 return -1;
508 }
509
510 buflen = req->cmd.xfer;
511 if (buflen > SCSI_MAX_INQUIRY_LEN)
512 buflen = SCSI_MAX_INQUIRY_LEN;
513
514 memset(outbuf, 0, buflen);
515
516 outbuf[0] = s->qdev.type & 0x1f;
517 if (s->qdev.type == TYPE_ROM) {
518 outbuf[1] = 0x80;
519 memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
520 } else {
521 outbuf[1] = s->removable ? 0x80 : 0;
522 memcpy(&outbuf[16], "QEMU HARDDISK ", 16);
523 }
524 memcpy(&outbuf[8], "QEMU ", 8);
525 memset(&outbuf[32], 0, 4);
526 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
527 /*
528 * We claim conformance to SPC-3, which is required for guests
529 * to ask for modern features like READ CAPACITY(16) or the
530 * block characteristics VPD page by default. Not all of SPC-3
531 * is actually implemented, but we're good enough.
532 */
533 outbuf[2] = 5;
534 outbuf[3] = 2; /* Format 2 */
535
536 if (buflen > 36) {
537 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
538 } else {
539 /* If the allocation length of CDB is too small,
540 the additional length is not adjusted */
541 outbuf[4] = 36 - 5;
542 }
543
544 /* Sync data transfer and TCQ. */
545 outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
546 return buflen;
547 }
548
549 static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
550 int page_control)
551 {
552 BlockDriverState *bdrv = s->bs;
553 int cylinders, heads, secs;
554 uint8_t *p = *p_outbuf;
555
556 /*
557 * If Changeable Values are requested, a mask denoting those mode parameters
558 * that are changeable shall be returned. As we currently don't support
559 * parameter changes via MODE_SELECT all bits are returned set to zero.
560 * The buffer was already menset to zero by the caller of this function.
561 */
562 switch (page) {
563 case 4: /* Rigid disk device geometry page. */
564 if (s->qdev.type == TYPE_ROM) {
565 return -1;
566 }
567 p[0] = 4;
568 p[1] = 0x16;
569 if (page_control == 1) { /* Changeable Values */
570 break;
571 }
572 /* if a geometry hint is available, use it */
573 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
574 p[2] = (cylinders >> 16) & 0xff;
575 p[3] = (cylinders >> 8) & 0xff;
576 p[4] = cylinders & 0xff;
577 p[5] = heads & 0xff;
578 /* Write precomp start cylinder, disabled */
579 p[6] = (cylinders >> 16) & 0xff;
580 p[7] = (cylinders >> 8) & 0xff;
581 p[8] = cylinders & 0xff;
582 /* Reduced current start cylinder, disabled */
583 p[9] = (cylinders >> 16) & 0xff;
584 p[10] = (cylinders >> 8) & 0xff;
585 p[11] = cylinders & 0xff;
586 /* Device step rate [ns], 200ns */
587 p[12] = 0;
588 p[13] = 200;
589 /* Landing zone cylinder */
590 p[14] = 0xff;
591 p[15] = 0xff;
592 p[16] = 0xff;
593 /* Medium rotation rate [rpm], 5400 rpm */
594 p[20] = (5400 >> 8) & 0xff;
595 p[21] = 5400 & 0xff;
596 break;
597
598 case 5: /* Flexible disk device geometry page. */
599 if (s->qdev.type == TYPE_ROM) {
600 return -1;
601 }
602 p[0] = 5;
603 p[1] = 0x1e;
604 if (page_control == 1) { /* Changeable Values */
605 break;
606 }
607 /* Transfer rate [kbit/s], 5Mbit/s */
608 p[2] = 5000 >> 8;
609 p[3] = 5000 & 0xff;
610 /* if a geometry hint is available, use it */
611 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
612 p[4] = heads & 0xff;
613 p[5] = secs & 0xff;
614 p[6] = s->cluster_size * 2;
615 p[8] = (cylinders >> 8) & 0xff;
616 p[9] = cylinders & 0xff;
617 /* Write precomp start cylinder, disabled */
618 p[10] = (cylinders >> 8) & 0xff;
619 p[11] = cylinders & 0xff;
620 /* Reduced current start cylinder, disabled */
621 p[12] = (cylinders >> 8) & 0xff;
622 p[13] = cylinders & 0xff;
623 /* Device step rate [100us], 100us */
624 p[14] = 0;
625 p[15] = 1;
626 /* Device step pulse width [us], 1us */
627 p[16] = 1;
628 /* Device head settle delay [100us], 100us */
629 p[17] = 0;
630 p[18] = 1;
631 /* Motor on delay [0.1s], 0.1s */
632 p[19] = 1;
633 /* Motor off delay [0.1s], 0.1s */
634 p[20] = 1;
635 /* Medium rotation rate [rpm], 5400 rpm */
636 p[28] = (5400 >> 8) & 0xff;
637 p[29] = 5400 & 0xff;
638 break;
639
640 case 8: /* Caching page. */
641 p[0] = 8;
642 p[1] = 0x12;
643 if (page_control == 1) { /* Changeable Values */
644 break;
645 }
646 if (bdrv_enable_write_cache(s->bs)) {
647 p[2] = 4; /* WCE */
648 }
649 break;
650
651 case 0x2a: /* CD Capabilities and Mechanical Status page. */
652 if (s->qdev.type != TYPE_ROM) {
653 return -1;
654 }
655 p[0] = 0x2a;
656 p[1] = 0x14;
657 if (page_control == 1) { /* Changeable Values */
658 break;
659 }
660 p[2] = 3; // CD-R & CD-RW read
661 p[3] = 0; // Writing not supported
662 p[4] = 0x7f; /* Audio, composite, digital out,
663 mode 2 form 1&2, multi session */
664 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
665 RW corrected, C2 errors, ISRC,
666 UPC, Bar code */
667 p[6] = 0x2d | (bdrv_is_locked(s->bs)? 2 : 0);
668 /* Locking supported, jumper present, eject, tray */
669 p[7] = 0; /* no volume & mute control, no
670 changer */
671 p[8] = (50 * 176) >> 8; // 50x read speed
672 p[9] = (50 * 176) & 0xff;
673 p[10] = 0 >> 8; // No volume
674 p[11] = 0 & 0xff;
675 p[12] = 2048 >> 8; // 2M buffer
676 p[13] = 2048 & 0xff;
677 p[14] = (16 * 176) >> 8; // 16x read speed current
678 p[15] = (16 * 176) & 0xff;
679 p[18] = (16 * 176) >> 8; // 16x write speed
680 p[19] = (16 * 176) & 0xff;
681 p[20] = (16 * 176) >> 8; // 16x write speed current
682 p[21] = (16 * 176) & 0xff;
683 break;
684
685 default:
686 return -1;
687 }
688
689 *p_outbuf += p[1] + 2;
690 return p[1] + 2;
691 }
692
693 static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
694 {
695 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
696 uint64_t nb_sectors;
697 int page, dbd, buflen, ret, page_control;
698 uint8_t *p;
699 uint8_t dev_specific_param;
700
701 dbd = r->req.cmd.buf[1] & 0x8;
702 page = r->req.cmd.buf[2] & 0x3f;
703 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
704 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
705 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
706 memset(outbuf, 0, r->req.cmd.xfer);
707 p = outbuf;
708
709 if (bdrv_is_read_only(s->bs)) {
710 dev_specific_param = 0x80; /* Readonly. */
711 } else {
712 dev_specific_param = 0x00;
713 }
714
715 if (r->req.cmd.buf[0] == MODE_SENSE) {
716 p[1] = 0; /* Default media type. */
717 p[2] = dev_specific_param;
718 p[3] = 0; /* Block descriptor length. */
719 p += 4;
720 } else { /* MODE_SENSE_10 */
721 p[2] = 0; /* Default media type. */
722 p[3] = dev_specific_param;
723 p[6] = p[7] = 0; /* Block descriptor length. */
724 p += 8;
725 }
726
727 bdrv_get_geometry(s->bs, &nb_sectors);
728 if (!dbd && nb_sectors) {
729 if (r->req.cmd.buf[0] == MODE_SENSE) {
730 outbuf[3] = 8; /* Block descriptor length */
731 } else { /* MODE_SENSE_10 */
732 outbuf[7] = 8; /* Block descriptor length */
733 }
734 nb_sectors /= s->cluster_size;
735 if (nb_sectors > 0xffffff)
736 nb_sectors = 0;
737 p[0] = 0; /* media density code */
738 p[1] = (nb_sectors >> 16) & 0xff;
739 p[2] = (nb_sectors >> 8) & 0xff;
740 p[3] = nb_sectors & 0xff;
741 p[4] = 0; /* reserved */
742 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
743 p[6] = s->cluster_size * 2;
744 p[7] = 0;
745 p += 8;
746 }
747
748 if (page_control == 3) {
749 /* Saved Values */
750 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
751 return -1;
752 }
753
754 if (page == 0x3f) {
755 for (page = 0; page <= 0x3e; page++) {
756 mode_sense_page(s, page, &p, page_control);
757 }
758 } else {
759 ret = mode_sense_page(s, page, &p, page_control);
760 if (ret == -1) {
761 return -1;
762 }
763 }
764
765 buflen = p - outbuf;
766 /*
767 * The mode data length field specifies the length in bytes of the
768 * following data that is available to be transferred. The mode data
769 * length does not include itself.
770 */
771 if (r->req.cmd.buf[0] == MODE_SENSE) {
772 outbuf[0] = buflen - 1;
773 } else { /* MODE_SENSE_10 */
774 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
775 outbuf[1] = (buflen - 2) & 0xff;
776 }
777 if (buflen > r->req.cmd.xfer)
778 buflen = r->req.cmd.xfer;
779 return buflen;
780 }
781
782 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
783 {
784 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
785 int start_track, format, msf, toclen;
786 uint64_t nb_sectors;
787
788 msf = req->cmd.buf[1] & 2;
789 format = req->cmd.buf[2] & 0xf;
790 start_track = req->cmd.buf[6];
791 bdrv_get_geometry(s->bs, &nb_sectors);
792 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
793 nb_sectors /= s->cluster_size;
794 switch (format) {
795 case 0:
796 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
797 break;
798 case 1:
799 /* multi session : only a single session defined */
800 toclen = 12;
801 memset(outbuf, 0, 12);
802 outbuf[1] = 0x0a;
803 outbuf[2] = 0x01;
804 outbuf[3] = 0x01;
805 break;
806 case 2:
807 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
808 break;
809 default:
810 return -1;
811 }
812 if (toclen > req->cmd.xfer)
813 toclen = req->cmd.xfer;
814 return toclen;
815 }
816
817 static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
818 {
819 SCSIRequest *req = &r->req;
820 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
821 bool start = req->cmd.buf[4] & 1;
822 bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
823
824 if (s->qdev.type == TYPE_ROM && loej) {
825 bdrv_eject(s->bs, !start);
826 }
827 }
828
829 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
830 {
831 SCSIRequest *req = &r->req;
832 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
833 uint64_t nb_sectors;
834 int buflen = 0;
835
836 switch (req->cmd.buf[0]) {
837 case TEST_UNIT_READY:
838 if (!bdrv_is_inserted(s->bs))
839 goto not_ready;
840 break;
841 case INQUIRY:
842 buflen = scsi_disk_emulate_inquiry(req, outbuf);
843 if (buflen < 0)
844 goto illegal_request;
845 break;
846 case MODE_SENSE:
847 case MODE_SENSE_10:
848 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
849 if (buflen < 0)
850 goto illegal_request;
851 break;
852 case READ_TOC:
853 buflen = scsi_disk_emulate_read_toc(req, outbuf);
854 if (buflen < 0)
855 goto illegal_request;
856 break;
857 case RESERVE:
858 if (req->cmd.buf[1] & 1)
859 goto illegal_request;
860 break;
861 case RESERVE_10:
862 if (req->cmd.buf[1] & 3)
863 goto illegal_request;
864 break;
865 case RELEASE:
866 if (req->cmd.buf[1] & 1)
867 goto illegal_request;
868 break;
869 case RELEASE_10:
870 if (req->cmd.buf[1] & 3)
871 goto illegal_request;
872 break;
873 case START_STOP:
874 scsi_disk_emulate_start_stop(r);
875 break;
876 case ALLOW_MEDIUM_REMOVAL:
877 bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
878 break;
879 case READ_CAPACITY_10:
880 /* The normal LEN field for this command is zero. */
881 memset(outbuf, 0, 8);
882 bdrv_get_geometry(s->bs, &nb_sectors);
883 if (!nb_sectors)
884 goto not_ready;
885 nb_sectors /= s->cluster_size;
886 /* Returned value is the address of the last sector. */
887 nb_sectors--;
888 /* Remember the new size for read/write sanity checking. */
889 s->max_lba = nb_sectors;
890 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
891 if (nb_sectors > UINT32_MAX)
892 nb_sectors = UINT32_MAX;
893 outbuf[0] = (nb_sectors >> 24) & 0xff;
894 outbuf[1] = (nb_sectors >> 16) & 0xff;
895 outbuf[2] = (nb_sectors >> 8) & 0xff;
896 outbuf[3] = nb_sectors & 0xff;
897 outbuf[4] = 0;
898 outbuf[5] = 0;
899 outbuf[6] = s->cluster_size * 2;
900 outbuf[7] = 0;
901 buflen = 8;
902 break;
903 case GET_CONFIGURATION:
904 memset(outbuf, 0, 8);
905 /* ??? This should probably return much more information. For now
906 just return the basic header indicating the CD-ROM profile. */
907 outbuf[7] = 8; // CD-ROM
908 buflen = 8;
909 break;
910 case SERVICE_ACTION_IN_16:
911 /* Service Action In subcommands. */
912 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
913 DPRINTF("SAI READ CAPACITY(16)\n");
914 memset(outbuf, 0, req->cmd.xfer);
915 bdrv_get_geometry(s->bs, &nb_sectors);
916 if (!nb_sectors)
917 goto not_ready;
918 nb_sectors /= s->cluster_size;
919 /* Returned value is the address of the last sector. */
920 nb_sectors--;
921 /* Remember the new size for read/write sanity checking. */
922 s->max_lba = nb_sectors;
923 outbuf[0] = (nb_sectors >> 56) & 0xff;
924 outbuf[1] = (nb_sectors >> 48) & 0xff;
925 outbuf[2] = (nb_sectors >> 40) & 0xff;
926 outbuf[3] = (nb_sectors >> 32) & 0xff;
927 outbuf[4] = (nb_sectors >> 24) & 0xff;
928 outbuf[5] = (nb_sectors >> 16) & 0xff;
929 outbuf[6] = (nb_sectors >> 8) & 0xff;
930 outbuf[7] = nb_sectors & 0xff;
931 outbuf[8] = 0;
932 outbuf[9] = 0;
933 outbuf[10] = s->cluster_size * 2;
934 outbuf[11] = 0;
935 outbuf[12] = 0;
936 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
937
938 /* set TPE bit if the format supports discard */
939 if (s->qdev.conf.discard_granularity) {
940 outbuf[14] = 0x80;
941 }
942
943 /* Protection, exponent and lowest lba field left blank. */
944 buflen = req->cmd.xfer;
945 break;
946 }
947 DPRINTF("Unsupported Service Action In\n");
948 goto illegal_request;
949 case VERIFY_10:
950 break;
951 default:
952 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
953 return -1;
954 }
955 return buflen;
956
957 not_ready:
958 if (!bdrv_is_inserted(s->bs)) {
959 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
960 } else {
961 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
962 }
963 return -1;
964
965 illegal_request:
966 if (r->req.status == -1) {
967 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
968 }
969 return -1;
970 }
971
972 /* Execute a scsi command. Returns the length of the data expected by the
973 command. This will be Positive for data transfers from the device
974 (eg. disk reads), negative for transfers to the device (eg. disk writes),
975 and zero if the command does not transfer any data. */
976
977 static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
978 {
979 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
980 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
981 int32_t len;
982 uint8_t command;
983 uint8_t *outbuf;
984 int rc;
985
986 command = buf[0];
987 outbuf = (uint8_t *)r->iov.iov_base;
988 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
989
990 #ifdef DEBUG_SCSI
991 {
992 int i;
993 for (i = 1; i < r->req.cmd.len; i++) {
994 printf(" 0x%02x", buf[i]);
995 }
996 printf("\n");
997 }
998 #endif
999
1000 switch (command) {
1001 case TEST_UNIT_READY:
1002 case INQUIRY:
1003 case MODE_SENSE:
1004 case MODE_SENSE_10:
1005 case RESERVE:
1006 case RESERVE_10:
1007 case RELEASE:
1008 case RELEASE_10:
1009 case START_STOP:
1010 case ALLOW_MEDIUM_REMOVAL:
1011 case READ_CAPACITY_10:
1012 case READ_TOC:
1013 case GET_CONFIGURATION:
1014 case SERVICE_ACTION_IN_16:
1015 case VERIFY_10:
1016 rc = scsi_disk_emulate_command(r, outbuf);
1017 if (rc < 0) {
1018 return 0;
1019 }
1020
1021 r->iov.iov_len = rc;
1022 break;
1023 case SYNCHRONIZE_CACHE:
1024 bdrv_acct_start(s->bs, &r->acct, 0, BDRV_ACCT_FLUSH);
1025 r->req.aiocb = bdrv_aio_flush(s->bs, scsi_flush_complete, r);
1026 if (r->req.aiocb == NULL) {
1027 scsi_flush_complete(r, -EIO);
1028 }
1029 return 0;
1030 case READ_6:
1031 case READ_10:
1032 case READ_12:
1033 case READ_16:
1034 len = r->req.cmd.xfer / s->qdev.blocksize;
1035 DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1036 if (r->req.cmd.lba > s->max_lba)
1037 goto illegal_lba;
1038 r->sector = r->req.cmd.lba * s->cluster_size;
1039 r->sector_count = len * s->cluster_size;
1040 break;
1041 case WRITE_6:
1042 case WRITE_10:
1043 case WRITE_12:
1044 case WRITE_16:
1045 case WRITE_VERIFY_10:
1046 case WRITE_VERIFY_12:
1047 case WRITE_VERIFY_16:
1048 len = r->req.cmd.xfer / s->qdev.blocksize;
1049 DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
1050 (command & 0xe) == 0xe ? "And Verify " : "",
1051 r->req.cmd.lba, len);
1052 if (r->req.cmd.lba > s->max_lba)
1053 goto illegal_lba;
1054 r->sector = r->req.cmd.lba * s->cluster_size;
1055 r->sector_count = len * s->cluster_size;
1056 break;
1057 case MODE_SELECT:
1058 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1059 /* We don't support mode parameter changes.
1060 Allow the mode parameter header + block descriptors only. */
1061 if (r->req.cmd.xfer > 12) {
1062 goto fail;
1063 }
1064 break;
1065 case MODE_SELECT_10:
1066 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1067 /* We don't support mode parameter changes.
1068 Allow the mode parameter header + block descriptors only. */
1069 if (r->req.cmd.xfer > 16) {
1070 goto fail;
1071 }
1072 break;
1073 case SEEK_6:
1074 case SEEK_10:
1075 DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
1076 r->req.cmd.lba);
1077 if (r->req.cmd.lba > s->max_lba) {
1078 goto illegal_lba;
1079 }
1080 break;
1081 case WRITE_SAME_16:
1082 len = r->req.cmd.xfer / s->qdev.blocksize;
1083
1084 DPRINTF("WRITE SAME(16) (sector %" PRId64 ", count %d)\n",
1085 r->req.cmd.lba, len);
1086
1087 if (r->req.cmd.lba > s->max_lba) {
1088 goto illegal_lba;
1089 }
1090
1091 /*
1092 * We only support WRITE SAME with the unmap bit set for now.
1093 */
1094 if (!(buf[1] & 0x8)) {
1095 goto fail;
1096 }
1097
1098 rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1099 len * s->cluster_size);
1100 if (rc < 0) {
1101 /* XXX: better error code ?*/
1102 goto fail;
1103 }
1104
1105 break;
1106 case REQUEST_SENSE:
1107 abort();
1108 default:
1109 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1110 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
1111 return 0;
1112 fail:
1113 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1114 return 0;
1115 illegal_lba:
1116 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1117 return 0;
1118 }
1119 if (r->sector_count == 0 && r->iov.iov_len == 0) {
1120 scsi_req_complete(&r->req, GOOD);
1121 }
1122 len = r->sector_count * 512 + r->iov.iov_len;
1123 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1124 return -len;
1125 } else {
1126 if (!r->sector_count)
1127 r->sector_count = -1;
1128 return len;
1129 }
1130 }
1131
1132 static void scsi_disk_reset(DeviceState *dev)
1133 {
1134 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1135 uint64_t nb_sectors;
1136
1137 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
1138
1139 bdrv_get_geometry(s->bs, &nb_sectors);
1140 nb_sectors /= s->cluster_size;
1141 if (nb_sectors) {
1142 nb_sectors--;
1143 }
1144 s->max_lba = nb_sectors;
1145 }
1146
1147 static void scsi_destroy(SCSIDevice *dev)
1148 {
1149 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1150
1151 scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
1152 blockdev_mark_auto_del(s->qdev.conf.bs);
1153 }
1154
1155 static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
1156 {
1157 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1158 DriveInfo *dinfo;
1159
1160 if (!s->qdev.conf.bs) {
1161 error_report("scsi-disk: drive property not set");
1162 return -1;
1163 }
1164 s->bs = s->qdev.conf.bs;
1165
1166 if (scsi_type == TYPE_DISK && !bdrv_is_inserted(s->bs)) {
1167 error_report("Device needs media, but drive is empty");
1168 return -1;
1169 }
1170
1171 if (!s->serial) {
1172 /* try to fall back to value set with legacy -drive serial=... */
1173 dinfo = drive_get_by_blockdev(s->bs);
1174 if (*dinfo->serial) {
1175 s->serial = g_strdup(dinfo->serial);
1176 }
1177 }
1178
1179 if (!s->version) {
1180 s->version = g_strdup(QEMU_VERSION);
1181 }
1182
1183 if (bdrv_is_sg(s->bs)) {
1184 error_report("scsi-disk: unwanted /dev/sg*");
1185 return -1;
1186 }
1187
1188 if (scsi_type == TYPE_ROM) {
1189 s->qdev.blocksize = 2048;
1190 } else if (scsi_type == TYPE_DISK) {
1191 s->qdev.blocksize = s->qdev.conf.logical_block_size;
1192 } else {
1193 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
1194 return -1;
1195 }
1196 s->cluster_size = s->qdev.blocksize / 512;
1197 s->bs->buffer_alignment = s->qdev.blocksize;
1198
1199 s->qdev.type = scsi_type;
1200 qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
1201 bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
1202 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
1203 return 0;
1204 }
1205
1206 static int scsi_hd_initfn(SCSIDevice *dev)
1207 {
1208 return scsi_initfn(dev, TYPE_DISK);
1209 }
1210
1211 static int scsi_cd_initfn(SCSIDevice *dev)
1212 {
1213 return scsi_initfn(dev, TYPE_ROM);
1214 }
1215
1216 static int scsi_disk_initfn(SCSIDevice *dev)
1217 {
1218 DriveInfo *dinfo;
1219 uint8_t scsi_type;
1220
1221 if (!dev->conf.bs) {
1222 scsi_type = TYPE_DISK; /* will die in scsi_initfn() */
1223 } else {
1224 dinfo = drive_get_by_blockdev(dev->conf.bs);
1225 scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
1226 }
1227
1228 return scsi_initfn(dev, scsi_type);
1229 }
1230
1231 static SCSIReqOps scsi_disk_reqops = {
1232 .size = sizeof(SCSIDiskReq),
1233 .free_req = scsi_free_request,
1234 .send_command = scsi_send_command,
1235 .read_data = scsi_read_data,
1236 .write_data = scsi_write_data,
1237 .cancel_io = scsi_cancel_io,
1238 .get_buf = scsi_get_buf,
1239 };
1240
1241 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
1242 uint32_t lun, void *hba_private)
1243 {
1244 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1245 SCSIRequest *req;
1246 SCSIDiskReq *r;
1247
1248 req = scsi_req_alloc(&scsi_disk_reqops, &s->qdev, tag, lun, hba_private);
1249 r = DO_UPCAST(SCSIDiskReq, req, req);
1250 r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
1251 return req;
1252 }
1253
1254 #define DEFINE_SCSI_DISK_PROPERTIES() \
1255 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1256 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1257 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1258
1259 static SCSIDeviceInfo scsi_disk_info[] = {
1260 {
1261 .qdev.name = "scsi-hd",
1262 .qdev.fw_name = "disk",
1263 .qdev.desc = "virtual SCSI disk",
1264 .qdev.size = sizeof(SCSIDiskState),
1265 .qdev.reset = scsi_disk_reset,
1266 .init = scsi_hd_initfn,
1267 .destroy = scsi_destroy,
1268 .alloc_req = scsi_new_request,
1269 .qdev.props = (Property[]) {
1270 DEFINE_SCSI_DISK_PROPERTIES(),
1271 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1272 DEFINE_PROP_END_OF_LIST(),
1273 }
1274 },{
1275 .qdev.name = "scsi-cd",
1276 .qdev.fw_name = "disk",
1277 .qdev.desc = "virtual SCSI CD-ROM",
1278 .qdev.size = sizeof(SCSIDiskState),
1279 .qdev.reset = scsi_disk_reset,
1280 .init = scsi_cd_initfn,
1281 .destroy = scsi_destroy,
1282 .alloc_req = scsi_new_request,
1283 .qdev.props = (Property[]) {
1284 DEFINE_SCSI_DISK_PROPERTIES(),
1285 DEFINE_PROP_END_OF_LIST(),
1286 },
1287 },{
1288 .qdev.name = "scsi-disk", /* legacy -device scsi-disk */
1289 .qdev.fw_name = "disk",
1290 .qdev.desc = "virtual SCSI disk or CD-ROM (legacy)",
1291 .qdev.size = sizeof(SCSIDiskState),
1292 .qdev.reset = scsi_disk_reset,
1293 .init = scsi_disk_initfn,
1294 .destroy = scsi_destroy,
1295 .alloc_req = scsi_new_request,
1296 .qdev.props = (Property[]) {
1297 DEFINE_SCSI_DISK_PROPERTIES(),
1298 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1299 DEFINE_PROP_END_OF_LIST(),
1300 }
1301 }
1302 };
1303
1304 static void scsi_disk_register_devices(void)
1305 {
1306 int i;
1307
1308 for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1309 scsi_qdev_register(&scsi_disk_info[i]);
1310 }
1311 }
1312 device_init(scsi_disk_register_devices)