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"
41 #define SCSI_DMA_BUF_SIZE 131072
42 #define SCSI_MAX_INQUIRY_LEN 256
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
50 typedef struct SCSIDiskState SCSIDiskState
;
52 typedef struct SCSIDiskReq
{
54 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
56 uint32_t sector_count
;
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. */
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
);
80 static void scsi_free_request(SCSIRequest
*req
)
82 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
84 qemu_vfree(r
->iov
.iov_base
);
87 /* Helper function for command completion with sense. */
88 static void scsi_check_condition(SCSIDiskReq
*r
, SCSISense sense
)
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
);
96 /* Cancel a pending data transfer. */
97 static void scsi_cancel_io(SCSIRequest
*req
)
99 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
101 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
103 bdrv_aio_cancel(r
->req
.aiocb
);
108 static void scsi_read_complete(void * opaque
, int ret
)
110 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
111 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
114 if (r
->req
.aiocb
!= NULL
) {
116 bdrv_acct_done(s
->bs
, &r
->acct
);
120 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_READ
)) {
125 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->iov
.iov_len
);
127 n
= r
->iov
.iov_len
/ 512;
129 r
->sector_count
-= n
;
130 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
133 static void scsi_flush_complete(void * opaque
, int ret
)
135 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
136 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
138 if (r
->req
.aiocb
!= NULL
) {
140 bdrv_acct_done(s
->bs
, &r
->acct
);
144 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
149 scsi_req_complete(&r
->req
, GOOD
);
152 /* Read more data from scsi device into buffer. */
153 static void scsi_read_data(SCSIRequest
*req
)
155 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
156 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
159 if (r
->sector_count
== (uint32_t)-1) {
160 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
162 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
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
);
172 /* No data transfer may already be in progress */
173 assert(r
->req
.aiocb
== NULL
);
175 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
176 DPRINTF("Data transfer direction invalid\n");
177 scsi_read_complete(r
, -EINVAL
);
182 if (n
> SCSI_DMA_BUF_SIZE
/ 512)
183 n
= SCSI_DMA_BUF_SIZE
/ 512;
185 r
->iov
.iov_len
= n
* 512;
186 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
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
);
196 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
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
);
202 if (action
== BLOCK_ERR_IGNORE
) {
203 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
207 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
208 || action
== BLOCK_ERR_STOP_ANY
) {
210 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
211 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
213 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
214 vm_stop(VMSTOP_DISKFULL
);
218 scsi_check_condition(r
, SENSE_CODE(TARGET_FAILURE
));
221 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
224 scsi_check_condition(r
, SENSE_CODE(IO_ERROR
));
227 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
232 static void scsi_write_complete(void * opaque
, int ret
)
234 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
235 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
239 if (r
->req
.aiocb
!= NULL
) {
241 bdrv_acct_done(s
->bs
, &r
->acct
);
245 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
250 n
= r
->iov
.iov_len
/ 512;
252 r
->sector_count
-= n
;
253 if (r
->sector_count
== 0) {
254 scsi_req_complete(&r
->req
, GOOD
);
256 len
= r
->sector_count
* 512;
257 if (len
> SCSI_DMA_BUF_SIZE
) {
258 len
= SCSI_DMA_BUF_SIZE
;
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
);
266 static void scsi_write_data(SCSIRequest
*req
)
268 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
269 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
272 /* No data transfer may already be in progress */
273 assert(r
->req
.aiocb
== NULL
);
275 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
276 DPRINTF("Data transfer direction invalid\n");
277 scsi_write_complete(r
, -EINVAL
);
281 n
= r
->iov
.iov_len
/ 512;
283 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
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
);
292 /* Invoke completion routine to fetch data from host. */
293 scsi_write_complete(r
, 0);
297 static void scsi_dma_restart_bh(void *opaque
)
299 SCSIDiskState
*s
= opaque
;
303 qemu_bh_delete(s
->bh
);
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
;
313 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
315 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
316 case SCSI_REQ_STATUS_RETRY_READ
:
317 scsi_read_data(&r
->req
);
319 case SCSI_REQ_STATUS_RETRY_WRITE
:
320 scsi_write_data(&r
->req
);
322 case SCSI_REQ_STATUS_RETRY_FLUSH
:
323 ret
= scsi_disk_emulate_command(r
, r
->iov
.iov_base
);
325 scsi_req_complete(&r
->req
, GOOD
);
332 static void scsi_dma_restart_cb(void *opaque
, int running
, int reason
)
334 SCSIDiskState
*s
= opaque
;
340 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
341 qemu_bh_schedule(s
->bh
);
345 /* Return a pointer to the data buffer. */
346 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
348 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
350 return (uint8_t *)r
->iov
.iov_base
;
353 static int scsi_disk_emulate_inquiry(SCSIRequest
*req
, uint8_t *outbuf
)
355 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
358 if (req
->cmd
.buf
[1] & 0x2) {
359 /* Command support data - optional, not implemented */
360 BADF("optional INQUIRY command support request not implemented\n");
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
);
373 if (s
->qdev
.type
== TYPE_ROM
) {
374 outbuf
[buflen
++] = 5;
376 outbuf
[buflen
++] = 0;
378 outbuf
[buflen
++] = page_code
; // this page
379 outbuf
[buflen
++] = 0x00;
382 case 0x00: /* Supported page codes, mandatory */
385 DPRINTF("Inquiry EVPD[Supported pages] "
386 "buffer size %zd\n", req
->cmd
.xfer
);
388 outbuf
[buflen
++] = 0x00; // list of supported pages (this page)
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
396 outbuf
[pages
] = buflen
- pages
- 1; // number of pages
399 case 0x80: /* Device serial number, optional */
404 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
408 l
= strlen(s
->serial
);
409 if (l
> req
->cmd
.xfer
)
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
);
422 case 0x83: /* Device identification page, mandatory */
424 int max_len
= 255 - 8;
425 int id_len
= strlen(bdrv_get_device_name(s
->bs
));
427 if (id_len
> max_len
)
429 DPRINTF("Inquiry EVPD[Device identification] "
430 "buffer size %zd\n", req
->cmd
.xfer
);
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
438 memcpy(outbuf
+buflen
, bdrv_get_device_name(s
->bs
), id_len
);
442 case 0xb0: /* block limits */
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
;
451 if (s
->qdev
.type
== TYPE_ROM
) {
452 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
456 /* required VPD size with unmap support */
457 outbuf
[3] = buflen
= 0x3c;
459 memset(outbuf
+ 4, 0, buflen
- 4);
461 /* optimal transfer length granularity */
462 outbuf
[6] = (min_io_size
>> 8) & 0xff;
463 outbuf
[7] = min_io_size
& 0xff;
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;
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;
478 case 0xb2: /* thin provisioning */
480 outbuf
[3] = buflen
= 8;
482 outbuf
[5] = 0x40; /* write same with unmap supported */
488 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
489 "buffer size %zd\n", page_code
, req
->cmd
.xfer
);
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]);
504 if (req
->cmd
.xfer
< 5) {
505 BADF("Error: Inquiry (STANDARD) buffer size %zd "
506 "is less than 5\n", req
->cmd
.xfer
);
510 buflen
= req
->cmd
.xfer
;
511 if (buflen
> SCSI_MAX_INQUIRY_LEN
)
512 buflen
= SCSI_MAX_INQUIRY_LEN
;
514 memset(outbuf
, 0, buflen
);
516 outbuf
[0] = s
->qdev
.type
& 0x1f;
517 if (s
->qdev
.type
== TYPE_ROM
) {
519 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
521 outbuf
[1] = s
->removable
? 0x80 : 0;
522 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
524 memcpy(&outbuf
[8], "QEMU ", 8);
525 memset(&outbuf
[32], 0, 4);
526 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
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.
534 outbuf
[3] = 2; /* Format 2 */
537 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
539 /* If the allocation length of CDB is too small,
540 the additional length is not adjusted */
544 /* Sync data transfer and TCQ. */
545 outbuf
[7] = 0x10 | (req
->bus
->tcq
? 0x02 : 0);
549 static int mode_sense_page(SCSIDiskState
*s
, int page
, uint8_t **p_outbuf
,
552 BlockDriverState
*bdrv
= s
->bs
;
553 int cylinders
, heads
, secs
;
554 uint8_t *p
= *p_outbuf
;
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.
563 case 4: /* Rigid disk device geometry page. */
564 if (s
->qdev
.type
== TYPE_ROM
) {
569 if (page_control
== 1) { /* Changeable Values */
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;
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 */
589 /* Landing zone cylinder */
593 /* Medium rotation rate [rpm], 5400 rpm */
594 p
[20] = (5400 >> 8) & 0xff;
598 case 5: /* Flexible disk device geometry page. */
599 if (s
->qdev
.type
== TYPE_ROM
) {
604 if (page_control
== 1) { /* Changeable Values */
607 /* Transfer rate [kbit/s], 5Mbit/s */
610 /* if a geometry hint is available, use it */
611 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
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 */
626 /* Device step pulse width [us], 1us */
628 /* Device head settle delay [100us], 100us */
631 /* Motor on delay [0.1s], 0.1s */
633 /* Motor off delay [0.1s], 0.1s */
635 /* Medium rotation rate [rpm], 5400 rpm */
636 p
[28] = (5400 >> 8) & 0xff;
640 case 8: /* Caching page. */
643 if (page_control
== 1) { /* Changeable Values */
646 if (bdrv_enable_write_cache(s
->bs
)) {
651 case 0x2a: /* CD Capabilities and Mechanical Status page. */
652 if (s
->qdev
.type
!= TYPE_ROM
) {
657 if (page_control
== 1) { /* Changeable Values */
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,
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
671 p
[8] = (50 * 176) >> 8; // 50x read speed
672 p
[9] = (50 * 176) & 0xff;
673 p
[10] = 0 >> 8; // No volume
675 p
[12] = 2048 >> 8; // 2M buffer
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;
689 *p_outbuf
+= p
[1] + 2;
693 static int scsi_disk_emulate_mode_sense(SCSIDiskReq
*r
, uint8_t *outbuf
)
695 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
697 int page
, dbd
, buflen
, ret
, page_control
;
699 uint8_t dev_specific_param
;
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
);
709 if (bdrv_is_read_only(s
->bs
)) {
710 dev_specific_param
= 0x80; /* Readonly. */
712 dev_specific_param
= 0x00;
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. */
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. */
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 */
734 nb_sectors
/= s
->cluster_size
;
735 if (nb_sectors
> 0xffffff)
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;
748 if (page_control
== 3) {
750 scsi_check_condition(r
, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED
));
755 for (page
= 0; page
<= 0x3e; page
++) {
756 mode_sense_page(s
, page
, &p
, page_control
);
759 ret
= mode_sense_page(s
, page
, &p
, page_control
);
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.
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;
777 if (buflen
> r
->req
.cmd
.xfer
)
778 buflen
= r
->req
.cmd
.xfer
;
782 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
784 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
785 int start_track
, format
, msf
, toclen
;
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
;
796 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
799 /* multi session : only a single session defined */
801 memset(outbuf
, 0, 12);
807 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
812 if (toclen
> req
->cmd
.xfer
)
813 toclen
= req
->cmd
.xfer
;
817 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
)
819 SCSIRequest
*req
= &r
->req
;
820 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
824 switch (req
->cmd
.buf
[0]) {
825 case TEST_UNIT_READY
:
826 if (!bdrv_is_inserted(s
->bs
))
830 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
832 goto illegal_request
;
836 buflen
= scsi_disk_emulate_mode_sense(r
, outbuf
);
838 goto illegal_request
;
841 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
843 goto illegal_request
;
846 if (req
->cmd
.buf
[1] & 1)
847 goto illegal_request
;
850 if (req
->cmd
.buf
[1] & 3)
851 goto illegal_request
;
854 if (req
->cmd
.buf
[1] & 1)
855 goto illegal_request
;
858 if (req
->cmd
.buf
[1] & 3)
859 goto illegal_request
;
862 if (s
->qdev
.type
== TYPE_ROM
&& (req
->cmd
.buf
[4] & 2)) {
863 /* load/eject medium */
864 bdrv_eject(s
->bs
, !(req
->cmd
.buf
[4] & 1));
867 case ALLOW_MEDIUM_REMOVAL
:
868 bdrv_set_locked(s
->bs
, req
->cmd
.buf
[4] & 1);
870 case READ_CAPACITY_10
:
871 /* The normal LEN field for this command is zero. */
872 memset(outbuf
, 0, 8);
873 bdrv_get_geometry(s
->bs
, &nb_sectors
);
876 nb_sectors
/= s
->cluster_size
;
877 /* Returned value is the address of the last sector. */
879 /* Remember the new size for read/write sanity checking. */
880 s
->max_lba
= nb_sectors
;
881 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
882 if (nb_sectors
> UINT32_MAX
)
883 nb_sectors
= UINT32_MAX
;
884 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
885 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
886 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
887 outbuf
[3] = nb_sectors
& 0xff;
890 outbuf
[6] = s
->cluster_size
* 2;
894 case GET_CONFIGURATION
:
895 memset(outbuf
, 0, 8);
896 /* ??? This should probably return much more information. For now
897 just return the basic header indicating the CD-ROM profile. */
898 outbuf
[7] = 8; // CD-ROM
901 case SERVICE_ACTION_IN_16
:
902 /* Service Action In subcommands. */
903 if ((req
->cmd
.buf
[1] & 31) == SAI_READ_CAPACITY_16
) {
904 DPRINTF("SAI READ CAPACITY(16)\n");
905 memset(outbuf
, 0, req
->cmd
.xfer
);
906 bdrv_get_geometry(s
->bs
, &nb_sectors
);
909 nb_sectors
/= s
->cluster_size
;
910 /* Returned value is the address of the last sector. */
912 /* Remember the new size for read/write sanity checking. */
913 s
->max_lba
= nb_sectors
;
914 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
915 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
916 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
917 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
918 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
919 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
920 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
921 outbuf
[7] = nb_sectors
& 0xff;
924 outbuf
[10] = s
->cluster_size
* 2;
927 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
929 /* set TPE bit if the format supports discard */
930 if (s
->qdev
.conf
.discard_granularity
) {
934 /* Protection, exponent and lowest lba field left blank. */
935 buflen
= req
->cmd
.xfer
;
938 DPRINTF("Unsupported Service Action In\n");
939 goto illegal_request
;
943 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
949 if (!bdrv_is_inserted(s
->bs
)) {
950 scsi_check_condition(r
, SENSE_CODE(NO_MEDIUM
));
952 scsi_check_condition(r
, SENSE_CODE(LUN_NOT_READY
));
957 if (r
->req
.status
== -1) {
958 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
963 /* Execute a scsi command. Returns the length of the data expected by the
964 command. This will be Positive for data transfers from the device
965 (eg. disk reads), negative for transfers to the device (eg. disk writes),
966 and zero if the command does not transfer any data. */
968 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
970 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
971 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
978 outbuf
= (uint8_t *)r
->iov
.iov_base
;
979 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
984 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
985 printf(" 0x%02x", buf
[i
]);
992 case TEST_UNIT_READY
:
1001 case ALLOW_MEDIUM_REMOVAL
:
1002 case READ_CAPACITY_10
:
1004 case GET_CONFIGURATION
:
1005 case SERVICE_ACTION_IN_16
:
1007 rc
= scsi_disk_emulate_command(r
, outbuf
);
1012 r
->iov
.iov_len
= rc
;
1014 case SYNCHRONIZE_CACHE
:
1015 bdrv_acct_start(s
->bs
, &r
->acct
, 0, BDRV_ACCT_FLUSH
);
1016 r
->req
.aiocb
= bdrv_aio_flush(s
->bs
, scsi_flush_complete
, r
);
1017 if (r
->req
.aiocb
== NULL
) {
1018 scsi_flush_complete(r
, -EIO
);
1025 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1026 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1027 if (r
->req
.cmd
.lba
> s
->max_lba
)
1029 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1030 r
->sector_count
= len
* s
->cluster_size
;
1036 case WRITE_VERIFY_10
:
1037 case WRITE_VERIFY_12
:
1038 case WRITE_VERIFY_16
:
1039 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1040 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1041 (command
& 0xe) == 0xe ? "And Verify " : "",
1042 r
->req
.cmd
.lba
, len
);
1043 if (r
->req
.cmd
.lba
> s
->max_lba
)
1045 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1046 r
->sector_count
= len
* s
->cluster_size
;
1049 DPRINTF("Mode Select(6) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1050 /* We don't support mode parameter changes.
1051 Allow the mode parameter header + block descriptors only. */
1052 if (r
->req
.cmd
.xfer
> 12) {
1056 case MODE_SELECT_10
:
1057 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1058 /* We don't support mode parameter changes.
1059 Allow the mode parameter header + block descriptors only. */
1060 if (r
->req
.cmd
.xfer
> 16) {
1066 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1068 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1073 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1075 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1076 r
->req
.cmd
.lba
, len
);
1078 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1083 * We only support WRITE SAME with the unmap bit set for now.
1085 if (!(buf
[1] & 0x8)) {
1089 rc
= bdrv_discard(s
->bs
, r
->req
.cmd
.lba
* s
->cluster_size
,
1090 len
* s
->cluster_size
);
1092 /* XXX: better error code ?*/
1100 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1101 scsi_check_condition(r
, SENSE_CODE(INVALID_OPCODE
));
1104 scsi_check_condition(r
, SENSE_CODE(INVALID_FIELD
));
1107 scsi_check_condition(r
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1110 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1111 scsi_req_complete(&r
->req
, GOOD
);
1113 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1114 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1117 if (!r
->sector_count
)
1118 r
->sector_count
= -1;
1123 static void scsi_disk_reset(DeviceState
*dev
)
1125 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1126 uint64_t nb_sectors
;
1128 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(RESET
));
1130 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1131 nb_sectors
/= s
->cluster_size
;
1135 s
->max_lba
= nb_sectors
;
1138 static void scsi_destroy(SCSIDevice
*dev
)
1140 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1142 scsi_device_purge_requests(&s
->qdev
, SENSE_CODE(NO_SENSE
));
1143 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1146 static int scsi_initfn(SCSIDevice
*dev
, uint8_t scsi_type
)
1148 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1151 if (!s
->qdev
.conf
.bs
) {
1152 error_report("scsi-disk: drive property not set");
1155 s
->bs
= s
->qdev
.conf
.bs
;
1157 if (scsi_type
== TYPE_DISK
&& !bdrv_is_inserted(s
->bs
)) {
1158 error_report("Device needs media, but drive is empty");
1163 /* try to fall back to value set with legacy -drive serial=... */
1164 dinfo
= drive_get_by_blockdev(s
->bs
);
1165 if (*dinfo
->serial
) {
1166 s
->serial
= g_strdup(dinfo
->serial
);
1171 s
->version
= g_strdup(QEMU_VERSION
);
1174 if (bdrv_is_sg(s
->bs
)) {
1175 error_report("scsi-disk: unwanted /dev/sg*");
1179 if (scsi_type
== TYPE_ROM
) {
1180 s
->qdev
.blocksize
= 2048;
1181 } else if (scsi_type
== TYPE_DISK
) {
1182 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1184 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type
);
1187 s
->cluster_size
= s
->qdev
.blocksize
/ 512;
1188 s
->bs
->buffer_alignment
= s
->qdev
.blocksize
;
1190 s
->qdev
.type
= scsi_type
;
1191 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1192 bdrv_set_removable(s
->bs
, scsi_type
== TYPE_ROM
);
1193 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1197 static int scsi_hd_initfn(SCSIDevice
*dev
)
1199 return scsi_initfn(dev
, TYPE_DISK
);
1202 static int scsi_cd_initfn(SCSIDevice
*dev
)
1204 return scsi_initfn(dev
, TYPE_ROM
);
1207 static int scsi_disk_initfn(SCSIDevice
*dev
)
1212 if (!dev
->conf
.bs
) {
1213 scsi_type
= TYPE_DISK
; /* will die in scsi_initfn() */
1215 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1216 scsi_type
= dinfo
->media_cd
? TYPE_ROM
: TYPE_DISK
;
1219 return scsi_initfn(dev
, scsi_type
);
1222 static SCSIReqOps scsi_disk_reqops
= {
1223 .size
= sizeof(SCSIDiskReq
),
1224 .free_req
= scsi_free_request
,
1225 .send_command
= scsi_send_command
,
1226 .read_data
= scsi_read_data
,
1227 .write_data
= scsi_write_data
,
1228 .cancel_io
= scsi_cancel_io
,
1229 .get_buf
= scsi_get_buf
,
1232 static SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
1233 uint32_t lun
, void *hba_private
)
1235 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
1239 req
= scsi_req_alloc(&scsi_disk_reqops
, &s
->qdev
, tag
, lun
, hba_private
);
1240 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
1241 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, SCSI_DMA_BUF_SIZE
);
1245 #define DEFINE_SCSI_DISK_PROPERTIES() \
1246 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1247 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1248 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1250 static SCSIDeviceInfo scsi_disk_info
[] = {
1252 .qdev
.name
= "scsi-hd",
1253 .qdev
.fw_name
= "disk",
1254 .qdev
.desc
= "virtual SCSI disk",
1255 .qdev
.size
= sizeof(SCSIDiskState
),
1256 .qdev
.reset
= scsi_disk_reset
,
1257 .init
= scsi_hd_initfn
,
1258 .destroy
= scsi_destroy
,
1259 .alloc_req
= scsi_new_request
,
1260 .qdev
.props
= (Property
[]) {
1261 DEFINE_SCSI_DISK_PROPERTIES(),
1262 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1263 DEFINE_PROP_END_OF_LIST(),
1266 .qdev
.name
= "scsi-cd",
1267 .qdev
.fw_name
= "disk",
1268 .qdev
.desc
= "virtual SCSI CD-ROM",
1269 .qdev
.size
= sizeof(SCSIDiskState
),
1270 .qdev
.reset
= scsi_disk_reset
,
1271 .init
= scsi_cd_initfn
,
1272 .destroy
= scsi_destroy
,
1273 .alloc_req
= scsi_new_request
,
1274 .qdev
.props
= (Property
[]) {
1275 DEFINE_SCSI_DISK_PROPERTIES(),
1276 DEFINE_PROP_END_OF_LIST(),
1279 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1280 .qdev
.fw_name
= "disk",
1281 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1282 .qdev
.size
= sizeof(SCSIDiskState
),
1283 .qdev
.reset
= scsi_disk_reset
,
1284 .init
= scsi_disk_initfn
,
1285 .destroy
= scsi_destroy
,
1286 .alloc_req
= scsi_new_request
,
1287 .qdev
.props
= (Property
[]) {
1288 DEFINE_SCSI_DISK_PROPERTIES(),
1289 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1290 DEFINE_PROP_END_OF_LIST(),
1295 static void scsi_disk_register_devices(void)
1299 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1300 scsi_qdev_register(&scsi_disk_info
[i
]);
1303 device_init(scsi_disk_register_devices
)