Add missing #define.
[qemu] / hw / scsi-disk.c
1 /*
2  * SCSI Device emulation
3  *
4  * Copyright (c) 2006 CodeSourcery.
5  * Based on code by Fabrice Bellard
6  *
7  * Written by Paul Brook
8  *
9  * This code is licenced under the LGPL.
10  */
11
12 //#define DEBUG_SCSI
13
14 #ifdef DEBUG_SCSI
15 #define DPRINTF(fmt, args...) \
16 do { printf("scsi-disk: " fmt , ##args); } while (0)
17 #else
18 #define DPRINTF(fmt, args...) do {} while(0)
19 #endif
20
21 #define BADF(fmt, args...) \
22 do { fprintf(stderr, "scsi-disk: " fmt , ##args); } while (0)
23
24 #include "vl.h"
25
26 #define SENSE_NO_SENSE        0
27 #define SENSE_NOT_READY       2
28 #define SENSE_ILLEGAL_REQUEST 5
29
30 struct SCSIDevice
31 {
32     int command;
33     uint32_t tag;
34     BlockDriverState *bdrv;
35     /* The qemu block layer uses a fixed 512 byte sector size.
36        This is the number of 512 byte blocks in a single scsi sector.  */
37     int cluster_size;
38     /* When transfering data buf_pos and buf_len contain a partially
39        transferred block of data (or response to a command), and
40        sector/sector_count identify any remaining sectors.
41        Both sector and sector_count are in terms of qemu 512 byte blocks.  */
42     /* ??? We should probably keep track of whether the data trasfer is
43        a read or a write.  Currently we rely on the host getting it right.  */
44     int sector;
45     int sector_count;
46     int buf_pos;
47     int buf_len;
48     int sense;
49     char buf[512];
50     scsi_completionfn completion;
51     void *opaque;
52 };
53
54 static void scsi_command_complete(SCSIDevice *s, int sense)
55 {
56     s->sense = sense;
57     s->completion(s->opaque, s->tag, sense);
58 }
59
60 /* Read data from a scsi device.  Returns nonzero on failure.  */
61 int scsi_read_data(SCSIDevice *s, uint8_t *data, uint32_t len)
62 {
63     uint32_t n;
64
65     DPRINTF("Read %d (%d/%d)\n", len, s->buf_len, s->sector_count);
66     if (s->buf_len == 0 && s->sector_count == 0)
67         return 1;
68
69     if (s->buf_len) {
70         n = s->buf_len;
71         if (n > len)
72             n = len;
73         memcpy(data, s->buf + s->buf_pos, n);
74         s->buf_pos += n;
75         s->buf_len -= n;
76         data += n;
77         len -= n;
78         if (s->buf_len == 0)
79             s->buf_pos = 0;
80     }
81
82     n = len / 512;
83     if (n > s->sector_count)
84       n = s->sector_count;
85
86     if (n != 0) {
87         bdrv_read(s->bdrv, s->sector, data, n);
88         data += n * 512;
89         len -= n * 512;
90         s->sector += n;
91         s->sector_count -= n;
92     }
93
94     if (len && s->sector_count) {
95         bdrv_read(s->bdrv, s->sector, s->buf, 1);
96         s->sector++;
97         s->sector_count--;
98         s->buf_pos = 0;
99         s->buf_len = 512;
100         /* Recurse to complete the partial read.  */
101         return scsi_read_data(s, data, len);
102     }
103
104     if (len != 0)
105         return 1;
106
107     if (s->buf_len == 0 && s->sector_count == 0)
108         scsi_command_complete(s, SENSE_NO_SENSE);
109
110     return 0;
111 }
112
113 /* Read data to a scsi device.  Returns nonzero on failure.  */
114 int scsi_write_data(SCSIDevice *s, uint8_t *data, uint32_t len)
115 {
116     uint32_t n;
117
118     DPRINTF("Write %d (%d/%d)\n", len, s->buf_len, s->sector_count);
119     if (s->buf_pos != 0) {
120         BADF("Bad state on write\n");
121         return 1;
122     }
123
124     if (s->sector_count == 0)
125         return 1;
126
127     if (s->buf_len != 0 || len < 512) {
128         n = 512 - s->buf_len;
129         if (n > len)
130             n = len;
131
132         memcpy(s->buf + s->buf_len, data, n);
133         data += n;
134         s->buf_len += n;
135         len -= n;
136         if (s->buf_len == 512) {
137             /* A full sector has been accumulated. Write it to disk.  */
138             bdrv_write(s->bdrv, s->sector, s->buf, 1);
139             s->buf_len = 0;
140             s->sector++;
141             s->sector_count--;
142         }
143     }
144
145     n = len / 512;
146     if (n > s->sector_count)
147         n = s->sector_count;
148
149     if (n != 0) {
150         bdrv_write(s->bdrv, s->sector, data, n);
151         data += n * 512;
152         len -= n * 512;
153         s->sector += n;
154         s->sector_count -= n;
155     }
156
157     if (len >= 512)
158         return 1;
159
160     if (len && s->sector_count) {
161         /* Recurse to complete the partial write.  */
162         return scsi_write_data(s, data, len);
163     }
164
165     if (len != 0)
166         return 1;
167
168     if (s->sector_count == 0)
169         scsi_command_complete(s, SENSE_NO_SENSE);
170
171     return 0;
172 }
173
174 /* Execute a scsi command.  Returns the length of the data expected by the
175    command.  This will be Positive for data transfers from the device
176    (eg. disk reads), negative for transfers to the device (eg. disk writes),
177    and zero if the command does not transfer any data.  */
178
179 int32_t scsi_send_command(SCSIDevice *s, uint32_t tag, uint8_t *buf, int lun)
180 {
181     int64_t nb_sectors;
182     uint32_t lba;
183     uint32_t len;
184     int cmdlen;
185     int is_write;
186
187     s->command = buf[0];
188     s->tag = tag;
189     s->sector_count = 0;
190     s->buf_pos = 0;
191     s->buf_len = 0;
192     is_write = 0;
193     DPRINTF("Command: 0x%02x", buf[0]);
194     switch (s->command >> 5) {
195     case 0:
196         lba = buf[3] | (buf[2] << 8) | ((buf[1] & 0x1f) << 16);
197         len = buf[4];
198         cmdlen = 6;
199         break;
200     case 1:
201     case 2:
202         lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
203         len = buf[8] | (buf[7] << 8);
204         cmdlen = 10;
205         break;
206     case 4:
207         lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
208         len = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24);
209         cmdlen = 16;
210         break;
211     case 5:
212         lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
213         len = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24);
214         cmdlen = 12;
215         break;
216     default:
217         BADF("Unsupported command length, command %x\n", s->command);
218         goto fail;
219     }
220 #ifdef DEBUG_SCSI
221     {
222         int i;
223         for (i = 1; i < cmdlen; i++) {
224             printf(" 0x%02x", buf[i]);
225         }
226         printf("\n");
227     }
228 #endif
229     if (lun || buf[1] >> 5) {
230         /* Only LUN 0 supported.  */
231         DPRINTF("Unimplemented LUN %d\n", lun ? lun : buf[1] >> 5);
232         goto fail;
233     }
234     switch (s->command) {
235     case 0x0:
236         DPRINTF("Test Unit Ready\n");
237         break;
238     case 0x03:
239         DPRINTF("Request Sense (len %d)\n", len);
240         if (len < 4)
241             goto fail;
242         memset(buf, 0, 4);
243         s->buf[0] = 0xf0;
244         s->buf[1] = 0;
245         s->buf[2] = s->sense;
246         s->buf_len = 4;
247         break;
248     case 0x12:
249         DPRINTF("Inquiry (len %d)\n", len);
250         if (len < 36) {
251             BADF("Inquiry buffer too small (%d)\n", len);
252         }
253         memset(s->buf, 0, 36);
254         if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
255             s->buf[0] = 5;
256             s->buf[1] = 0x80;
257             memcpy(&s->buf[16], "QEMU CD-ROM    ", 16);
258         } else {
259             s->buf[0] = 0;
260             memcpy(&s->buf[16], "QEMU HARDDISK  ", 16);
261         }
262         memcpy(&s->buf[8], "QEMU   ", 8);
263         memcpy(&s->buf[32], QEMU_VERSION, 4);
264         /* Identify device as SCSI-3 rev 1.
265            Some later commands are also implemented. */
266         s->buf[2] = 3;
267         s->buf[3] = 2; /* Format 2 */
268         s->buf[4] = 32;
269         s->buf_len = 36;
270         break;
271     case 0x16:
272         DPRINTF("Reserve(6)\n");
273         if (buf[1] & 1)
274             goto fail;
275         break;
276     case 0x17:
277         DPRINTF("Release(6)\n");
278         if (buf[1] & 1)
279             goto fail;
280         break;
281     case 0x1a:
282     case 0x5a:
283         {
284             char *p;
285             int page;
286
287             page = buf[2] & 0x3f;
288             DPRINTF("Mode Sense (page %d, len %d)\n", page, len);
289             p = s->buf;
290             memset(p, 0, 4);
291             s->buf[1] = 0; /* Default media type.  */
292             s->buf[3] = 0; /* Block descriptor length.  */
293             if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
294                 s->buf[2] = 0x80; /* Readonly.  */
295             }
296             p += 4;
297             if ((page == 8 || page == 0x3f)) {
298                 /* Caching page.  */
299                 p[0] = 8;
300                 p[1] = 0x12;
301                 p[2] = 4; /* WCE */
302                 p += 19;
303             }
304             if ((page == 0x3f || page == 0x2a)
305                     && (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM)) {
306                 /* CD Capabilities and Mechanical Status page. */
307                 p[0] = 0x2a;
308                 p[1] = 0x14;
309                 p[2] = 3; // CD-R & CD-RW read
310                 p[3] = 0; // Writing not supported
311                 p[4] = 0x7f; /* Audio, composite, digital out,
312                                          mode 2 form 1&2, multi session */
313                 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
314                                          RW corrected, C2 errors, ISRC,
315                                          UPC, Bar code */
316                 p[6] = 0x2d | (bdrv_is_locked(s->bdrv)? 2 : 0);
317                 /* Locking supported, jumper present, eject, tray */
318                 p[7] = 0; /* no volume & mute control, no
319                                       changer */
320                 p[8] = (50 * 176) >> 8; // 50x read speed
321                 p[9] = (50 * 176) & 0xff;
322                 p[10] = 0 >> 8; // No volume
323                 p[11] = 0 & 0xff;
324                 p[12] = 2048 >> 8; // 2M buffer
325                 p[13] = 2048 & 0xff;
326                 p[14] = (16 * 176) >> 8; // 16x read speed current
327                 p[15] = (16 * 176) & 0xff;
328                 p[18] = (16 * 176) >> 8; // 16x write speed
329                 p[19] = (16 * 176) & 0xff;
330                 p[20] = (16 * 176) >> 8; // 16x write speed current
331                 p[21] = (16 * 176) & 0xff;
332                 p += 21;
333             }
334             s->buf_len = p - s->buf;
335             s->buf[0] = s->buf_len - 4;
336             if (s->buf_len > len)
337                 s->buf_len = len;
338         }
339         break;
340     case 0x1b:
341         DPRINTF("Start Stop Unit\n");
342         break;
343     case 0x1e:
344         DPRINTF("Prevent Allow Medium Removal (prevent = %d)\n", buf[4] & 3);
345         bdrv_set_locked(s->bdrv, buf[4] & 1);
346         break;
347     case 0x25:
348         DPRINTF("Read Capacity\n");
349         /* The normal LEN field for this command is zero.  */
350         memset(s->buf, 0, 8);
351         bdrv_get_geometry(s->bdrv, &nb_sectors);
352         /* Returned value is the address of the last sector.  */
353         if (nb_sectors) {
354             nb_sectors--;
355             s->buf[0] = (nb_sectors >> 24) & 0xff;
356             s->buf[1] = (nb_sectors >> 16) & 0xff;
357             s->buf[2] = (nb_sectors >> 8) & 0xff;
358             s->buf[3] = nb_sectors & 0xff;
359             s->buf[4] = 0;
360             s->buf[5] = 0;
361             s->buf[6] = s->cluster_size * 2;
362             s->buf[7] = 0;
363             s->buf_len = 8;
364         } else {
365             scsi_command_complete(s, SENSE_NOT_READY);
366         }
367         break;
368     case 0x08:
369     case 0x28:
370         DPRINTF("Read (sector %d, count %d)\n", lba, len);
371         s->sector = lba * s->cluster_size;
372         s->sector_count = len * s->cluster_size;
373         break;
374     case 0x0a:
375     case 0x2a:
376         DPRINTF("Write (sector %d, count %d)\n", lba, len);
377         s->sector = lba * s->cluster_size;
378         s->sector_count = len * s->cluster_size;
379         is_write = 1;
380         break;
381     case 0x35:
382         DPRINTF("Syncronise cache (sector %d, count %d)\n", lba, len);
383         bdrv_flush(s->bdrv);
384         break;
385     case 0x43:
386         {
387             int start_track, format, msf, toclen;
388
389             msf = buf[1] & 2;
390             format = buf[2] & 0xf;
391             start_track = buf[6];
392             bdrv_get_geometry(s->bdrv, &nb_sectors);
393             DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
394             switch(format) {
395             case 0:
396                 toclen = cdrom_read_toc(nb_sectors, s->buf, msf, start_track);
397                 break;
398             case 1:
399                 /* multi session : only a single session defined */
400                 toclen = 12;
401                 memset(s->buf, 0, 12);
402                 s->buf[1] = 0x0a;
403                 s->buf[2] = 0x01;
404                 s->buf[3] = 0x01;
405                 break;
406             case 2:
407                 toclen = cdrom_read_toc_raw(nb_sectors, s->buf, msf, start_track);
408                 break;
409             default:
410                 goto error_cmd;
411             }
412             if (toclen > 0) {
413                 if (len > toclen)
414                   len = toclen;
415                 s->buf_len = len;
416                 break;
417             }
418         error_cmd:
419             DPRINTF("Read TOC error\n");
420             goto fail;
421         }
422     case 0x46:
423         DPRINTF("Get Configuration (rt %d, maxlen %d)\n", buf[1] & 3, len);
424         memset(s->buf, 0, 8);
425         /* ??? This shoud probably return much more information.  For now
426            just return the basic header indicating the CD-ROM profile.  */
427         s->buf[7] = 8; // CD-ROM
428         s->buf_len = 8;
429         break;
430     case 0x56:
431         DPRINTF("Reserve(10)\n");
432         if (buf[1] & 3)
433             goto fail;
434         break;
435     case 0x57:
436         DPRINTF("Release(10)\n");
437         if (buf[1] & 3)
438             goto fail;
439         break;
440     case 0xa0:
441         DPRINTF("Report LUNs (len %d)\n", len);
442         if (len < 16)
443             goto fail;
444         memset(s->buf, 0, 16);
445         s->buf[3] = 8;
446         s->buf_len = 16;
447         break;
448     default:
449         DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
450     fail:
451         scsi_command_complete(s, SENSE_ILLEGAL_REQUEST);
452         return 0;
453     }
454     if (s->sector_count == 0 && s->buf_len == 0) {
455         scsi_command_complete(s, SENSE_NO_SENSE);
456     }
457     len = s->sector_count * 512 + s->buf_len;
458     return is_write ? -len : len;
459 }
460
461 void scsi_disk_destroy(SCSIDevice *s)
462 {
463     bdrv_close(s->bdrv);
464     qemu_free(s);
465 }
466
467 SCSIDevice *scsi_disk_init(BlockDriverState *bdrv,
468                            scsi_completionfn completion,
469                            void *opaque)
470 {
471     SCSIDevice *s;
472
473     s = (SCSIDevice *)qemu_mallocz(sizeof(SCSIDevice));
474     s->bdrv = bdrv;
475     s->completion = completion;
476     s->opaque = opaque;
477     if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
478         s->cluster_size = 4;
479     } else {
480         s->cluster_size = 1;
481     }
482
483     return s;
484 }
485