target-xtensa: implement LSAI group
[qemu.git] / hw / scsi-disk.c
blob9724d0fe9af0c6eb5a672e6267453c873bbfc1aa
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;
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 void scsi_free_request(SCSIRequest *req)
82 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
84 qemu_vfree(r->iov.iov_base);
87 /* Helper function for command completion with sense. */
88 static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
90 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
91 r->req.tag, sense.key, sense.asc, sense.ascq);
92 scsi_req_build_sense(&r->req, sense);
93 scsi_req_complete(&r->req, CHECK_CONDITION);
96 /* Cancel a pending data transfer. */
97 static void scsi_cancel_io(SCSIRequest *req)
99 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
101 DPRINTF("Cancel tag=0x%x\n", req->tag);
102 if (r->req.aiocb) {
103 bdrv_aio_cancel(r->req.aiocb);
105 r->req.aiocb = NULL;
108 static void scsi_read_complete(void * opaque, int ret)
110 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
111 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
112 int n;
114 if (r->req.aiocb != NULL) {
115 r->req.aiocb = NULL;
116 bdrv_acct_done(s->bs, &r->acct);
119 if (ret) {
120 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_READ)) {
121 return;
125 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->iov.iov_len);
127 n = r->iov.iov_len / 512;
128 r->sector += n;
129 r->sector_count -= n;
130 scsi_req_data(&r->req, r->iov.iov_len);
133 static void scsi_flush_complete(void * opaque, int ret)
135 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
136 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
138 if (r->req.aiocb != NULL) {
139 r->req.aiocb = NULL;
140 bdrv_acct_done(s->bs, &r->acct);
143 if (ret < 0) {
144 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_FLUSH)) {
145 return;
149 scsi_req_complete(&r->req, GOOD);
152 /* Read more data from scsi device into buffer. */
153 static void scsi_read_data(SCSIRequest *req)
155 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
156 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
157 uint32_t n;
159 if (r->sector_count == (uint32_t)-1) {
160 DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
161 r->sector_count = 0;
162 scsi_req_data(&r->req, r->iov.iov_len);
163 return;
165 DPRINTF("Read sector_count=%d\n", r->sector_count);
166 if (r->sector_count == 0) {
167 /* This also clears the sense buffer for REQUEST SENSE. */
168 scsi_req_complete(&r->req, GOOD);
169 return;
172 /* No data transfer may already be in progress */
173 assert(r->req.aiocb == NULL);
175 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
176 DPRINTF("Data transfer direction invalid\n");
177 scsi_read_complete(r, -EINVAL);
178 return;
181 n = r->sector_count;
182 if (n > SCSI_DMA_BUF_SIZE / 512)
183 n = SCSI_DMA_BUF_SIZE / 512;
185 r->iov.iov_len = n * 512;
186 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
188 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
189 r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
190 scsi_read_complete, r);
191 if (r->req.aiocb == NULL) {
192 scsi_read_complete(r, -EIO);
196 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
198 int is_read = (type == SCSI_REQ_STATUS_RETRY_READ);
199 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
200 BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
202 if (action == BLOCK_ERR_IGNORE) {
203 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
204 return 0;
207 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
208 || action == BLOCK_ERR_STOP_ANY) {
210 type &= SCSI_REQ_STATUS_RETRY_TYPE_MASK;
211 r->status |= SCSI_REQ_STATUS_RETRY | type;
213 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
214 vm_stop(VMSTOP_DISKFULL);
215 } else {
216 switch (error) {
217 case ENOMEM:
218 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
219 break;
220 case EINVAL:
221 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
222 break;
223 default:
224 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
225 break;
227 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
229 return 1;
232 static void scsi_write_complete(void * opaque, int ret)
234 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
235 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
236 uint32_t len;
237 uint32_t n;
239 if (r->req.aiocb != NULL) {
240 r->req.aiocb = NULL;
241 bdrv_acct_done(s->bs, &r->acct);
244 if (ret) {
245 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
246 return;
250 n = r->iov.iov_len / 512;
251 r->sector += n;
252 r->sector_count -= n;
253 if (r->sector_count == 0) {
254 scsi_req_complete(&r->req, GOOD);
255 } else {
256 len = r->sector_count * 512;
257 if (len > SCSI_DMA_BUF_SIZE) {
258 len = SCSI_DMA_BUF_SIZE;
260 r->iov.iov_len = len;
261 DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
262 scsi_req_data(&r->req, len);
266 static void scsi_write_data(SCSIRequest *req)
268 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
269 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
270 uint32_t n;
272 /* No data transfer may already be in progress */
273 assert(r->req.aiocb == NULL);
275 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
276 DPRINTF("Data transfer direction invalid\n");
277 scsi_write_complete(r, -EINVAL);
278 return;
281 n = r->iov.iov_len / 512;
282 if (n) {
283 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
285 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE);
286 r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
287 scsi_write_complete, r);
288 if (r->req.aiocb == NULL) {
289 scsi_write_complete(r, -ENOMEM);
291 } else {
292 /* Invoke completion routine to fetch data from host. */
293 scsi_write_complete(r, 0);
297 static void scsi_dma_restart_bh(void *opaque)
299 SCSIDiskState *s = opaque;
300 SCSIRequest *req;
301 SCSIDiskReq *r;
303 qemu_bh_delete(s->bh);
304 s->bh = NULL;
306 QTAILQ_FOREACH(req, &s->qdev.requests, next) {
307 r = DO_UPCAST(SCSIDiskReq, req, req);
308 if (r->status & SCSI_REQ_STATUS_RETRY) {
309 int status = r->status;
310 int ret;
312 r->status &=
313 ~(SCSI_REQ_STATUS_RETRY | SCSI_REQ_STATUS_RETRY_TYPE_MASK);
315 switch (status & SCSI_REQ_STATUS_RETRY_TYPE_MASK) {
316 case SCSI_REQ_STATUS_RETRY_READ:
317 scsi_read_data(&r->req);
318 break;
319 case SCSI_REQ_STATUS_RETRY_WRITE:
320 scsi_write_data(&r->req);
321 break;
322 case SCSI_REQ_STATUS_RETRY_FLUSH:
323 ret = scsi_disk_emulate_command(r, r->iov.iov_base);
324 if (ret == 0) {
325 scsi_req_complete(&r->req, GOOD);
332 static void scsi_dma_restart_cb(void *opaque, int running, int reason)
334 SCSIDiskState *s = opaque;
336 if (!running)
337 return;
339 if (!s->bh) {
340 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
341 qemu_bh_schedule(s->bh);
345 /* Return a pointer to the data buffer. */
346 static uint8_t *scsi_get_buf(SCSIRequest *req)
348 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
350 return (uint8_t *)r->iov.iov_base;
353 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
355 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
356 int buflen = 0;
358 if (req->cmd.buf[1] & 0x2) {
359 /* Command support data - optional, not implemented */
360 BADF("optional INQUIRY command support request not implemented\n");
361 return -1;
364 if (req->cmd.buf[1] & 0x1) {
365 /* Vital product data */
366 uint8_t page_code = req->cmd.buf[2];
367 if (req->cmd.xfer < 4) {
368 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
369 "less than 4\n", page_code, req->cmd.xfer);
370 return -1;
373 if (s->qdev.type == TYPE_ROM) {
374 outbuf[buflen++] = 5;
375 } else {
376 outbuf[buflen++] = 0;
378 outbuf[buflen++] = page_code ; // this page
379 outbuf[buflen++] = 0x00;
381 switch (page_code) {
382 case 0x00: /* Supported page codes, mandatory */
384 int pages;
385 DPRINTF("Inquiry EVPD[Supported pages] "
386 "buffer size %zd\n", req->cmd.xfer);
387 pages = buflen++;
388 outbuf[buflen++] = 0x00; // list of supported pages (this page)
389 if (s->serial)
390 outbuf[buflen++] = 0x80; // unit serial number
391 outbuf[buflen++] = 0x83; // device identification
392 if (s->qdev.type == TYPE_DISK) {
393 outbuf[buflen++] = 0xb0; // block limits
394 outbuf[buflen++] = 0xb2; // thin provisioning
396 outbuf[pages] = buflen - pages - 1; // number of pages
397 break;
399 case 0x80: /* Device serial number, optional */
401 int l;
403 if (!s->serial) {
404 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
405 return -1;
408 l = strlen(s->serial);
409 if (l > req->cmd.xfer)
410 l = req->cmd.xfer;
411 if (l > 20)
412 l = 20;
414 DPRINTF("Inquiry EVPD[Serial number] "
415 "buffer size %zd\n", req->cmd.xfer);
416 outbuf[buflen++] = l;
417 memcpy(outbuf+buflen, s->serial, l);
418 buflen += l;
419 break;
422 case 0x83: /* Device identification page, mandatory */
424 int max_len = 255 - 8;
425 int id_len = strlen(bdrv_get_device_name(s->bs));
427 if (id_len > max_len)
428 id_len = max_len;
429 DPRINTF("Inquiry EVPD[Device identification] "
430 "buffer size %zd\n", req->cmd.xfer);
432 outbuf[buflen++] = 4 + id_len;
433 outbuf[buflen++] = 0x2; // ASCII
434 outbuf[buflen++] = 0; // not officially assigned
435 outbuf[buflen++] = 0; // reserved
436 outbuf[buflen++] = id_len; // length of data following
438 memcpy(outbuf+buflen, bdrv_get_device_name(s->bs), id_len);
439 buflen += id_len;
440 break;
442 case 0xb0: /* block limits */
444 unsigned int unmap_sectors =
445 s->qdev.conf.discard_granularity / s->qdev.blocksize;
446 unsigned int min_io_size =
447 s->qdev.conf.min_io_size / s->qdev.blocksize;
448 unsigned int opt_io_size =
449 s->qdev.conf.opt_io_size / s->qdev.blocksize;
451 if (s->qdev.type == TYPE_ROM) {
452 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
453 page_code);
454 return -1;
456 /* required VPD size with unmap support */
457 outbuf[3] = buflen = 0x3c;
459 memset(outbuf + 4, 0, buflen - 4);
461 /* optimal transfer length granularity */
462 outbuf[6] = (min_io_size >> 8) & 0xff;
463 outbuf[7] = min_io_size & 0xff;
465 /* optimal transfer length */
466 outbuf[12] = (opt_io_size >> 24) & 0xff;
467 outbuf[13] = (opt_io_size >> 16) & 0xff;
468 outbuf[14] = (opt_io_size >> 8) & 0xff;
469 outbuf[15] = opt_io_size & 0xff;
471 /* optimal unmap granularity */
472 outbuf[28] = (unmap_sectors >> 24) & 0xff;
473 outbuf[29] = (unmap_sectors >> 16) & 0xff;
474 outbuf[30] = (unmap_sectors >> 8) & 0xff;
475 outbuf[31] = unmap_sectors & 0xff;
476 break;
478 case 0xb2: /* thin provisioning */
480 outbuf[3] = buflen = 8;
481 outbuf[4] = 0;
482 outbuf[5] = 0x40; /* write same with unmap supported */
483 outbuf[6] = 0;
484 outbuf[7] = 0;
485 break;
487 default:
488 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
489 "buffer size %zd\n", page_code, req->cmd.xfer);
490 return -1;
492 /* done with EVPD */
493 return buflen;
496 /* Standard INQUIRY data */
497 if (req->cmd.buf[2] != 0) {
498 BADF("Error: Inquiry (STANDARD) page or code "
499 "is non-zero [%02X]\n", req->cmd.buf[2]);
500 return -1;
503 /* PAGE CODE == 0 */
504 if (req->cmd.xfer < 5) {
505 BADF("Error: Inquiry (STANDARD) buffer size %zd "
506 "is less than 5\n", req->cmd.xfer);
507 return -1;
510 buflen = req->cmd.xfer;
511 if (buflen > SCSI_MAX_INQUIRY_LEN)
512 buflen = SCSI_MAX_INQUIRY_LEN;
514 memset(outbuf, 0, buflen);
516 outbuf[0] = s->qdev.type & 0x1f;
517 if (s->qdev.type == TYPE_ROM) {
518 outbuf[1] = 0x80;
519 memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
520 } else {
521 outbuf[1] = s->removable ? 0x80 : 0;
522 memcpy(&outbuf[16], "QEMU HARDDISK ", 16);
524 memcpy(&outbuf[8], "QEMU ", 8);
525 memset(&outbuf[32], 0, 4);
526 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
528 * We claim conformance to SPC-3, which is required for guests
529 * to ask for modern features like READ CAPACITY(16) or the
530 * block characteristics VPD page by default. Not all of SPC-3
531 * is actually implemented, but we're good enough.
533 outbuf[2] = 5;
534 outbuf[3] = 2; /* Format 2 */
536 if (buflen > 36) {
537 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
538 } else {
539 /* If the allocation length of CDB is too small,
540 the additional length is not adjusted */
541 outbuf[4] = 36 - 5;
544 /* Sync data transfer and TCQ. */
545 outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
546 return buflen;
549 static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
550 int page_control)
552 BlockDriverState *bdrv = s->bs;
553 int cylinders, heads, secs;
554 uint8_t *p = *p_outbuf;
557 * If Changeable Values are requested, a mask denoting those mode parameters
558 * that are changeable shall be returned. As we currently don't support
559 * parameter changes via MODE_SELECT all bits are returned set to zero.
560 * The buffer was already menset to zero by the caller of this function.
562 switch (page) {
563 case 4: /* Rigid disk device geometry page. */
564 if (s->qdev.type == TYPE_ROM) {
565 return -1;
567 p[0] = 4;
568 p[1] = 0x16;
569 if (page_control == 1) { /* Changeable Values */
570 break;
572 /* if a geometry hint is available, use it */
573 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
574 p[2] = (cylinders >> 16) & 0xff;
575 p[3] = (cylinders >> 8) & 0xff;
576 p[4] = cylinders & 0xff;
577 p[5] = heads & 0xff;
578 /* Write precomp start cylinder, disabled */
579 p[6] = (cylinders >> 16) & 0xff;
580 p[7] = (cylinders >> 8) & 0xff;
581 p[8] = cylinders & 0xff;
582 /* Reduced current start cylinder, disabled */
583 p[9] = (cylinders >> 16) & 0xff;
584 p[10] = (cylinders >> 8) & 0xff;
585 p[11] = cylinders & 0xff;
586 /* Device step rate [ns], 200ns */
587 p[12] = 0;
588 p[13] = 200;
589 /* Landing zone cylinder */
590 p[14] = 0xff;
591 p[15] = 0xff;
592 p[16] = 0xff;
593 /* Medium rotation rate [rpm], 5400 rpm */
594 p[20] = (5400 >> 8) & 0xff;
595 p[21] = 5400 & 0xff;
596 break;
598 case 5: /* Flexible disk device geometry page. */
599 if (s->qdev.type == TYPE_ROM) {
600 return -1;
602 p[0] = 5;
603 p[1] = 0x1e;
604 if (page_control == 1) { /* Changeable Values */
605 break;
607 /* Transfer rate [kbit/s], 5Mbit/s */
608 p[2] = 5000 >> 8;
609 p[3] = 5000 & 0xff;
610 /* if a geometry hint is available, use it */
611 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
612 p[4] = heads & 0xff;
613 p[5] = secs & 0xff;
614 p[6] = s->cluster_size * 2;
615 p[8] = (cylinders >> 8) & 0xff;
616 p[9] = cylinders & 0xff;
617 /* Write precomp start cylinder, disabled */
618 p[10] = (cylinders >> 8) & 0xff;
619 p[11] = cylinders & 0xff;
620 /* Reduced current start cylinder, disabled */
621 p[12] = (cylinders >> 8) & 0xff;
622 p[13] = cylinders & 0xff;
623 /* Device step rate [100us], 100us */
624 p[14] = 0;
625 p[15] = 1;
626 /* Device step pulse width [us], 1us */
627 p[16] = 1;
628 /* Device head settle delay [100us], 100us */
629 p[17] = 0;
630 p[18] = 1;
631 /* Motor on delay [0.1s], 0.1s */
632 p[19] = 1;
633 /* Motor off delay [0.1s], 0.1s */
634 p[20] = 1;
635 /* Medium rotation rate [rpm], 5400 rpm */
636 p[28] = (5400 >> 8) & 0xff;
637 p[29] = 5400 & 0xff;
638 break;
640 case 8: /* Caching page. */
641 p[0] = 8;
642 p[1] = 0x12;
643 if (page_control == 1) { /* Changeable Values */
644 break;
646 if (bdrv_enable_write_cache(s->bs)) {
647 p[2] = 4; /* WCE */
649 break;
651 case 0x2a: /* CD Capabilities and Mechanical Status page. */
652 if (s->qdev.type != TYPE_ROM) {
653 return -1;
655 p[0] = 0x2a;
656 p[1] = 0x14;
657 if (page_control == 1) { /* Changeable Values */
658 break;
660 p[2] = 3; // CD-R & CD-RW read
661 p[3] = 0; // Writing not supported
662 p[4] = 0x7f; /* Audio, composite, digital out,
663 mode 2 form 1&2, multi session */
664 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
665 RW corrected, C2 errors, ISRC,
666 UPC, Bar code */
667 p[6] = 0x2d | (bdrv_is_locked(s->bs)? 2 : 0);
668 /* Locking supported, jumper present, eject, tray */
669 p[7] = 0; /* no volume & mute control, no
670 changer */
671 p[8] = (50 * 176) >> 8; // 50x read speed
672 p[9] = (50 * 176) & 0xff;
673 p[10] = 0 >> 8; // No volume
674 p[11] = 0 & 0xff;
675 p[12] = 2048 >> 8; // 2M buffer
676 p[13] = 2048 & 0xff;
677 p[14] = (16 * 176) >> 8; // 16x read speed current
678 p[15] = (16 * 176) & 0xff;
679 p[18] = (16 * 176) >> 8; // 16x write speed
680 p[19] = (16 * 176) & 0xff;
681 p[20] = (16 * 176) >> 8; // 16x write speed current
682 p[21] = (16 * 176) & 0xff;
683 break;
685 default:
686 return -1;
689 *p_outbuf += p[1] + 2;
690 return p[1] + 2;
693 static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
695 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
696 uint64_t nb_sectors;
697 int page, dbd, buflen, ret, page_control;
698 uint8_t *p;
699 uint8_t dev_specific_param;
701 dbd = r->req.cmd.buf[1] & 0x8;
702 page = r->req.cmd.buf[2] & 0x3f;
703 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
704 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
705 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
706 memset(outbuf, 0, r->req.cmd.xfer);
707 p = outbuf;
709 if (bdrv_is_read_only(s->bs)) {
710 dev_specific_param = 0x80; /* Readonly. */
711 } else {
712 dev_specific_param = 0x00;
715 if (r->req.cmd.buf[0] == MODE_SENSE) {
716 p[1] = 0; /* Default media type. */
717 p[2] = dev_specific_param;
718 p[3] = 0; /* Block descriptor length. */
719 p += 4;
720 } else { /* MODE_SENSE_10 */
721 p[2] = 0; /* Default media type. */
722 p[3] = dev_specific_param;
723 p[6] = p[7] = 0; /* Block descriptor length. */
724 p += 8;
727 bdrv_get_geometry(s->bs, &nb_sectors);
728 if (!dbd && nb_sectors) {
729 if (r->req.cmd.buf[0] == MODE_SENSE) {
730 outbuf[3] = 8; /* Block descriptor length */
731 } else { /* MODE_SENSE_10 */
732 outbuf[7] = 8; /* Block descriptor length */
734 nb_sectors /= s->cluster_size;
735 if (nb_sectors > 0xffffff)
736 nb_sectors = 0;
737 p[0] = 0; /* media density code */
738 p[1] = (nb_sectors >> 16) & 0xff;
739 p[2] = (nb_sectors >> 8) & 0xff;
740 p[3] = nb_sectors & 0xff;
741 p[4] = 0; /* reserved */
742 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
743 p[6] = s->cluster_size * 2;
744 p[7] = 0;
745 p += 8;
748 if (page_control == 3) {
749 /* Saved Values */
750 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
751 return -1;
754 if (page == 0x3f) {
755 for (page = 0; page <= 0x3e; page++) {
756 mode_sense_page(s, page, &p, page_control);
758 } else {
759 ret = mode_sense_page(s, page, &p, page_control);
760 if (ret == -1) {
761 return -1;
765 buflen = p - outbuf;
767 * The mode data length field specifies the length in bytes of the
768 * following data that is available to be transferred. The mode data
769 * length does not include itself.
771 if (r->req.cmd.buf[0] == MODE_SENSE) {
772 outbuf[0] = buflen - 1;
773 } else { /* MODE_SENSE_10 */
774 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
775 outbuf[1] = (buflen - 2) & 0xff;
777 if (buflen > r->req.cmd.xfer)
778 buflen = r->req.cmd.xfer;
779 return buflen;
782 static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
784 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
785 int start_track, format, msf, toclen;
786 uint64_t nb_sectors;
788 msf = req->cmd.buf[1] & 2;
789 format = req->cmd.buf[2] & 0xf;
790 start_track = req->cmd.buf[6];
791 bdrv_get_geometry(s->bs, &nb_sectors);
792 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
793 nb_sectors /= s->cluster_size;
794 switch (format) {
795 case 0:
796 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
797 break;
798 case 1:
799 /* multi session : only a single session defined */
800 toclen = 12;
801 memset(outbuf, 0, 12);
802 outbuf[1] = 0x0a;
803 outbuf[2] = 0x01;
804 outbuf[3] = 0x01;
805 break;
806 case 2:
807 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
808 break;
809 default:
810 return -1;
812 if (toclen > req->cmd.xfer)
813 toclen = req->cmd.xfer;
814 return toclen;
817 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
819 SCSIRequest *req = &r->req;
820 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
821 uint64_t nb_sectors;
822 int buflen = 0;
824 switch (req->cmd.buf[0]) {
825 case TEST_UNIT_READY:
826 if (!bdrv_is_inserted(s->bs))
827 goto not_ready;
828 break;
829 case INQUIRY:
830 buflen = scsi_disk_emulate_inquiry(req, outbuf);
831 if (buflen < 0)
832 goto illegal_request;
833 break;
834 case MODE_SENSE:
835 case MODE_SENSE_10:
836 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
837 if (buflen < 0)
838 goto illegal_request;
839 break;
840 case READ_TOC:
841 buflen = scsi_disk_emulate_read_toc(req, outbuf);
842 if (buflen < 0)
843 goto illegal_request;
844 break;
845 case RESERVE:
846 if (req->cmd.buf[1] & 1)
847 goto illegal_request;
848 break;
849 case RESERVE_10:
850 if (req->cmd.buf[1] & 3)
851 goto illegal_request;
852 break;
853 case RELEASE:
854 if (req->cmd.buf[1] & 1)
855 goto illegal_request;
856 break;
857 case RELEASE_10:
858 if (req->cmd.buf[1] & 3)
859 goto illegal_request;
860 break;
861 case START_STOP:
862 if (s->qdev.type == TYPE_ROM && (req->cmd.buf[4] & 2)) {
863 /* load/eject medium */
864 bdrv_eject(s->bs, !(req->cmd.buf[4] & 1));
866 break;
867 case ALLOW_MEDIUM_REMOVAL:
868 bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
869 break;
870 case READ_CAPACITY_10:
871 /* The normal LEN field for this command is zero. */
872 memset(outbuf, 0, 8);
873 bdrv_get_geometry(s->bs, &nb_sectors);
874 if (!nb_sectors)
875 goto not_ready;
876 nb_sectors /= s->cluster_size;
877 /* Returned value is the address of the last sector. */
878 nb_sectors--;
879 /* Remember the new size for read/write sanity checking. */
880 s->max_lba = nb_sectors;
881 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
882 if (nb_sectors > UINT32_MAX)
883 nb_sectors = UINT32_MAX;
884 outbuf[0] = (nb_sectors >> 24) & 0xff;
885 outbuf[1] = (nb_sectors >> 16) & 0xff;
886 outbuf[2] = (nb_sectors >> 8) & 0xff;
887 outbuf[3] = nb_sectors & 0xff;
888 outbuf[4] = 0;
889 outbuf[5] = 0;
890 outbuf[6] = s->cluster_size * 2;
891 outbuf[7] = 0;
892 buflen = 8;
893 break;
894 case GET_CONFIGURATION:
895 memset(outbuf, 0, 8);
896 /* ??? This should probably return much more information. For now
897 just return the basic header indicating the CD-ROM profile. */
898 outbuf[7] = 8; // CD-ROM
899 buflen = 8;
900 break;
901 case SERVICE_ACTION_IN_16:
902 /* Service Action In subcommands. */
903 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
904 DPRINTF("SAI READ CAPACITY(16)\n");
905 memset(outbuf, 0, req->cmd.xfer);
906 bdrv_get_geometry(s->bs, &nb_sectors);
907 if (!nb_sectors)
908 goto not_ready;
909 nb_sectors /= s->cluster_size;
910 /* Returned value is the address of the last sector. */
911 nb_sectors--;
912 /* Remember the new size for read/write sanity checking. */
913 s->max_lba = nb_sectors;
914 outbuf[0] = (nb_sectors >> 56) & 0xff;
915 outbuf[1] = (nb_sectors >> 48) & 0xff;
916 outbuf[2] = (nb_sectors >> 40) & 0xff;
917 outbuf[3] = (nb_sectors >> 32) & 0xff;
918 outbuf[4] = (nb_sectors >> 24) & 0xff;
919 outbuf[5] = (nb_sectors >> 16) & 0xff;
920 outbuf[6] = (nb_sectors >> 8) & 0xff;
921 outbuf[7] = nb_sectors & 0xff;
922 outbuf[8] = 0;
923 outbuf[9] = 0;
924 outbuf[10] = s->cluster_size * 2;
925 outbuf[11] = 0;
926 outbuf[12] = 0;
927 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
929 /* set TPE bit if the format supports discard */
930 if (s->qdev.conf.discard_granularity) {
931 outbuf[14] = 0x80;
934 /* Protection, exponent and lowest lba field left blank. */
935 buflen = req->cmd.xfer;
936 break;
938 DPRINTF("Unsupported Service Action In\n");
939 goto illegal_request;
940 case VERIFY_10:
941 break;
942 default:
943 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
944 return -1;
946 return buflen;
948 not_ready:
949 if (!bdrv_is_inserted(s->bs)) {
950 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
951 } else {
952 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
954 return -1;
956 illegal_request:
957 if (r->req.status == -1) {
958 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
960 return -1;
963 /* Execute a scsi command. Returns the length of the data expected by the
964 command. This will be Positive for data transfers from the device
965 (eg. disk reads), negative for transfers to the device (eg. disk writes),
966 and zero if the command does not transfer any data. */
968 static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
970 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
971 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
972 int32_t len;
973 uint8_t command;
974 uint8_t *outbuf;
975 int rc;
977 command = buf[0];
978 outbuf = (uint8_t *)r->iov.iov_base;
979 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
981 #ifdef DEBUG_SCSI
983 int i;
984 for (i = 1; i < r->req.cmd.len; i++) {
985 printf(" 0x%02x", buf[i]);
987 printf("\n");
989 #endif
991 switch (command) {
992 case TEST_UNIT_READY:
993 case INQUIRY:
994 case MODE_SENSE:
995 case MODE_SENSE_10:
996 case RESERVE:
997 case RESERVE_10:
998 case RELEASE:
999 case RELEASE_10:
1000 case START_STOP:
1001 case ALLOW_MEDIUM_REMOVAL:
1002 case READ_CAPACITY_10:
1003 case READ_TOC:
1004 case GET_CONFIGURATION:
1005 case SERVICE_ACTION_IN_16:
1006 case VERIFY_10:
1007 rc = scsi_disk_emulate_command(r, outbuf);
1008 if (rc < 0) {
1009 return 0;
1012 r->iov.iov_len = rc;
1013 break;
1014 case SYNCHRONIZE_CACHE:
1015 bdrv_acct_start(s->bs, &r->acct, 0, BDRV_ACCT_FLUSH);
1016 r->req.aiocb = bdrv_aio_flush(s->bs, scsi_flush_complete, r);
1017 if (r->req.aiocb == NULL) {
1018 scsi_flush_complete(r, -EIO);
1020 return 0;
1021 case READ_6:
1022 case READ_10:
1023 case READ_12:
1024 case READ_16:
1025 len = r->req.cmd.xfer / s->qdev.blocksize;
1026 DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1027 if (r->req.cmd.lba > s->max_lba)
1028 goto illegal_lba;
1029 r->sector = r->req.cmd.lba * s->cluster_size;
1030 r->sector_count = len * s->cluster_size;
1031 break;
1032 case WRITE_6:
1033 case WRITE_10:
1034 case WRITE_12:
1035 case WRITE_16:
1036 case WRITE_VERIFY_10:
1037 case WRITE_VERIFY_12:
1038 case WRITE_VERIFY_16:
1039 len = r->req.cmd.xfer / s->qdev.blocksize;
1040 DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
1041 (command & 0xe) == 0xe ? "And Verify " : "",
1042 r->req.cmd.lba, len);
1043 if (r->req.cmd.lba > s->max_lba)
1044 goto illegal_lba;
1045 r->sector = r->req.cmd.lba * s->cluster_size;
1046 r->sector_count = len * s->cluster_size;
1047 break;
1048 case MODE_SELECT:
1049 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1050 /* We don't support mode parameter changes.
1051 Allow the mode parameter header + block descriptors only. */
1052 if (r->req.cmd.xfer > 12) {
1053 goto fail;
1055 break;
1056 case MODE_SELECT_10:
1057 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1058 /* We don't support mode parameter changes.
1059 Allow the mode parameter header + block descriptors only. */
1060 if (r->req.cmd.xfer > 16) {
1061 goto fail;
1063 break;
1064 case SEEK_6:
1065 case SEEK_10:
1066 DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
1067 r->req.cmd.lba);
1068 if (r->req.cmd.lba > s->max_lba) {
1069 goto illegal_lba;
1071 break;
1072 case WRITE_SAME_16:
1073 len = r->req.cmd.xfer / s->qdev.blocksize;
1075 DPRINTF("WRITE SAME(16) (sector %" PRId64 ", count %d)\n",
1076 r->req.cmd.lba, len);
1078 if (r->req.cmd.lba > s->max_lba) {
1079 goto illegal_lba;
1083 * We only support WRITE SAME with the unmap bit set for now.
1085 if (!(buf[1] & 0x8)) {
1086 goto fail;
1089 rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1090 len * s->cluster_size);
1091 if (rc < 0) {
1092 /* XXX: better error code ?*/
1093 goto fail;
1096 break;
1097 case REQUEST_SENSE:
1098 abort();
1099 default:
1100 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1101 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
1102 return 0;
1103 fail:
1104 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1105 return 0;
1106 illegal_lba:
1107 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1108 return 0;
1110 if (r->sector_count == 0 && r->iov.iov_len == 0) {
1111 scsi_req_complete(&r->req, GOOD);
1113 len = r->sector_count * 512 + r->iov.iov_len;
1114 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1115 return -len;
1116 } else {
1117 if (!r->sector_count)
1118 r->sector_count = -1;
1119 return len;
1123 static void scsi_disk_reset(DeviceState *dev)
1125 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1126 uint64_t nb_sectors;
1128 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
1130 bdrv_get_geometry(s->bs, &nb_sectors);
1131 nb_sectors /= s->cluster_size;
1132 if (nb_sectors) {
1133 nb_sectors--;
1135 s->max_lba = nb_sectors;
1138 static void scsi_destroy(SCSIDevice *dev)
1140 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1142 scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
1143 blockdev_mark_auto_del(s->qdev.conf.bs);
1146 static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
1148 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1149 DriveInfo *dinfo;
1151 if (!s->qdev.conf.bs) {
1152 error_report("scsi-disk: drive property not set");
1153 return -1;
1155 s->bs = s->qdev.conf.bs;
1157 if (scsi_type == TYPE_DISK && !bdrv_is_inserted(s->bs)) {
1158 error_report("Device needs media, but drive is empty");
1159 return -1;
1162 if (!s->serial) {
1163 /* try to fall back to value set with legacy -drive serial=... */
1164 dinfo = drive_get_by_blockdev(s->bs);
1165 if (*dinfo->serial) {
1166 s->serial = g_strdup(dinfo->serial);
1170 if (!s->version) {
1171 s->version = g_strdup(QEMU_VERSION);
1174 if (bdrv_is_sg(s->bs)) {
1175 error_report("scsi-disk: unwanted /dev/sg*");
1176 return -1;
1179 if (scsi_type == TYPE_ROM) {
1180 s->qdev.blocksize = 2048;
1181 } else if (scsi_type == TYPE_DISK) {
1182 s->qdev.blocksize = s->qdev.conf.logical_block_size;
1183 } else {
1184 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
1185 return -1;
1187 s->cluster_size = s->qdev.blocksize / 512;
1188 s->bs->buffer_alignment = s->qdev.blocksize;
1190 s->qdev.type = scsi_type;
1191 qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
1192 bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
1193 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
1194 return 0;
1197 static int scsi_hd_initfn(SCSIDevice *dev)
1199 return scsi_initfn(dev, TYPE_DISK);
1202 static int scsi_cd_initfn(SCSIDevice *dev)
1204 return scsi_initfn(dev, TYPE_ROM);
1207 static int scsi_disk_initfn(SCSIDevice *dev)
1209 DriveInfo *dinfo;
1210 uint8_t scsi_type;
1212 if (!dev->conf.bs) {
1213 scsi_type = TYPE_DISK; /* will die in scsi_initfn() */
1214 } else {
1215 dinfo = drive_get_by_blockdev(dev->conf.bs);
1216 scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
1219 return scsi_initfn(dev, scsi_type);
1222 static SCSIReqOps scsi_disk_reqops = {
1223 .size = sizeof(SCSIDiskReq),
1224 .free_req = scsi_free_request,
1225 .send_command = scsi_send_command,
1226 .read_data = scsi_read_data,
1227 .write_data = scsi_write_data,
1228 .cancel_io = scsi_cancel_io,
1229 .get_buf = scsi_get_buf,
1232 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
1233 uint32_t lun, void *hba_private)
1235 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1236 SCSIRequest *req;
1237 SCSIDiskReq *r;
1239 req = scsi_req_alloc(&scsi_disk_reqops, &s->qdev, tag, lun, hba_private);
1240 r = DO_UPCAST(SCSIDiskReq, req, req);
1241 r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
1242 return req;
1245 #define DEFINE_SCSI_DISK_PROPERTIES() \
1246 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1247 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1248 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1250 static SCSIDeviceInfo scsi_disk_info[] = {
1252 .qdev.name = "scsi-hd",
1253 .qdev.fw_name = "disk",
1254 .qdev.desc = "virtual SCSI disk",
1255 .qdev.size = sizeof(SCSIDiskState),
1256 .qdev.reset = scsi_disk_reset,
1257 .init = scsi_hd_initfn,
1258 .destroy = scsi_destroy,
1259 .alloc_req = scsi_new_request,
1260 .qdev.props = (Property[]) {
1261 DEFINE_SCSI_DISK_PROPERTIES(),
1262 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1263 DEFINE_PROP_END_OF_LIST(),
1266 .qdev.name = "scsi-cd",
1267 .qdev.fw_name = "disk",
1268 .qdev.desc = "virtual SCSI CD-ROM",
1269 .qdev.size = sizeof(SCSIDiskState),
1270 .qdev.reset = scsi_disk_reset,
1271 .init = scsi_cd_initfn,
1272 .destroy = scsi_destroy,
1273 .alloc_req = scsi_new_request,
1274 .qdev.props = (Property[]) {
1275 DEFINE_SCSI_DISK_PROPERTIES(),
1276 DEFINE_PROP_END_OF_LIST(),
1279 .qdev.name = "scsi-disk", /* legacy -device scsi-disk */
1280 .qdev.fw_name = "disk",
1281 .qdev.desc = "virtual SCSI disk or CD-ROM (legacy)",
1282 .qdev.size = sizeof(SCSIDiskState),
1283 .qdev.reset = scsi_disk_reset,
1284 .init = scsi_disk_initfn,
1285 .destroy = scsi_destroy,
1286 .alloc_req = scsi_new_request,
1287 .qdev.props = (Property[]) {
1288 DEFINE_SCSI_DISK_PROPERTIES(),
1289 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1290 DEFINE_PROP_END_OF_LIST(),
1295 static void scsi_disk_register_devices(void)
1297 int i;
1299 for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1300 scsi_qdev_register(&scsi_disk_info[i]);
1303 device_init(scsi_disk_register_devices)