scsi-disk: Track tray open/close state
[qemu/ar7.git] / hw / scsi-disk.c
blobf18ddd742a9520404e9d8614f75d105383c6a310
1 /*
2 * SCSI Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
7 * Written by Paul Brook
8 * Modifications:
9 * 2009-Dec-12 Artyom Tarasenko : implemented stamdard inquiry for the case
10 * when the allocation length of CDB is smaller
11 * than 36.
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.
22 //#define DEBUG_SCSI
24 #ifdef DEBUG_SCSI
25 #define DPRINTF(fmt, ...) \
26 do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
27 #else
28 #define DPRINTF(fmt, ...) do {} while(0)
29 #endif
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"
36 #include "scsi.h"
37 #include "scsi-defs.h"
38 #include "sysemu.h"
39 #include "blockdev.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 {
53 SCSIRequest req;
54 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
55 uint64_t sector;
56 uint32_t sector_count;
57 struct iovec iov;
58 QEMUIOVector qiov;
59 uint32_t status;
60 BlockAcctCookie acct;
61 } SCSIDiskReq;
63 struct SCSIDiskState
65 SCSIDevice qdev;
66 BlockDriverState *bs;
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. */
69 int cluster_size;
70 uint32_t removable;
71 uint64_t max_lba;
72 QEMUBH *bh;
73 char *version;
74 char *serial;
75 bool tray_open;
78 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
79 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf);
81 static void scsi_free_request(SCSIRequest *req)
83 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
85 qemu_vfree(r->iov.iov_base);
88 /* Helper function for command completion with sense. */
89 static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
91 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
92 r->req.tag, sense.key, sense.asc, sense.ascq);
93 scsi_req_build_sense(&r->req, sense);
94 scsi_req_complete(&r->req, CHECK_CONDITION);
97 /* Cancel a pending data transfer. */
98 static void scsi_cancel_io(SCSIRequest *req)
100 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
102 DPRINTF("Cancel tag=0x%x\n", req->tag);
103 if (r->req.aiocb) {
104 bdrv_aio_cancel(r->req.aiocb);
106 r->req.aiocb = NULL;
109 static void scsi_read_complete(void * opaque, int ret)
111 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
112 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
113 int n;
115 if (r->req.aiocb != NULL) {
116 r->req.aiocb = NULL;
117 bdrv_acct_done(s->bs, &r->acct);
120 if (ret) {
121 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_READ)) {
122 return;
126 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->iov.iov_len);
128 n = r->iov.iov_len / 512;
129 r->sector += n;
130 r->sector_count -= n;
131 scsi_req_data(&r->req, r->iov.iov_len);
134 static void scsi_flush_complete(void * opaque, int ret)
136 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
137 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
139 if (r->req.aiocb != NULL) {
140 r->req.aiocb = NULL;
141 bdrv_acct_done(s->bs, &r->acct);
144 if (ret < 0) {
145 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_FLUSH)) {
146 return;
150 scsi_req_complete(&r->req, GOOD);
153 /* Read more data from scsi device into buffer. */
154 static void scsi_read_data(SCSIRequest *req)
156 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
157 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
158 uint32_t n;
160 if (r->sector_count == (uint32_t)-1) {
161 DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
162 r->sector_count = 0;
163 scsi_req_data(&r->req, r->iov.iov_len);
164 return;
166 DPRINTF("Read sector_count=%d\n", r->sector_count);
167 if (r->sector_count == 0) {
168 /* This also clears the sense buffer for REQUEST SENSE. */
169 scsi_req_complete(&r->req, GOOD);
170 return;
173 /* No data transfer may already be in progress */
174 assert(r->req.aiocb == NULL);
176 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
177 DPRINTF("Data transfer direction invalid\n");
178 scsi_read_complete(r, -EINVAL);
179 return;
182 n = r->sector_count;
183 if (n > SCSI_DMA_BUF_SIZE / 512)
184 n = SCSI_DMA_BUF_SIZE / 512;
186 r->iov.iov_len = n * 512;
187 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
189 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
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);
205 return 0;
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);
216 } else {
217 switch (error) {
218 case ENOMEM:
219 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
220 break;
221 case EINVAL:
222 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
223 break;
224 default:
225 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
226 break;
228 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
230 return 1;
233 static void scsi_write_complete(void * opaque, int ret)
235 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
236 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
237 uint32_t len;
238 uint32_t n;
240 if (r->req.aiocb != NULL) {
241 r->req.aiocb = NULL;
242 bdrv_acct_done(s->bs, &r->acct);
245 if (ret) {
246 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
247 return;
251 n = r->iov.iov_len / 512;
252 r->sector += n;
253 r->sector_count -= n;
254 if (r->sector_count == 0) {
255 scsi_req_complete(&r->req, GOOD);
256 } else {
257 len = r->sector_count * 512;
258 if (len > SCSI_DMA_BUF_SIZE) {
259 len = SCSI_DMA_BUF_SIZE;
261 r->iov.iov_len = len;
262 DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
263 scsi_req_data(&r->req, len);
267 static void scsi_write_data(SCSIRequest *req)
269 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
270 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
271 uint32_t n;
273 /* No data transfer may already be in progress */
274 assert(r->req.aiocb == NULL);
276 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
277 DPRINTF("Data transfer direction invalid\n");
278 scsi_write_complete(r, -EINVAL);
279 return;
282 n = r->iov.iov_len / 512;
283 if (n) {
284 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
286 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE);
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);
292 } else {
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;
301 SCSIRequest *req;
302 SCSIDiskReq *r;
304 qemu_bh_delete(s->bh);
305 s->bh = NULL;
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;
311 int ret;
313 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);
319 break;
320 case SCSI_REQ_STATUS_RETRY_WRITE:
321 scsi_write_data(&r->req);
322 break;
323 case SCSI_REQ_STATUS_RETRY_FLUSH:
324 ret = scsi_disk_emulate_command(r, r->iov.iov_base);
325 if (ret == 0) {
326 scsi_req_complete(&r->req, GOOD);
333 static void scsi_dma_restart_cb(void *opaque, int running, int reason)
335 SCSIDiskState *s = opaque;
337 if (!running)
338 return;
340 if (!s->bh) {
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 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
356 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
357 int buflen = 0;
359 if (req->cmd.buf[1] & 0x2) {
360 /* Command support data - optional, not implemented */
361 BADF("optional INQUIRY command support request not implemented\n");
362 return -1;
365 if (req->cmd.buf[1] & 0x1) {
366 /* Vital product data */
367 uint8_t page_code = req->cmd.buf[2];
368 if (req->cmd.xfer < 4) {
369 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
370 "less than 4\n", page_code, req->cmd.xfer);
371 return -1;
374 if (s->qdev.type == TYPE_ROM) {
375 outbuf[buflen++] = 5;
376 } else {
377 outbuf[buflen++] = 0;
379 outbuf[buflen++] = page_code ; // this page
380 outbuf[buflen++] = 0x00;
382 switch (page_code) {
383 case 0x00: /* Supported page codes, mandatory */
385 int pages;
386 DPRINTF("Inquiry EVPD[Supported pages] "
387 "buffer size %zd\n", req->cmd.xfer);
388 pages = buflen++;
389 outbuf[buflen++] = 0x00; // list of supported pages (this page)
390 if (s->serial)
391 outbuf[buflen++] = 0x80; // unit serial number
392 outbuf[buflen++] = 0x83; // device identification
393 if (s->qdev.type == TYPE_DISK) {
394 outbuf[buflen++] = 0xb0; // block limits
395 outbuf[buflen++] = 0xb2; // thin provisioning
397 outbuf[pages] = buflen - pages - 1; // number of pages
398 break;
400 case 0x80: /* Device serial number, optional */
402 int l;
404 if (!s->serial) {
405 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
406 return -1;
409 l = strlen(s->serial);
410 if (l > req->cmd.xfer)
411 l = req->cmd.xfer;
412 if (l > 20)
413 l = 20;
415 DPRINTF("Inquiry EVPD[Serial number] "
416 "buffer size %zd\n", req->cmd.xfer);
417 outbuf[buflen++] = l;
418 memcpy(outbuf+buflen, s->serial, l);
419 buflen += l;
420 break;
423 case 0x83: /* Device identification page, mandatory */
425 int max_len = 255 - 8;
426 int id_len = strlen(bdrv_get_device_name(s->bs));
428 if (id_len > max_len)
429 id_len = max_len;
430 DPRINTF("Inquiry EVPD[Device identification] "
431 "buffer size %zd\n", req->cmd.xfer);
433 outbuf[buflen++] = 4 + id_len;
434 outbuf[buflen++] = 0x2; // ASCII
435 outbuf[buflen++] = 0; // not officially assigned
436 outbuf[buflen++] = 0; // reserved
437 outbuf[buflen++] = id_len; // length of data following
439 memcpy(outbuf+buflen, bdrv_get_device_name(s->bs), id_len);
440 buflen += id_len;
441 break;
443 case 0xb0: /* block limits */
445 unsigned int unmap_sectors =
446 s->qdev.conf.discard_granularity / s->qdev.blocksize;
447 unsigned int min_io_size =
448 s->qdev.conf.min_io_size / s->qdev.blocksize;
449 unsigned int opt_io_size =
450 s->qdev.conf.opt_io_size / s->qdev.blocksize;
452 if (s->qdev.type == TYPE_ROM) {
453 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
454 page_code);
455 return -1;
457 /* required VPD size with unmap support */
458 outbuf[3] = buflen = 0x3c;
460 memset(outbuf + 4, 0, buflen - 4);
462 /* optimal transfer length granularity */
463 outbuf[6] = (min_io_size >> 8) & 0xff;
464 outbuf[7] = min_io_size & 0xff;
466 /* optimal transfer length */
467 outbuf[12] = (opt_io_size >> 24) & 0xff;
468 outbuf[13] = (opt_io_size >> 16) & 0xff;
469 outbuf[14] = (opt_io_size >> 8) & 0xff;
470 outbuf[15] = opt_io_size & 0xff;
472 /* optimal unmap granularity */
473 outbuf[28] = (unmap_sectors >> 24) & 0xff;
474 outbuf[29] = (unmap_sectors >> 16) & 0xff;
475 outbuf[30] = (unmap_sectors >> 8) & 0xff;
476 outbuf[31] = unmap_sectors & 0xff;
477 break;
479 case 0xb2: /* thin provisioning */
481 outbuf[3] = buflen = 8;
482 outbuf[4] = 0;
483 outbuf[5] = 0x40; /* write same with unmap supported */
484 outbuf[6] = 0;
485 outbuf[7] = 0;
486 break;
488 default:
489 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
490 "buffer size %zd\n", page_code, req->cmd.xfer);
491 return -1;
493 /* done with EVPD */
494 return buflen;
497 /* Standard INQUIRY data */
498 if (req->cmd.buf[2] != 0) {
499 BADF("Error: Inquiry (STANDARD) page or code "
500 "is non-zero [%02X]\n", req->cmd.buf[2]);
501 return -1;
504 /* PAGE CODE == 0 */
505 if (req->cmd.xfer < 5) {
506 BADF("Error: Inquiry (STANDARD) buffer size %zd "
507 "is less than 5\n", req->cmd.xfer);
508 return -1;
511 buflen = req->cmd.xfer;
512 if (buflen > SCSI_MAX_INQUIRY_LEN)
513 buflen = SCSI_MAX_INQUIRY_LEN;
515 memset(outbuf, 0, buflen);
517 outbuf[0] = s->qdev.type & 0x1f;
518 if (s->qdev.type == TYPE_ROM) {
519 outbuf[1] = 0x80;
520 memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
521 } else {
522 outbuf[1] = s->removable ? 0x80 : 0;
523 memcpy(&outbuf[16], "QEMU HARDDISK ", 16);
525 memcpy(&outbuf[8], "QEMU ", 8);
526 memset(&outbuf[32], 0, 4);
527 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
529 * We claim conformance to SPC-3, which is required for guests
530 * to ask for modern features like READ CAPACITY(16) or the
531 * block characteristics VPD page by default. Not all of SPC-3
532 * is actually implemented, but we're good enough.
534 outbuf[2] = 5;
535 outbuf[3] = 2; /* Format 2 */
537 if (buflen > 36) {
538 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
539 } else {
540 /* If the allocation length of CDB is too small,
541 the additional length is not adjusted */
542 outbuf[4] = 36 - 5;
545 /* Sync data transfer and TCQ. */
546 outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
547 return buflen;
550 static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
551 int page_control)
553 BlockDriverState *bdrv = s->bs;
554 int cylinders, heads, secs;
555 uint8_t *p = *p_outbuf;
558 * If Changeable Values are requested, a mask denoting those mode parameters
559 * that are changeable shall be returned. As we currently don't support
560 * parameter changes via MODE_SELECT all bits are returned set to zero.
561 * The buffer was already menset to zero by the caller of this function.
563 switch (page) {
564 case 4: /* Rigid disk device geometry page. */
565 if (s->qdev.type == TYPE_ROM) {
566 return -1;
568 p[0] = 4;
569 p[1] = 0x16;
570 if (page_control == 1) { /* Changeable Values */
571 break;
573 /* if a geometry hint is available, use it */
574 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
575 p[2] = (cylinders >> 16) & 0xff;
576 p[3] = (cylinders >> 8) & 0xff;
577 p[4] = cylinders & 0xff;
578 p[5] = heads & 0xff;
579 /* Write precomp start cylinder, disabled */
580 p[6] = (cylinders >> 16) & 0xff;
581 p[7] = (cylinders >> 8) & 0xff;
582 p[8] = cylinders & 0xff;
583 /* Reduced current start cylinder, disabled */
584 p[9] = (cylinders >> 16) & 0xff;
585 p[10] = (cylinders >> 8) & 0xff;
586 p[11] = cylinders & 0xff;
587 /* Device step rate [ns], 200ns */
588 p[12] = 0;
589 p[13] = 200;
590 /* Landing zone cylinder */
591 p[14] = 0xff;
592 p[15] = 0xff;
593 p[16] = 0xff;
594 /* Medium rotation rate [rpm], 5400 rpm */
595 p[20] = (5400 >> 8) & 0xff;
596 p[21] = 5400 & 0xff;
597 break;
599 case 5: /* Flexible disk device geometry page. */
600 if (s->qdev.type == TYPE_ROM) {
601 return -1;
603 p[0] = 5;
604 p[1] = 0x1e;
605 if (page_control == 1) { /* Changeable Values */
606 break;
608 /* Transfer rate [kbit/s], 5Mbit/s */
609 p[2] = 5000 >> 8;
610 p[3] = 5000 & 0xff;
611 /* if a geometry hint is available, use it */
612 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
613 p[4] = heads & 0xff;
614 p[5] = secs & 0xff;
615 p[6] = s->cluster_size * 2;
616 p[8] = (cylinders >> 8) & 0xff;
617 p[9] = cylinders & 0xff;
618 /* Write precomp start cylinder, disabled */
619 p[10] = (cylinders >> 8) & 0xff;
620 p[11] = cylinders & 0xff;
621 /* Reduced current start cylinder, disabled */
622 p[12] = (cylinders >> 8) & 0xff;
623 p[13] = cylinders & 0xff;
624 /* Device step rate [100us], 100us */
625 p[14] = 0;
626 p[15] = 1;
627 /* Device step pulse width [us], 1us */
628 p[16] = 1;
629 /* Device head settle delay [100us], 100us */
630 p[17] = 0;
631 p[18] = 1;
632 /* Motor on delay [0.1s], 0.1s */
633 p[19] = 1;
634 /* Motor off delay [0.1s], 0.1s */
635 p[20] = 1;
636 /* Medium rotation rate [rpm], 5400 rpm */
637 p[28] = (5400 >> 8) & 0xff;
638 p[29] = 5400 & 0xff;
639 break;
641 case 8: /* Caching page. */
642 p[0] = 8;
643 p[1] = 0x12;
644 if (page_control == 1) { /* Changeable Values */
645 break;
647 if (bdrv_enable_write_cache(s->bs)) {
648 p[2] = 4; /* WCE */
650 break;
652 case 0x2a: /* CD Capabilities and Mechanical Status page. */
653 if (s->qdev.type != TYPE_ROM) {
654 return -1;
656 p[0] = 0x2a;
657 p[1] = 0x14;
658 if (page_control == 1) { /* Changeable Values */
659 break;
661 p[2] = 3; // CD-R & CD-RW read
662 p[3] = 0; // Writing not supported
663 p[4] = 0x7f; /* Audio, composite, digital out,
664 mode 2 form 1&2, multi session */
665 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
666 RW corrected, C2 errors, ISRC,
667 UPC, Bar code */
668 p[6] = 0x2d | (bdrv_is_locked(s->bs)? 2 : 0);
669 /* Locking supported, jumper present, eject, tray */
670 p[7] = 0; /* no volume & mute control, no
671 changer */
672 p[8] = (50 * 176) >> 8; // 50x read speed
673 p[9] = (50 * 176) & 0xff;
674 p[10] = 0 >> 8; // No volume
675 p[11] = 0 & 0xff;
676 p[12] = 2048 >> 8; // 2M buffer
677 p[13] = 2048 & 0xff;
678 p[14] = (16 * 176) >> 8; // 16x read speed current
679 p[15] = (16 * 176) & 0xff;
680 p[18] = (16 * 176) >> 8; // 16x write speed
681 p[19] = (16 * 176) & 0xff;
682 p[20] = (16 * 176) >> 8; // 16x write speed current
683 p[21] = (16 * 176) & 0xff;
684 break;
686 default:
687 return -1;
690 *p_outbuf += p[1] + 2;
691 return p[1] + 2;
694 static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
696 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
697 uint64_t nb_sectors;
698 int page, dbd, buflen, ret, page_control;
699 uint8_t *p;
700 uint8_t dev_specific_param;
702 dbd = r->req.cmd.buf[1] & 0x8;
703 page = r->req.cmd.buf[2] & 0x3f;
704 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
705 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
706 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
707 memset(outbuf, 0, r->req.cmd.xfer);
708 p = outbuf;
710 if (bdrv_is_read_only(s->bs)) {
711 dev_specific_param = 0x80; /* Readonly. */
712 } else {
713 dev_specific_param = 0x00;
716 if (r->req.cmd.buf[0] == MODE_SENSE) {
717 p[1] = 0; /* Default media type. */
718 p[2] = dev_specific_param;
719 p[3] = 0; /* Block descriptor length. */
720 p += 4;
721 } else { /* MODE_SENSE_10 */
722 p[2] = 0; /* Default media type. */
723 p[3] = dev_specific_param;
724 p[6] = p[7] = 0; /* Block descriptor length. */
725 p += 8;
728 bdrv_get_geometry(s->bs, &nb_sectors);
729 if (!dbd && nb_sectors) {
730 if (r->req.cmd.buf[0] == MODE_SENSE) {
731 outbuf[3] = 8; /* Block descriptor length */
732 } else { /* MODE_SENSE_10 */
733 outbuf[7] = 8; /* Block descriptor length */
735 nb_sectors /= s->cluster_size;
736 if (nb_sectors > 0xffffff)
737 nb_sectors = 0;
738 p[0] = 0; /* media density code */
739 p[1] = (nb_sectors >> 16) & 0xff;
740 p[2] = (nb_sectors >> 8) & 0xff;
741 p[3] = nb_sectors & 0xff;
742 p[4] = 0; /* reserved */
743 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
744 p[6] = s->cluster_size * 2;
745 p[7] = 0;
746 p += 8;
749 if (page_control == 3) {
750 /* Saved Values */
751 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
752 return -1;
755 if (page == 0x3f) {
756 for (page = 0; page <= 0x3e; page++) {
757 mode_sense_page(s, page, &p, page_control);
759 } else {
760 ret = mode_sense_page(s, page, &p, page_control);
761 if (ret == -1) {
762 return -1;
766 buflen = p - outbuf;
768 * The mode data length field specifies the length in bytes of the
769 * following data that is available to be transferred. The mode data
770 * length does not include itself.
772 if (r->req.cmd.buf[0] == MODE_SENSE) {
773 outbuf[0] = buflen - 1;
774 } else { /* MODE_SENSE_10 */
775 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
776 outbuf[1] = (buflen - 2) & 0xff;
778 if (buflen > r->req.cmd.xfer)
779 buflen = r->req.cmd.xfer;
780 return buflen;
783 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
785 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
786 int start_track, format, msf, toclen;
787 uint64_t nb_sectors;
789 msf = req->cmd.buf[1] & 2;
790 format = req->cmd.buf[2] & 0xf;
791 start_track = req->cmd.buf[6];
792 bdrv_get_geometry(s->bs, &nb_sectors);
793 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
794 nb_sectors /= s->cluster_size;
795 switch (format) {
796 case 0:
797 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
798 break;
799 case 1:
800 /* multi session : only a single session defined */
801 toclen = 12;
802 memset(outbuf, 0, 12);
803 outbuf[1] = 0x0a;
804 outbuf[2] = 0x01;
805 outbuf[3] = 0x01;
806 break;
807 case 2:
808 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
809 break;
810 default:
811 return -1;
813 if (toclen > req->cmd.xfer)
814 toclen = req->cmd.xfer;
815 return toclen;
818 static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
820 SCSIRequest *req = &r->req;
821 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
822 bool start = req->cmd.buf[4] & 1;
823 bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
825 if (s->qdev.type == TYPE_ROM && loej) {
826 bdrv_eject(s->bs, !start);
827 s->tray_open = !start;
831 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
833 SCSIRequest *req = &r->req;
834 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
835 uint64_t nb_sectors;
836 int buflen = 0;
838 switch (req->cmd.buf[0]) {
839 case TEST_UNIT_READY:
840 if (!bdrv_is_inserted(s->bs))
841 goto not_ready;
842 break;
843 case INQUIRY:
844 buflen = scsi_disk_emulate_inquiry(req, outbuf);
845 if (buflen < 0)
846 goto illegal_request;
847 break;
848 case MODE_SENSE:
849 case MODE_SENSE_10:
850 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
851 if (buflen < 0)
852 goto illegal_request;
853 break;
854 case READ_TOC:
855 buflen = scsi_disk_emulate_read_toc(req, outbuf);
856 if (buflen < 0)
857 goto illegal_request;
858 break;
859 case RESERVE:
860 if (req->cmd.buf[1] & 1)
861 goto illegal_request;
862 break;
863 case RESERVE_10:
864 if (req->cmd.buf[1] & 3)
865 goto illegal_request;
866 break;
867 case RELEASE:
868 if (req->cmd.buf[1] & 1)
869 goto illegal_request;
870 break;
871 case RELEASE_10:
872 if (req->cmd.buf[1] & 3)
873 goto illegal_request;
874 break;
875 case START_STOP:
876 scsi_disk_emulate_start_stop(r);
877 break;
878 case ALLOW_MEDIUM_REMOVAL:
879 bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
880 break;
881 case READ_CAPACITY_10:
882 /* The normal LEN field for this command is zero. */
883 memset(outbuf, 0, 8);
884 bdrv_get_geometry(s->bs, &nb_sectors);
885 if (!nb_sectors)
886 goto not_ready;
887 nb_sectors /= s->cluster_size;
888 /* Returned value is the address of the last sector. */
889 nb_sectors--;
890 /* Remember the new size for read/write sanity checking. */
891 s->max_lba = nb_sectors;
892 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
893 if (nb_sectors > UINT32_MAX)
894 nb_sectors = UINT32_MAX;
895 outbuf[0] = (nb_sectors >> 24) & 0xff;
896 outbuf[1] = (nb_sectors >> 16) & 0xff;
897 outbuf[2] = (nb_sectors >> 8) & 0xff;
898 outbuf[3] = nb_sectors & 0xff;
899 outbuf[4] = 0;
900 outbuf[5] = 0;
901 outbuf[6] = s->cluster_size * 2;
902 outbuf[7] = 0;
903 buflen = 8;
904 break;
905 case GET_CONFIGURATION:
906 memset(outbuf, 0, 8);
907 /* ??? This should probably return much more information. For now
908 just return the basic header indicating the CD-ROM profile. */
909 outbuf[7] = 8; // CD-ROM
910 buflen = 8;
911 break;
912 case SERVICE_ACTION_IN_16:
913 /* Service Action In subcommands. */
914 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
915 DPRINTF("SAI READ CAPACITY(16)\n");
916 memset(outbuf, 0, req->cmd.xfer);
917 bdrv_get_geometry(s->bs, &nb_sectors);
918 if (!nb_sectors)
919 goto not_ready;
920 nb_sectors /= s->cluster_size;
921 /* Returned value is the address of the last sector. */
922 nb_sectors--;
923 /* Remember the new size for read/write sanity checking. */
924 s->max_lba = nb_sectors;
925 outbuf[0] = (nb_sectors >> 56) & 0xff;
926 outbuf[1] = (nb_sectors >> 48) & 0xff;
927 outbuf[2] = (nb_sectors >> 40) & 0xff;
928 outbuf[3] = (nb_sectors >> 32) & 0xff;
929 outbuf[4] = (nb_sectors >> 24) & 0xff;
930 outbuf[5] = (nb_sectors >> 16) & 0xff;
931 outbuf[6] = (nb_sectors >> 8) & 0xff;
932 outbuf[7] = nb_sectors & 0xff;
933 outbuf[8] = 0;
934 outbuf[9] = 0;
935 outbuf[10] = s->cluster_size * 2;
936 outbuf[11] = 0;
937 outbuf[12] = 0;
938 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
940 /* set TPE bit if the format supports discard */
941 if (s->qdev.conf.discard_granularity) {
942 outbuf[14] = 0x80;
945 /* Protection, exponent and lowest lba field left blank. */
946 buflen = req->cmd.xfer;
947 break;
949 DPRINTF("Unsupported Service Action In\n");
950 goto illegal_request;
951 case VERIFY_10:
952 break;
953 default:
954 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
955 return -1;
957 return buflen;
959 not_ready:
960 if (!bdrv_is_inserted(s->bs)) {
961 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
962 } else {
963 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
965 return -1;
967 illegal_request:
968 if (r->req.status == -1) {
969 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
971 return -1;
974 /* Execute a scsi command. Returns the length of the data expected by the
975 command. This will be Positive for data transfers from the device
976 (eg. disk reads), negative for transfers to the device (eg. disk writes),
977 and zero if the command does not transfer any data. */
979 static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
981 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
982 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
983 int32_t len;
984 uint8_t command;
985 uint8_t *outbuf;
986 int rc;
988 command = buf[0];
989 outbuf = (uint8_t *)r->iov.iov_base;
990 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
992 #ifdef DEBUG_SCSI
994 int i;
995 for (i = 1; i < r->req.cmd.len; i++) {
996 printf(" 0x%02x", buf[i]);
998 printf("\n");
1000 #endif
1002 switch (command) {
1003 case TEST_UNIT_READY:
1004 case INQUIRY:
1005 case MODE_SENSE:
1006 case MODE_SENSE_10:
1007 case RESERVE:
1008 case RESERVE_10:
1009 case RELEASE:
1010 case RELEASE_10:
1011 case START_STOP:
1012 case ALLOW_MEDIUM_REMOVAL:
1013 case READ_CAPACITY_10:
1014 case READ_TOC:
1015 case GET_CONFIGURATION:
1016 case SERVICE_ACTION_IN_16:
1017 case VERIFY_10:
1018 rc = scsi_disk_emulate_command(r, outbuf);
1019 if (rc < 0) {
1020 return 0;
1023 r->iov.iov_len = rc;
1024 break;
1025 case SYNCHRONIZE_CACHE:
1026 bdrv_acct_start(s->bs, &r->acct, 0, BDRV_ACCT_FLUSH);
1027 r->req.aiocb = bdrv_aio_flush(s->bs, scsi_flush_complete, r);
1028 if (r->req.aiocb == NULL) {
1029 scsi_flush_complete(r, -EIO);
1031 return 0;
1032 case READ_6:
1033 case READ_10:
1034 case READ_12:
1035 case READ_16:
1036 len = r->req.cmd.xfer / s->qdev.blocksize;
1037 DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1038 if (r->req.cmd.lba > s->max_lba)
1039 goto illegal_lba;
1040 r->sector = r->req.cmd.lba * s->cluster_size;
1041 r->sector_count = len * s->cluster_size;
1042 break;
1043 case WRITE_6:
1044 case WRITE_10:
1045 case WRITE_12:
1046 case WRITE_16:
1047 case WRITE_VERIFY_10:
1048 case WRITE_VERIFY_12:
1049 case WRITE_VERIFY_16:
1050 len = r->req.cmd.xfer / s->qdev.blocksize;
1051 DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
1052 (command & 0xe) == 0xe ? "And Verify " : "",
1053 r->req.cmd.lba, len);
1054 if (r->req.cmd.lba > s->max_lba)
1055 goto illegal_lba;
1056 r->sector = r->req.cmd.lba * s->cluster_size;
1057 r->sector_count = len * s->cluster_size;
1058 break;
1059 case MODE_SELECT:
1060 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1061 /* We don't support mode parameter changes.
1062 Allow the mode parameter header + block descriptors only. */
1063 if (r->req.cmd.xfer > 12) {
1064 goto fail;
1066 break;
1067 case MODE_SELECT_10:
1068 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1069 /* We don't support mode parameter changes.
1070 Allow the mode parameter header + block descriptors only. */
1071 if (r->req.cmd.xfer > 16) {
1072 goto fail;
1074 break;
1075 case SEEK_6:
1076 case SEEK_10:
1077 DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
1078 r->req.cmd.lba);
1079 if (r->req.cmd.lba > s->max_lba) {
1080 goto illegal_lba;
1082 break;
1083 case WRITE_SAME_16:
1084 len = r->req.cmd.xfer / s->qdev.blocksize;
1086 DPRINTF("WRITE SAME(16) (sector %" PRId64 ", count %d)\n",
1087 r->req.cmd.lba, len);
1089 if (r->req.cmd.lba > s->max_lba) {
1090 goto illegal_lba;
1094 * We only support WRITE SAME with the unmap bit set for now.
1096 if (!(buf[1] & 0x8)) {
1097 goto fail;
1100 rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1101 len * s->cluster_size);
1102 if (rc < 0) {
1103 /* XXX: better error code ?*/
1104 goto fail;
1107 break;
1108 case REQUEST_SENSE:
1109 abort();
1110 default:
1111 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1112 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
1113 return 0;
1114 fail:
1115 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1116 return 0;
1117 illegal_lba:
1118 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1119 return 0;
1121 if (r->sector_count == 0 && r->iov.iov_len == 0) {
1122 scsi_req_complete(&r->req, GOOD);
1124 len = r->sector_count * 512 + r->iov.iov_len;
1125 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1126 return -len;
1127 } else {
1128 if (!r->sector_count)
1129 r->sector_count = -1;
1130 return len;
1134 static void scsi_disk_reset(DeviceState *dev)
1136 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1137 uint64_t nb_sectors;
1139 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
1141 bdrv_get_geometry(s->bs, &nb_sectors);
1142 nb_sectors /= s->cluster_size;
1143 if (nb_sectors) {
1144 nb_sectors--;
1146 s->max_lba = nb_sectors;
1149 static void scsi_destroy(SCSIDevice *dev)
1151 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1153 scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
1154 blockdev_mark_auto_del(s->qdev.conf.bs);
1157 static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
1159 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1160 DriveInfo *dinfo;
1162 if (!s->qdev.conf.bs) {
1163 error_report("scsi-disk: drive property not set");
1164 return -1;
1166 s->bs = s->qdev.conf.bs;
1168 if (scsi_type == TYPE_DISK && !bdrv_is_inserted(s->bs)) {
1169 error_report("Device needs media, but drive is empty");
1170 return -1;
1173 if (!s->serial) {
1174 /* try to fall back to value set with legacy -drive serial=... */
1175 dinfo = drive_get_by_blockdev(s->bs);
1176 if (*dinfo->serial) {
1177 s->serial = g_strdup(dinfo->serial);
1181 if (!s->version) {
1182 s->version = g_strdup(QEMU_VERSION);
1185 if (bdrv_is_sg(s->bs)) {
1186 error_report("scsi-disk: unwanted /dev/sg*");
1187 return -1;
1190 if (scsi_type == TYPE_ROM) {
1191 s->qdev.blocksize = 2048;
1192 } else if (scsi_type == TYPE_DISK) {
1193 s->qdev.blocksize = s->qdev.conf.logical_block_size;
1194 } else {
1195 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
1196 return -1;
1198 s->cluster_size = s->qdev.blocksize / 512;
1199 s->bs->buffer_alignment = s->qdev.blocksize;
1201 s->qdev.type = scsi_type;
1202 qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
1203 bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
1204 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
1205 return 0;
1208 static int scsi_hd_initfn(SCSIDevice *dev)
1210 return scsi_initfn(dev, TYPE_DISK);
1213 static int scsi_cd_initfn(SCSIDevice *dev)
1215 return scsi_initfn(dev, TYPE_ROM);
1218 static int scsi_disk_initfn(SCSIDevice *dev)
1220 DriveInfo *dinfo;
1221 uint8_t scsi_type;
1223 if (!dev->conf.bs) {
1224 scsi_type = TYPE_DISK; /* will die in scsi_initfn() */
1225 } else {
1226 dinfo = drive_get_by_blockdev(dev->conf.bs);
1227 scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
1230 return scsi_initfn(dev, scsi_type);
1233 static SCSIReqOps scsi_disk_reqops = {
1234 .size = sizeof(SCSIDiskReq),
1235 .free_req = scsi_free_request,
1236 .send_command = scsi_send_command,
1237 .read_data = scsi_read_data,
1238 .write_data = scsi_write_data,
1239 .cancel_io = scsi_cancel_io,
1240 .get_buf = scsi_get_buf,
1243 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
1244 uint32_t lun, void *hba_private)
1246 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1247 SCSIRequest *req;
1248 SCSIDiskReq *r;
1250 req = scsi_req_alloc(&scsi_disk_reqops, &s->qdev, tag, lun, hba_private);
1251 r = DO_UPCAST(SCSIDiskReq, req, req);
1252 r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
1253 return req;
1256 #define DEFINE_SCSI_DISK_PROPERTIES() \
1257 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1258 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1259 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1261 static SCSIDeviceInfo scsi_disk_info[] = {
1263 .qdev.name = "scsi-hd",
1264 .qdev.fw_name = "disk",
1265 .qdev.desc = "virtual SCSI disk",
1266 .qdev.size = sizeof(SCSIDiskState),
1267 .qdev.reset = scsi_disk_reset,
1268 .init = scsi_hd_initfn,
1269 .destroy = scsi_destroy,
1270 .alloc_req = scsi_new_request,
1271 .qdev.props = (Property[]) {
1272 DEFINE_SCSI_DISK_PROPERTIES(),
1273 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1274 DEFINE_PROP_END_OF_LIST(),
1277 .qdev.name = "scsi-cd",
1278 .qdev.fw_name = "disk",
1279 .qdev.desc = "virtual SCSI CD-ROM",
1280 .qdev.size = sizeof(SCSIDiskState),
1281 .qdev.reset = scsi_disk_reset,
1282 .init = scsi_cd_initfn,
1283 .destroy = scsi_destroy,
1284 .alloc_req = scsi_new_request,
1285 .qdev.props = (Property[]) {
1286 DEFINE_SCSI_DISK_PROPERTIES(),
1287 DEFINE_PROP_END_OF_LIST(),
1290 .qdev.name = "scsi-disk", /* legacy -device scsi-disk */
1291 .qdev.fw_name = "disk",
1292 .qdev.desc = "virtual SCSI disk or CD-ROM (legacy)",
1293 .qdev.size = sizeof(SCSIDiskState),
1294 .qdev.reset = scsi_disk_reset,
1295 .init = scsi_disk_initfn,
1296 .destroy = scsi_destroy,
1297 .alloc_req = scsi_new_request,
1298 .qdev.props = (Property[]) {
1299 DEFINE_SCSI_DISK_PROPERTIES(),
1300 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1301 DEFINE_PROP_END_OF_LIST(),
1306 static void scsi_disk_register_devices(void)
1308 int i;
1310 for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1311 scsi_qdev_register(&scsi_disk_info[i]);
1314 device_init(scsi_disk_register_devices)