2 * SCSI Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
7 * Written by Paul Brook
9 * This code is licenced under the LGPL.
11 * Note that this file only handles the SCSI architecture model and device
12 * commands. Emulation of interface/link layer protocols is handled by
13 * the host adapter emulator.
19 #define DPRINTF(fmt, args...) \
20 do { printf("scsi-disk: " fmt , ##args); } while (0)
22 #define DPRINTF(fmt, args...) do {} while(0)
25 #define BADF(fmt, args...) \
26 do { fprintf(stderr, "scsi-disk: " fmt , ##args); } while (0)
28 #include "qemu-common.h"
30 #include "scsi-disk.h"
32 #define SENSE_NO_SENSE 0
33 #define SENSE_NOT_READY 2
34 #define SENSE_HARDWARE_ERROR 4
35 #define SENSE_ILLEGAL_REQUEST 5
38 #define STATUS_CHECK_CONDITION 2
40 #define SCSI_DMA_BUF_SIZE 131072
41 #define SCSI_MAX_INQUIRY_LEN 256
43 typedef struct SCSIRequest
{
46 /* ??? We should probably keep track of whether the data trasfer is
47 a read or a write. Currently we rely on the host getting it right. */
48 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
51 /* The amounnt of data in the buffer. */
54 BlockDriverAIOCB
*aiocb
;
55 struct SCSIRequest
*next
;
58 struct SCSIDeviceState
60 BlockDriverState
*bdrv
;
61 SCSIRequest
*requests
;
62 /* The qemu block layer uses a fixed 512 byte sector size.
63 This is the number of 512 byte blocks in a single scsi sector. */
67 /* Completion functions may be called from either scsi_{read,write}_data
68 or from the AIO completion routines. */
69 scsi_completionfn completion
;
73 /* Global pool of SCSIRequest structures. */
74 static SCSIRequest
*free_requests
= NULL
;
76 static SCSIRequest
*scsi_new_request(SCSIDeviceState
*s
, uint32_t tag
)
82 free_requests
= r
->next
;
84 r
= qemu_malloc(sizeof(SCSIRequest
));
85 r
->dma_buf
= qemu_memalign(512, SCSI_DMA_BUF_SIZE
);
93 r
->next
= s
->requests
;
98 static void scsi_remove_request(SCSIRequest
*r
)
101 SCSIDeviceState
*s
= r
->dev
;
103 if (s
->requests
== r
) {
104 s
->requests
= r
->next
;
107 while (last
&& last
->next
!= r
)
110 last
->next
= r
->next
;
112 BADF("Orphaned request\n");
115 r
->next
= free_requests
;
119 static SCSIRequest
*scsi_find_request(SCSIDeviceState
*s
, uint32_t tag
)
124 while (r
&& r
->tag
!= tag
)
130 /* Helper function for command completion. */
131 static void scsi_command_complete(SCSIRequest
*r
, int status
, int sense
)
133 SCSIDeviceState
*s
= r
->dev
;
135 DPRINTF("Command complete tag=0x%x status=%d sense=%d\n", r
->tag
, status
, sense
);
138 scsi_remove_request(r
);
139 s
->completion(s
->opaque
, SCSI_REASON_DONE
, tag
, status
);
142 /* Cancel a pending data transfer. */
143 static void scsi_cancel_io(SCSIDevice
*d
, uint32_t tag
)
145 SCSIDeviceState
*s
= d
->state
;
147 DPRINTF("Cancel tag=0x%x\n", tag
);
148 r
= scsi_find_request(s
, tag
);
151 bdrv_aio_cancel(r
->aiocb
);
153 scsi_remove_request(r
);
157 static void scsi_read_complete(void * opaque
, int ret
)
159 SCSIRequest
*r
= (SCSIRequest
*)opaque
;
160 SCSIDeviceState
*s
= r
->dev
;
163 DPRINTF("IO error\n");
164 s
->completion(s
->opaque
, SCSI_REASON_DATA
, r
->tag
, 0);
165 scsi_command_complete(r
, STATUS_CHECK_CONDITION
, SENSE_NO_SENSE
);
168 DPRINTF("Data ready tag=0x%x len=%d\n", r
->tag
, r
->buf_len
);
170 s
->completion(s
->opaque
, SCSI_REASON_DATA
, r
->tag
, r
->buf_len
);
173 /* Read more data from scsi device into buffer. */
174 static void scsi_read_data(SCSIDevice
*d
, uint32_t tag
)
176 SCSIDeviceState
*s
= d
->state
;
180 r
= scsi_find_request(s
, tag
);
182 BADF("Bad read tag 0x%x\n", tag
);
183 /* ??? This is the wrong error. */
184 scsi_command_complete(r
, STATUS_CHECK_CONDITION
, SENSE_HARDWARE_ERROR
);
187 if (r
->sector_count
== (uint32_t)-1) {
188 DPRINTF("Read buf_len=%d\n", r
->buf_len
);
190 s
->completion(s
->opaque
, SCSI_REASON_DATA
, r
->tag
, r
->buf_len
);
193 DPRINTF("Read sector_count=%d\n", r
->sector_count
);
194 if (r
->sector_count
== 0) {
195 scsi_command_complete(r
, STATUS_GOOD
, SENSE_NO_SENSE
);
200 if (n
> SCSI_DMA_BUF_SIZE
/ 512)
201 n
= SCSI_DMA_BUF_SIZE
/ 512;
203 r
->buf_len
= n
* 512;
204 r
->aiocb
= bdrv_aio_read(s
->bdrv
, r
->sector
, r
->dma_buf
, n
,
205 scsi_read_complete
, r
);
206 if (r
->aiocb
== NULL
)
207 scsi_command_complete(r
, STATUS_CHECK_CONDITION
, SENSE_HARDWARE_ERROR
);
209 r
->sector_count
-= n
;
212 static void scsi_write_complete(void * opaque
, int ret
)
214 SCSIRequest
*r
= (SCSIRequest
*)opaque
;
215 SCSIDeviceState
*s
= r
->dev
;
219 fprintf(stderr
, "scsi-disc: IO write error\n");
224 if (r
->sector_count
== 0) {
225 scsi_command_complete(r
, STATUS_GOOD
, SENSE_NO_SENSE
);
227 len
= r
->sector_count
* 512;
228 if (len
> SCSI_DMA_BUF_SIZE
) {
229 len
= SCSI_DMA_BUF_SIZE
;
232 DPRINTF("Write complete tag=0x%x more=%d\n", r
->tag
, len
);
233 s
->completion(s
->opaque
, SCSI_REASON_DATA
, r
->tag
, len
);
237 /* Write data to a scsi device. Returns nonzero on failure.
238 The transfer may complete asynchronously. */
239 static int scsi_write_data(SCSIDevice
*d
, uint32_t tag
)
241 SCSIDeviceState
*s
= d
->state
;
245 DPRINTF("Write data tag=0x%x\n", tag
);
246 r
= scsi_find_request(s
, tag
);
248 BADF("Bad write tag 0x%x\n", tag
);
249 scsi_command_complete(r
, STATUS_CHECK_CONDITION
, SENSE_HARDWARE_ERROR
);
253 BADF("Data transfer already in progress\n");
254 n
= r
->buf_len
/ 512;
256 r
->aiocb
= bdrv_aio_write(s
->bdrv
, r
->sector
, r
->dma_buf
, n
,
257 scsi_write_complete
, r
);
258 if (r
->aiocb
== NULL
)
259 scsi_command_complete(r
, STATUS_CHECK_CONDITION
,
260 SENSE_HARDWARE_ERROR
);
262 r
->sector_count
-= n
;
264 /* Invoke completion routine to fetch data from host. */
265 scsi_write_complete(r
, 0);
271 /* Return a pointer to the data buffer. */
272 static uint8_t *scsi_get_buf(SCSIDevice
*d
, uint32_t tag
)
274 SCSIDeviceState
*s
= d
->state
;
277 r
= scsi_find_request(s
, tag
);
279 BADF("Bad buffer tag 0x%x\n", tag
);
285 /* Execute a scsi command. Returns the length of the data expected by the
286 command. This will be Positive for data transfers from the device
287 (eg. disk reads), negative for transfers to the device (eg. disk writes),
288 and zero if the command does not transfer any data. */
290 static int32_t scsi_send_command(SCSIDevice
*d
, uint32_t tag
,
291 uint8_t *buf
, int lun
)
293 SCSIDeviceState
*s
= d
->state
;
304 r
= scsi_find_request(s
, tag
);
306 BADF("Tag 0x%x already in use\n", tag
);
307 scsi_cancel_io(d
, tag
);
309 /* ??? Tags are not unique for different luns. We only implement a
310 single lun, so this should not matter. */
311 r
= scsi_new_request(s
, tag
);
314 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun
, tag
, buf
[0]);
315 switch (command
>> 5) {
317 lba
= buf
[3] | (buf
[2] << 8) | ((buf
[1] & 0x1f) << 16);
323 lba
= buf
[5] | (buf
[4] << 8) | (buf
[3] << 16) | (buf
[2] << 24);
324 len
= buf
[8] | (buf
[7] << 8);
328 lba
= buf
[5] | (buf
[4] << 8) | (buf
[3] << 16) | (buf
[2] << 24);
329 len
= buf
[13] | (buf
[12] << 8) | (buf
[11] << 16) | (buf
[10] << 24);
333 lba
= buf
[5] | (buf
[4] << 8) | (buf
[3] << 16) | (buf
[2] << 24);
334 len
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16) | (buf
[6] << 24);
338 BADF("Unsupported command length, command %x\n", command
);
344 for (i
= 1; i
< cmdlen
; i
++) {
345 printf(" 0x%02x", buf
[i
]);
350 if (lun
|| buf
[1] >> 5) {
351 /* Only LUN 0 supported. */
352 DPRINTF("Unimplemented LUN %d\n", lun
? lun
: buf
[1] >> 5);
353 if (command
!= 0x03 && command
!= 0x12) /* REQUEST SENSE and INQUIRY */
358 DPRINTF("Test Unit Ready\n");
361 DPRINTF("Request Sense (len %d)\n", len
);
364 memset(outbuf
, 0, 4);
367 outbuf
[2] = s
->sense
;
371 DPRINTF("Inquiry (len %d)\n", len
);
373 /* Command support data - optional, not implemented */
374 BADF("optional INQUIRY command support request not implemented\n");
377 else if (buf
[1] & 0x1) {
378 /* Vital product data */
379 uint8_t page_code
= buf
[2];
381 BADF("Error: Inquiry (EVPD[%02X]) buffer size %d is "
382 "less than 4\n", page_code
, len
);
389 /* Supported page codes, mandatory */
390 DPRINTF("Inquiry EVPD[Supported pages] "
391 "buffer size %d\n", len
);
395 if (bdrv_get_type_hint(s
->bdrv
) == BDRV_TYPE_CDROM
) {
396 outbuf
[r
->buf_len
++] = 5;
398 outbuf
[r
->buf_len
++] = 0;
401 outbuf
[r
->buf_len
++] = 0x00; // this page
402 outbuf
[r
->buf_len
++] = 0x00;
403 outbuf
[r
->buf_len
++] = 3; // number of pages
404 outbuf
[r
->buf_len
++] = 0x00; // list of supported pages (this page)
405 outbuf
[r
->buf_len
++] = 0x80; // unit serial number
406 outbuf
[r
->buf_len
++] = 0x83; // device identification
411 /* Device serial number, optional */
413 BADF("Error: EVPD[Serial number] Inquiry buffer "
414 "size %d too small, %d needed\n", len
, 4);
418 DPRINTF("Inquiry EVPD[Serial number] buffer size %d\n", len
);
422 /* Supported page codes */
423 if (bdrv_get_type_hint(s
->bdrv
) == BDRV_TYPE_CDROM
) {
424 outbuf
[r
->buf_len
++] = 5;
426 outbuf
[r
->buf_len
++] = 0;
429 outbuf
[r
->buf_len
++] = 0x80; // this page
430 outbuf
[r
->buf_len
++] = 0x00;
431 outbuf
[r
->buf_len
++] = 0x01; // 1 byte data follow
433 outbuf
[r
->buf_len
++] = '0'; // 1 byte data follow
439 /* Device identification page, mandatory */
440 int max_len
= 255 - 8;
441 int id_len
= strlen(bdrv_get_device_name(s
->bdrv
));
442 if (id_len
> max_len
)
445 DPRINTF("Inquiry EVPD[Device identification] "
446 "buffer size %d\n", len
);
448 if (bdrv_get_type_hint(s
->bdrv
) == BDRV_TYPE_CDROM
) {
449 outbuf
[r
->buf_len
++] = 5;
451 outbuf
[r
->buf_len
++] = 0;
454 outbuf
[r
->buf_len
++] = 0x83; // this page
455 outbuf
[r
->buf_len
++] = 0x00;
456 outbuf
[r
->buf_len
++] = 3 + id_len
;
458 outbuf
[r
->buf_len
++] = 0x2; // ASCII
459 outbuf
[r
->buf_len
++] = 0; // not officially assigned
460 outbuf
[r
->buf_len
++] = 0; // reserved
461 outbuf
[r
->buf_len
++] = id_len
; // length of data following
463 memcpy(&outbuf
[r
->buf_len
],
464 bdrv_get_device_name(s
->bdrv
), id_len
);
465 r
->buf_len
+= id_len
;
469 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
470 "buffer size %d\n", page_code
, len
);
477 /* Standard INQUIRY data */
479 BADF("Error: Inquiry (STANDARD) page or code "
480 "is non-zero [%02X]\n", buf
[2]);
486 BADF("Error: Inquiry (STANDARD) buffer size %d "
487 "is less than 5\n", len
);
492 BADF("Error: Inquiry (STANDARD) buffer size %d "
493 "is less than 36 (TODO: only 5 required)\n", len
);
497 if(len
> SCSI_MAX_INQUIRY_LEN
)
498 len
= SCSI_MAX_INQUIRY_LEN
;
500 memset(outbuf
, 0, len
);
502 if (lun
|| buf
[1] >> 5) {
503 outbuf
[0] = 0x7f; /* LUN not supported */
504 } else if (bdrv_get_type_hint(s
->bdrv
) == BDRV_TYPE_CDROM
) {
507 memcpy(&outbuf
[16], "QEMU CD-ROM ", 16);
510 memcpy(&outbuf
[16], "QEMU HARDDISK ", 16);
512 memcpy(&outbuf
[8], "QEMU ", 8);
513 memcpy(&outbuf
[32], QEMU_VERSION
, 4);
514 /* Identify device as SCSI-3 rev 1.
515 Some later commands are also implemented. */
517 outbuf
[3] = 2; /* Format 2 */
518 outbuf
[4] = len
- 5; /* Additional Length = (Len - 1) - 4 */
519 /* Sync data transfer and TCQ. */
520 outbuf
[7] = 0x10 | (s
->tcq
? 0x02 : 0);
524 DPRINTF("Reserve(6)\n");
529 DPRINTF("Release(6)\n");
539 page
= buf
[2] & 0x3f;
540 DPRINTF("Mode Sense (page %d, len %d)\n", page
, len
);
543 outbuf
[1] = 0; /* Default media type. */
544 outbuf
[3] = 0; /* Block descriptor length. */
545 if (bdrv_get_type_hint(s
->bdrv
) == BDRV_TYPE_CDROM
) {
546 outbuf
[2] = 0x80; /* Readonly. */
550 int cylinders
, heads
, secs
;
552 /* Rigid disk device geometry page. */
555 /* if a geometry hint is available, use it */
556 bdrv_get_geometry_hint(s
->bdrv
, &cylinders
, &heads
, &secs
);
557 p
[2] = (cylinders
>> 16) & 0xff;
558 p
[3] = (cylinders
>> 8) & 0xff;
559 p
[4] = cylinders
& 0xff;
561 /* Write precomp start cylinder, disabled */
562 p
[6] = (cylinders
>> 16) & 0xff;
563 p
[7] = (cylinders
>> 8) & 0xff;
564 p
[8] = cylinders
& 0xff;
565 /* Reduced current start cylinder, disabled */
566 p
[9] = (cylinders
>> 16) & 0xff;
567 p
[10] = (cylinders
>> 8) & 0xff;
568 p
[11] = cylinders
& 0xff;
569 /* Device step rate [ns], 200ns */
572 /* Landing zone cylinder */
576 /* Medium rotation rate [rpm], 5400 rpm */
577 p
[20] = (5400 >> 8) & 0xff;
580 } else if (page
== 5) {
581 int cylinders
, heads
, secs
;
583 /* Flexible disk device geometry page. */
586 /* Transfer rate [kbit/s], 5Mbit/s */
589 /* if a geometry hint is available, use it */
590 bdrv_get_geometry_hint(s
->bdrv
, &cylinders
, &heads
, &secs
);
593 p
[6] = s
->cluster_size
* 2;
594 p
[8] = (cylinders
>> 8) & 0xff;
595 p
[9] = cylinders
& 0xff;
596 /* Write precomp start cylinder, disabled */
597 p
[10] = (cylinders
>> 8) & 0xff;
598 p
[11] = cylinders
& 0xff;
599 /* Reduced current start cylinder, disabled */
600 p
[12] = (cylinders
>> 8) & 0xff;
601 p
[13] = cylinders
& 0xff;
602 /* Device step rate [100us], 100us */
605 /* Device step pulse width [us], 1us */
607 /* Device head settle delay [100us], 100us */
610 /* Motor on delay [0.1s], 0.1s */
612 /* Motor off delay [0.1s], 0.1s */
614 /* Medium rotation rate [rpm], 5400 rpm */
615 p
[28] = (5400 >> 8) & 0xff;
618 } else if ((page
== 8 || page
== 0x3f)) {
626 if ((page
== 0x3f || page
== 0x2a)
627 && (bdrv_get_type_hint(s
->bdrv
) == BDRV_TYPE_CDROM
)) {
628 /* CD Capabilities and Mechanical Status page. */
631 p
[2] = 3; // CD-R & CD-RW read
632 p
[3] = 0; // Writing not supported
633 p
[4] = 0x7f; /* Audio, composite, digital out,
634 mode 2 form 1&2, multi session */
635 p
[5] = 0xff; /* CD DA, DA accurate, RW supported,
636 RW corrected, C2 errors, ISRC,
638 p
[6] = 0x2d | (bdrv_is_locked(s
->bdrv
)? 2 : 0);
639 /* Locking supported, jumper present, eject, tray */
640 p
[7] = 0; /* no volume & mute control, no
642 p
[8] = (50 * 176) >> 8; // 50x read speed
643 p
[9] = (50 * 176) & 0xff;
644 p
[10] = 0 >> 8; // No volume
646 p
[12] = 2048 >> 8; // 2M buffer
648 p
[14] = (16 * 176) >> 8; // 16x read speed current
649 p
[15] = (16 * 176) & 0xff;
650 p
[18] = (16 * 176) >> 8; // 16x write speed
651 p
[19] = (16 * 176) & 0xff;
652 p
[20] = (16 * 176) >> 8; // 16x write speed current
653 p
[21] = (16 * 176) & 0xff;
656 r
->buf_len
= p
- outbuf
;
657 outbuf
[0] = r
->buf_len
- 4;
658 if (r
->buf_len
> len
)
663 DPRINTF("Start Stop Unit\n");
666 DPRINTF("Prevent Allow Medium Removal (prevent = %d)\n", buf
[4] & 3);
667 bdrv_set_locked(s
->bdrv
, buf
[4] & 1);
670 DPRINTF("Read Capacity\n");
671 /* The normal LEN field for this command is zero. */
672 memset(outbuf
, 0, 8);
673 bdrv_get_geometry(s
->bdrv
, &nb_sectors
);
674 /* Returned value is the address of the last sector. */
677 outbuf
[0] = (nb_sectors
>> 24) & 0xff;
678 outbuf
[1] = (nb_sectors
>> 16) & 0xff;
679 outbuf
[2] = (nb_sectors
>> 8) & 0xff;
680 outbuf
[3] = nb_sectors
& 0xff;
683 outbuf
[6] = s
->cluster_size
* 2;
687 scsi_command_complete(r
, STATUS_CHECK_CONDITION
, SENSE_NOT_READY
);
693 DPRINTF("Read (sector %d, count %d)\n", lba
, len
);
694 r
->sector
= lba
* s
->cluster_size
;
695 r
->sector_count
= len
* s
->cluster_size
;
699 DPRINTF("Write (sector %d, count %d)\n", lba
, len
);
700 r
->sector
= lba
* s
->cluster_size
;
701 r
->sector_count
= len
* s
->cluster_size
;
705 DPRINTF("Synchronise cache (sector %d, count %d)\n", lba
, len
);
710 int start_track
, format
, msf
, toclen
;
713 format
= buf
[2] & 0xf;
714 start_track
= buf
[6];
715 bdrv_get_geometry(s
->bdrv
, &nb_sectors
);
716 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track
, format
, msf
>> 1);
719 toclen
= cdrom_read_toc(nb_sectors
, outbuf
, msf
, start_track
);
722 /* multi session : only a single session defined */
724 memset(outbuf
, 0, 12);
730 toclen
= cdrom_read_toc_raw(nb_sectors
, outbuf
, msf
, start_track
);
742 DPRINTF("Read TOC error\n");
746 DPRINTF("Get Configuration (rt %d, maxlen %d)\n", buf
[1] & 3, len
);
747 memset(outbuf
, 0, 8);
748 /* ??? This should probably return much more information. For now
749 just return the basic header indicating the CD-ROM profile. */
750 outbuf
[7] = 8; // CD-ROM
754 DPRINTF("Reserve(10)\n");
759 DPRINTF("Release(10)\n");
764 DPRINTF("Report LUNs (len %d)\n", len
);
767 memset(outbuf
, 0, 16);
772 DPRINTF("Verify (sector %d, count %d)\n", lba
, len
);
775 DPRINTF("Unknown SCSI command (%2.2x)\n", buf
[0]);
777 scsi_command_complete(r
, STATUS_CHECK_CONDITION
, SENSE_ILLEGAL_REQUEST
);
780 if (r
->sector_count
== 0 && r
->buf_len
== 0) {
781 scsi_command_complete(r
, STATUS_GOOD
, SENSE_NO_SENSE
);
783 len
= r
->sector_count
* 512 + r
->buf_len
;
787 if (!r
->sector_count
)
788 r
->sector_count
= -1;
793 static void scsi_destroy(SCSIDevice
*d
)
799 SCSIDevice
*scsi_disk_init(BlockDriverState
*bdrv
, int tcq
,
800 scsi_completionfn completion
, void *opaque
)
805 s
= (SCSIDeviceState
*)qemu_mallocz(sizeof(SCSIDeviceState
));
808 s
->completion
= completion
;
810 if (bdrv_get_type_hint(s
->bdrv
) == BDRV_TYPE_CDROM
) {
816 d
= (SCSIDevice
*)qemu_mallocz(sizeof(SCSIDevice
));
818 d
->destroy
= scsi_destroy
;
819 d
->send_command
= scsi_send_command
;
820 d
->read_data
= scsi_read_data
;
821 d
->write_data
= scsi_write_data
;
822 d
->cancel_io
= scsi_cancel_io
;
823 d
->get_buf
= scsi_get_buf
;