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
;
66 /* The qemu block layer uses a fixed 512 byte sector size.
67 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 SCSIRequest
*scsi_new_request(SCSIDevice
*d
, uint32_t tag
,
81 uint32_t lun
, void *hba_private
)
83 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, d
);
87 req
= scsi_req_alloc(sizeof(SCSIDiskReq
), &s
->qdev
, tag
, lun
, hba_private
);
88 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
89 r
->iov
.iov_base
= qemu_blockalign(s
->bs
, SCSI_DMA_BUF_SIZE
);
93 static void scsi_free_request(SCSIRequest
*req
)
95 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
97 qemu_vfree(r
->iov
.iov_base
);
100 static void scsi_disk_clear_sense(SCSIDiskState
*s
)
102 memset(&s
->sense
, 0, sizeof(s
->sense
));
105 static void scsi_req_set_status(SCSIDiskReq
*r
, int status
, SCSISense sense
)
107 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
109 r
->req
.status
= status
;
113 /* Helper function for command completion. */
114 static void scsi_command_complete(SCSIDiskReq
*r
, int status
, SCSISense sense
)
116 DPRINTF("Command complete tag=0x%x status=%d sense=%d/%d/%d\n",
117 r
->req
.tag
, status
, sense
.key
, sense
.asc
, sense
.ascq
);
118 scsi_req_set_status(r
, status
, sense
);
119 scsi_req_complete(&r
->req
);
122 /* Cancel a pending data transfer. */
123 static void scsi_cancel_io(SCSIRequest
*req
)
125 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
127 DPRINTF("Cancel tag=0x%x\n", req
->tag
);
129 bdrv_aio_cancel(r
->req
.aiocb
);
134 static void scsi_read_complete(void * opaque
, int ret
)
136 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
142 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_READ
)) {
147 DPRINTF("Data ready tag=0x%x len=%zd\n", r
->req
.tag
, r
->iov
.iov_len
);
149 n
= r
->iov
.iov_len
/ 512;
151 r
->sector_count
-= n
;
152 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
156 /* Read more data from scsi device into buffer. */
157 static void scsi_read_data(SCSIRequest
*req
)
159 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
160 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
163 if (r
->sector_count
== (uint32_t)-1) {
164 DPRINTF("Read buf_len=%zd\n", r
->iov
.iov_len
);
166 scsi_req_data(&r
->req
, r
->iov
.iov_len
);
169 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
170 if (r
->sector_count
== 0) {
171 scsi_command_complete(r
, GOOD
, SENSE_CODE(NO_SENSE
));
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;
188 r
->iov
.iov_len
= n
* 512;
189 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
190 r
->req
.aiocb
= bdrv_aio_readv(s
->bs
, r
->sector
, &r
->qiov
, n
,
191 scsi_read_complete
, r
);
192 if (r
->req
.aiocb
== NULL
) {
193 scsi_read_complete(r
, -EIO
);
197 static int scsi_handle_rw_error(SCSIDiskReq
*r
, int error
, int type
)
199 int is_read
= (type
== SCSI_REQ_STATUS_RETRY_READ
);
200 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
201 BlockErrorAction action
= bdrv_get_on_error(s
->bs
, is_read
);
203 if (action
== BLOCK_ERR_IGNORE
) {
204 bdrv_mon_event(s
->bs
, BDRV_ACTION_IGNORE
, is_read
);
208 if ((error
== ENOSPC
&& action
== BLOCK_ERR_STOP_ENOSPC
)
209 || action
== BLOCK_ERR_STOP_ANY
) {
211 type
&= SCSI_REQ_STATUS_RETRY_TYPE_MASK
;
212 r
->status
|= SCSI_REQ_STATUS_RETRY
| type
;
214 bdrv_mon_event(s
->bs
, BDRV_ACTION_STOP
, is_read
);
215 vm_stop(VMSTOP_DISKFULL
);
217 if (type
== SCSI_REQ_STATUS_RETRY_READ
) {
218 scsi_req_data(&r
->req
, 0);
222 scsi_command_complete(r
, CHECK_CONDITION
,
223 SENSE_CODE(TARGET_FAILURE
));
226 scsi_command_complete(r
, CHECK_CONDITION
,
227 SENSE_CODE(INVALID_FIELD
));
230 scsi_command_complete(r
, CHECK_CONDITION
,
231 SENSE_CODE(IO_ERROR
));
234 bdrv_mon_event(s
->bs
, BDRV_ACTION_REPORT
, is_read
);
239 static void scsi_write_complete(void * opaque
, int ret
)
241 SCSIDiskReq
*r
= (SCSIDiskReq
*)opaque
;
248 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_WRITE
)) {
253 n
= r
->iov
.iov_len
/ 512;
255 r
->sector_count
-= n
;
256 if (r
->sector_count
== 0) {
257 scsi_command_complete(r
, GOOD
, SENSE_CODE(NO_SENSE
));
259 len
= r
->sector_count
* 512;
260 if (len
> SCSI_DMA_BUF_SIZE
) {
261 len
= SCSI_DMA_BUF_SIZE
;
263 r
->iov
.iov_len
= len
;
264 DPRINTF("Write complete tag=0x%x more=%d\n", r
->req
.tag
, len
);
265 scsi_req_data(&r
->req
, len
);
269 static void scsi_write_data(SCSIRequest
*req
)
271 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
272 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, r
->req
.dev
);
275 /* No data transfer may already be in progress */
276 assert(r
->req
.aiocb
== NULL
);
278 if (r
->req
.cmd
.mode
!= SCSI_XFER_TO_DEV
) {
279 DPRINTF("Data transfer direction invalid\n");
280 scsi_write_complete(r
, -EINVAL
);
284 n
= r
->iov
.iov_len
/ 512;
286 qemu_iovec_init_external(&r
->qiov
, &r
->iov
, 1);
287 r
->req
.aiocb
= bdrv_aio_writev(s
->bs
, r
->sector
, &r
->qiov
, n
,
288 scsi_write_complete
, r
);
289 if (r
->req
.aiocb
== NULL
) {
290 scsi_write_complete(r
, -ENOMEM
);
293 /* Invoke completion routine to fetch data from host. */
294 scsi_write_complete(r
, 0);
298 static void scsi_dma_restart_bh(void *opaque
)
300 SCSIDiskState
*s
= opaque
;
304 qemu_bh_delete(s
->bh
);
307 QTAILQ_FOREACH(req
, &s
->qdev
.requests
, next
) {
308 r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
309 if (r
->status
& SCSI_REQ_STATUS_RETRY
) {
310 int status
= r
->status
;
314 ~(SCSI_REQ_STATUS_RETRY
| SCSI_REQ_STATUS_RETRY_TYPE_MASK
);
316 switch (status
& SCSI_REQ_STATUS_RETRY_TYPE_MASK
) {
317 case SCSI_REQ_STATUS_RETRY_READ
:
318 scsi_read_data(&r
->req
);
320 case SCSI_REQ_STATUS_RETRY_WRITE
:
321 scsi_write_data(&r
->req
);
323 case SCSI_REQ_STATUS_RETRY_FLUSH
:
324 ret
= scsi_disk_emulate_command(r
, r
->iov
.iov_base
);
326 scsi_command_complete(r
, GOOD
, SENSE_CODE(NO_SENSE
));
333 static void scsi_dma_restart_cb(void *opaque
, int running
, int reason
)
335 SCSIDiskState
*s
= opaque
;
341 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
342 qemu_bh_schedule(s
->bh
);
346 /* Return a pointer to the data buffer. */
347 static uint8_t *scsi_get_buf(SCSIRequest
*req
)
349 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
351 return (uint8_t *)r
->iov
.iov_base
;
354 /* Copy sense information into the provided buffer */
355 static int scsi_get_sense(SCSIRequest
*req
, uint8_t *outbuf
, int len
)
357 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
359 return scsi_build_sense(s
->sense
, outbuf
, len
, len
> 14);
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
);
526 outbuf
[0] = 0x7f; /* LUN not supported */
530 outbuf
[0] = s
->qdev
.type
& 0x1f;
531 if (s
->qdev
.type
== TYPE_ROM
) {
533 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
535 outbuf
[1] = s
->removable
? 0x80 : 0;
536 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
538 memcpy(&outbuf
[8], "QEMU ", 8);
539 memset(&outbuf
[32], 0, 4);
540 memcpy(&outbuf
[32], s
->version
, MIN(4, strlen(s
->version
)));
542 * We claim conformance to SPC-3, which is required for guests
543 * to ask for modern features like READ CAPACITY(16) or the
544 * block characteristics VPD page by default. Not all of SPC-3
545 * is actually implemented, but we're good enough.
548 outbuf
[3] = 2; /* Format 2 */
551 outbuf
[4] = buflen
- 5; /* Additional Length = (Len - 1) - 4 */
553 /* If the allocation length of CDB is too small,
554 the additional length is not adjusted */
558 /* Sync data transfer and TCQ. */
559 outbuf
[7] = 0x10 | (req
->bus
->tcq
? 0x02 : 0);
563 static int mode_sense_page(SCSIRequest
*req
, int page
, uint8_t *p
,
566 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
567 BlockDriverState
*bdrv
= s
->bs
;
568 int cylinders
, heads
, secs
;
571 * If Changeable Values are requested, a mask denoting those mode parameters
572 * that are changeable shall be returned. As we currently don't support
573 * parameter changes via MODE_SELECT all bits are returned set to zero.
574 * The buffer was already menset to zero by the caller of this function.
577 case 4: /* Rigid disk device geometry page. */
580 if (page_control
== 1) { /* Changeable Values */
583 /* if a geometry hint is available, use it */
584 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
585 p
[2] = (cylinders
>> 16) & 0xff;
586 p
[3] = (cylinders
>> 8) & 0xff;
587 p
[4] = cylinders
& 0xff;
589 /* Write precomp start cylinder, disabled */
590 p
[6] = (cylinders
>> 16) & 0xff;
591 p
[7] = (cylinders
>> 8) & 0xff;
592 p
[8] = cylinders
& 0xff;
593 /* Reduced current start cylinder, disabled */
594 p
[9] = (cylinders
>> 16) & 0xff;
595 p
[10] = (cylinders
>> 8) & 0xff;
596 p
[11] = cylinders
& 0xff;
597 /* Device step rate [ns], 200ns */
600 /* Landing zone cylinder */
604 /* Medium rotation rate [rpm], 5400 rpm */
605 p
[20] = (5400 >> 8) & 0xff;
609 case 5: /* Flexible disk device geometry page. */
612 if (page_control
== 1) { /* Changeable Values */
615 /* Transfer rate [kbit/s], 5Mbit/s */
618 /* if a geometry hint is available, use it */
619 bdrv_get_geometry_hint(bdrv
, &cylinders
, &heads
, &secs
);
622 p
[6] = s
->cluster_size
* 2;
623 p
[8] = (cylinders
>> 8) & 0xff;
624 p
[9] = cylinders
& 0xff;
625 /* Write precomp start cylinder, disabled */
626 p
[10] = (cylinders
>> 8) & 0xff;
627 p
[11] = cylinders
& 0xff;
628 /* Reduced current start cylinder, disabled */
629 p
[12] = (cylinders
>> 8) & 0xff;
630 p
[13] = cylinders
& 0xff;
631 /* Device step rate [100us], 100us */
634 /* Device step pulse width [us], 1us */
636 /* Device head settle delay [100us], 100us */
639 /* Motor on delay [0.1s], 0.1s */
641 /* Motor off delay [0.1s], 0.1s */
643 /* Medium rotation rate [rpm], 5400 rpm */
644 p
[28] = (5400 >> 8) & 0xff;
648 case 8: /* Caching page. */
651 if (page_control
== 1) { /* Changeable Values */
654 if (bdrv_enable_write_cache(s
->bs
)) {
659 case 0x2a: /* CD Capabilities and Mechanical Status page. */
660 if (s
->qdev
.type
!= TYPE_ROM
)
664 if (page_control
== 1) { /* Changeable Values */
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,
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
678 p
[8] = (50 * 176) >> 8; // 50x read speed
679 p
[9] = (50 * 176) & 0xff;
680 p
[10] = 0 >> 8; // No volume
682 p
[12] = 2048 >> 8; // 2M buffer
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;
697 static int scsi_disk_emulate_mode_sense(SCSIRequest
*req
, uint8_t *outbuf
)
699 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
701 int page
, dbd
, buflen
, page_control
;
703 uint8_t dev_specific_param
;
705 dbd
= req
->cmd
.buf
[1] & 0x8;
706 page
= req
->cmd
.buf
[2] & 0x3f;
707 page_control
= (req
->cmd
.buf
[2] & 0xc0) >> 6;
708 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
709 (req
->cmd
.buf
[0] == MODE_SENSE
) ? 6 : 10, page
, req
->cmd
.xfer
, page_control
);
710 memset(outbuf
, 0, req
->cmd
.xfer
);
713 if (bdrv_is_read_only(s
->bs
)) {
714 dev_specific_param
= 0x80; /* Readonly. */
716 dev_specific_param
= 0x00;
719 if (req
->cmd
.buf
[0] == MODE_SENSE
) {
720 p
[1] = 0; /* Default media type. */
721 p
[2] = dev_specific_param
;
722 p
[3] = 0; /* Block descriptor length. */
724 } else { /* MODE_SENSE_10 */
725 p
[2] = 0; /* Default media type. */
726 p
[3] = dev_specific_param
;
727 p
[6] = p
[7] = 0; /* Block descriptor length. */
731 bdrv_get_geometry(s
->bs
, &nb_sectors
);
732 if (!dbd
&& nb_sectors
) {
733 if (req
->cmd
.buf
[0] == MODE_SENSE
) {
734 outbuf
[3] = 8; /* Block descriptor length */
735 } else { /* MODE_SENSE_10 */
736 outbuf
[7] = 8; /* Block descriptor length */
738 nb_sectors
/= s
->cluster_size
;
739 if (nb_sectors
> 0xffffff)
741 p
[0] = 0; /* media density code */
742 p
[1] = (nb_sectors
>> 16) & 0xff;
743 p
[2] = (nb_sectors
>> 8) & 0xff;
744 p
[3] = nb_sectors
& 0xff;
745 p
[4] = 0; /* reserved */
746 p
[5] = 0; /* bytes 5-7 are the sector size in bytes */
747 p
[6] = s
->cluster_size
* 2;
752 if (page_control
== 3) { /* Saved Values */
753 return -1; /* ILLEGAL_REQUEST */
761 p
+= mode_sense_page(req
, page
, p
, page_control
);
764 p
+= mode_sense_page(req
, 0x08, p
, page_control
);
765 p
+= mode_sense_page(req
, 0x2a, p
, page_control
);
768 return -1; /* ILLEGAL_REQUEST */
773 * The mode data length field specifies the length in bytes of the
774 * following data that is available to be transferred. The mode data
775 * length does not include itself.
777 if (req
->cmd
.buf
[0] == MODE_SENSE
) {
778 outbuf
[0] = buflen
- 1;
779 } else { /* MODE_SENSE_10 */
780 outbuf
[0] = ((buflen
- 2) >> 8) & 0xff;
781 outbuf
[1] = (buflen
- 2) & 0xff;
783 if (buflen
> req
->cmd
.xfer
)
784 buflen
= req
->cmd
.xfer
;
788 static int scsi_disk_emulate_read_toc(SCSIRequest
*req
, uint8_t *outbuf
)
790 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
791 int start_track
, format
, msf
, toclen
;
794 msf
= req
->cmd
.buf
[1] & 2;
795 format
= req
->cmd
.buf
[2] & 0xf;
796 start_track
= req
->cmd
.buf
[6];
797 bdrv_get_geometry(s
->bs
, &nb_sectors
);
798 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
799 nb_sectors
/= s
->cluster_size
;
802 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
805 /* multi session : only a single session defined */
807 memset(outbuf
, 0, 12);
813 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
818 if (toclen
> req
->cmd
.xfer
)
819 toclen
= req
->cmd
.xfer
;
823 static int scsi_disk_emulate_command(SCSIDiskReq
*r
, uint8_t *outbuf
)
825 SCSIRequest
*req
= &r
->req
;
826 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
831 switch (req
->cmd
.buf
[0]) {
832 case TEST_UNIT_READY
:
833 if (!bdrv_is_inserted(s
->bs
))
837 if (req
->cmd
.xfer
< 4)
838 goto illegal_request
;
839 buflen
= scsi_build_sense(s
->sense
, outbuf
, req
->cmd
.xfer
,
841 scsi_disk_clear_sense(s
);
844 buflen
= scsi_disk_emulate_inquiry(req
, outbuf
);
846 goto illegal_request
;
850 buflen
= scsi_disk_emulate_mode_sense(req
, outbuf
);
852 goto illegal_request
;
855 buflen
= scsi_disk_emulate_read_toc(req
, outbuf
);
857 goto illegal_request
;
860 if (req
->cmd
.buf
[1] & 1)
861 goto illegal_request
;
864 if (req
->cmd
.buf
[1] & 3)
865 goto illegal_request
;
868 if (req
->cmd
.buf
[1] & 1)
869 goto illegal_request
;
872 if (req
->cmd
.buf
[1] & 3)
873 goto illegal_request
;
876 if (s
->qdev
.type
== TYPE_ROM
&& (req
->cmd
.buf
[4] & 2)) {
877 /* load/eject medium */
878 bdrv_eject(s
->bs
, !(req
->cmd
.buf
[4] & 1));
881 case ALLOW_MEDIUM_REMOVAL
:
882 bdrv_set_locked(s
->bs
, req
->cmd
.buf
[4] & 1);
884 case READ_CAPACITY_10
:
885 /* The normal LEN field for this command is zero. */
886 memset(outbuf
, 0, 8);
887 bdrv_get_geometry(s
->bs
, &nb_sectors
);
890 nb_sectors
/= s
->cluster_size
;
891 /* Returned value is the address of the last sector. */
893 /* Remember the new size for read/write sanity checking. */
894 s
->max_lba
= nb_sectors
;
895 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
896 if (nb_sectors
> UINT32_MAX
)
897 nb_sectors
= UINT32_MAX
;
898 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
899 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
900 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
901 outbuf
[3] = nb_sectors
& 0xff;
904 outbuf
[6] = s
->cluster_size
* 2;
908 case SYNCHRONIZE_CACHE
:
909 ret
= bdrv_flush(s
->bs
);
911 if (scsi_handle_rw_error(r
, -ret
, SCSI_REQ_STATUS_RETRY_FLUSH
)) {
916 case GET_CONFIGURATION
:
917 memset(outbuf
, 0, 8);
918 /* ??? This should probably return much more information. For now
919 just return the basic header indicating the CD-ROM profile. */
920 outbuf
[7] = 8; // CD-ROM
923 case SERVICE_ACTION_IN
:
924 /* Service Action In subcommands. */
925 if ((req
->cmd
.buf
[1] & 31) == 0x10) {
926 DPRINTF("SAI READ CAPACITY(16)\n");
927 memset(outbuf
, 0, req
->cmd
.xfer
);
928 bdrv_get_geometry(s
->bs
, &nb_sectors
);
931 nb_sectors
/= s
->cluster_size
;
932 /* Returned value is the address of the last sector. */
934 /* Remember the new size for read/write sanity checking. */
935 s
->max_lba
= nb_sectors
;
936 outbuf
[0] = (nb_sectors
>> 56) & 0xff;
937 outbuf
[1] = (nb_sectors
>> 48) & 0xff;
938 outbuf
[2] = (nb_sectors
>> 40) & 0xff;
939 outbuf
[3] = (nb_sectors
>> 32) & 0xff;
940 outbuf
[4] = (nb_sectors
>> 24) & 0xff;
941 outbuf
[5] = (nb_sectors
>> 16) & 0xff;
942 outbuf
[6] = (nb_sectors
>> 8) & 0xff;
943 outbuf
[7] = nb_sectors
& 0xff;
946 outbuf
[10] = s
->cluster_size
* 2;
949 outbuf
[13] = get_physical_block_exp(&s
->qdev
.conf
);
951 /* set TPE bit if the format supports discard */
952 if (s
->qdev
.conf
.discard_granularity
) {
956 /* Protection, exponent and lowest lba field left blank. */
957 buflen
= req
->cmd
.xfer
;
960 DPRINTF("Unsupported Service Action In\n");
961 goto illegal_request
;
963 if (req
->cmd
.xfer
< 16)
964 goto illegal_request
;
965 memset(outbuf
, 0, 16);
972 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(INVALID_OPCODE
));
975 scsi_req_set_status(r
, GOOD
, SENSE_CODE(NO_SENSE
));
979 if (!bdrv_is_inserted(s
->bs
)) {
980 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(NO_MEDIUM
));
982 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(LUN_NOT_READY
));
987 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(INVALID_FIELD
));
991 /* Execute a scsi command. Returns the length of the data expected by the
992 command. This will be Positive for data transfers from the device
993 (eg. disk reads), negative for transfers to the device (eg. disk writes),
994 and zero if the command does not transfer any data. */
996 static int32_t scsi_send_command(SCSIRequest
*req
, uint8_t *buf
)
998 SCSIDiskReq
*r
= DO_UPCAST(SCSIDiskReq
, req
, req
);
999 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, req
->dev
);
1006 outbuf
= (uint8_t *)r
->iov
.iov_base
;
1007 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req
->lun
, req
->tag
, buf
[0]);
1009 if (scsi_req_parse(&r
->req
, buf
) != 0) {
1010 BADF("Unsupported command length, command %x\n", command
);
1011 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(INVALID_OPCODE
));
1017 for (i
= 1; i
< r
->req
.cmd
.len
; i
++) {
1018 printf(" 0x%02x", buf
[i
]);
1025 /* Only LUN 0 supported. */
1026 DPRINTF("Unimplemented LUN %d\n", req
->lun
);
1027 if (command
!= REQUEST_SENSE
&& command
!= INQUIRY
) {
1028 scsi_command_complete(r
, CHECK_CONDITION
,
1029 SENSE_CODE(LUN_NOT_SUPPORTED
));
1034 case TEST_UNIT_READY
:
1044 case ALLOW_MEDIUM_REMOVAL
:
1045 case READ_CAPACITY_10
:
1046 case SYNCHRONIZE_CACHE
:
1048 case GET_CONFIGURATION
:
1049 case SERVICE_ACTION_IN
:
1052 rc
= scsi_disk_emulate_command(r
, outbuf
);
1057 r
->iov
.iov_len
= rc
;
1063 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1064 DPRINTF("Read (sector %" PRId64
", count %d)\n", r
->req
.cmd
.lba
, len
);
1065 if (r
->req
.cmd
.lba
> s
->max_lba
)
1067 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1068 r
->sector_count
= len
* s
->cluster_size
;
1074 case WRITE_VERIFY_10
:
1075 case WRITE_VERIFY_12
:
1076 case WRITE_VERIFY_16
:
1077 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1078 DPRINTF("Write %s(sector %" PRId64
", count %d)\n",
1079 (command
& 0xe) == 0xe ? "And Verify " : "",
1080 r
->req
.cmd
.lba
, len
);
1081 if (r
->req
.cmd
.lba
> s
->max_lba
)
1083 r
->sector
= r
->req
.cmd
.lba
* s
->cluster_size
;
1084 r
->sector_count
= len
* s
->cluster_size
;
1087 DPRINTF("Mode Select(6) (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
> 12) {
1094 case MODE_SELECT_10
:
1095 DPRINTF("Mode Select(10) (len %lu)\n", (long)r
->req
.cmd
.xfer
);
1096 /* We don't support mode parameter changes.
1097 Allow the mode parameter header + block descriptors only. */
1098 if (r
->req
.cmd
.xfer
> 16) {
1104 DPRINTF("Seek(%d) (sector %" PRId64
")\n", command
== SEEK_6
? 6 : 10,
1106 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1111 len
= r
->req
.cmd
.xfer
/ s
->qdev
.blocksize
;
1113 DPRINTF("WRITE SAME(16) (sector %" PRId64
", count %d)\n",
1114 r
->req
.cmd
.lba
, len
);
1116 if (r
->req
.cmd
.lba
> s
->max_lba
) {
1121 * We only support WRITE SAME with the unmap bit set for now.
1123 if (!(buf
[1] & 0x8)) {
1127 rc
= bdrv_discard(s
->bs
, r
->req
.cmd
.lba
* s
->cluster_size
,
1128 len
* s
->cluster_size
);
1130 /* XXX: better error code ?*/
1136 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
1137 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(INVALID_OPCODE
));
1140 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(INVALID_FIELD
));
1143 scsi_command_complete(r
, CHECK_CONDITION
, SENSE_CODE(LBA_OUT_OF_RANGE
));
1146 if (r
->sector_count
== 0 && r
->iov
.iov_len
== 0) {
1147 scsi_command_complete(r
, GOOD
, SENSE_CODE(NO_SENSE
));
1149 len
= r
->sector_count
* 512 + r
->iov
.iov_len
;
1150 if (r
->req
.cmd
.mode
== SCSI_XFER_TO_DEV
) {
1153 if (!r
->sector_count
)
1154 r
->sector_count
= -1;
1159 static void scsi_disk_reset(DeviceState
*dev
)
1161 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
.qdev
, dev
);
1162 uint64_t nb_sectors
;
1164 scsi_device_purge_requests(&s
->qdev
);
1166 bdrv_get_geometry(s
->bs
, &nb_sectors
);
1167 nb_sectors
/= s
->cluster_size
;
1171 s
->max_lba
= nb_sectors
;
1174 static void scsi_destroy(SCSIDevice
*dev
)
1176 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1178 scsi_device_purge_requests(&s
->qdev
);
1179 blockdev_mark_auto_del(s
->qdev
.conf
.bs
);
1182 static int scsi_initfn(SCSIDevice
*dev
, uint8_t scsi_type
)
1184 SCSIDiskState
*s
= DO_UPCAST(SCSIDiskState
, qdev
, dev
);
1187 if (!s
->qdev
.conf
.bs
) {
1188 error_report("scsi-disk: drive property not set");
1191 s
->bs
= s
->qdev
.conf
.bs
;
1193 if (scsi_type
== TYPE_DISK
&& !bdrv_is_inserted(s
->bs
)) {
1194 error_report("Device needs media, but drive is empty");
1199 /* try to fall back to value set with legacy -drive serial=... */
1200 dinfo
= drive_get_by_blockdev(s
->bs
);
1201 if (*dinfo
->serial
) {
1202 s
->serial
= qemu_strdup(dinfo
->serial
);
1207 s
->version
= qemu_strdup(QEMU_VERSION
);
1210 if (bdrv_is_sg(s
->bs
)) {
1211 error_report("scsi-disk: unwanted /dev/sg*");
1215 if (scsi_type
== TYPE_ROM
) {
1216 s
->qdev
.blocksize
= 2048;
1217 } else if (scsi_type
== TYPE_DISK
) {
1218 s
->qdev
.blocksize
= s
->qdev
.conf
.logical_block_size
;
1220 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type
);
1223 s
->cluster_size
= s
->qdev
.blocksize
/ 512;
1224 s
->bs
->buffer_alignment
= s
->qdev
.blocksize
;
1226 s
->qdev
.type
= scsi_type
;
1227 qemu_add_vm_change_state_handler(scsi_dma_restart_cb
, s
);
1228 bdrv_set_removable(s
->bs
, scsi_type
== TYPE_ROM
);
1229 add_boot_device_path(s
->qdev
.conf
.bootindex
, &dev
->qdev
, ",0");
1233 static int scsi_hd_initfn(SCSIDevice
*dev
)
1235 return scsi_initfn(dev
, TYPE_DISK
);
1238 static int scsi_cd_initfn(SCSIDevice
*dev
)
1240 return scsi_initfn(dev
, TYPE_ROM
);
1243 static int scsi_disk_initfn(SCSIDevice
*dev
)
1248 if (!dev
->conf
.bs
) {
1249 scsi_type
= TYPE_DISK
; /* will die in scsi_initfn() */
1251 dinfo
= drive_get_by_blockdev(dev
->conf
.bs
);
1252 scsi_type
= dinfo
->media_cd
? TYPE_ROM
: TYPE_DISK
;
1255 return scsi_initfn(dev
, scsi_type
);
1258 #define DEFINE_SCSI_DISK_PROPERTIES() \
1259 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1260 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1261 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1263 static SCSIDeviceInfo scsi_disk_info
[] = {
1265 .qdev
.name
= "scsi-hd",
1266 .qdev
.fw_name
= "disk",
1267 .qdev
.desc
= "virtual SCSI disk",
1268 .qdev
.size
= sizeof(SCSIDiskState
),
1269 .qdev
.reset
= scsi_disk_reset
,
1270 .init
= scsi_hd_initfn
,
1271 .destroy
= scsi_destroy
,
1272 .alloc_req
= scsi_new_request
,
1273 .free_req
= scsi_free_request
,
1274 .send_command
= scsi_send_command
,
1275 .read_data
= scsi_read_data
,
1276 .write_data
= scsi_write_data
,
1277 .cancel_io
= scsi_cancel_io
,
1278 .get_buf
= scsi_get_buf
,
1279 .get_sense
= scsi_get_sense
,
1280 .qdev
.props
= (Property
[]) {
1281 DEFINE_SCSI_DISK_PROPERTIES(),
1282 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1283 DEFINE_PROP_END_OF_LIST(),
1286 .qdev
.name
= "scsi-cd",
1287 .qdev
.fw_name
= "disk",
1288 .qdev
.desc
= "virtual SCSI CD-ROM",
1289 .qdev
.size
= sizeof(SCSIDiskState
),
1290 .qdev
.reset
= scsi_disk_reset
,
1291 .init
= scsi_cd_initfn
,
1292 .destroy
= scsi_destroy
,
1293 .alloc_req
= scsi_new_request
,
1294 .free_req
= scsi_free_request
,
1295 .send_command
= scsi_send_command
,
1296 .read_data
= scsi_read_data
,
1297 .write_data
= scsi_write_data
,
1298 .cancel_io
= scsi_cancel_io
,
1299 .get_buf
= scsi_get_buf
,
1300 .get_sense
= scsi_get_sense
,
1301 .qdev
.props
= (Property
[]) {
1302 DEFINE_SCSI_DISK_PROPERTIES(),
1303 DEFINE_PROP_END_OF_LIST(),
1306 .qdev
.name
= "scsi-disk", /* legacy -device scsi-disk */
1307 .qdev
.fw_name
= "disk",
1308 .qdev
.desc
= "virtual SCSI disk or CD-ROM (legacy)",
1309 .qdev
.size
= sizeof(SCSIDiskState
),
1310 .qdev
.reset
= scsi_disk_reset
,
1311 .init
= scsi_disk_initfn
,
1312 .destroy
= scsi_destroy
,
1313 .alloc_req
= scsi_new_request
,
1314 .free_req
= scsi_free_request
,
1315 .send_command
= scsi_send_command
,
1316 .read_data
= scsi_read_data
,
1317 .write_data
= scsi_write_data
,
1318 .cancel_io
= scsi_cancel_io
,
1319 .get_buf
= scsi_get_buf
,
1320 .get_sense
= scsi_get_sense
,
1321 .qdev
.props
= (Property
[]) {
1322 DEFINE_SCSI_DISK_PROPERTIES(),
1323 DEFINE_PROP_BIT("removable", SCSIDiskState
, removable
, 0, false),
1324 DEFINE_PROP_END_OF_LIST(),
1329 static void scsi_disk_register_devices(void)
1333 for (i
= 0; i
< ARRAY_SIZE(scsi_disk_info
); i
++) {
1334 scsi_qdev_register(&scsi_disk_info
[i
]);
1337 device_init(scsi_disk_register_devices
)