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