2 * SCSI Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
7 * Written by Paul Brook
9 * 2009-Dec-12 Artyom Tarasenko : implemented stamdard inquiry for the case
10 * when the allocation length of CDB is smaller
12 * 2009-Oct-13 Artyom Tarasenko : implemented the block descriptor in the
13 * MODE SENSE response.
15 * This code is licensed under the LGPL.
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.
25 #define DPRINTF(fmt, ...) \
26 do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
28 #define DPRINTF(fmt, ...) do {} while(0)
31 #define BADF(fmt, ...) \
32 do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
34 #include "qemu-common.h"
35 #include "qemu-error.h"
37 #include "scsi-defs.h"
40 #include "block_int.h"
42 #define SCSI_DMA_BUF_SIZE 131072
43 #define SCSI_MAX_INQUIRY_LEN 256
45 #define SCSI_REQ_STATUS_RETRY 0x01
46 #define SCSI_REQ_STATUS_RETRY_TYPE_MASK 0x06
47 #define SCSI_REQ_STATUS_RETRY_READ 0x00
48 #define SCSI_REQ_STATUS_RETRY_WRITE 0x02
49 #define SCSI_REQ_STATUS_RETRY_FLUSH 0x04
51 typedef struct SCSIDiskState SCSIDiskState
;
53 typedef struct SCSIDiskReq
{
55 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
57 uint32_t sector_count
;
68 /* The qemu block layer uses a fixed 512 byte sector size.
69 This is the number of 512 byte blocks in a single scsi sector. */
80 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
);
81 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
);
83 static void scsi_free_request(SCSIRequest
*req
)
85 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
87 qemu_vfree(r
->iov
.iov_base
);
90 /* Helper function for command completion with sense. */
91 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
93 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
94 r
->req
.tag
, sense
.key
, sense
.asc
, sense
.ascq
);
95 scsi_req_build_sense(&r
->req
, sense
);
96 scsi_req_complete(&r
->req
, CHECK_CONDITION
);
99 /* Cancel a pending data transfer. */
100 static void scsi_cancel_io(SCSIRequest
*req
)
102 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
104 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
106 bdrv_aio_cancel(r
->req
.aiocb
);
111 static void scsi_read_complete(void * opaque
, int ret
)
113 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
114 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
117 if (r
->req
.aiocb
!= NULL
) {
119 bdrv_acct_done(s
->bs
, &r
->acct
);
123 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_READ
)) {
128 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->iov
.iov_len
);
130 n
= r
->iov
.iov_len
/ 512;
132 r
->sector_count
-= n
;
133 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
136 static void scsi_flush_complete(void * opaque
, int ret
)
138 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
139 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
141 if (r
->req
.aiocb
!= NULL
) {
143 bdrv_acct_done(s
->bs
, &r
->acct
);
147 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
152 scsi_req_complete(&r
->req
, GOOD
);
155 /* Read more data from scsi device into buffer. */
156 static void scsi_read_data(SCSIRequest
*req
)
158 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
159 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
162 if (r
->sector_count
== (uint32_t)-1) {
163 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
165 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
168 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
169 if (r
->sector_count
== 0) {
170 /* This also clears the sense buffer for REQUEST SENSE. */
171 scsi_req_complete(&r
->req
, GOOD
);
175 /* No data transfer may already be in progress */
176 assert(r
->req
.aiocb
== NULL
);
178 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
179 DPRINTF("Data transfer direction invalid\n");
180 scsi_read_complete(r
, -EINVAL
);
185 if (n
> SCSI_DMA_BUF_SIZE
/ 512)
186 n
= SCSI_DMA_BUF_SIZE
/ 512;
189 scsi_read_complete(r
, -ENOMEDIUM
);
191 r
->iov
.iov_len
= n
* 512;
192 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
194 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_READ
);
195 r
->req
.aiocb
= bdrv_aio_readv(s
->bs
, r
->sector
, &r
->qiov
, n
,
196 scsi_read_complete
, r
);
197 if (r
->req
.aiocb
== NULL
) {
198 scsi_read_complete(r
, -EIO
);
202 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
204 int is_read
= (type
== SCSI_REQ_STATUS_RETRY_READ
);
205 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
206 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
208 if (action
== BLOCK_ERR_IGNORE
) {
209 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
213 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
214 || action
== BLOCK_ERR_STOP_ANY
) {
216 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
217 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
219 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
220 vm_stop(VMSTOP_DISKFULL
);
224 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
227 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
230 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
233 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
238 static void scsi_write_complete(void * opaque
, int ret
)
240 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
241 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
245 if (r
->req
.aiocb
!= NULL
) {
247 bdrv_acct_done(s
->bs
, &r
->acct
);
251 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
256 n
= r
->iov
.iov_len
/ 512;
258 r
->sector_count
-= n
;
259 if (r
->sector_count
== 0) {
260 scsi_req_complete(&r
->req
, GOOD
);
262 len
= r
->sector_count
* 512;
263 if (len
> SCSI_DMA_BUF_SIZE
) {
264 len
= SCSI_DMA_BUF_SIZE
;
266 r
->iov
.iov_len
= len
;
267 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, len
);
268 scsi_req_data(&r
->req
, len
);
272 static void scsi_write_data(SCSIRequest
*req
)
274 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
275 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
278 /* No data transfer may already be in progress */
279 assert(r
->req
.aiocb
== NULL
);
281 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
282 DPRINTF("Data transfer direction invalid\n");
283 scsi_write_complete(r
, -EINVAL
);
287 n
= r
->iov
.iov_len
/ 512;
290 scsi_write_complete(r
, -ENOMEDIUM
);
292 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
294 bdrv_acct_start(s
->bs
, &r
->acct
, n
* BDRV_SECTOR_SIZE
, BDRV_ACCT_WRITE
);
295 r
->req
.aiocb
= bdrv_aio_writev(s
->bs
, r
->sector
, &r
->qiov
, n
,
296 scsi_write_complete
, r
);
297 if (r
->req
.aiocb
== NULL
) {
298 scsi_write_complete(r
, -ENOMEM
);
301 /* Invoke completion routine to fetch data from host. */
302 scsi_write_complete(r
, 0);
306 static void scsi_dma_restart_bh(void *opaque
)
308 SCSIDiskState
*s
= opaque
;
312 qemu_bh_delete(s
->bh
);
315 QTAILQ_FOREACH(req
, &s
->qdev
.requests
, next
) {
316 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
317 if (r
->status
& SCSI_REQ_STATUS_RETRY
) {
318 int status
= r
->status
;
322 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
324 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
325 case SCSI_REQ_STATUS_RETRY_READ
:
326 scsi_read_data(&r
->req
);
328 case SCSI_REQ_STATUS_RETRY_WRITE
:
329 scsi_write_data(&r
->req
);
331 case SCSI_REQ_STATUS_RETRY_FLUSH
:
332 ret
= scsi_disk_emulate_command(r
, r
->iov
.iov_base
);
334 scsi_req_complete(&r
->req
, GOOD
);
341 static void scsi_dma_restart_cb(void *opaque
, int running
, int reason
)
343 SCSIDiskState
*s
= opaque
;
349 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
350 qemu_bh_schedule(s
->bh
);
354 /* Return a pointer to the data buffer. */
355 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
357 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
359 return (uint8_t *)r
->iov
.iov_base
;
362 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
364 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
367 if (req
->cmd
.buf
[1] & 0x2) {
368 /* Command support data - optional, not implemented */
369 BADF("optional INQUIRY command support request not implemented\n");
373 if (req
->cmd
.buf
[1] & 0x1) {
374 /* Vital product data */
375 uint8_t page_code
= req
->cmd
.buf
[2];
376 if (req
->cmd
.xfer
< 4) {
377 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
378 "less than 4\n", page_code
, req
->cmd
.xfer
);
382 if (s
->qdev
.type
== TYPE_ROM
) {
383 outbuf
[buflen
++] = 5;
385 outbuf
[buflen
++] = 0;
387 outbuf
[buflen
++] = page_code
; // this page
388 outbuf
[buflen
++] = 0x00;
391 case 0x00: /* Supported page codes, mandatory */
394 DPRINTF("Inquiry EVPD[Supported pages] "
395 "buffer size %zd\n", req
->cmd
.xfer
);
397 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
399 outbuf
[buflen
++] = 0x80; // unit serial number
400 outbuf
[buflen
++] = 0x83; // device identification
401 if (s
->qdev
.type
== TYPE_DISK
) {
402 outbuf
[buflen
++] = 0xb0; // block limits
403 outbuf
[buflen
++] = 0xb2; // thin provisioning
405 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
408 case 0x80: /* Device serial number, optional */
413 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
417 l
= strlen(s
->serial
);
418 if (l
> req
->cmd
.xfer
)
423 DPRINTF("Inquiry EVPD[Serial number] "
424 "buffer size %zd\n", req
->cmd
.xfer
);
425 outbuf
[buflen
++] = l
;
426 memcpy(outbuf
+buflen
, s
->serial
, l
);
431 case 0x83: /* Device identification page, mandatory */
433 int max_len
= 255 - 8;
434 int id_len
= strlen(bdrv_get_device_name(s
->bs
));
436 if (id_len
> max_len
)
438 DPRINTF("Inquiry EVPD[Device identification] "
439 "buffer size %zd\n", req
->cmd
.xfer
);
441 outbuf
[buflen
++] = 4 + id_len
;
442 outbuf
[buflen
++] = 0x2; // ASCII
443 outbuf
[buflen
++] = 0; // not officially assigned
444 outbuf
[buflen
++] = 0; // reserved
445 outbuf
[buflen
++] = id_len
; // length of data following
447 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->bs
), id_len
);
451 case 0xb0: /* block limits */
453 unsigned int unmap_sectors
=
454 s
->qdev
.conf
.discard_granularity
/ s
->qdev
.blocksize
;
455 unsigned int min_io_size
=
456 s
->qdev
.conf
.min_io_size
/ s
->qdev
.blocksize
;
457 unsigned int opt_io_size
=
458 s
->qdev
.conf
.opt_io_size
/ s
->qdev
.blocksize
;
460 if (s
->qdev
.type
== TYPE_ROM
) {
461 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
465 /* required VPD size with unmap support */
466 outbuf
[3] = buflen
= 0x3c;
468 memset(outbuf
+ 4, 0, buflen
- 4);
470 /* optimal transfer length granularity */
471 outbuf
[6] = (min_io_size
>> 8) & 0xff;
472 outbuf
[7] = min_io_size
& 0xff;
474 /* optimal transfer length */
475 outbuf
[12] = (opt_io_size
>> 24) & 0xff;
476 outbuf
[13] = (opt_io_size
>> 16) & 0xff;
477 outbuf
[14] = (opt_io_size
>> 8) & 0xff;
478 outbuf
[15] = opt_io_size
& 0xff;
480 /* optimal unmap granularity */
481 outbuf
[28] = (unmap_sectors
>> 24) & 0xff;
482 outbuf
[29] = (unmap_sectors
>> 16) & 0xff;
483 outbuf
[30] = (unmap_sectors
>> 8) & 0xff;
484 outbuf
[31] = unmap_sectors
& 0xff;
487 case 0xb2: /* thin provisioning */
489 outbuf
[3] = buflen
= 8;
491 outbuf
[5] = 0x40; /* write same with unmap supported */
497 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
498 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
505 /* Standard INQUIRY data */
506 if (req
->cmd
.buf
[2] != 0) {
507 BADF("Error: Inquiry (STANDARD) page or code "
508 "is non-zero [%02X]\n", req
->cmd
.buf
[2]);
513 if (req
->cmd
.xfer
< 5) {
514 BADF("Error: Inquiry (STANDARD) buffer size %zd "
515 "is less than 5\n", req
->cmd
.xfer
);
519 buflen
= req
->cmd
.xfer
;
520 if (buflen
> SCSI_MAX_INQUIRY_LEN
)
521 buflen
= SCSI_MAX_INQUIRY_LEN
;
523 memset(outbuf
, 0, buflen
);
525 outbuf
[0] = s
->qdev
.type
& 0x1f;
526 if (s
->qdev
.type
== TYPE_ROM
) {
528 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
530 outbuf
[1] = s
->removable
? 0x80 : 0;
531 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
533 memcpy(&outbuf
[8], "QEMU ", 8);
534 memset(&outbuf
[32], 0, 4);
535 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
537 * We claim conformance to SPC-3, which is required for guests
538 * to ask for modern features like READ CAPACITY(16) or the
539 * block characteristics VPD page by default. Not all of SPC-3
540 * is actually implemented, but we're good enough.
543 outbuf
[3] = 2; /* Format 2 */
546 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
548 /* If the allocation length of CDB is too small,
549 the additional length is not adjusted */
553 /* Sync data transfer and TCQ. */
554 outbuf
[7] = 0x10 | (req
->bus
->tcq
? 0x02 : 0);
558 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
561 BlockDriverState
*bdrv
= s
->bs
;
562 int cylinders
, heads
, secs
;
563 uint8_t *p
= *p_outbuf
;
566 * If Changeable Values are requested, a mask denoting those mode parameters
567 * that are changeable shall be returned. As we currently don't support
568 * parameter changes via MODE_SELECT all bits are returned set to zero.
569 * The buffer was already menset to zero by the caller of this function.
572 case 4: /* Rigid disk device geometry page. */
573 if (s
->qdev
.type
== TYPE_ROM
) {
578 if (page_control
== 1) { /* Changeable Values */
581 /* if a geometry hint is available, use it */
582 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
583 p
[2] = (cylinders
>> 16) & 0xff;
584 p
[3] = (cylinders
>> 8) & 0xff;
585 p
[4] = cylinders
& 0xff;
587 /* Write precomp start cylinder, disabled */
588 p
[6] = (cylinders
>> 16) & 0xff;
589 p
[7] = (cylinders
>> 8) & 0xff;
590 p
[8] = cylinders
& 0xff;
591 /* Reduced current start cylinder, disabled */
592 p
[9] = (cylinders
>> 16) & 0xff;
593 p
[10] = (cylinders
>> 8) & 0xff;
594 p
[11] = cylinders
& 0xff;
595 /* Device step rate [ns], 200ns */
598 /* Landing zone cylinder */
602 /* Medium rotation rate [rpm], 5400 rpm */
603 p
[20] = (5400 >> 8) & 0xff;
607 case 5: /* Flexible disk device geometry page. */
608 if (s
->qdev
.type
== TYPE_ROM
) {
613 if (page_control
== 1) { /* Changeable Values */
616 /* Transfer rate [kbit/s], 5Mbit/s */
619 /* if a geometry hint is available, use it */
620 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
623 p
[6] = s
->cluster_size
* 2;
624 p
[8] = (cylinders
>> 8) & 0xff;
625 p
[9] = cylinders
& 0xff;
626 /* Write precomp start cylinder, disabled */
627 p
[10] = (cylinders
>> 8) & 0xff;
628 p
[11] = cylinders
& 0xff;
629 /* Reduced current start cylinder, disabled */
630 p
[12] = (cylinders
>> 8) & 0xff;
631 p
[13] = cylinders
& 0xff;
632 /* Device step rate [100us], 100us */
635 /* Device step pulse width [us], 1us */
637 /* Device head settle delay [100us], 100us */
640 /* Motor on delay [0.1s], 0.1s */
642 /* Motor off delay [0.1s], 0.1s */
644 /* Medium rotation rate [rpm], 5400 rpm */
645 p
[28] = (5400 >> 8) & 0xff;
649 case 8: /* Caching page. */
652 if (page_control
== 1) { /* Changeable Values */
655 if (bdrv_enable_write_cache(s
->bs
)) {
660 case 0x2a: /* CD Capabilities and Mechanical Status page. */
661 if (s
->qdev
.type
!= TYPE_ROM
) {
666 if (page_control
== 1) { /* Changeable Values */
669 p
[2] = 3; // CD-R & CD-RW read
670 p
[3] = 0; // Writing not supported
671 p
[4] = 0x7f; /* Audio, composite, digital out,
672 mode 2 form 1&2, multi session */
673 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
674 RW corrected, C2 errors, ISRC,
676 p
[6] = 0x2d | (s
->tray_locked
? 2 : 0);
677 /* Locking supported, jumper present, eject, tray */
678 p
[7] = 0; /* no volume & mute control, no
680 p
[8] = (50 * 176) >> 8; // 50x read speed
681 p
[9] = (50 * 176) & 0xff;
682 p
[10] = 0 >> 8; // No volume
684 p
[12] = 2048 >> 8; // 2M buffer
686 p
[14] = (16 * 176) >> 8; // 16x read speed current
687 p
[15] = (16 * 176) & 0xff;
688 p
[18] = (16 * 176) >> 8; // 16x write speed
689 p
[19] = (16 * 176) & 0xff;
690 p
[20] = (16 * 176) >> 8; // 16x write speed current
691 p
[21] = (16 * 176) & 0xff;
698 *p_outbuf
+= p
[1] + 2;
702 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
704 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
706 int page
, dbd
, buflen
, ret
, page_control
;
708 uint8_t dev_specific_param
;
710 dbd
= r
->req
.cmd
.buf
[1] & 0x8;
711 page
= r
->req
.cmd
.buf
[2] & 0x3f;
712 page_control
= (r
->req
.cmd
.buf
[2] & 0xc0) >> 6;
713 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
714 (r
->req
.cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, r
->req
.cmd
.xfer
, page_control
);
715 memset(outbuf
, 0, r
->req
.cmd
.xfer
);
718 if (bdrv_is_read_only(s
->bs
)) {
719 dev_specific_param
= 0x80; /* Readonly. */
721 dev_specific_param
= 0x00;
724 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
725 p
[1] = 0; /* Default media type. */
726 p
[2] = dev_specific_param
;
727 p
[3] = 0; /* Block descriptor length. */
729 } else { /* MODE_SENSE_10 */
730 p
[2] = 0; /* Default media type. */
731 p
[3] = dev_specific_param
;
732 p
[6] = p
[7] = 0; /* Block descriptor length. */
736 bdrv_get_geometry(s
->bs
, &nb_sectors
);
737 if (!dbd
&& nb_sectors
) {
738 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
739 outbuf
[3] = 8; /* Block descriptor length */
740 } else { /* MODE_SENSE_10 */
741 outbuf
[7] = 8; /* Block descriptor length */
743 nb_sectors
/= s
->cluster_size
;
744 if (nb_sectors
> 0xffffff)
746 p
[0] = 0; /* media density code */
747 p
[1] = (nb_sectors
>> 16) & 0xff;
748 p
[2] = (nb_sectors
>> 8) & 0xff;
749 p
[3] = nb_sectors
& 0xff;
750 p
[4] = 0; /* reserved */
751 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
752 p
[6] = s
->cluster_size
* 2;
757 if (page_control
== 3) {
759 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
764 for (page
= 0; page
<= 0x3e; page
++) {
765 mode_sense_page(s
, page
, &p
, page_control
);
768 ret
= mode_sense_page(s
, page
, &p
, page_control
);
776 * The mode data length field specifies the length in bytes of the
777 * following data that is available to be transferred. The mode data
778 * length does not include itself.
780 if (r
->req
.cmd
.buf
[0] == MODE_SENSE
) {
781 outbuf
[0] = buflen
- 1;
782 } else { /* MODE_SENSE_10 */
783 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
784 outbuf
[1] = (buflen
- 2) & 0xff;
786 if (buflen
> r
->req
.cmd
.xfer
)
787 buflen
= r
->req
.cmd
.xfer
;
791 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
793 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
794 int start_track
, format
, msf
, toclen
;
797 msf
= req
->cmd
.buf
[1] & 2;
798 format
= req
->cmd
.buf
[2] & 0xf;
799 start_track
= req
->cmd
.buf
[6];
800 bdrv_get_geometry(s
->bs
, &nb_sectors
);
801 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
802 nb_sectors
/= s
->cluster_size
;
805 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
808 /* multi session : only a single session defined */
810 memset(outbuf
, 0, 12);
816 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
821 if (toclen
> req
->cmd
.xfer
)
822 toclen
= req
->cmd
.xfer
;
826 static int scsi_disk_emulate_start_stop(SCSIDiskReq
*r
)
828 SCSIRequest
*req
= &r
->req
;
829 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
830 bool start
= req
->cmd
.buf
[4] & 1;
831 bool loej
= req
->cmd
.buf
[4] & 2; /* load on start, eject on !start */
833 if (s
->qdev
.type
== TYPE_ROM
&& loej
) {
834 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
835 scsi_check_condition(r
,
836 bdrv_is_inserted(s
->bs
)
837 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED
)
838 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED
));
841 bdrv_eject(s
->bs
, !start
);
842 s
->tray_open
= !start
;
847 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
)
849 SCSIRequest
*req
= &r
->req
;
850 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
854 switch (req
->cmd
.buf
[0]) {
855 case TEST_UNIT_READY
:
856 if (s
->tray_open
|| !bdrv_is_inserted(s
->bs
))
860 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
862 goto illegal_request
;
866 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
868 goto illegal_request
;
871 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
873 goto illegal_request
;
876 if (req
->cmd
.buf
[1] & 1)
877 goto illegal_request
;
880 if (req
->cmd
.buf
[1] & 3)
881 goto illegal_request
;
884 if (req
->cmd
.buf
[1] & 1)
885 goto illegal_request
;
888 if (req
->cmd
.buf
[1] & 3)
889 goto illegal_request
;
892 if (scsi_disk_emulate_start_stop(r
) < 0) {
896 case ALLOW_MEDIUM_REMOVAL
:
897 s
->tray_locked
= req
->cmd
.buf
[4] & 1;
898 bdrv_lock_medium(s
->bs
, req
->cmd
.buf
[4] & 1);
900 case READ_CAPACITY_10
:
901 /* The normal LEN field for this command is zero. */
902 memset(outbuf
, 0, 8);
903 bdrv_get_geometry(s
->bs
, &nb_sectors
);
906 nb_sectors
/= s
->cluster_size
;
907 /* Returned value is the address of the last sector. */
909 /* Remember the new size for read/write sanity checking. */
910 s
->max_lba
= nb_sectors
;
911 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
912 if (nb_sectors
> UINT32_MAX
)
913 nb_sectors
= UINT32_MAX
;
914 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
915 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
916 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
917 outbuf
[3] = nb_sectors
& 0xff;
920 outbuf
[6] = s
->cluster_size
* 2;
924 case GET_CONFIGURATION
:
925 memset(outbuf
, 0, 8);
926 /* ??? This should probably return much more information. For now
927 just return the basic header indicating the CD-ROM profile. */
928 outbuf
[7] = 8; // CD-ROM
931 case SERVICE_ACTION_IN_16
:
932 /* Service Action In subcommands. */
933 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
934 DPRINTF("SAI READ CAPACITY(16)\n");
935 memset(outbuf
, 0, req
->cmd
.xfer
);
936 bdrv_get_geometry(s
->bs
, &nb_sectors
);
939 nb_sectors
/= s
->cluster_size
;
940 /* Returned value is the address of the last sector. */
942 /* Remember the new size for read/write sanity checking. */
943 s
->max_lba
= nb_sectors
;
944 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
945 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
946 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
947 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
948 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
949 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
950 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
951 outbuf
[7] = nb_sectors
& 0xff;
954 outbuf
[10] = s
->cluster_size
* 2;
957 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
959 /* set TPE bit if the format supports discard */
960 if (s
->qdev
.conf
.discard_granularity
) {
964 /* Protection, exponent and lowest lba field left blank. */
965 buflen
= req
->cmd
.xfer
;
968 DPRINTF("Unsupported Service Action In\n");
969 goto illegal_request
;
973 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
979 if (s
->tray_open
|| !bdrv_is_inserted(s
->bs
)) {
980 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
982 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
987 if (r
->req
.status
== -1) {
988 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
993 /* Execute a scsi command. Returns the length of the data expected by the
994 command. This will be Positive for data transfers from the device
995 (eg. disk reads), negative for transfers to the device (eg. disk writes),
996 and zero if the command does not transfer any data. */
998 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
1000 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1001 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1008 outbuf
= (uint8_t *)r
->iov
.iov_base
;
1009 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
1014 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1015 printf(" 0x%02x", buf
[i
]);
1022 case TEST_UNIT_READY
:
1031 case ALLOW_MEDIUM_REMOVAL
:
1032 case READ_CAPACITY_10
:
1034 case GET_CONFIGURATION
:
1035 case SERVICE_ACTION_IN_16
:
1037 rc
= scsi_disk_emulate_command(r
, outbuf
);
1042 r
->iov
.iov_len
= rc
;
1044 case SYNCHRONIZE_CACHE
:
1045 bdrv_acct_start(s
->bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1046 r
->req
.aiocb
= bdrv_aio_flush(s
->bs
, scsi_flush_complete
, r
);
1047 if (r
->req
.aiocb
== NULL
) {
1048 scsi_flush_complete(r
, -EIO
);
1055 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1056 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1057 if (r
->req
.cmd
.lba
> s
->max_lba
)
1059 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1060 r
->sector_count
= len
* s
->cluster_size
;
1066 case WRITE_VERIFY_10
:
1067 case WRITE_VERIFY_12
:
1068 case WRITE_VERIFY_16
:
1069 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1070 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1071 (command
& 0xe) == 0xe ? "And Verify " : "",
1072 r
->req
.cmd
.lba
, len
);
1073 if (r
->req
.cmd
.lba
> s
->max_lba
)
1075 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1076 r
->sector_count
= len
* s
->cluster_size
;
1079 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1080 /* We don't support mode parameter changes.
1081 Allow the mode parameter header + block descriptors only. */
1082 if (r
->req
.cmd
.xfer
> 12) {
1086 case MODE_SELECT_10
:
1087 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1088 /* We don't support mode parameter changes.
1089 Allow the mode parameter header + block descriptors only. */
1090 if (r
->req
.cmd
.xfer
> 16) {
1096 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1098 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1103 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1105 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1106 r
->req
.cmd
.lba
, len
);
1108 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1113 * We only support WRITE SAME with the unmap bit set for now.
1115 if (!(buf
[1] & 0x8)) {
1119 rc
= bdrv_discard(s
->bs
, r
->req
.cmd
.lba
* s
->cluster_size
,
1120 len
* s
->cluster_size
);
1122 /* XXX: better error code ?*/
1130 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1131 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1134 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1137 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1140 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1141 scsi_req_complete(&r
->req
, GOOD
);
1143 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1144 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1147 if (!r
->sector_count
)
1148 r
->sector_count
= -1;
1153 static void scsi_disk_reset(DeviceState
*dev
)
1155 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1156 uint64_t nb_sectors
;
1158 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1160 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1161 nb_sectors
/= s
->cluster_size
;
1165 s
->max_lba
= nb_sectors
;
1168 static void scsi_destroy(SCSIDevice
*dev
)
1170 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1172 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1173 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1176 static void scsi_cd_change_media_cb(void *opaque
, bool load
)
1178 ((SCSIDiskState
*)opaque
)->tray_open
= !load
;
1181 static bool scsi_cd_is_tray_open(void *opaque
)
1183 return ((SCSIDiskState
*)opaque
)->tray_open
;
1186 static bool scsi_cd_is_medium_locked(void *opaque
)
1188 return ((SCSIDiskState
*)opaque
)->tray_locked
;
1191 static const BlockDevOps scsi_cd_block_ops
= {
1192 .change_media_cb
= scsi_cd_change_media_cb
,
1193 .is_tray_open
= scsi_cd_is_tray_open
,
1194 .is_medium_locked
= scsi_cd_is_medium_locked
,
1197 static int scsi_initfn(SCSIDevice
*dev
, uint8_t scsi_type
)
1199 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1202 if (!s
->qdev
.conf
.bs
) {
1203 error_report("scsi-disk: drive property not set");
1206 s
->bs
= s
->qdev
.conf
.bs
;
1208 if (scsi_type
== TYPE_DISK
&& !bdrv_is_inserted(s
->bs
)) {
1209 error_report("Device needs media, but drive is empty");
1214 /* try to fall back to value set with legacy -drive serial=... */
1215 dinfo
= drive_get_by_blockdev(s
->bs
);
1216 if (*dinfo
->serial
) {
1217 s
->serial
= g_strdup(dinfo
->serial
);
1222 s
->version
= g_strdup(QEMU_VERSION
);
1225 if (bdrv_is_sg(s
->bs
)) {
1226 error_report("scsi-disk: unwanted /dev/sg*");
1230 if (scsi_type
== TYPE_ROM
) {
1231 bdrv_set_dev_ops(s
->bs
, &scsi_cd_block_ops
, s
);
1232 s
->qdev
.blocksize
= 2048;
1233 } else if (scsi_type
== TYPE_DISK
) {
1234 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1236 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type
);
1239 s
->cluster_size
= s
->qdev
.blocksize
/ 512;
1240 bdrv_set_buffer_alignment(s
->bs
, s
->qdev
.blocksize
);
1242 s
->qdev
.type
= scsi_type
;
1243 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1244 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1248 static int scsi_hd_initfn(SCSIDevice
*dev
)
1250 return scsi_initfn(dev
, TYPE_DISK
);
1253 static int scsi_cd_initfn(SCSIDevice
*dev
)
1255 return scsi_initfn(dev
, TYPE_ROM
);
1258 static int scsi_disk_initfn(SCSIDevice
*dev
)
1263 if (!dev
->conf
.bs
) {
1264 scsi_type
= TYPE_DISK
; /* will die in scsi_initfn() */
1266 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1267 scsi_type
= dinfo
->media_cd
? TYPE_ROM
: TYPE_DISK
;
1270 return scsi_initfn(dev
, scsi_type
);
1273 static SCSIReqOps scsi_disk_reqops
= {
1274 .size
= sizeof(SCSIDiskReq
),
1275 .free_req
= scsi_free_request
,
1276 .send_command
= scsi_send_command
,
1277 .read_data
= scsi_read_data
,
1278 .write_data
= scsi_write_data
,
1279 .cancel_io
= scsi_cancel_io
,
1280 .get_buf
= scsi_get_buf
,
1283 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
1284 uint32_t lun
, void *hba_private
)
1286 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1290 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1291 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1292 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, SCSI_DMA_BUF_SIZE
);
1296 #define DEFINE_SCSI_DISK_PROPERTIES() \
1297 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1298 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1299 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1301 static SCSIDeviceInfo scsi_disk_info
[] = {
1303 .qdev
.name
= "scsi-hd",
1304 .qdev
.fw_name
= "disk",
1305 .qdev
.desc
= "virtual SCSI disk",
1306 .qdev
.size
= sizeof(SCSIDiskState
),
1307 .qdev
.reset
= scsi_disk_reset
,
1308 .init
= scsi_hd_initfn
,
1309 .destroy
= scsi_destroy
,
1310 .alloc_req
= scsi_new_request
,
1311 .qdev
.props
= (Property
[]) {
1312 DEFINE_SCSI_DISK_PROPERTIES(),
1313 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1314 DEFINE_PROP_END_OF_LIST(),
1317 .qdev
.name
= "scsi-cd",
1318 .qdev
.fw_name
= "disk",
1319 .qdev
.desc
= "virtual SCSI CD-ROM",
1320 .qdev
.size
= sizeof(SCSIDiskState
),
1321 .qdev
.reset
= scsi_disk_reset
,
1322 .init
= scsi_cd_initfn
,
1323 .destroy
= scsi_destroy
,
1324 .alloc_req
= scsi_new_request
,
1325 .qdev
.props
= (Property
[]) {
1326 DEFINE_SCSI_DISK_PROPERTIES(),
1327 DEFINE_PROP_END_OF_LIST(),
1330 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1331 .qdev
.fw_name
= "disk",
1332 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1333 .qdev
.size
= sizeof(SCSIDiskState
),
1334 .qdev
.reset
= scsi_disk_reset
,
1335 .init
= scsi_disk_initfn
,
1336 .destroy
= scsi_destroy
,
1337 .alloc_req
= scsi_new_request
,
1338 .qdev
.props
= (Property
[]) {
1339 DEFINE_SCSI_DISK_PROPERTIES(),
1340 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1341 DEFINE_PROP_END_OF_LIST(),
1346 static void scsi_disk_register_devices(void)
1350 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1351 scsi_qdev_register(&scsi_disk_info
[i
]);
1354 device_init(scsi_disk_register_devices
)