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.
15 #define DPRINTF(fmt, args...) \
16 do { printf("scsi-disk: " fmt , ##args); } while (0)
18 #define DPRINTF(fmt, args...) do {} while(0)
21 #define BADF(fmt, args...) \
22 do { fprintf(stderr, "scsi-disk: " fmt , ##args); } while (0)
26 #define SENSE_NO_SENSE 0
27 #define SENSE_ILLEGAL_REQUEST 5
33 BlockDriverState *bdrv;
34 /* The qemu block layer uses a fixed 512 byte sector size.
35 This is the number of 512 byte blocks in a single scsi sector. */
37 /* When transfering data buf_pos and buf_len contain a partially
38 transferred block of data (or response to a command), and
39 sector/sector_count identify any remaining sectors.
40 Both sector and sector_count are in terms of qemu 512 byte blocks. */
41 /* ??? We should probably keep track of whether the data trasfer is
42 a read or a write. Currently we rely on the host getting it right. */
49 scsi_completionfn completion;
53 static void scsi_command_complete(SCSIDevice *s, int sense)
56 s->completion(s->opaque, s->tag, sense != SENSE_NO_SENSE);
59 /* Read data from a scsi device. Returns nonzero on failure. */
60 int scsi_read_data(SCSIDevice *s, uint8_t *data, uint32_t len)
64 DPRINTF("Read %d (%d/%d)\n", len, s->buf_len, s->sector_count);
65 if (s->buf_len == 0 && s->sector_count == 0)
72 memcpy(data, s->buf + s->buf_pos, n);
82 if (n > s->sector_count)
86 bdrv_read(s->bdrv, s->sector, data, n);
93 if (len && s->sector_count) {
94 bdrv_read(s->bdrv, s->sector, s->buf, 512);
99 /* Recurse to complete the partial read. */
100 return scsi_read_data(s, data, len);
106 if (s->buf_len == 0 && s->sector_count == 0)
107 scsi_command_complete(s, SENSE_NO_SENSE);
112 /* Read data to a scsi device. Returns nonzero on failure. */
113 int scsi_write_data(SCSIDevice *s, uint8_t *data, uint32_t len)
117 DPRINTF("Write %d (%d/%d)\n", len, s->buf_len, s->sector_count);
118 if (s->buf_pos != 0) {
119 BADF("Bad state on write\n");
123 if (s->sector_count == 0)
126 if (s->buf_len != 0 || len < 512) {
127 n = 512 - s->buf_len;
131 memcpy(s->buf + s->buf_len, data, n);
135 if (s->buf_len == 512) {
136 /* A full sector has been accumulated. Write it to disk. */
137 bdrv_write(s->bdrv, s->sector, s->buf, 1);
145 if (n > s->sector_count)
149 bdrv_write(s->bdrv, s->sector, data, n);
153 s->sector_count -= n;
159 if (len && s->sector_count) {
160 /* Recurse to complete the partial write. */
161 return scsi_write_data(s, data, len);
167 if (s->sector_count == 0)
168 scsi_command_complete(s, SENSE_NO_SENSE);
173 /* Execute a scsi command. Returns the length of the data expected by the
174 command. This will be Positive for data transfers from the device
175 (eg. disk reads), negative for transfers to the device (eg. disk writes),
176 and zero if the command does not transfer any data. */
178 int32_t scsi_send_command(SCSIDevice *s, uint32_t tag, uint8_t *buf)
192 DPRINTF("Command: 0x%02x", buf[0]);
193 switch (s->command >> 5) {
195 lba = buf[3] | (buf[2] << 8) | ((buf[1] & 0x1f) << 16);
201 lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
202 len = buf[8] | (buf[7] << 8);
206 lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
207 len = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24);
211 lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
212 len = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24);
216 BADF("Unsupported command length\n");
222 for (i = 1; i < cmdlen; i++) {
223 printf(" 0x%02x", buf[i]);
229 /* Only LUN 0 supported. */
232 switch (s->command) {
234 DPRINTF("Test Unit Ready\n");
237 DPRINTF("Request Sense (len %d)\n", len);
243 s->buf[2] = s->sense;
247 DPRINTF("Inquiry (len %d)\n", len);
249 BADF("Inquiry buffer too small (%d)\n", len);
251 memset(s->buf, 0, 36);
252 if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
255 memcpy(&s->buf[16], "QEMU CDROM ", 16);
258 memcpy(&s->buf[16], "QEMU HARDDISK ", 16);
260 memcpy(&s->buf[8], "QEMU ", 8);
261 s->buf[2] = 3; /* SCSI-3 */
262 s->buf[3] = 2; /* Format 2 */
267 DPRINTF("Reserve(6)\n");
272 DPRINTF("Release(6)\n");
277 DPRINTF("Mode Sense(6) (page %d, len %d)\n", buf[2], len);
278 memset(s->buf, 0, 4);
279 s->buf[0] = 0x16; /* Mode data length (4 + 0x12). */
280 s->buf[1] = 0; /* Default media type. */
281 s->buf[2] = 0; /* Write enabled. */
282 s->buf[3] = 0; /* Block descriptor length. */
285 s->buf[4 + 1] = 0x12;
286 s->buf[4 + 2] = 4; /* WCE */
292 DPRINTF("Read Capacity\n");
293 /* The normal LEN field for this command is zero. */
294 memset(s->buf, 0, 8);
295 bdrv_get_geometry(s->bdrv, &nb_sectors);
296 s->buf[0] = (nb_sectors >> 24) & 0xff;
297 s->buf[1] = (nb_sectors >> 16) & 0xff;
298 s->buf[2] = (nb_sectors >> 8) & 0xff;
299 s->buf[3] = nb_sectors & 0xff;
302 s->buf[6] = s->cluster_size * 2;
308 DPRINTF("Read (sector %d, count %d)\n", lba, len);
309 s->sector = lba * s->cluster_size;
310 s->sector_count = len * s->cluster_size;
314 DPRINTF("Write (sector %d, count %d)\n", lba, len);
315 s->sector = lba * s->cluster_size;
316 s->sector_count = len * s->cluster_size;
321 int start_track, format, msf, toclen;
324 format = buf[2] & 0xf;
325 start_track = buf[6];
326 bdrv_get_geometry(s->bdrv, &nb_sectors);
327 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
330 toclen = cdrom_read_toc(nb_sectors, s->buf, msf, start_track);
333 /* multi session : only a single session defined */
335 memset(s->buf, 0, 12);
341 toclen = cdrom_read_toc_raw(nb_sectors, s->buf, msf, start_track);
353 DPRINTF("Read TOC error\n");
357 DPRINTF("Reserve(10)\n");
362 DPRINTF("Release(10)\n");
367 DPRINTF("Report LUNs (len %d)\n", len);
370 memset(s->buf, 0, 16);
375 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
377 scsi_command_complete(s, SENSE_ILLEGAL_REQUEST);
380 if (s->sector_count == 0 && s->buf_len == 0) {
381 scsi_command_complete(s, SENSE_NO_SENSE);
383 len = s->sector_count * 512 + s->buf_len;
384 return is_write ? -len : len;
387 void scsi_disk_destroy(SCSIDevice *s)
393 SCSIDevice *scsi_disk_init(BlockDriverState *bdrv,
394 scsi_completionfn completion,
399 s = (SCSIDevice *)qemu_mallocz(sizeof(SCSIDevice));
401 s->completion = completion;
403 if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {