pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SCSI Device emulation |
| 3 | * |
| 4 | * Copyright (c) 2006 CodeSourcery. |
| 5 | * Based on code by Fabrice Bellard |
| 6 | * |
| 7 | * Written by Paul Brook |
Artyom Tarasenko | ad3cea4 | 2009-12-13 11:32:36 +0100 | [diff] [blame] | 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. |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 14 | * |
Matthew Fernandez | 8e31bf3 | 2011-06-26 12:21:35 +1000 | [diff] [blame] | 15 | * This code is licensed under the LGPL. |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 16 | * |
| 17 | * Note that this file only handles the SCSI architecture model and device |
balrog | 1d4db89 | 2008-02-03 04:05:50 +0000 | [diff] [blame] | 18 | * commands. Emulation of interface/link layer protocols is handled by |
| 19 | * the host adapter emulator. |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | //#define DEBUG_SCSI |
| 23 | |
| 24 | #ifdef DEBUG_SCSI |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 25 | #define DPRINTF(fmt, ...) \ |
| 26 | do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0) |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 27 | #else |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 28 | #define DPRINTF(fmt, ...) do {} while(0) |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 31 | #include "qemu-common.h" |
Markus Armbruster | 2f79201 | 2010-02-18 16:24:31 +0100 | [diff] [blame] | 32 | #include "qemu-error.h" |
Gerd Hoffmann | 43b443b | 2009-10-30 09:54:00 +0100 | [diff] [blame] | 33 | #include "scsi.h" |
Gerd Hoffmann | 0d65e1f | 2009-11-26 15:33:53 +0100 | [diff] [blame] | 34 | #include "scsi-defs.h" |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 35 | #include "sysemu.h" |
Blue Swirl | 2446333 | 2010-08-24 15:22:24 +0000 | [diff] [blame] | 36 | #include "blockdev.h" |
Markus Armbruster | 9db1c0f | 2012-07-10 11:12:31 +0200 | [diff] [blame] | 37 | #include "hw/block-common.h" |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 38 | #include "dma.h" |
blueswir1 | 2286425 | 2008-10-11 09:33:03 +0000 | [diff] [blame] | 39 | |
Paolo Bonzini | 336a691 | 2011-10-25 12:53:37 +0200 | [diff] [blame] | 40 | #ifdef __linux |
| 41 | #include <scsi/sg.h> |
| 42 | #endif |
| 43 | |
aurel32 | f0f72ff | 2008-10-01 21:46:23 +0000 | [diff] [blame] | 44 | #define SCSI_DMA_BUF_SIZE 131072 |
balrog | 5757505 | 2008-12-07 03:12:54 +0000 | [diff] [blame] | 45 | #define SCSI_MAX_INQUIRY_LEN 256 |
Paolo Bonzini | 380feaf | 2012-07-10 15:02:55 +0200 | [diff] [blame] | 46 | #define SCSI_MAX_MODE_LEN 256 |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 47 | |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 48 | typedef struct SCSIDiskState SCSIDiskState; |
| 49 | |
Gerd Hoffmann | 4c41d2e | 2009-11-26 15:33:48 +0100 | [diff] [blame] | 50 | typedef struct SCSIDiskReq { |
| 51 | SCSIRequest req; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 52 | /* Both sector and sector_count are in terms of qemu 512 byte blocks. */ |
aliguori | e035b43 | 2009-01-28 21:58:22 +0000 | [diff] [blame] | 53 | uint64_t sector; |
| 54 | uint32_t sector_count; |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 55 | uint32_t buflen; |
Paolo Bonzini | a0e66a6 | 2012-04-19 11:51:42 +0200 | [diff] [blame] | 56 | bool started; |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 57 | struct iovec iov; |
| 58 | QEMUIOVector qiov; |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 59 | BlockAcctCookie acct; |
Gerd Hoffmann | 4c41d2e | 2009-11-26 15:33:48 +0100 | [diff] [blame] | 60 | } SCSIDiskReq; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 61 | |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 62 | #define SCSI_DISK_F_REMOVABLE 0 |
Paolo Bonzini | da8365d | 2012-05-01 10:25:16 +0200 | [diff] [blame] | 63 | #define SCSI_DISK_F_DPOFUA 1 |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 64 | |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 65 | struct SCSIDiskState |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 66 | { |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 67 | SCSIDevice qdev; |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 68 | uint32_t features; |
Paolo Bonzini | 8a9c16f | 2011-09-13 16:26:06 +0200 | [diff] [blame] | 69 | bool media_changed; |
Paolo Bonzini | 3c2f7c1 | 2011-09-13 16:26:51 +0200 | [diff] [blame] | 70 | bool media_event; |
Paolo Bonzini | 4480de1 | 2011-10-25 12:53:40 +0200 | [diff] [blame] | 71 | bool eject_request; |
Paolo Bonzini | 27395ad | 2012-05-15 12:46:09 +0200 | [diff] [blame] | 72 | uint64_t wwn; |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 73 | QEMUBH *bh; |
Gerd Hoffmann | 383b4d9 | 2010-01-14 14:44:13 +0100 | [diff] [blame] | 74 | char *version; |
Markus Armbruster | a0fef65 | 2010-06-01 20:32:34 +0200 | [diff] [blame] | 75 | char *serial; |
Dmitry Fleytman | 353815a | 2012-07-06 22:03:35 -0700 | [diff] [blame] | 76 | char *vendor; |
| 77 | char *product; |
Markus Armbruster | ece0d5e | 2011-09-06 18:58:40 +0200 | [diff] [blame] | 78 | bool tray_open; |
Markus Armbruster | 81b1008 | 2011-09-06 18:58:44 +0200 | [diff] [blame] | 79 | bool tray_locked; |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Paolo Bonzini | 71544d3 | 2011-10-25 12:53:36 +0200 | [diff] [blame] | 82 | static int scsi_handle_rw_error(SCSIDiskReq *r, int error); |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 83 | |
Paolo Bonzini | ad2d30f | 2011-04-18 16:01:56 +0200 | [diff] [blame] | 84 | static void scsi_free_request(SCSIRequest *req) |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 85 | { |
Paolo Bonzini | ad2d30f | 2011-04-18 16:01:56 +0200 | [diff] [blame] | 86 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
| 87 | |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 88 | if (r->iov.iov_base) { |
| 89 | qemu_vfree(r->iov.iov_base); |
| 90 | } |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 93 | /* Helper function for command completion with sense. */ |
| 94 | static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense) |
Gerd Hoffmann | ed3a34a | 2009-11-26 15:34:00 +0100 | [diff] [blame] | 95 | { |
Blue Swirl | 02fa69b | 2011-08-20 09:21:42 +0000 | [diff] [blame] | 96 | DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n", |
| 97 | r->req.tag, sense.key, sense.asc, sense.ascq); |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 98 | scsi_req_build_sense(&r->req, sense); |
| 99 | scsi_req_complete(&r->req, CHECK_CONDITION); |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Cancel a pending data transfer. */ |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 103 | static void scsi_cancel_io(SCSIRequest *req) |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 104 | { |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 105 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
| 106 | |
| 107 | DPRINTF("Cancel tag=0x%x\n", req->tag); |
| 108 | if (r->req.aiocb) { |
| 109 | bdrv_aio_cancel(r->req.aiocb); |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 110 | |
| 111 | /* This reference was left in by scsi_*_data. We take ownership of |
| 112 | * it the moment scsi_req_cancel is called, independent of whether |
| 113 | * bdrv_aio_cancel completes the request or not. */ |
| 114 | scsi_req_unref(&r->req); |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 115 | } |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 116 | r->req.aiocb = NULL; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 119 | static uint32_t scsi_init_iovec(SCSIDiskReq *r, size_t size) |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 120 | { |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 121 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
| 122 | |
| 123 | if (!r->iov.iov_base) { |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 124 | r->buflen = size; |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 125 | r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen); |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 126 | } |
| 127 | r->iov.iov_len = MIN(r->sector_count * 512, r->buflen); |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 128 | qemu_iovec_init_external(&r->qiov, &r->iov, 1); |
| 129 | return r->qiov.size / 512; |
| 130 | } |
| 131 | |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 132 | static void scsi_disk_save_request(QEMUFile *f, SCSIRequest *req) |
| 133 | { |
| 134 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
| 135 | |
| 136 | qemu_put_be64s(f, &r->sector); |
| 137 | qemu_put_be32s(f, &r->sector_count); |
| 138 | qemu_put_be32s(f, &r->buflen); |
Gerd Hoffmann | 18eef3b | 2012-05-16 14:20:03 +0200 | [diff] [blame] | 139 | if (r->buflen) { |
| 140 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 141 | qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len); |
| 142 | } else if (!req->retry) { |
| 143 | uint32_t len = r->iov.iov_len; |
| 144 | qemu_put_be32s(f, &len); |
| 145 | qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len); |
| 146 | } |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req) |
| 151 | { |
| 152 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
| 153 | |
| 154 | qemu_get_be64s(f, &r->sector); |
| 155 | qemu_get_be32s(f, &r->sector_count); |
| 156 | qemu_get_be32s(f, &r->buflen); |
| 157 | if (r->buflen) { |
| 158 | scsi_init_iovec(r, r->buflen); |
| 159 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 160 | qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len); |
Gerd Hoffmann | 18eef3b | 2012-05-16 14:20:03 +0200 | [diff] [blame] | 161 | } else if (!r->req.retry) { |
| 162 | uint32_t len; |
| 163 | qemu_get_be32s(f, &len); |
| 164 | r->iov.iov_len = len; |
| 165 | assert(r->iov.iov_len <= r->buflen); |
| 166 | qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len); |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | qemu_iovec_init_external(&r->qiov, &r->iov, 1); |
| 171 | } |
| 172 | |
Paolo Bonzini | c1b3524 | 2012-02-08 10:37:48 +0100 | [diff] [blame] | 173 | static void scsi_aio_complete(void *opaque, int ret) |
Paolo Bonzini | b77912a | 2012-04-19 10:05:04 +0200 | [diff] [blame] | 174 | { |
| 175 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
| 176 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
| 177 | |
Paolo Bonzini | 46e3f30 | 2012-08-09 11:33:26 +0200 | [diff] [blame] | 178 | assert(r->req.aiocb != NULL); |
| 179 | r->req.aiocb = NULL; |
Paolo Bonzini | b77912a | 2012-04-19 10:05:04 +0200 | [diff] [blame] | 180 | bdrv_acct_done(s->qdev.conf.bs, &r->acct); |
| 181 | |
| 182 | if (ret < 0) { |
| 183 | if (scsi_handle_rw_error(r, -ret)) { |
| 184 | goto done; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | scsi_req_complete(&r->req, GOOD); |
| 189 | |
| 190 | done: |
| 191 | if (!r->req.io_canceled) { |
| 192 | scsi_req_unref(&r->req); |
| 193 | } |
| 194 | } |
| 195 | |
Paolo Bonzini | 7e8c49c | 2012-04-19 10:10:54 +0200 | [diff] [blame] | 196 | static bool scsi_is_cmd_fua(SCSICommand *cmd) |
| 197 | { |
| 198 | switch (cmd->buf[0]) { |
| 199 | case READ_10: |
| 200 | case READ_12: |
| 201 | case READ_16: |
| 202 | case WRITE_10: |
| 203 | case WRITE_12: |
| 204 | case WRITE_16: |
| 205 | return (cmd->buf[1] & 8) != 0; |
| 206 | |
Paolo Bonzini | 7f64f8e | 2012-04-19 11:46:13 +0200 | [diff] [blame] | 207 | case VERIFY_10: |
| 208 | case VERIFY_12: |
| 209 | case VERIFY_16: |
Paolo Bonzini | 7e8c49c | 2012-04-19 10:10:54 +0200 | [diff] [blame] | 210 | case WRITE_VERIFY_10: |
| 211 | case WRITE_VERIFY_12: |
| 212 | case WRITE_VERIFY_16: |
| 213 | return true; |
| 214 | |
| 215 | case READ_6: |
| 216 | case WRITE_6: |
| 217 | default: |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void scsi_write_do_fua(SCSIDiskReq *r) |
| 223 | { |
| 224 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
| 225 | |
| 226 | if (scsi_is_cmd_fua(&r->req.cmd)) { |
| 227 | bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH); |
Paolo Bonzini | c1b3524 | 2012-02-08 10:37:48 +0100 | [diff] [blame] | 228 | r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r); |
Paolo Bonzini | 7e8c49c | 2012-04-19 10:10:54 +0200 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
| 232 | scsi_req_complete(&r->req, GOOD); |
| 233 | if (!r->req.io_canceled) { |
| 234 | scsi_req_unref(&r->req); |
| 235 | } |
| 236 | } |
| 237 | |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 238 | static void scsi_dma_complete(void *opaque, int ret) |
| 239 | { |
| 240 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
| 241 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
| 242 | |
Paolo Bonzini | 46e3f30 | 2012-08-09 11:33:26 +0200 | [diff] [blame] | 243 | assert(r->req.aiocb != NULL); |
| 244 | r->req.aiocb = NULL; |
| 245 | bdrv_acct_done(s->qdev.conf.bs, &r->acct); |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 246 | |
Paolo Bonzini | 80624c9 | 2012-04-19 14:00:24 +0200 | [diff] [blame] | 247 | if (ret < 0) { |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 248 | if (scsi_handle_rw_error(r, -ret)) { |
| 249 | goto done; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | r->sector += r->sector_count; |
| 254 | r->sector_count = 0; |
Paolo Bonzini | 7e8c49c | 2012-04-19 10:10:54 +0200 | [diff] [blame] | 255 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 256 | scsi_write_do_fua(r); |
| 257 | return; |
| 258 | } else { |
| 259 | scsi_req_complete(&r->req, GOOD); |
| 260 | } |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 261 | |
| 262 | done: |
Paolo Bonzini | b8aba8d | 2012-04-19 10:09:49 +0200 | [diff] [blame] | 263 | if (!r->req.io_canceled) { |
| 264 | scsi_req_unref(&r->req); |
| 265 | } |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 266 | } |
| 267 | |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 268 | static void scsi_read_complete(void * opaque, int ret) |
| 269 | { |
Gerd Hoffmann | 4c41d2e | 2009-11-26 15:33:48 +0100 | [diff] [blame] | 270 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 271 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 272 | int n; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 273 | |
Paolo Bonzini | 46e3f30 | 2012-08-09 11:33:26 +0200 | [diff] [blame] | 274 | assert(r->req.aiocb != NULL); |
| 275 | r->req.aiocb = NULL; |
| 276 | bdrv_acct_done(s->qdev.conf.bs, &r->acct); |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 277 | |
Paolo Bonzini | 80624c9 | 2012-04-19 14:00:24 +0200 | [diff] [blame] | 278 | if (ret < 0) { |
Paolo Bonzini | 71544d3 | 2011-10-25 12:53:36 +0200 | [diff] [blame] | 279 | if (scsi_handle_rw_error(r, -ret)) { |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 280 | goto done; |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 281 | } |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 282 | } |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 283 | |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 284 | DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->qiov.size); |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 285 | |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 286 | n = r->qiov.size / 512; |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 287 | r->sector += n; |
| 288 | r->sector_count -= n; |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 289 | scsi_req_data(&r->req, r->qiov.size); |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 290 | |
| 291 | done: |
| 292 | if (!r->req.io_canceled) { |
| 293 | scsi_req_unref(&r->req); |
| 294 | } |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Paolo Bonzini | ac66842 | 2012-04-19 11:55:28 +0200 | [diff] [blame] | 297 | /* Actually issue a read to the block device. */ |
| 298 | static void scsi_do_read(void *opaque, int ret) |
| 299 | { |
| 300 | SCSIDiskReq *r = opaque; |
| 301 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
| 302 | uint32_t n; |
| 303 | |
| 304 | if (r->req.aiocb != NULL) { |
| 305 | r->req.aiocb = NULL; |
| 306 | bdrv_acct_done(s->qdev.conf.bs, &r->acct); |
| 307 | } |
| 308 | |
| 309 | if (ret < 0) { |
| 310 | if (scsi_handle_rw_error(r, -ret)) { |
| 311 | goto done; |
| 312 | } |
| 313 | } |
| 314 | |
Paolo Bonzini | 31e8fd8 | 2012-04-24 08:41:04 +0200 | [diff] [blame] | 315 | if (r->req.io_canceled) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 320 | scsi_req_ref(&r->req); |
| 321 | |
Paolo Bonzini | ac66842 | 2012-04-19 11:55:28 +0200 | [diff] [blame] | 322 | if (r->req.sg) { |
| 323 | dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BDRV_ACCT_READ); |
| 324 | r->req.resid -= r->req.sg->size; |
| 325 | r->req.aiocb = dma_bdrv_read(s->qdev.conf.bs, r->req.sg, r->sector, |
| 326 | scsi_dma_complete, r); |
| 327 | } else { |
| 328 | n = scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); |
| 329 | bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ); |
| 330 | r->req.aiocb = bdrv_aio_readv(s->qdev.conf.bs, r->sector, &r->qiov, n, |
| 331 | scsi_read_complete, r); |
| 332 | } |
| 333 | |
| 334 | done: |
| 335 | if (!r->req.io_canceled) { |
| 336 | scsi_req_unref(&r->req); |
| 337 | } |
| 338 | } |
| 339 | |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 340 | /* Read more data from scsi device into buffer. */ |
| 341 | static void scsi_read_data(SCSIRequest *req) |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 342 | { |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 343 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 344 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
Paolo Bonzini | ac66842 | 2012-04-19 11:55:28 +0200 | [diff] [blame] | 345 | bool first; |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 346 | |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 347 | DPRINTF("Read sector_count=%d\n", r->sector_count); |
| 348 | if (r->sector_count == 0) { |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 349 | /* This also clears the sense buffer for REQUEST SENSE. */ |
| 350 | scsi_req_complete(&r->req, GOOD); |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 351 | return; |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Stefan Hajnoczi | 6fa2c95 | 2010-11-12 09:57:11 +0000 | [diff] [blame] | 354 | /* No data transfer may already be in progress */ |
| 355 | assert(r->req.aiocb == NULL); |
| 356 | |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 357 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 358 | scsi_req_ref(&r->req); |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 359 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 360 | DPRINTF("Data transfer direction invalid\n"); |
| 361 | scsi_read_complete(r, -EINVAL); |
| 362 | return; |
| 363 | } |
| 364 | |
Markus Armbruster | a1aff5b | 2011-09-06 18:58:41 +0200 | [diff] [blame] | 365 | if (s->tray_open) { |
| 366 | scsi_read_complete(r, -ENOMEDIUM); |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 367 | return; |
Markus Armbruster | a1aff5b | 2011-09-06 18:58:41 +0200 | [diff] [blame] | 368 | } |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 369 | |
Paolo Bonzini | ac66842 | 2012-04-19 11:55:28 +0200 | [diff] [blame] | 370 | first = !r->started; |
Paolo Bonzini | a0e66a6 | 2012-04-19 11:51:42 +0200 | [diff] [blame] | 371 | r->started = true; |
Paolo Bonzini | ac66842 | 2012-04-19 11:55:28 +0200 | [diff] [blame] | 372 | if (first && scsi_is_cmd_fua(&r->req.cmd)) { |
| 373 | bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH); |
| 374 | r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_do_read, r); |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 375 | } else { |
Paolo Bonzini | ac66842 | 2012-04-19 11:55:28 +0200 | [diff] [blame] | 376 | scsi_do_read(r, 0); |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 377 | } |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 380 | /* |
| 381 | * scsi_handle_rw_error has two return values. 0 means that the error |
| 382 | * must be ignored, 1 means that the error has been processed and the |
| 383 | * caller should not do anything else for this request. Note that |
| 384 | * scsi_handle_rw_error always manages its reference counts, independent |
| 385 | * of the return value. |
| 386 | */ |
Paolo Bonzini | 71544d3 | 2011-10-25 12:53:36 +0200 | [diff] [blame] | 387 | static int scsi_handle_rw_error(SCSIDiskReq *r, int error) |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 388 | { |
Paolo Bonzini | 1ceee0d | 2012-09-28 17:22:56 +0200 | [diff] [blame] | 389 | bool is_read = (r->req.cmd.xfer == SCSI_XFER_FROM_DEV); |
Gerd Hoffmann | 4c41d2e | 2009-11-26 15:33:48 +0100 | [diff] [blame] | 390 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
Paolo Bonzini | 3e1caa5 | 2012-09-28 17:22:57 +0200 | [diff] [blame] | 391 | BlockErrorAction action = bdrv_get_error_action(s->qdev.conf.bs, is_read, error); |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 392 | |
Paolo Bonzini | 3e1caa5 | 2012-09-28 17:22:57 +0200 | [diff] [blame] | 393 | if (action == BDRV_ACTION_REPORT) { |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 394 | switch (error) { |
Paolo Bonzini | 7e218df | 2011-09-19 17:19:21 +0200 | [diff] [blame] | 395 | case ENOMEDIUM: |
| 396 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
| 397 | break; |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 398 | case ENOMEM: |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 399 | scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE)); |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 400 | break; |
| 401 | case EINVAL: |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 402 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 403 | break; |
| 404 | default: |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 405 | scsi_check_condition(r, SENSE_CODE(IO_ERROR)); |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 406 | break; |
Hannes Reinecke | a1f0cce | 2011-04-18 12:53:14 +0200 | [diff] [blame] | 407 | } |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 408 | } |
Paolo Bonzini | 3e1caa5 | 2012-09-28 17:22:57 +0200 | [diff] [blame] | 409 | bdrv_error_action(s->qdev.conf.bs, action, is_read, error); |
| 410 | if (action == BDRV_ACTION_STOP) { |
| 411 | scsi_req_retry(&r->req); |
| 412 | } |
| 413 | return action != BDRV_ACTION_IGNORE; |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 414 | } |
| 415 | |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 416 | static void scsi_write_complete(void * opaque, int ret) |
| 417 | { |
Gerd Hoffmann | 4c41d2e | 2009-11-26 15:33:48 +0100 | [diff] [blame] | 418 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 419 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 420 | uint32_t n; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 421 | |
Paolo Bonzini | 8e321cc | 2011-09-05 18:11:51 +0200 | [diff] [blame] | 422 | if (r->req.aiocb != NULL) { |
| 423 | r->req.aiocb = NULL; |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 424 | bdrv_acct_done(s->qdev.conf.bs, &r->acct); |
Paolo Bonzini | 8e321cc | 2011-09-05 18:11:51 +0200 | [diff] [blame] | 425 | } |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 426 | |
Paolo Bonzini | 80624c9 | 2012-04-19 14:00:24 +0200 | [diff] [blame] | 427 | if (ret < 0) { |
Paolo Bonzini | 71544d3 | 2011-10-25 12:53:36 +0200 | [diff] [blame] | 428 | if (scsi_handle_rw_error(r, -ret)) { |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 429 | goto done; |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 430 | } |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 433 | n = r->qiov.size / 512; |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 434 | r->sector += n; |
| 435 | r->sector_count -= n; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 436 | if (r->sector_count == 0) { |
Paolo Bonzini | 7e8c49c | 2012-04-19 10:10:54 +0200 | [diff] [blame] | 437 | scsi_write_do_fua(r); |
| 438 | return; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 439 | } else { |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 440 | scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); |
Paolo Bonzini | 79fb50b | 2012-07-31 16:10:23 +0200 | [diff] [blame] | 441 | DPRINTF("Write complete tag=0x%x more=%zd\n", r->req.tag, r->qiov.size); |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 442 | scsi_req_data(&r->req, r->qiov.size); |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 443 | } |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 444 | |
| 445 | done: |
| 446 | if (!r->req.io_canceled) { |
| 447 | scsi_req_unref(&r->req); |
| 448 | } |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Paolo Bonzini | 4274121 | 2011-04-22 09:39:16 +0200 | [diff] [blame] | 451 | static void scsi_write_data(SCSIRequest *req) |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 452 | { |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 453 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
Gerd Hoffmann | 4c41d2e | 2009-11-26 15:33:48 +0100 | [diff] [blame] | 454 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 455 | uint32_t n; |
| 456 | |
Stefan Hajnoczi | 6fa2c95 | 2010-11-12 09:57:11 +0000 | [diff] [blame] | 457 | /* No data transfer may already be in progress */ |
| 458 | assert(r->req.aiocb == NULL); |
| 459 | |
Paolo Bonzini | c7bae6a | 2011-10-25 12:53:34 +0200 | [diff] [blame] | 460 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 461 | scsi_req_ref(&r->req); |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 462 | if (r->req.cmd.mode != SCSI_XFER_TO_DEV) { |
| 463 | DPRINTF("Data transfer direction invalid\n"); |
| 464 | scsi_write_complete(r, -EINVAL); |
Paolo Bonzini | 4274121 | 2011-04-22 09:39:16 +0200 | [diff] [blame] | 465 | return; |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 468 | if (!r->req.sg && !r->qiov.size) { |
| 469 | /* Called for the first time. Ask the driver to send us more data. */ |
Paolo Bonzini | a0e66a6 | 2012-04-19 11:51:42 +0200 | [diff] [blame] | 470 | r->started = true; |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 471 | scsi_write_complete(r, 0); |
| 472 | return; |
| 473 | } |
| 474 | if (s->tray_open) { |
| 475 | scsi_write_complete(r, -ENOMEDIUM); |
| 476 | return; |
| 477 | } |
| 478 | |
Paolo Bonzini | 7f64f8e | 2012-04-19 11:46:13 +0200 | [diff] [blame] | 479 | if (r->req.cmd.buf[0] == VERIFY_10 || r->req.cmd.buf[0] == VERIFY_12 || |
| 480 | r->req.cmd.buf[0] == VERIFY_16) { |
| 481 | if (r->req.sg) { |
| 482 | scsi_dma_complete(r, 0); |
| 483 | } else { |
| 484 | scsi_write_complete(r, 0); |
| 485 | } |
| 486 | return; |
| 487 | } |
| 488 | |
Paolo Bonzini | 5d0d246 | 2011-10-12 15:12:14 +0200 | [diff] [blame] | 489 | if (r->req.sg) { |
| 490 | dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BDRV_ACCT_WRITE); |
| 491 | r->req.resid -= r->req.sg->size; |
| 492 | r->req.aiocb = dma_bdrv_write(s->qdev.conf.bs, r->req.sg, r->sector, |
| 493 | scsi_dma_complete, r); |
| 494 | } else { |
| 495 | n = r->qiov.size / 512; |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 496 | bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE); |
| 497 | r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, r->sector, &r->qiov, n, |
Paolo Bonzini | 103b40f | 2011-09-16 16:40:03 +0200 | [diff] [blame] | 498 | scsi_write_complete, r); |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 499 | } |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 500 | } |
| 501 | |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 502 | /* Return a pointer to the data buffer. */ |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 503 | static uint8_t *scsi_get_buf(SCSIRequest *req) |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 504 | { |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 505 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 506 | |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 507 | return (uint8_t *)r->iov.iov_base; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 510 | static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) |
| 511 | { |
Gerd Hoffmann | 383b4d9 | 2010-01-14 14:44:13 +0100 | [diff] [blame] | 512 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 513 | int buflen = 0; |
Paolo Bonzini | 8257939 | 2012-05-15 12:45:20 +0200 | [diff] [blame] | 514 | int start; |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 515 | |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 516 | if (req->cmd.buf[1] & 0x1) { |
| 517 | /* Vital product data */ |
| 518 | uint8_t page_code = req->cmd.buf[2]; |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 519 | |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 520 | outbuf[buflen++] = s->qdev.type & 0x1f; |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 521 | outbuf[buflen++] = page_code ; // this page |
| 522 | outbuf[buflen++] = 0x00; |
Paolo Bonzini | 8257939 | 2012-05-15 12:45:20 +0200 | [diff] [blame] | 523 | outbuf[buflen++] = 0x00; |
| 524 | start = buflen; |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 525 | |
| 526 | switch (page_code) { |
| 527 | case 0x00: /* Supported page codes, mandatory */ |
Hannes Reinecke | 39d9898 | 2010-11-24 12:15:58 +0100 | [diff] [blame] | 528 | { |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 529 | DPRINTF("Inquiry EVPD[Supported pages] " |
| 530 | "buffer size %zd\n", req->cmd.xfer); |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 531 | outbuf[buflen++] = 0x00; // list of supported pages (this page) |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 532 | if (s->serial) { |
Hannes Reinecke | 3e1c0c9 | 2011-07-11 15:02:26 +0200 | [diff] [blame] | 533 | outbuf[buflen++] = 0x80; // unit serial number |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 534 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 535 | outbuf[buflen++] = 0x83; // device identification |
Hannes Reinecke | f37bd73 | 2011-07-22 16:44:46 +0200 | [diff] [blame] | 536 | if (s->qdev.type == TYPE_DISK) { |
Christoph Hellwig | ea3bd56 | 2010-12-16 19:36:43 +0100 | [diff] [blame] | 537 | outbuf[buflen++] = 0xb0; // block limits |
| 538 | outbuf[buflen++] = 0xb2; // thin provisioning |
Hannes Reinecke | 39d9898 | 2010-11-24 12:15:58 +0100 | [diff] [blame] | 539 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 540 | break; |
Hannes Reinecke | 39d9898 | 2010-11-24 12:15:58 +0100 | [diff] [blame] | 541 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 542 | case 0x80: /* Device serial number, optional */ |
| 543 | { |
Hannes Reinecke | 3e1c0c9 | 2011-07-11 15:02:26 +0200 | [diff] [blame] | 544 | int l; |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 545 | |
Hannes Reinecke | 3e1c0c9 | 2011-07-11 15:02:26 +0200 | [diff] [blame] | 546 | if (!s->serial) { |
| 547 | DPRINTF("Inquiry (EVPD[Serial number] not supported\n"); |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | l = strlen(s->serial); |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 552 | if (l > 20) { |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 553 | l = 20; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 554 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 555 | |
| 556 | DPRINTF("Inquiry EVPD[Serial number] " |
| 557 | "buffer size %zd\n", req->cmd.xfer); |
Markus Armbruster | a0fef65 | 2010-06-01 20:32:34 +0200 | [diff] [blame] | 558 | memcpy(outbuf+buflen, s->serial, l); |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 559 | buflen += l; |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | case 0x83: /* Device identification page, mandatory */ |
| 564 | { |
Paolo Bonzini | fd93079 | 2012-03-16 19:12:43 +0100 | [diff] [blame] | 565 | const char *str = s->serial ?: bdrv_get_device_name(s->qdev.conf.bs); |
| 566 | int max_len = s->serial ? 20 : 255 - 8; |
| 567 | int id_len = strlen(str); |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 568 | |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 569 | if (id_len > max_len) { |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 570 | id_len = max_len; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 571 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 572 | DPRINTF("Inquiry EVPD[Device identification] " |
| 573 | "buffer size %zd\n", req->cmd.xfer); |
| 574 | |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 575 | outbuf[buflen++] = 0x2; // ASCII |
| 576 | outbuf[buflen++] = 0; // not officially assigned |
| 577 | outbuf[buflen++] = 0; // reserved |
| 578 | outbuf[buflen++] = id_len; // length of data following |
Paolo Bonzini | fd93079 | 2012-03-16 19:12:43 +0100 | [diff] [blame] | 579 | memcpy(outbuf+buflen, str, id_len); |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 580 | buflen += id_len; |
Paolo Bonzini | 27395ad | 2012-05-15 12:46:09 +0200 | [diff] [blame] | 581 | |
| 582 | if (s->wwn) { |
| 583 | outbuf[buflen++] = 0x1; // Binary |
| 584 | outbuf[buflen++] = 0x3; // NAA |
| 585 | outbuf[buflen++] = 0; // reserved |
| 586 | outbuf[buflen++] = 8; |
| 587 | stq_be_p(&outbuf[buflen], s->wwn); |
| 588 | buflen += 8; |
| 589 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 590 | break; |
| 591 | } |
Christoph Hellwig | ea3bd56 | 2010-12-16 19:36:43 +0100 | [diff] [blame] | 592 | case 0xb0: /* block limits */ |
Christoph Hellwig | ee3659e | 2010-02-10 23:37:37 +0100 | [diff] [blame] | 593 | { |
Christoph Hellwig | ea3bd56 | 2010-12-16 19:36:43 +0100 | [diff] [blame] | 594 | unsigned int unmap_sectors = |
| 595 | s->qdev.conf.discard_granularity / s->qdev.blocksize; |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 596 | unsigned int min_io_size = |
| 597 | s->qdev.conf.min_io_size / s->qdev.blocksize; |
| 598 | unsigned int opt_io_size = |
| 599 | s->qdev.conf.opt_io_size / s->qdev.blocksize; |
Christoph Hellwig | ee3659e | 2010-02-10 23:37:37 +0100 | [diff] [blame] | 600 | |
Hannes Reinecke | f37bd73 | 2011-07-22 16:44:46 +0200 | [diff] [blame] | 601 | if (s->qdev.type == TYPE_ROM) { |
Hannes Reinecke | 39d9898 | 2010-11-24 12:15:58 +0100 | [diff] [blame] | 602 | DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n", |
| 603 | page_code); |
| 604 | return -1; |
| 605 | } |
Christoph Hellwig | ee3659e | 2010-02-10 23:37:37 +0100 | [diff] [blame] | 606 | /* required VPD size with unmap support */ |
Paolo Bonzini | 8257939 | 2012-05-15 12:45:20 +0200 | [diff] [blame] | 607 | buflen = 0x40; |
Christoph Hellwig | ee3659e | 2010-02-10 23:37:37 +0100 | [diff] [blame] | 608 | memset(outbuf + 4, 0, buflen - 4); |
| 609 | |
| 610 | /* optimal transfer length granularity */ |
| 611 | outbuf[6] = (min_io_size >> 8) & 0xff; |
| 612 | outbuf[7] = min_io_size & 0xff; |
| 613 | |
| 614 | /* optimal transfer length */ |
| 615 | outbuf[12] = (opt_io_size >> 24) & 0xff; |
| 616 | outbuf[13] = (opt_io_size >> 16) & 0xff; |
| 617 | outbuf[14] = (opt_io_size >> 8) & 0xff; |
| 618 | outbuf[15] = opt_io_size & 0xff; |
Christoph Hellwig | ea3bd56 | 2010-12-16 19:36:43 +0100 | [diff] [blame] | 619 | |
| 620 | /* optimal unmap granularity */ |
| 621 | outbuf[28] = (unmap_sectors >> 24) & 0xff; |
| 622 | outbuf[29] = (unmap_sectors >> 16) & 0xff; |
| 623 | outbuf[30] = (unmap_sectors >> 8) & 0xff; |
| 624 | outbuf[31] = unmap_sectors & 0xff; |
| 625 | break; |
| 626 | } |
| 627 | case 0xb2: /* thin provisioning */ |
| 628 | { |
Paolo Bonzini | 8257939 | 2012-05-15 12:45:20 +0200 | [diff] [blame] | 629 | buflen = 8; |
Christoph Hellwig | ea3bd56 | 2010-12-16 19:36:43 +0100 | [diff] [blame] | 630 | outbuf[4] = 0; |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 631 | outbuf[5] = 0xe0; /* unmap & write_same 10/16 all supported */ |
Ronnie Sahlberg | f644a29 | 2012-04-19 20:41:17 +1000 | [diff] [blame] | 632 | outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1; |
Christoph Hellwig | ea3bd56 | 2010-12-16 19:36:43 +0100 | [diff] [blame] | 633 | outbuf[7] = 0; |
Christoph Hellwig | ee3659e | 2010-02-10 23:37:37 +0100 | [diff] [blame] | 634 | break; |
| 635 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 636 | default: |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 637 | return -1; |
| 638 | } |
| 639 | /* done with EVPD */ |
Paolo Bonzini | 8257939 | 2012-05-15 12:45:20 +0200 | [diff] [blame] | 640 | assert(buflen - start <= 255); |
| 641 | outbuf[start - 1] = buflen - start; |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 642 | return buflen; |
| 643 | } |
| 644 | |
| 645 | /* Standard INQUIRY data */ |
| 646 | if (req->cmd.buf[2] != 0) { |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | /* PAGE CODE == 0 */ |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 651 | buflen = req->cmd.xfer; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 652 | if (buflen > SCSI_MAX_INQUIRY_LEN) { |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 653 | buflen = SCSI_MAX_INQUIRY_LEN; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 654 | } |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 655 | |
Hannes Reinecke | f37bd73 | 2011-07-22 16:44:46 +0200 | [diff] [blame] | 656 | outbuf[0] = s->qdev.type & 0x1f; |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 657 | outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0; |
Dmitry Fleytman | 353815a | 2012-07-06 22:03:35 -0700 | [diff] [blame] | 658 | |
| 659 | strpadcpy((char *) &outbuf[16], 16, s->product, ' '); |
| 660 | strpadcpy((char *) &outbuf[8], 8, s->vendor, ' '); |
| 661 | |
Gerd Hoffmann | 314b181 | 2010-03-10 17:47:17 +0100 | [diff] [blame] | 662 | memset(&outbuf[32], 0, 4); |
Markus Armbruster | 552fee9 | 2010-06-01 20:32:35 +0200 | [diff] [blame] | 663 | memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version))); |
Christoph Hellwig | 99aba0c | 2010-03-04 14:45:44 +0100 | [diff] [blame] | 664 | /* |
| 665 | * We claim conformance to SPC-3, which is required for guests |
| 666 | * to ask for modern features like READ CAPACITY(16) or the |
| 667 | * block characteristics VPD page by default. Not all of SPC-3 |
| 668 | * is actually implemented, but we're good enough. |
| 669 | */ |
Christoph Hellwig | ee3659e | 2010-02-10 23:37:37 +0100 | [diff] [blame] | 670 | outbuf[2] = 5; |
Ronnie Sahlberg | 1109c89 | 2012-09-14 18:13:29 -0700 | [diff] [blame] | 671 | outbuf[3] = 2 | 0x10; /* Format 2, HiSup */ |
Artyom Tarasenko | ad3cea4 | 2009-12-13 11:32:36 +0100 | [diff] [blame] | 672 | |
| 673 | if (buflen > 36) { |
| 674 | outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */ |
| 675 | } else { |
| 676 | /* If the allocation length of CDB is too small, |
| 677 | the additional length is not adjusted */ |
| 678 | outbuf[4] = 36 - 5; |
| 679 | } |
| 680 | |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 681 | /* Sync data transfer and TCQ. */ |
Paolo Bonzini | afd4030 | 2011-08-13 15:44:45 +0200 | [diff] [blame] | 682 | outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0); |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 683 | return buflen; |
| 684 | } |
| 685 | |
Paolo Bonzini | 430ee2f | 2011-09-13 15:22:31 +0200 | [diff] [blame] | 686 | static inline bool media_is_dvd(SCSIDiskState *s) |
| 687 | { |
| 688 | uint64_t nb_sectors; |
| 689 | if (s->qdev.type != TYPE_ROM) { |
| 690 | return false; |
| 691 | } |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 692 | if (!bdrv_is_inserted(s->qdev.conf.bs)) { |
Paolo Bonzini | 430ee2f | 2011-09-13 15:22:31 +0200 | [diff] [blame] | 693 | return false; |
| 694 | } |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 695 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Paolo Bonzini | 430ee2f | 2011-09-13 15:22:31 +0200 | [diff] [blame] | 696 | return nb_sectors > CD_MAX_SECTORS; |
| 697 | } |
| 698 | |
Paolo Bonzini | ceb792e | 2011-09-13 15:57:15 +0200 | [diff] [blame] | 699 | static inline bool media_is_cd(SCSIDiskState *s) |
| 700 | { |
| 701 | uint64_t nb_sectors; |
| 702 | if (s->qdev.type != TYPE_ROM) { |
| 703 | return false; |
| 704 | } |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 705 | if (!bdrv_is_inserted(s->qdev.conf.bs)) { |
Paolo Bonzini | ceb792e | 2011-09-13 15:57:15 +0200 | [diff] [blame] | 706 | return false; |
| 707 | } |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 708 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Paolo Bonzini | ceb792e | 2011-09-13 15:57:15 +0200 | [diff] [blame] | 709 | return nb_sectors <= CD_MAX_SECTORS; |
| 710 | } |
| 711 | |
Paolo Bonzini | 1a4f0c3 | 2012-05-25 12:59:55 +0200 | [diff] [blame] | 712 | static int scsi_read_disc_information(SCSIDiskState *s, SCSIDiskReq *r, |
| 713 | uint8_t *outbuf) |
| 714 | { |
| 715 | uint8_t type = r->req.cmd.buf[1] & 7; |
| 716 | |
| 717 | if (s->qdev.type != TYPE_ROM) { |
| 718 | return -1; |
| 719 | } |
| 720 | |
| 721 | /* Types 1/2 are only defined for Blu-Ray. */ |
| 722 | if (type != 0) { |
| 723 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | memset(outbuf, 0, 34); |
| 728 | outbuf[1] = 32; |
| 729 | outbuf[2] = 0xe; /* last session complete, disc finalized */ |
| 730 | outbuf[3] = 1; /* first track on disc */ |
| 731 | outbuf[4] = 1; /* # of sessions */ |
| 732 | outbuf[5] = 1; /* first track of last session */ |
| 733 | outbuf[6] = 1; /* last track of last session */ |
| 734 | outbuf[7] = 0x20; /* unrestricted use */ |
| 735 | outbuf[8] = 0x00; /* CD-ROM or DVD-ROM */ |
| 736 | /* 9-10-11: most significant byte corresponding bytes 4-5-6 */ |
| 737 | /* 12-23: not meaningful for CD-ROM or DVD-ROM */ |
| 738 | /* 24-31: disc bar code */ |
| 739 | /* 32: disc application code */ |
| 740 | /* 33: number of OPC tables */ |
| 741 | |
| 742 | return 34; |
| 743 | } |
| 744 | |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 745 | static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r, |
| 746 | uint8_t *outbuf) |
| 747 | { |
Paolo Bonzini | ceb792e | 2011-09-13 15:57:15 +0200 | [diff] [blame] | 748 | static const int rds_caps_size[5] = { |
| 749 | [0] = 2048 + 4, |
| 750 | [1] = 4 + 4, |
| 751 | [3] = 188 + 4, |
| 752 | [4] = 2048 + 4, |
| 753 | }; |
| 754 | |
| 755 | uint8_t media = r->req.cmd.buf[1]; |
| 756 | uint8_t layer = r->req.cmd.buf[6]; |
| 757 | uint8_t format = r->req.cmd.buf[7]; |
| 758 | int size = -1; |
| 759 | |
| 760 | if (s->qdev.type != TYPE_ROM) { |
| 761 | return -1; |
| 762 | } |
| 763 | if (media != 0) { |
| 764 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | if (format != 0xff) { |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 769 | if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) { |
Paolo Bonzini | ceb792e | 2011-09-13 15:57:15 +0200 | [diff] [blame] | 770 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
| 771 | return -1; |
| 772 | } |
| 773 | if (media_is_cd(s)) { |
| 774 | scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT)); |
| 775 | return -1; |
| 776 | } |
| 777 | if (format >= ARRAY_SIZE(rds_caps_size)) { |
| 778 | return -1; |
| 779 | } |
| 780 | size = rds_caps_size[format]; |
| 781 | memset(outbuf, 0, size); |
| 782 | } |
| 783 | |
| 784 | switch (format) { |
| 785 | case 0x00: { |
| 786 | /* Physical format information */ |
| 787 | uint64_t nb_sectors; |
| 788 | if (layer != 0) { |
| 789 | goto fail; |
| 790 | } |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 791 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Paolo Bonzini | ceb792e | 2011-09-13 15:57:15 +0200 | [diff] [blame] | 792 | |
| 793 | outbuf[4] = 1; /* DVD-ROM, part version 1 */ |
| 794 | outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */ |
| 795 | outbuf[6] = 1; /* one layer, read-only (per MMC-2 spec) */ |
| 796 | outbuf[7] = 0; /* default densities */ |
| 797 | |
| 798 | stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */ |
| 799 | stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */ |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | case 0x01: /* DVD copyright information, all zeros */ |
| 804 | break; |
| 805 | |
| 806 | case 0x03: /* BCA information - invalid field for no BCA info */ |
| 807 | return -1; |
| 808 | |
| 809 | case 0x04: /* DVD disc manufacturing information, all zeros */ |
| 810 | break; |
| 811 | |
| 812 | case 0xff: { /* List capabilities */ |
| 813 | int i; |
| 814 | size = 4; |
| 815 | for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) { |
| 816 | if (!rds_caps_size[i]) { |
| 817 | continue; |
| 818 | } |
| 819 | outbuf[size] = i; |
| 820 | outbuf[size + 1] = 0x40; /* Not writable, readable */ |
| 821 | stw_be_p(&outbuf[size + 2], rds_caps_size[i]); |
| 822 | size += 4; |
| 823 | } |
| 824 | break; |
| 825 | } |
| 826 | |
| 827 | default: |
| 828 | return -1; |
| 829 | } |
| 830 | |
| 831 | /* Size of buffer, not including 2 byte size field */ |
| 832 | stw_be_p(outbuf, size - 2); |
| 833 | return size; |
| 834 | |
| 835 | fail: |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 836 | return -1; |
| 837 | } |
| 838 | |
Paolo Bonzini | 3c2f7c1 | 2011-09-13 16:26:51 +0200 | [diff] [blame] | 839 | static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf) |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 840 | { |
Paolo Bonzini | 3c2f7c1 | 2011-09-13 16:26:51 +0200 | [diff] [blame] | 841 | uint8_t event_code, media_status; |
| 842 | |
| 843 | media_status = 0; |
| 844 | if (s->tray_open) { |
| 845 | media_status = MS_TRAY_OPEN; |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 846 | } else if (bdrv_is_inserted(s->qdev.conf.bs)) { |
Paolo Bonzini | 3c2f7c1 | 2011-09-13 16:26:51 +0200 | [diff] [blame] | 847 | media_status = MS_MEDIA_PRESENT; |
| 848 | } |
| 849 | |
| 850 | /* Event notification descriptor */ |
| 851 | event_code = MEC_NO_CHANGE; |
Paolo Bonzini | 4480de1 | 2011-10-25 12:53:40 +0200 | [diff] [blame] | 852 | if (media_status != MS_TRAY_OPEN) { |
| 853 | if (s->media_event) { |
| 854 | event_code = MEC_NEW_MEDIA; |
| 855 | s->media_event = false; |
| 856 | } else if (s->eject_request) { |
| 857 | event_code = MEC_EJECT_REQUESTED; |
| 858 | s->eject_request = false; |
| 859 | } |
Paolo Bonzini | 3c2f7c1 | 2011-09-13 16:26:51 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | outbuf[0] = event_code; |
| 863 | outbuf[1] = media_status; |
| 864 | |
| 865 | /* These fields are reserved, just clear them. */ |
| 866 | outbuf[2] = 0; |
| 867 | outbuf[3] = 0; |
| 868 | return 4; |
| 869 | } |
| 870 | |
| 871 | static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r, |
| 872 | uint8_t *outbuf) |
| 873 | { |
| 874 | int size; |
| 875 | uint8_t *buf = r->req.cmd.buf; |
| 876 | uint8_t notification_class_request = buf[4]; |
| 877 | if (s->qdev.type != TYPE_ROM) { |
| 878 | return -1; |
| 879 | } |
| 880 | if ((buf[1] & 1) == 0) { |
| 881 | /* asynchronous */ |
| 882 | return -1; |
| 883 | } |
| 884 | |
| 885 | size = 4; |
| 886 | outbuf[0] = outbuf[1] = 0; |
| 887 | outbuf[3] = 1 << GESN_MEDIA; /* supported events */ |
| 888 | if (notification_class_request & (1 << GESN_MEDIA)) { |
| 889 | outbuf[2] = GESN_MEDIA; |
| 890 | size += scsi_event_status_media(s, &outbuf[size]); |
| 891 | } else { |
| 892 | outbuf[2] = 0x80; |
| 893 | } |
| 894 | stw_be_p(outbuf, size - 4); |
| 895 | return size; |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Paolo Bonzini | 430ee2f | 2011-09-13 15:22:31 +0200 | [diff] [blame] | 898 | static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf) |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 899 | { |
Paolo Bonzini | 430ee2f | 2011-09-13 15:22:31 +0200 | [diff] [blame] | 900 | int current; |
| 901 | |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 902 | if (s->qdev.type != TYPE_ROM) { |
| 903 | return -1; |
| 904 | } |
Paolo Bonzini | 430ee2f | 2011-09-13 15:22:31 +0200 | [diff] [blame] | 905 | current = media_is_dvd(s) ? MMC_PROFILE_DVD_ROM : MMC_PROFILE_CD_ROM; |
| 906 | memset(outbuf, 0, 40); |
| 907 | stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */ |
| 908 | stw_be_p(&outbuf[6], current); |
| 909 | /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */ |
| 910 | outbuf[10] = 0x03; /* persistent, current */ |
| 911 | outbuf[11] = 8; /* two profiles */ |
| 912 | stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM); |
| 913 | outbuf[14] = (current == MMC_PROFILE_DVD_ROM); |
| 914 | stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM); |
| 915 | outbuf[18] = (current == MMC_PROFILE_CD_ROM); |
| 916 | /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */ |
| 917 | stw_be_p(&outbuf[20], 1); |
| 918 | outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */ |
| 919 | outbuf[23] = 8; |
| 920 | stl_be_p(&outbuf[24], 1); /* SCSI */ |
| 921 | outbuf[28] = 1; /* DBE = 1, mandatory */ |
| 922 | /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */ |
| 923 | stw_be_p(&outbuf[32], 3); |
| 924 | outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */ |
| 925 | outbuf[35] = 4; |
| 926 | outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */ |
| 927 | /* TODO: Random readable, CD read, DVD read, drive serial number, |
| 928 | power management */ |
| 929 | return 40; |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf) |
| 933 | { |
| 934 | if (s->qdev.type != TYPE_ROM) { |
| 935 | return -1; |
| 936 | } |
| 937 | memset(outbuf, 0, 8); |
| 938 | outbuf[5] = 1; /* CD-ROM */ |
| 939 | return 8; |
| 940 | } |
| 941 | |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 942 | static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf, |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 943 | int page_control) |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 944 | { |
Paolo Bonzini | a8f4bbe | 2011-09-13 14:50:15 +0200 | [diff] [blame] | 945 | static const int mode_sense_valid[0x3f] = { |
| 946 | [MODE_PAGE_HD_GEOMETRY] = (1 << TYPE_DISK), |
| 947 | [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK), |
| 948 | [MODE_PAGE_CACHING] = (1 << TYPE_DISK) | (1 << TYPE_ROM), |
Paolo Bonzini | a07c7dc | 2011-09-13 15:08:22 +0200 | [diff] [blame] | 949 | [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM), |
| 950 | [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM), |
Paolo Bonzini | a8f4bbe | 2011-09-13 14:50:15 +0200 | [diff] [blame] | 951 | [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM), |
| 952 | }; |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 953 | |
| 954 | uint8_t *p = *p_outbuf + 2; |
| 955 | int length; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 956 | |
Paolo Bonzini | a8f4bbe | 2011-09-13 14:50:15 +0200 | [diff] [blame] | 957 | if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) { |
| 958 | return -1; |
| 959 | } |
| 960 | |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 961 | /* |
| 962 | * If Changeable Values are requested, a mask denoting those mode parameters |
| 963 | * that are changeable shall be returned. As we currently don't support |
| 964 | * parameter changes via MODE_SELECT all bits are returned set to zero. |
| 965 | * The buffer was already menset to zero by the caller of this function. |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 966 | * |
| 967 | * The offsets here are off by two compared to the descriptions in the |
| 968 | * SCSI specs, because those include a 2-byte header. This is unfortunate, |
| 969 | * but it is done so that offsets are consistent within our implementation |
| 970 | * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both |
| 971 | * 2-byte and 4-byte headers. |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 972 | */ |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 973 | switch (page) { |
Paolo Bonzini | 67cc61e | 2011-09-13 14:41:56 +0200 | [diff] [blame] | 974 | case MODE_PAGE_HD_GEOMETRY: |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 975 | length = 0x16; |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 976 | if (page_control == 1) { /* Changeable Values */ |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 977 | break; |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 978 | } |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 979 | /* if a geometry hint is available, use it */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 980 | p[0] = (s->qdev.conf.cyls >> 16) & 0xff; |
| 981 | p[1] = (s->qdev.conf.cyls >> 8) & 0xff; |
| 982 | p[2] = s->qdev.conf.cyls & 0xff; |
| 983 | p[3] = s->qdev.conf.heads & 0xff; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 984 | /* Write precomp start cylinder, disabled */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 985 | p[4] = (s->qdev.conf.cyls >> 16) & 0xff; |
| 986 | p[5] = (s->qdev.conf.cyls >> 8) & 0xff; |
| 987 | p[6] = s->qdev.conf.cyls & 0xff; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 988 | /* Reduced current start cylinder, disabled */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 989 | p[7] = (s->qdev.conf.cyls >> 16) & 0xff; |
| 990 | p[8] = (s->qdev.conf.cyls >> 8) & 0xff; |
| 991 | p[9] = s->qdev.conf.cyls & 0xff; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 992 | /* Device step rate [ns], 200ns */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 993 | p[10] = 0; |
| 994 | p[11] = 200; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 995 | /* Landing zone cylinder */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 996 | p[12] = 0xff; |
| 997 | p[13] = 0xff; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 998 | p[14] = 0xff; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 999 | /* Medium rotation rate [rpm], 5400 rpm */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1000 | p[18] = (5400 >> 8) & 0xff; |
| 1001 | p[19] = 5400 & 0xff; |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1002 | break; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1003 | |
Paolo Bonzini | 67cc61e | 2011-09-13 14:41:56 +0200 | [diff] [blame] | 1004 | case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY: |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1005 | length = 0x1e; |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 1006 | if (page_control == 1) { /* Changeable Values */ |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1007 | break; |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 1008 | } |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1009 | /* Transfer rate [kbit/s], 5Mbit/s */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1010 | p[0] = 5000 >> 8; |
| 1011 | p[1] = 5000 & 0xff; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1012 | /* if a geometry hint is available, use it */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1013 | p[2] = s->qdev.conf.heads & 0xff; |
| 1014 | p[3] = s->qdev.conf.secs & 0xff; |
| 1015 | p[4] = s->qdev.blocksize >> 8; |
| 1016 | p[6] = (s->qdev.conf.cyls >> 8) & 0xff; |
| 1017 | p[7] = s->qdev.conf.cyls & 0xff; |
| 1018 | /* Write precomp start cylinder, disabled */ |
Markus Armbruster | d252df4 | 2012-07-10 11:12:42 +0200 | [diff] [blame] | 1019 | p[8] = (s->qdev.conf.cyls >> 8) & 0xff; |
| 1020 | p[9] = s->qdev.conf.cyls & 0xff; |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1021 | /* Reduced current start cylinder, disabled */ |
Markus Armbruster | d252df4 | 2012-07-10 11:12:42 +0200 | [diff] [blame] | 1022 | p[10] = (s->qdev.conf.cyls >> 8) & 0xff; |
| 1023 | p[11] = s->qdev.conf.cyls & 0xff; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1024 | /* Device step rate [100us], 100us */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1025 | p[12] = 0; |
| 1026 | p[13] = 1; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1027 | /* Device step pulse width [us], 1us */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1028 | p[14] = 1; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1029 | /* Device head settle delay [100us], 100us */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1030 | p[15] = 0; |
| 1031 | p[16] = 1; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1032 | /* Motor on delay [0.1s], 0.1s */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1033 | p[17] = 1; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1034 | /* Motor off delay [0.1s], 0.1s */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1035 | p[18] = 1; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1036 | /* Medium rotation rate [rpm], 5400 rpm */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1037 | p[26] = (5400 >> 8) & 0xff; |
| 1038 | p[27] = 5400 & 0xff; |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1039 | break; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1040 | |
Paolo Bonzini | 67cc61e | 2011-09-13 14:41:56 +0200 | [diff] [blame] | 1041 | case MODE_PAGE_CACHING: |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1042 | length = 0x12; |
Paolo Bonzini | 96c91bb | 2012-07-10 15:03:17 +0200 | [diff] [blame] | 1043 | if (page_control == 1 || /* Changeable Values */ |
| 1044 | bdrv_enable_write_cache(s->qdev.conf.bs)) { |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1045 | p[0] = 4; /* WCE */ |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1046 | } |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1047 | break; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1048 | |
Paolo Bonzini | a07c7dc | 2011-09-13 15:08:22 +0200 | [diff] [blame] | 1049 | case MODE_PAGE_R_W_ERROR: |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1050 | length = 10; |
Paolo Bonzini | 4f588b1 | 2012-07-10 14:04:51 +0200 | [diff] [blame] | 1051 | if (page_control == 1) { /* Changeable Values */ |
| 1052 | break; |
| 1053 | } |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1054 | p[0] = 0x80; /* Automatic Write Reallocation Enabled */ |
Paolo Bonzini | a07c7dc | 2011-09-13 15:08:22 +0200 | [diff] [blame] | 1055 | if (s->qdev.type == TYPE_ROM) { |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1056 | p[1] = 0x20; /* Read Retry Count */ |
Paolo Bonzini | a07c7dc | 2011-09-13 15:08:22 +0200 | [diff] [blame] | 1057 | } |
| 1058 | break; |
| 1059 | |
| 1060 | case MODE_PAGE_AUDIO_CTL: |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1061 | length = 14; |
Paolo Bonzini | a07c7dc | 2011-09-13 15:08:22 +0200 | [diff] [blame] | 1062 | break; |
| 1063 | |
Paolo Bonzini | 67cc61e | 2011-09-13 14:41:56 +0200 | [diff] [blame] | 1064 | case MODE_PAGE_CAPABILITIES: |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1065 | length = 0x14; |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 1066 | if (page_control == 1) { /* Changeable Values */ |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1067 | break; |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 1068 | } |
Paolo Bonzini | a07c7dc | 2011-09-13 15:08:22 +0200 | [diff] [blame] | 1069 | |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1070 | p[0] = 0x3b; /* CD-R & CD-RW read */ |
| 1071 | p[1] = 0; /* Writing not supported */ |
| 1072 | p[2] = 0x7f; /* Audio, composite, digital out, |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1073 | mode 2 form 1&2, multi session */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1074 | p[3] = 0xff; /* CD DA, DA accurate, RW supported, |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1075 | RW corrected, C2 errors, ISRC, |
| 1076 | UPC, Bar code */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1077 | p[4] = 0x2d | (s->tray_locked ? 2 : 0); |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1078 | /* Locking supported, jumper present, eject, tray */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1079 | p[5] = 0; /* no volume & mute control, no |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1080 | changer */ |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1081 | p[6] = (50 * 176) >> 8; /* 50x read speed */ |
| 1082 | p[7] = (50 * 176) & 0xff; |
| 1083 | p[8] = 2 >> 8; /* Two volume levels */ |
| 1084 | p[9] = 2 & 0xff; |
| 1085 | p[10] = 2048 >> 8; /* 2M buffer */ |
| 1086 | p[11] = 2048 & 0xff; |
| 1087 | p[12] = (16 * 176) >> 8; /* 16x read speed current */ |
| 1088 | p[13] = (16 * 176) & 0xff; |
| 1089 | p[16] = (16 * 176) >> 8; /* 16x write speed */ |
| 1090 | p[17] = (16 * 176) & 0xff; |
| 1091 | p[18] = (16 * 176) >> 8; /* 16x write speed current */ |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1092 | p[19] = (16 * 176) & 0xff; |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1093 | break; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1094 | |
| 1095 | default: |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1096 | return -1; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1097 | } |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1098 | |
Paolo Bonzini | ef40561 | 2012-07-12 16:08:24 +0200 | [diff] [blame] | 1099 | assert(length < 256); |
| 1100 | (*p_outbuf)[0] = page; |
| 1101 | (*p_outbuf)[1] = length; |
| 1102 | *p_outbuf += length + 2; |
| 1103 | return length + 2; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1106 | static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf) |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1107 | { |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1108 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1109 | uint64_t nb_sectors; |
Paolo Bonzini | e590ecb | 2012-04-19 10:24:18 +0200 | [diff] [blame] | 1110 | bool dbd; |
| 1111 | int page, buflen, ret, page_control; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1112 | uint8_t *p; |
Bernhard Kohl | ce512ee | 2010-08-31 14:08:23 +0200 | [diff] [blame] | 1113 | uint8_t dev_specific_param; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1114 | |
Paolo Bonzini | e590ecb | 2012-04-19 10:24:18 +0200 | [diff] [blame] | 1115 | dbd = (r->req.cmd.buf[1] & 0x8) != 0; |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1116 | page = r->req.cmd.buf[2] & 0x3f; |
| 1117 | page_control = (r->req.cmd.buf[2] & 0xc0) >> 6; |
Bernhard Kohl | aa2b1e8 | 2010-09-01 16:33:21 +0200 | [diff] [blame] | 1118 | DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n", |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1119 | (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control); |
| 1120 | memset(outbuf, 0, r->req.cmd.xfer); |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1121 | p = outbuf; |
| 1122 | |
Paolo Bonzini | e590ecb | 2012-04-19 10:24:18 +0200 | [diff] [blame] | 1123 | if (s->qdev.type == TYPE_DISK) { |
Paolo Bonzini | da8365d | 2012-05-01 10:25:16 +0200 | [diff] [blame] | 1124 | dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0; |
Paolo Bonzini | e590ecb | 2012-04-19 10:24:18 +0200 | [diff] [blame] | 1125 | if (bdrv_is_read_only(s->qdev.conf.bs)) { |
| 1126 | dev_specific_param |= 0x80; /* Readonly. */ |
| 1127 | } |
Bernhard Kohl | ce512ee | 2010-08-31 14:08:23 +0200 | [diff] [blame] | 1128 | } else { |
Paolo Bonzini | e590ecb | 2012-04-19 10:24:18 +0200 | [diff] [blame] | 1129 | /* MMC prescribes that CD/DVD drives have no block descriptors, |
| 1130 | * and defines no device-specific parameter. */ |
Paolo Bonzini | 6a2de0f | 2012-04-19 10:28:15 +0200 | [diff] [blame] | 1131 | dev_specific_param = 0x00; |
Paolo Bonzini | e590ecb | 2012-04-19 10:24:18 +0200 | [diff] [blame] | 1132 | dbd = true; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1133 | } |
Bernhard Kohl | ce512ee | 2010-08-31 14:08:23 +0200 | [diff] [blame] | 1134 | |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1135 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
Bernhard Kohl | ce512ee | 2010-08-31 14:08:23 +0200 | [diff] [blame] | 1136 | p[1] = 0; /* Default media type. */ |
| 1137 | p[2] = dev_specific_param; |
| 1138 | p[3] = 0; /* Block descriptor length. */ |
| 1139 | p += 4; |
| 1140 | } else { /* MODE_SENSE_10 */ |
| 1141 | p[2] = 0; /* Default media type. */ |
| 1142 | p[3] = dev_specific_param; |
| 1143 | p[6] = p[7] = 0; /* Block descriptor length. */ |
| 1144 | p += 8; |
| 1145 | } |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1146 | |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1147 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Paolo Bonzini | e590ecb | 2012-04-19 10:24:18 +0200 | [diff] [blame] | 1148 | if (!dbd && nb_sectors) { |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1149 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
Bernhard Kohl | ce512ee | 2010-08-31 14:08:23 +0200 | [diff] [blame] | 1150 | outbuf[3] = 8; /* Block descriptor length */ |
| 1151 | } else { /* MODE_SENSE_10 */ |
| 1152 | outbuf[7] = 8; /* Block descriptor length */ |
| 1153 | } |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1154 | nb_sectors /= (s->qdev.blocksize / 512); |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1155 | if (nb_sectors > 0xffffff) { |
Bernhard Kohl | 2488b74 | 2010-08-31 14:08:25 +0200 | [diff] [blame] | 1156 | nb_sectors = 0; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1157 | } |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1158 | p[0] = 0; /* media density code */ |
| 1159 | p[1] = (nb_sectors >> 16) & 0xff; |
| 1160 | p[2] = (nb_sectors >> 8) & 0xff; |
| 1161 | p[3] = nb_sectors & 0xff; |
| 1162 | p[4] = 0; /* reserved */ |
| 1163 | p[5] = 0; /* bytes 5-7 are the sector size in bytes */ |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1164 | p[6] = s->qdev.blocksize >> 8; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1165 | p[7] = 0; |
| 1166 | p += 8; |
| 1167 | } |
| 1168 | |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1169 | if (page_control == 3) { |
| 1170 | /* Saved Values */ |
| 1171 | scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED)); |
| 1172 | return -1; |
Bernhard Kohl | 282ab04 | 2010-08-31 14:08:24 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1175 | if (page == 0x3f) { |
| 1176 | for (page = 0; page <= 0x3e; page++) { |
| 1177 | mode_sense_page(s, page, &p, page_control); |
| 1178 | } |
| 1179 | } else { |
| 1180 | ret = mode_sense_page(s, page, &p, page_control); |
| 1181 | if (ret == -1) { |
| 1182 | return -1; |
| 1183 | } |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | buflen = p - outbuf; |
Bernhard Kohl | ce512ee | 2010-08-31 14:08:23 +0200 | [diff] [blame] | 1187 | /* |
| 1188 | * The mode data length field specifies the length in bytes of the |
| 1189 | * following data that is available to be transferred. The mode data |
| 1190 | * length does not include itself. |
| 1191 | */ |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1192 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
Bernhard Kohl | ce512ee | 2010-08-31 14:08:23 +0200 | [diff] [blame] | 1193 | outbuf[0] = buflen - 1; |
| 1194 | } else { /* MODE_SENSE_10 */ |
| 1195 | outbuf[0] = ((buflen - 2) >> 8) & 0xff; |
| 1196 | outbuf[1] = (buflen - 2) & 0xff; |
| 1197 | } |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1198 | return buflen; |
| 1199 | } |
| 1200 | |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1201 | static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf) |
| 1202 | { |
| 1203 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1204 | int start_track, format, msf, toclen; |
| 1205 | uint64_t nb_sectors; |
| 1206 | |
| 1207 | msf = req->cmd.buf[1] & 2; |
| 1208 | format = req->cmd.buf[2] & 0xf; |
| 1209 | start_track = req->cmd.buf[6]; |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1210 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1211 | DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1); |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1212 | nb_sectors /= s->qdev.blocksize / 512; |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1213 | switch (format) { |
| 1214 | case 0: |
| 1215 | toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track); |
| 1216 | break; |
| 1217 | case 1: |
| 1218 | /* multi session : only a single session defined */ |
| 1219 | toclen = 12; |
| 1220 | memset(outbuf, 0, 12); |
| 1221 | outbuf[1] = 0x0a; |
| 1222 | outbuf[2] = 0x01; |
| 1223 | outbuf[3] = 0x01; |
| 1224 | break; |
| 1225 | case 2: |
| 1226 | toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track); |
| 1227 | break; |
| 1228 | default: |
| 1229 | return -1; |
| 1230 | } |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1231 | return toclen; |
| 1232 | } |
| 1233 | |
Markus Armbruster | 68bb01f | 2011-09-06 18:58:49 +0200 | [diff] [blame] | 1234 | static int scsi_disk_emulate_start_stop(SCSIDiskReq *r) |
Markus Armbruster | bfd5264 | 2011-09-06 18:58:39 +0200 | [diff] [blame] | 1235 | { |
| 1236 | SCSIRequest *req = &r->req; |
| 1237 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
| 1238 | bool start = req->cmd.buf[4] & 1; |
| 1239 | bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */ |
Ronnie Sahlberg | ae5708b | 2012-07-28 11:14:53 +1000 | [diff] [blame] | 1240 | int pwrcnd = req->cmd.buf[4] & 0xf0; |
| 1241 | |
| 1242 | if (pwrcnd) { |
| 1243 | /* eject/load only happens for power condition == 0 */ |
| 1244 | return 0; |
| 1245 | } |
Markus Armbruster | bfd5264 | 2011-09-06 18:58:39 +0200 | [diff] [blame] | 1246 | |
Paolo Bonzini | b456a71 | 2012-07-16 15:59:57 +0200 | [diff] [blame] | 1247 | if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && loej) { |
Markus Armbruster | 68bb01f | 2011-09-06 18:58:49 +0200 | [diff] [blame] | 1248 | if (!start && !s->tray_open && s->tray_locked) { |
| 1249 | scsi_check_condition(r, |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1250 | bdrv_is_inserted(s->qdev.conf.bs) |
Markus Armbruster | 68bb01f | 2011-09-06 18:58:49 +0200 | [diff] [blame] | 1251 | ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED) |
| 1252 | : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED)); |
| 1253 | return -1; |
Markus Armbruster | fdec440 | 2011-09-06 18:58:45 +0200 | [diff] [blame] | 1254 | } |
Luiz Capitulino | d88b181 | 2012-02-17 11:05:21 -0200 | [diff] [blame] | 1255 | |
| 1256 | if (s->tray_open != !start) { |
| 1257 | bdrv_eject(s->qdev.conf.bs, !start); |
| 1258 | s->tray_open = !start; |
| 1259 | } |
Markus Armbruster | bfd5264 | 2011-09-06 18:58:39 +0200 | [diff] [blame] | 1260 | } |
Markus Armbruster | 68bb01f | 2011-09-06 18:58:49 +0200 | [diff] [blame] | 1261 | return 0; |
Markus Armbruster | bfd5264 | 2011-09-06 18:58:39 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
Paolo Bonzini | 314a329 | 2012-02-08 10:26:21 +0100 | [diff] [blame] | 1264 | static void scsi_disk_emulate_read_data(SCSIRequest *req) |
| 1265 | { |
| 1266 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
| 1267 | int buflen = r->iov.iov_len; |
| 1268 | |
| 1269 | if (buflen) { |
Paolo Bonzini | 79fb50b | 2012-07-31 16:10:23 +0200 | [diff] [blame] | 1270 | DPRINTF("Read buf_len=%d\n", buflen); |
Paolo Bonzini | 314a329 | 2012-02-08 10:26:21 +0100 | [diff] [blame] | 1271 | r->iov.iov_len = 0; |
| 1272 | r->started = true; |
| 1273 | scsi_req_data(&r->req, buflen); |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | /* This also clears the sense buffer for REQUEST SENSE. */ |
| 1278 | scsi_req_complete(&r->req, GOOD); |
| 1279 | } |
| 1280 | |
Paolo Bonzini | 380feaf | 2012-07-10 15:02:55 +0200 | [diff] [blame] | 1281 | static int scsi_disk_check_mode_select(SCSIDiskState *s, int page, |
| 1282 | uint8_t *inbuf, int inlen) |
| 1283 | { |
| 1284 | uint8_t mode_current[SCSI_MAX_MODE_LEN]; |
| 1285 | uint8_t mode_changeable[SCSI_MAX_MODE_LEN]; |
| 1286 | uint8_t *p; |
| 1287 | int len, expected_len, changeable_len, i; |
| 1288 | |
| 1289 | /* The input buffer does not include the page header, so it is |
| 1290 | * off by 2 bytes. |
| 1291 | */ |
| 1292 | expected_len = inlen + 2; |
| 1293 | if (expected_len > SCSI_MAX_MODE_LEN) { |
| 1294 | return -1; |
| 1295 | } |
| 1296 | |
| 1297 | p = mode_current; |
| 1298 | memset(mode_current, 0, inlen + 2); |
| 1299 | len = mode_sense_page(s, page, &p, 0); |
| 1300 | if (len < 0 || len != expected_len) { |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
| 1304 | p = mode_changeable; |
| 1305 | memset(mode_changeable, 0, inlen + 2); |
| 1306 | changeable_len = mode_sense_page(s, page, &p, 1); |
| 1307 | assert(changeable_len == len); |
| 1308 | |
| 1309 | /* Check that unchangeable bits are the same as what MODE SENSE |
| 1310 | * would return. |
| 1311 | */ |
| 1312 | for (i = 2; i < len; i++) { |
| 1313 | if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) != 0) { |
| 1314 | return -1; |
| 1315 | } |
| 1316 | } |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | static void scsi_disk_apply_mode_select(SCSIDiskState *s, int page, uint8_t *p) |
| 1321 | { |
Paolo Bonzini | 96c91bb | 2012-07-10 15:03:17 +0200 | [diff] [blame] | 1322 | switch (page) { |
| 1323 | case MODE_PAGE_CACHING: |
| 1324 | bdrv_set_enable_write_cache(s->qdev.conf.bs, (p[0] & 4) != 0); |
| 1325 | break; |
| 1326 | |
| 1327 | default: |
| 1328 | break; |
| 1329 | } |
Paolo Bonzini | 380feaf | 2012-07-10 15:02:55 +0200 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static int mode_select_pages(SCSIDiskReq *r, uint8_t *p, int len, bool change) |
| 1333 | { |
| 1334 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
| 1335 | |
| 1336 | while (len > 0) { |
| 1337 | int page, subpage, page_len; |
| 1338 | |
| 1339 | /* Parse both possible formats for the mode page headers. */ |
| 1340 | page = p[0] & 0x3f; |
| 1341 | if (p[0] & 0x40) { |
| 1342 | if (len < 4) { |
| 1343 | goto invalid_param_len; |
| 1344 | } |
| 1345 | subpage = p[1]; |
| 1346 | page_len = lduw_be_p(&p[2]); |
| 1347 | p += 4; |
| 1348 | len -= 4; |
| 1349 | } else { |
| 1350 | if (len < 2) { |
| 1351 | goto invalid_param_len; |
| 1352 | } |
| 1353 | subpage = 0; |
| 1354 | page_len = p[1]; |
| 1355 | p += 2; |
| 1356 | len -= 2; |
| 1357 | } |
| 1358 | |
| 1359 | if (subpage) { |
| 1360 | goto invalid_param; |
| 1361 | } |
| 1362 | if (page_len > len) { |
| 1363 | goto invalid_param_len; |
| 1364 | } |
| 1365 | |
| 1366 | if (!change) { |
| 1367 | if (scsi_disk_check_mode_select(s, page, p, page_len) < 0) { |
| 1368 | goto invalid_param; |
| 1369 | } |
| 1370 | } else { |
| 1371 | scsi_disk_apply_mode_select(s, page, p); |
| 1372 | } |
| 1373 | |
| 1374 | p += page_len; |
| 1375 | len -= page_len; |
| 1376 | } |
| 1377 | return 0; |
| 1378 | |
| 1379 | invalid_param: |
| 1380 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); |
| 1381 | return -1; |
| 1382 | |
| 1383 | invalid_param_len: |
| 1384 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); |
| 1385 | return -1; |
| 1386 | } |
| 1387 | |
| 1388 | static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf) |
| 1389 | { |
Paolo Bonzini | accfeb2 | 2012-10-31 17:14:41 +0100 | [diff] [blame] | 1390 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
Paolo Bonzini | 380feaf | 2012-07-10 15:02:55 +0200 | [diff] [blame] | 1391 | uint8_t *p = inbuf; |
| 1392 | int cmd = r->req.cmd.buf[0]; |
| 1393 | int len = r->req.cmd.xfer; |
| 1394 | int hdr_len = (cmd == MODE_SELECT ? 4 : 8); |
| 1395 | int bd_len; |
| 1396 | int pass; |
| 1397 | |
| 1398 | /* We only support PF=1, SP=0. */ |
| 1399 | if ((r->req.cmd.buf[1] & 0x11) != 0x10) { |
| 1400 | goto invalid_field; |
| 1401 | } |
| 1402 | |
| 1403 | if (len < hdr_len) { |
| 1404 | goto invalid_param_len; |
| 1405 | } |
| 1406 | |
| 1407 | bd_len = (cmd == MODE_SELECT ? p[3] : lduw_be_p(&p[6])); |
| 1408 | len -= hdr_len; |
| 1409 | p += hdr_len; |
| 1410 | if (len < bd_len) { |
| 1411 | goto invalid_param_len; |
| 1412 | } |
| 1413 | if (bd_len != 0 && bd_len != 8) { |
| 1414 | goto invalid_param; |
| 1415 | } |
| 1416 | |
| 1417 | len -= bd_len; |
| 1418 | p += bd_len; |
| 1419 | |
| 1420 | /* Ensure no change is made if there is an error! */ |
| 1421 | for (pass = 0; pass < 2; pass++) { |
| 1422 | if (mode_select_pages(r, p, len, pass == 1) < 0) { |
| 1423 | assert(pass == 0); |
| 1424 | return; |
| 1425 | } |
| 1426 | } |
Paolo Bonzini | accfeb2 | 2012-10-31 17:14:41 +0100 | [diff] [blame] | 1427 | if (!bdrv_enable_write_cache(s->qdev.conf.bs)) { |
| 1428 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 1429 | scsi_req_ref(&r->req); |
| 1430 | bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH); |
| 1431 | r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r); |
| 1432 | return; |
| 1433 | } |
| 1434 | |
Paolo Bonzini | 380feaf | 2012-07-10 15:02:55 +0200 | [diff] [blame] | 1435 | scsi_req_complete(&r->req, GOOD); |
| 1436 | return; |
| 1437 | |
| 1438 | invalid_param: |
| 1439 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); |
| 1440 | return; |
| 1441 | |
| 1442 | invalid_param_len: |
| 1443 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); |
| 1444 | return; |
| 1445 | |
| 1446 | invalid_field: |
| 1447 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
Paolo Bonzini | 380feaf | 2012-07-10 15:02:55 +0200 | [diff] [blame] | 1448 | } |
| 1449 | |
Paolo Bonzini | 444bc90 | 2012-09-05 17:46:18 +0200 | [diff] [blame] | 1450 | static inline bool check_lba_range(SCSIDiskState *s, |
| 1451 | uint64_t sector_num, uint32_t nb_sectors) |
| 1452 | { |
| 1453 | /* |
| 1454 | * The first line tests that no overflow happens when computing the last |
| 1455 | * sector. The second line tests that the last accessed sector is in |
| 1456 | * range. |
Paolo Bonzini | 12ca76f | 2012-09-05 17:54:36 +0200 | [diff] [blame] | 1457 | * |
| 1458 | * Careful, the computations should not underflow for nb_sectors == 0, |
| 1459 | * and a 0-block read to the first LBA beyond the end of device is |
| 1460 | * valid. |
Paolo Bonzini | 444bc90 | 2012-09-05 17:46:18 +0200 | [diff] [blame] | 1461 | */ |
| 1462 | return (sector_num <= sector_num + nb_sectors && |
Paolo Bonzini | 12ca76f | 2012-09-05 17:54:36 +0200 | [diff] [blame] | 1463 | sector_num + nb_sectors <= s->qdev.max_lba + 1); |
Paolo Bonzini | 444bc90 | 2012-09-05 17:46:18 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 1466 | typedef struct UnmapCBData { |
| 1467 | SCSIDiskReq *r; |
| 1468 | uint8_t *inbuf; |
| 1469 | int count; |
| 1470 | } UnmapCBData; |
| 1471 | |
| 1472 | static void scsi_unmap_complete(void *opaque, int ret) |
| 1473 | { |
| 1474 | UnmapCBData *data = opaque; |
| 1475 | SCSIDiskReq *r = data->r; |
| 1476 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
| 1477 | uint64_t sector_num; |
Stefan Weil | 5bb0b62 | 2012-08-29 20:39:25 +0200 | [diff] [blame] | 1478 | uint32_t nb_sectors; |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 1479 | |
| 1480 | r->req.aiocb = NULL; |
| 1481 | if (ret < 0) { |
| 1482 | if (scsi_handle_rw_error(r, -ret)) { |
| 1483 | goto done; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | if (data->count > 0 && !r->req.io_canceled) { |
| 1488 | sector_num = ldq_be_p(&data->inbuf[0]); |
| 1489 | nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL; |
Paolo Bonzini | 444bc90 | 2012-09-05 17:46:18 +0200 | [diff] [blame] | 1490 | if (!check_lba_range(s, sector_num, nb_sectors)) { |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 1491 | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
| 1492 | goto done; |
| 1493 | } |
| 1494 | |
| 1495 | r->req.aiocb = bdrv_aio_discard(s->qdev.conf.bs, |
| 1496 | sector_num * (s->qdev.blocksize / 512), |
| 1497 | nb_sectors * (s->qdev.blocksize / 512), |
| 1498 | scsi_unmap_complete, data); |
| 1499 | data->count--; |
| 1500 | data->inbuf += 16; |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | done: |
| 1505 | if (data->count == 0) { |
| 1506 | scsi_req_complete(&r->req, GOOD); |
| 1507 | } |
| 1508 | if (!r->req.io_canceled) { |
| 1509 | scsi_req_unref(&r->req); |
| 1510 | } |
| 1511 | g_free(data); |
| 1512 | } |
| 1513 | |
| 1514 | static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf) |
| 1515 | { |
| 1516 | uint8_t *p = inbuf; |
| 1517 | int len = r->req.cmd.xfer; |
| 1518 | UnmapCBData *data; |
| 1519 | |
| 1520 | if (len < 8) { |
| 1521 | goto invalid_param_len; |
| 1522 | } |
| 1523 | if (len < lduw_be_p(&p[0]) + 2) { |
| 1524 | goto invalid_param_len; |
| 1525 | } |
| 1526 | if (len < lduw_be_p(&p[2]) + 8) { |
| 1527 | goto invalid_param_len; |
| 1528 | } |
| 1529 | if (lduw_be_p(&p[2]) & 15) { |
| 1530 | goto invalid_param_len; |
| 1531 | } |
| 1532 | |
| 1533 | data = g_new0(UnmapCBData, 1); |
| 1534 | data->r = r; |
| 1535 | data->inbuf = &p[8]; |
| 1536 | data->count = lduw_be_p(&p[2]) >> 4; |
| 1537 | |
| 1538 | /* The matching unref is in scsi_unmap_complete, before data is freed. */ |
| 1539 | scsi_req_ref(&r->req); |
| 1540 | scsi_unmap_complete(data, 0); |
| 1541 | return; |
| 1542 | |
| 1543 | invalid_param_len: |
| 1544 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 1545 | } |
| 1546 | |
Paolo Bonzini | 314a329 | 2012-02-08 10:26:21 +0100 | [diff] [blame] | 1547 | static void scsi_disk_emulate_write_data(SCSIRequest *req) |
| 1548 | { |
Paolo Bonzini | af6d510 | 2012-02-08 10:32:55 +0100 | [diff] [blame] | 1549 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
| 1550 | |
| 1551 | if (r->iov.iov_len) { |
| 1552 | int buflen = r->iov.iov_len; |
Paolo Bonzini | 79fb50b | 2012-07-31 16:10:23 +0200 | [diff] [blame] | 1553 | DPRINTF("Write buf_len=%d\n", buflen); |
Paolo Bonzini | af6d510 | 2012-02-08 10:32:55 +0100 | [diff] [blame] | 1554 | r->iov.iov_len = 0; |
| 1555 | scsi_req_data(&r->req, buflen); |
| 1556 | return; |
| 1557 | } |
| 1558 | |
| 1559 | switch (req->cmd.buf[0]) { |
| 1560 | case MODE_SELECT: |
| 1561 | case MODE_SELECT_10: |
| 1562 | /* This also clears the sense buffer for REQUEST SENSE. */ |
Paolo Bonzini | 380feaf | 2012-07-10 15:02:55 +0200 | [diff] [blame] | 1563 | scsi_disk_emulate_mode_select(r, r->iov.iov_base); |
Paolo Bonzini | af6d510 | 2012-02-08 10:32:55 +0100 | [diff] [blame] | 1564 | break; |
| 1565 | |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 1566 | case UNMAP: |
| 1567 | scsi_disk_emulate_unmap(r, r->iov.iov_base); |
| 1568 | break; |
| 1569 | |
Paolo Bonzini | af6d510 | 2012-02-08 10:32:55 +0100 | [diff] [blame] | 1570 | default: |
| 1571 | abort(); |
| 1572 | } |
Paolo Bonzini | 314a329 | 2012-02-08 10:26:21 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1575 | static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf) |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1576 | { |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1577 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1578 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1579 | uint64_t nb_sectors; |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 1580 | uint8_t *outbuf; |
Paolo Bonzini | af6d510 | 2012-02-08 10:32:55 +0100 | [diff] [blame] | 1581 | int buflen; |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1582 | |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1583 | switch (req->cmd.buf[0]) { |
| 1584 | case INQUIRY: |
| 1585 | case MODE_SENSE: |
| 1586 | case MODE_SENSE_10: |
| 1587 | case RESERVE: |
| 1588 | case RESERVE_10: |
| 1589 | case RELEASE: |
| 1590 | case RELEASE_10: |
| 1591 | case START_STOP: |
| 1592 | case ALLOW_MEDIUM_REMOVAL: |
| 1593 | case GET_CONFIGURATION: |
| 1594 | case GET_EVENT_STATUS_NOTIFICATION: |
| 1595 | case MECHANISM_STATUS: |
| 1596 | case REQUEST_SENSE: |
| 1597 | break; |
| 1598 | |
| 1599 | default: |
| 1600 | if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) { |
| 1601 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
| 1602 | return 0; |
| 1603 | } |
| 1604 | break; |
| 1605 | } |
| 1606 | |
Paolo Bonzini | c8dcb53 | 2012-10-10 12:18:03 +0200 | [diff] [blame] | 1607 | /* |
| 1608 | * FIXME: we shouldn't return anything bigger than 4k, but the code |
| 1609 | * requires the buffer to be as big as req->cmd.xfer in several |
| 1610 | * places. So, do not allow CDBs with a very large ALLOCATION |
| 1611 | * LENGTH. The real fix would be to modify scsi_read_data and |
| 1612 | * dma_buf_read, so that they return data beyond the buflen |
| 1613 | * as all zeros. |
| 1614 | */ |
| 1615 | if (req->cmd.xfer > 65536) { |
| 1616 | goto illegal_request; |
| 1617 | } |
| 1618 | r->buflen = MAX(4096, req->cmd.xfer); |
| 1619 | |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 1620 | if (!r->iov.iov_base) { |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1621 | r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen); |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Paolo Bonzini | af6d510 | 2012-02-08 10:32:55 +0100 | [diff] [blame] | 1624 | buflen = req->cmd.xfer; |
Paolo Bonzini | 7285477 | 2011-09-16 16:40:04 +0200 | [diff] [blame] | 1625 | outbuf = r->iov.iov_base; |
Paolo Bonzini | c8dcb53 | 2012-10-10 12:18:03 +0200 | [diff] [blame] | 1626 | memset(outbuf, 0, r->buflen); |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1627 | switch (req->cmd.buf[0]) { |
| 1628 | case TEST_UNIT_READY: |
Paolo Bonzini | 9bcaf4f | 2012-03-16 19:11:43 +0100 | [diff] [blame] | 1629 | assert(!s->tray_open && bdrv_is_inserted(s->qdev.conf.bs)); |
Hannes Reinecke | 5f71d32 | 2011-07-22 16:51:12 +0200 | [diff] [blame] | 1630 | break; |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 1631 | case INQUIRY: |
| 1632 | buflen = scsi_disk_emulate_inquiry(req, outbuf); |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1633 | if (buflen < 0) { |
Gerd Hoffmann | 0b06c05 | 2009-11-26 15:34:04 +0100 | [diff] [blame] | 1634 | goto illegal_request; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1635 | } |
Hannes Reinecke | 5f71d32 | 2011-07-22 16:51:12 +0200 | [diff] [blame] | 1636 | break; |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1637 | case MODE_SENSE: |
| 1638 | case MODE_SENSE_10: |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1639 | buflen = scsi_disk_emulate_mode_sense(r, outbuf); |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1640 | if (buflen < 0) { |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1641 | goto illegal_request; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1642 | } |
Gerd Hoffmann | ebddfcb | 2009-11-26 15:34:06 +0100 | [diff] [blame] | 1643 | break; |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1644 | case READ_TOC: |
| 1645 | buflen = scsi_disk_emulate_read_toc(req, outbuf); |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1646 | if (buflen < 0) { |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1647 | goto illegal_request; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1648 | } |
Gerd Hoffmann | 02880f4 | 2009-11-26 15:34:11 +0100 | [diff] [blame] | 1649 | break; |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1650 | case RESERVE: |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1651 | if (req->cmd.buf[1] & 1) { |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1652 | goto illegal_request; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1653 | } |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1654 | break; |
| 1655 | case RESERVE_10: |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1656 | if (req->cmd.buf[1] & 3) { |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1657 | goto illegal_request; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1658 | } |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1659 | break; |
| 1660 | case RELEASE: |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1661 | if (req->cmd.buf[1] & 1) { |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1662 | goto illegal_request; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1663 | } |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1664 | break; |
| 1665 | case RELEASE_10: |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1666 | if (req->cmd.buf[1] & 3) { |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1667 | goto illegal_request; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1668 | } |
Gerd Hoffmann | 3d53ba1 | 2009-11-26 15:34:05 +0100 | [diff] [blame] | 1669 | break; |
Gerd Hoffmann | 8d3628f | 2009-11-26 15:34:07 +0100 | [diff] [blame] | 1670 | case START_STOP: |
Markus Armbruster | 68bb01f | 2011-09-06 18:58:49 +0200 | [diff] [blame] | 1671 | if (scsi_disk_emulate_start_stop(r) < 0) { |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1672 | return 0; |
Markus Armbruster | 68bb01f | 2011-09-06 18:58:49 +0200 | [diff] [blame] | 1673 | } |
Hannes Reinecke | 5f71d32 | 2011-07-22 16:51:12 +0200 | [diff] [blame] | 1674 | break; |
Gerd Hoffmann | c68b9f3 | 2009-11-26 15:34:08 +0100 | [diff] [blame] | 1675 | case ALLOW_MEDIUM_REMOVAL: |
Markus Armbruster | 81b1008 | 2011-09-06 18:58:44 +0200 | [diff] [blame] | 1676 | s->tray_locked = req->cmd.buf[4] & 1; |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1677 | bdrv_lock_medium(s->qdev.conf.bs, req->cmd.buf[4] & 1); |
Hannes Reinecke | 5f71d32 | 2011-07-22 16:51:12 +0200 | [diff] [blame] | 1678 | break; |
Hannes Reinecke | 5e30a07 | 2011-07-22 16:51:15 +0200 | [diff] [blame] | 1679 | case READ_CAPACITY_10: |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1680 | /* The normal LEN field for this command is zero. */ |
Hannes Reinecke | 5f71d32 | 2011-07-22 16:51:12 +0200 | [diff] [blame] | 1681 | memset(outbuf, 0, 8); |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1682 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1683 | if (!nb_sectors) { |
Paolo Bonzini | 9bcaf4f | 2012-03-16 19:11:43 +0100 | [diff] [blame] | 1684 | scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY)); |
| 1685 | return -1; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1686 | } |
Paolo Bonzini | 7cec78b | 2011-10-03 13:57:06 +0200 | [diff] [blame] | 1687 | if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) { |
| 1688 | goto illegal_request; |
| 1689 | } |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1690 | nb_sectors /= s->qdev.blocksize / 512; |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1691 | /* Returned value is the address of the last sector. */ |
| 1692 | nb_sectors--; |
| 1693 | /* Remember the new size for read/write sanity checking. */ |
Paolo Bonzini | 7877903 | 2011-10-13 10:39:50 +0200 | [diff] [blame] | 1694 | s->qdev.max_lba = nb_sectors; |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1695 | /* Clip to 2TB, instead of returning capacity modulo 2TB. */ |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1696 | if (nb_sectors > UINT32_MAX) { |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1697 | nb_sectors = UINT32_MAX; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1698 | } |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1699 | outbuf[0] = (nb_sectors >> 24) & 0xff; |
| 1700 | outbuf[1] = (nb_sectors >> 16) & 0xff; |
| 1701 | outbuf[2] = (nb_sectors >> 8) & 0xff; |
| 1702 | outbuf[3] = nb_sectors & 0xff; |
| 1703 | outbuf[4] = 0; |
| 1704 | outbuf[5] = 0; |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1705 | outbuf[6] = s->qdev.blocksize >> 8; |
Gerd Hoffmann | e7e25e3 | 2009-11-26 15:34:09 +0100 | [diff] [blame] | 1706 | outbuf[7] = 0; |
Hannes Reinecke | 5f71d32 | 2011-07-22 16:51:12 +0200 | [diff] [blame] | 1707 | break; |
Paolo Bonzini | f3b338e | 2011-11-14 14:31:51 +0100 | [diff] [blame] | 1708 | case REQUEST_SENSE: |
| 1709 | /* Just return "NO SENSE". */ |
| 1710 | buflen = scsi_build_sense(NULL, 0, outbuf, r->buflen, |
| 1711 | (req->cmd.buf[1] & 1) == 0); |
Paolo Bonzini | c8dcb53 | 2012-10-10 12:18:03 +0200 | [diff] [blame] | 1712 | if (buflen < 0) { |
| 1713 | goto illegal_request; |
| 1714 | } |
Paolo Bonzini | f3b338e | 2011-11-14 14:31:51 +0100 | [diff] [blame] | 1715 | break; |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 1716 | case MECHANISM_STATUS: |
| 1717 | buflen = scsi_emulate_mechanism_status(s, outbuf); |
| 1718 | if (buflen < 0) { |
| 1719 | goto illegal_request; |
| 1720 | } |
| 1721 | break; |
Gerd Hoffmann | 3821555 | 2009-11-26 15:34:12 +0100 | [diff] [blame] | 1722 | case GET_CONFIGURATION: |
Paolo Bonzini | 430ee2f | 2011-09-13 15:22:31 +0200 | [diff] [blame] | 1723 | buflen = scsi_get_configuration(s, outbuf); |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 1724 | if (buflen < 0) { |
| 1725 | goto illegal_request; |
| 1726 | } |
| 1727 | break; |
| 1728 | case GET_EVENT_STATUS_NOTIFICATION: |
| 1729 | buflen = scsi_get_event_status_notification(s, r, outbuf); |
| 1730 | if (buflen < 0) { |
| 1731 | goto illegal_request; |
| 1732 | } |
| 1733 | break; |
Paolo Bonzini | 1a4f0c3 | 2012-05-25 12:59:55 +0200 | [diff] [blame] | 1734 | case READ_DISC_INFORMATION: |
| 1735 | buflen = scsi_read_disc_information(s, r, outbuf); |
| 1736 | if (buflen < 0) { |
| 1737 | goto illegal_request; |
| 1738 | } |
| 1739 | break; |
Paolo Bonzini | b6c251a | 2011-09-13 14:33:47 +0200 | [diff] [blame] | 1740 | case READ_DVD_STRUCTURE: |
| 1741 | buflen = scsi_read_dvd_structure(s, r, outbuf); |
| 1742 | if (buflen < 0) { |
| 1743 | goto illegal_request; |
| 1744 | } |
Gerd Hoffmann | 3821555 | 2009-11-26 15:34:12 +0100 | [diff] [blame] | 1745 | break; |
Paolo Bonzini | f651526 | 2011-09-06 12:31:42 +0200 | [diff] [blame] | 1746 | case SERVICE_ACTION_IN_16: |
Gerd Hoffmann | 5dd90e2 | 2009-11-26 15:34:13 +0100 | [diff] [blame] | 1747 | /* Service Action In subcommands. */ |
Paolo Bonzini | f651526 | 2011-09-06 12:31:42 +0200 | [diff] [blame] | 1748 | if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) { |
Gerd Hoffmann | 5dd90e2 | 2009-11-26 15:34:13 +0100 | [diff] [blame] | 1749 | DPRINTF("SAI READ CAPACITY(16)\n"); |
| 1750 | memset(outbuf, 0, req->cmd.xfer); |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1751 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1752 | if (!nb_sectors) { |
Paolo Bonzini | 9bcaf4f | 2012-03-16 19:11:43 +0100 | [diff] [blame] | 1753 | scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY)); |
| 1754 | return -1; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1755 | } |
Paolo Bonzini | 7cec78b | 2011-10-03 13:57:06 +0200 | [diff] [blame] | 1756 | if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) { |
| 1757 | goto illegal_request; |
| 1758 | } |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1759 | nb_sectors /= s->qdev.blocksize / 512; |
Gerd Hoffmann | 5dd90e2 | 2009-11-26 15:34:13 +0100 | [diff] [blame] | 1760 | /* Returned value is the address of the last sector. */ |
| 1761 | nb_sectors--; |
| 1762 | /* Remember the new size for read/write sanity checking. */ |
Paolo Bonzini | 7877903 | 2011-10-13 10:39:50 +0200 | [diff] [blame] | 1763 | s->qdev.max_lba = nb_sectors; |
Gerd Hoffmann | 5dd90e2 | 2009-11-26 15:34:13 +0100 | [diff] [blame] | 1764 | outbuf[0] = (nb_sectors >> 56) & 0xff; |
| 1765 | outbuf[1] = (nb_sectors >> 48) & 0xff; |
| 1766 | outbuf[2] = (nb_sectors >> 40) & 0xff; |
| 1767 | outbuf[3] = (nb_sectors >> 32) & 0xff; |
| 1768 | outbuf[4] = (nb_sectors >> 24) & 0xff; |
| 1769 | outbuf[5] = (nb_sectors >> 16) & 0xff; |
| 1770 | outbuf[6] = (nb_sectors >> 8) & 0xff; |
| 1771 | outbuf[7] = nb_sectors & 0xff; |
| 1772 | outbuf[8] = 0; |
| 1773 | outbuf[9] = 0; |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1774 | outbuf[10] = s->qdev.blocksize >> 8; |
Gerd Hoffmann | 5dd90e2 | 2009-11-26 15:34:13 +0100 | [diff] [blame] | 1775 | outbuf[11] = 0; |
Christoph Hellwig | ee3659e | 2010-02-10 23:37:37 +0100 | [diff] [blame] | 1776 | outbuf[12] = 0; |
| 1777 | outbuf[13] = get_physical_block_exp(&s->qdev.conf); |
Christoph Hellwig | ea3bd56 | 2010-12-16 19:36:43 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* set TPE bit if the format supports discard */ |
| 1780 | if (s->qdev.conf.discard_granularity) { |
| 1781 | outbuf[14] = 0x80; |
| 1782 | } |
| 1783 | |
Gerd Hoffmann | 5dd90e2 | 2009-11-26 15:34:13 +0100 | [diff] [blame] | 1784 | /* Protection, exponent and lowest lba field left blank. */ |
Gerd Hoffmann | 5dd90e2 | 2009-11-26 15:34:13 +0100 | [diff] [blame] | 1785 | break; |
| 1786 | } |
| 1787 | DPRINTF("Unsupported Service Action In\n"); |
| 1788 | goto illegal_request; |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1789 | case SYNCHRONIZE_CACHE: |
| 1790 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 1791 | scsi_req_ref(&r->req); |
| 1792 | bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH); |
| 1793 | r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_aio_complete, r); |
| 1794 | return 0; |
| 1795 | case SEEK_10: |
| 1796 | DPRINTF("Seek(10) (sector %" PRId64 ")\n", r->req.cmd.lba); |
| 1797 | if (r->req.cmd.lba > s->qdev.max_lba) { |
| 1798 | goto illegal_lba; |
| 1799 | } |
| 1800 | break; |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1801 | case MODE_SELECT: |
| 1802 | DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer); |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1803 | break; |
| 1804 | case MODE_SELECT_10: |
| 1805 | DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer); |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1806 | break; |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 1807 | case UNMAP: |
| 1808 | DPRINTF("Unmap (len %lu)\n", (long)r->req.cmd.xfer); |
| 1809 | break; |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1810 | case WRITE_SAME_10: |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1811 | case WRITE_SAME_16: |
Paolo Bonzini | e93176d | 2012-09-05 18:00:57 +0200 | [diff] [blame] | 1812 | nb_sectors = scsi_data_cdb_length(r->req.cmd.buf); |
Ronnie Sahlberg | 6a8a685 | 2012-07-16 08:53:28 +0200 | [diff] [blame] | 1813 | if (bdrv_is_read_only(s->qdev.conf.bs)) { |
| 1814 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
| 1815 | return 0; |
| 1816 | } |
Paolo Bonzini | 444bc90 | 2012-09-05 17:46:18 +0200 | [diff] [blame] | 1817 | if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) { |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1818 | goto illegal_lba; |
| 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | * We only support WRITE SAME with the unmap bit set for now. |
| 1823 | */ |
| 1824 | if (!(req->cmd.buf[1] & 0x8)) { |
| 1825 | goto illegal_request; |
| 1826 | } |
| 1827 | |
| 1828 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 1829 | scsi_req_ref(&r->req); |
| 1830 | r->req.aiocb = bdrv_aio_discard(s->qdev.conf.bs, |
| 1831 | r->req.cmd.lba * (s->qdev.blocksize / 512), |
| 1832 | nb_sectors * (s->qdev.blocksize / 512), |
| 1833 | scsi_aio_complete, r); |
| 1834 | return 0; |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1835 | default: |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1836 | DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]); |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 1837 | scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE)); |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1838 | return 0; |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1839 | } |
Paolo Bonzini | 314a329 | 2012-02-08 10:26:21 +0100 | [diff] [blame] | 1840 | assert(!r->req.aiocb); |
Paolo Bonzini | c8dcb53 | 2012-10-10 12:18:03 +0200 | [diff] [blame] | 1841 | r->iov.iov_len = MIN(r->buflen, req->cmd.xfer); |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1842 | if (r->iov.iov_len == 0) { |
| 1843 | scsi_req_complete(&r->req, GOOD); |
| 1844 | } |
Paolo Bonzini | af6d510 | 2012-02-08 10:32:55 +0100 | [diff] [blame] | 1845 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 1846 | assert(r->iov.iov_len == req->cmd.xfer); |
| 1847 | return -r->iov.iov_len; |
| 1848 | } else { |
| 1849 | return r->iov.iov_len; |
| 1850 | } |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1851 | |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1852 | illegal_request: |
Paolo Bonzini | cfc606d | 2011-09-06 12:31:44 +0200 | [diff] [blame] | 1853 | if (r->req.status == -1) { |
| 1854 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
| 1855 | } |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1856 | return 0; |
Paolo Bonzini | 101aa85 | 2012-02-08 09:43:52 +0100 | [diff] [blame] | 1857 | |
| 1858 | illegal_lba: |
| 1859 | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
| 1860 | return 0; |
Gerd Hoffmann | aa5dbdc | 2009-11-26 15:34:02 +0100 | [diff] [blame] | 1861 | } |
| 1862 | |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1863 | /* Execute a scsi command. Returns the length of the data expected by the |
| 1864 | command. This will be Positive for data transfers from the device |
| 1865 | (eg. disk reads), negative for transfers to the device (eg. disk writes), |
| 1866 | and zero if the command does not transfer any data. */ |
| 1867 | |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1868 | static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf) |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1869 | { |
Hannes Reinecke | 5c6c0e5 | 2011-04-18 12:35:39 +0200 | [diff] [blame] | 1870 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
| 1871 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
Paolo Bonzini | e93176d | 2012-09-05 18:00:57 +0200 | [diff] [blame] | 1872 | uint32_t len; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 1873 | uint8_t command; |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1874 | |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 1875 | command = buf[0]; |
Hannes Reinecke | 2dd791b | 2010-11-24 12:16:00 +0100 | [diff] [blame] | 1876 | |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1877 | if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) { |
| 1878 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
| 1879 | return 0; |
Paolo Bonzini | 9bcaf4f | 2012-03-16 19:11:43 +0100 | [diff] [blame] | 1880 | } |
| 1881 | |
Paolo Bonzini | e93176d | 2012-09-05 18:00:57 +0200 | [diff] [blame] | 1882 | len = scsi_data_cdb_length(r->req.cmd.buf); |
Paolo Bonzini | 9bcaf4f | 2012-03-16 19:11:43 +0100 | [diff] [blame] | 1883 | switch (command) { |
Gerd Hoffmann | ebf4602 | 2009-11-26 15:33:56 +0100 | [diff] [blame] | 1884 | case READ_6: |
| 1885 | case READ_10: |
Gerd Hoffmann | bd536cf | 2009-11-26 15:34:16 +0100 | [diff] [blame] | 1886 | case READ_12: |
| 1887 | case READ_16: |
Paolo Bonzini | e93176d | 2012-09-05 18:00:57 +0200 | [diff] [blame] | 1888 | DPRINTF("Read (sector %" PRId64 ", count %u)\n", r->req.cmd.lba, len); |
Ronnie Sahlberg | 96bdbba | 2012-07-12 10:19:38 +1000 | [diff] [blame] | 1889 | if (r->req.cmd.buf[1] & 0xe0) { |
| 1890 | goto illegal_request; |
| 1891 | } |
Paolo Bonzini | 444bc90 | 2012-09-05 17:46:18 +0200 | [diff] [blame] | 1892 | if (!check_lba_range(s, r->req.cmd.lba, len)) { |
aliguori | 274fb0e | 2009-01-29 19:59:04 +0000 | [diff] [blame] | 1893 | goto illegal_lba; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1894 | } |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1895 | r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512); |
| 1896 | r->sector_count = len * (s->qdev.blocksize / 512); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1897 | break; |
Gerd Hoffmann | ebf4602 | 2009-11-26 15:33:56 +0100 | [diff] [blame] | 1898 | case WRITE_6: |
| 1899 | case WRITE_10: |
Gerd Hoffmann | bd536cf | 2009-11-26 15:34:16 +0100 | [diff] [blame] | 1900 | case WRITE_12: |
| 1901 | case WRITE_16: |
Hannes Reinecke | 5e30a07 | 2011-07-22 16:51:15 +0200 | [diff] [blame] | 1902 | case WRITE_VERIFY_10: |
Bernhard Kohl | ebef0bb | 2010-09-06 11:50:55 +0200 | [diff] [blame] | 1903 | case WRITE_VERIFY_12: |
| 1904 | case WRITE_VERIFY_16: |
Ronnie Sahlberg | 6a8a685 | 2012-07-16 08:53:28 +0200 | [diff] [blame] | 1905 | if (bdrv_is_read_only(s->qdev.conf.bs)) { |
| 1906 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
| 1907 | return 0; |
| 1908 | } |
| 1909 | /* fallthrough */ |
| 1910 | case VERIFY_10: |
| 1911 | case VERIFY_12: |
| 1912 | case VERIFY_16: |
Paolo Bonzini | e93176d | 2012-09-05 18:00:57 +0200 | [diff] [blame] | 1913 | DPRINTF("Write %s(sector %" PRId64 ", count %u)\n", |
Hannes Reinecke | 2dd791b | 2010-11-24 12:16:00 +0100 | [diff] [blame] | 1914 | (command & 0xe) == 0xe ? "And Verify " : "", |
| 1915 | r->req.cmd.lba, len); |
Ronnie Sahlberg | 96bdbba | 2012-07-12 10:19:38 +1000 | [diff] [blame] | 1916 | if (r->req.cmd.buf[1] & 0xe0) { |
| 1917 | goto illegal_request; |
| 1918 | } |
Paolo Bonzini | 444bc90 | 2012-09-05 17:46:18 +0200 | [diff] [blame] | 1919 | if (!check_lba_range(s, r->req.cmd.lba, len)) { |
aliguori | 274fb0e | 2009-01-29 19:59:04 +0000 | [diff] [blame] | 1920 | goto illegal_lba; |
Paolo Bonzini | f01b593 | 2011-10-17 16:39:27 +0200 | [diff] [blame] | 1921 | } |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1922 | r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512); |
| 1923 | r->sector_count = len * (s->qdev.blocksize / 512); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1924 | break; |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1925 | default: |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1926 | abort(); |
Ronnie Sahlberg | 96bdbba | 2012-07-12 10:19:38 +1000 | [diff] [blame] | 1927 | illegal_request: |
| 1928 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
| 1929 | return 0; |
aliguori | 274fb0e | 2009-01-29 19:59:04 +0000 | [diff] [blame] | 1930 | illegal_lba: |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 1931 | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
aliguori | 274fb0e | 2009-01-29 19:59:04 +0000 | [diff] [blame] | 1932 | return 0; |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1933 | } |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1934 | if (r->sector_count == 0) { |
Paolo Bonzini | b45ef67 | 2011-08-03 10:49:07 +0200 | [diff] [blame] | 1935 | scsi_req_complete(&r->req, GOOD); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1936 | } |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1937 | assert(r->iov.iov_len == 0); |
Hannes Reinecke | efb9ee0 | 2011-04-18 12:57:22 +0200 | [diff] [blame] | 1938 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1939 | return -r->sector_count * 512; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 1940 | } else { |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 1941 | return r->sector_count * 512; |
pbrook | a917d38 | 2006-08-29 04:52:16 +0000 | [diff] [blame] | 1942 | } |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Jan Kiszka | e9447f3 | 2010-05-04 14:20:59 +0200 | [diff] [blame] | 1945 | static void scsi_disk_reset(DeviceState *dev) |
| 1946 | { |
| 1947 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev); |
| 1948 | uint64_t nb_sectors; |
| 1949 | |
Paolo Bonzini | c7b4887 | 2011-08-03 10:49:18 +0200 | [diff] [blame] | 1950 | scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET)); |
Jan Kiszka | e9447f3 | 2010-05-04 14:20:59 +0200 | [diff] [blame] | 1951 | |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 1952 | bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors); |
Paolo Bonzini | 6937730 | 2011-10-13 10:35:46 +0200 | [diff] [blame] | 1953 | nb_sectors /= s->qdev.blocksize / 512; |
Jan Kiszka | e9447f3 | 2010-05-04 14:20:59 +0200 | [diff] [blame] | 1954 | if (nb_sectors) { |
| 1955 | nb_sectors--; |
| 1956 | } |
Paolo Bonzini | 7877903 | 2011-10-13 10:39:50 +0200 | [diff] [blame] | 1957 | s->qdev.max_lba = nb_sectors; |
Jan Kiszka | e9447f3 | 2010-05-04 14:20:59 +0200 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | static void scsi_destroy(SCSIDevice *dev) |
| 1961 | { |
| 1962 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
| 1963 | |
Paolo Bonzini | c7b4887 | 2011-08-03 10:49:18 +0200 | [diff] [blame] | 1964 | scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE)); |
Markus Armbruster | f8b6cc0 | 2010-05-05 16:36:52 +0200 | [diff] [blame] | 1965 | blockdev_mark_auto_del(s->qdev.conf.bs); |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 1966 | } |
| 1967 | |
Paolo Bonzini | aaebace | 2012-07-16 14:07:55 +0200 | [diff] [blame] | 1968 | static void scsi_disk_resize_cb(void *opaque) |
| 1969 | { |
| 1970 | SCSIDiskState *s = opaque; |
| 1971 | |
| 1972 | /* SPC lists this sense code as available only for |
| 1973 | * direct-access devices. |
| 1974 | */ |
| 1975 | if (s->qdev.type == TYPE_DISK) { |
Paolo Bonzini | 53200fa | 2012-07-16 14:22:36 +0200 | [diff] [blame] | 1976 | scsi_device_report_change(&s->qdev, SENSE_CODE(CAPACITY_CHANGED)); |
Paolo Bonzini | aaebace | 2012-07-16 14:07:55 +0200 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | |
Markus Armbruster | 7d4b4ba | 2011-09-06 18:58:59 +0200 | [diff] [blame] | 1980 | static void scsi_cd_change_media_cb(void *opaque, bool load) |
Markus Armbruster | 2c6942f | 2011-09-06 18:58:51 +0200 | [diff] [blame] | 1981 | { |
Paolo Bonzini | 8a9c16f | 2011-09-13 16:26:06 +0200 | [diff] [blame] | 1982 | SCSIDiskState *s = opaque; |
| 1983 | |
| 1984 | /* |
| 1985 | * When a CD gets changed, we have to report an ejected state and |
| 1986 | * then a loaded state to guests so that they detect tray |
| 1987 | * open/close and media change events. Guests that do not use |
| 1988 | * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close |
| 1989 | * states rely on this behavior. |
| 1990 | * |
| 1991 | * media_changed governs the state machine used for unit attention |
| 1992 | * report. media_event is used by GET EVENT STATUS NOTIFICATION. |
| 1993 | */ |
| 1994 | s->media_changed = load; |
| 1995 | s->tray_open = !load; |
Paolo Bonzini | e48e84e | 2012-07-16 14:18:58 +0200 | [diff] [blame] | 1996 | scsi_device_set_ua(&s->qdev, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM)); |
Paolo Bonzini | 3c2f7c1 | 2011-09-13 16:26:51 +0200 | [diff] [blame] | 1997 | s->media_event = true; |
Paolo Bonzini | 4480de1 | 2011-10-25 12:53:40 +0200 | [diff] [blame] | 1998 | s->eject_request = false; |
| 1999 | } |
| 2000 | |
| 2001 | static void scsi_cd_eject_request_cb(void *opaque, bool force) |
| 2002 | { |
| 2003 | SCSIDiskState *s = opaque; |
| 2004 | |
| 2005 | s->eject_request = true; |
| 2006 | if (force) { |
| 2007 | s->tray_locked = false; |
| 2008 | } |
Markus Armbruster | 2c6942f | 2011-09-06 18:58:51 +0200 | [diff] [blame] | 2009 | } |
| 2010 | |
Markus Armbruster | e4def80 | 2011-09-06 18:58:53 +0200 | [diff] [blame] | 2011 | static bool scsi_cd_is_tray_open(void *opaque) |
| 2012 | { |
| 2013 | return ((SCSIDiskState *)opaque)->tray_open; |
| 2014 | } |
| 2015 | |
Markus Armbruster | f107639 | 2011-09-06 18:58:46 +0200 | [diff] [blame] | 2016 | static bool scsi_cd_is_medium_locked(void *opaque) |
| 2017 | { |
| 2018 | return ((SCSIDiskState *)opaque)->tray_locked; |
| 2019 | } |
| 2020 | |
Paolo Bonzini | aaebace | 2012-07-16 14:07:55 +0200 | [diff] [blame] | 2021 | static const BlockDevOps scsi_disk_removable_block_ops = { |
Markus Armbruster | 2c6942f | 2011-09-06 18:58:51 +0200 | [diff] [blame] | 2022 | .change_media_cb = scsi_cd_change_media_cb, |
Paolo Bonzini | 4480de1 | 2011-10-25 12:53:40 +0200 | [diff] [blame] | 2023 | .eject_request_cb = scsi_cd_eject_request_cb, |
Markus Armbruster | e4def80 | 2011-09-06 18:58:53 +0200 | [diff] [blame] | 2024 | .is_tray_open = scsi_cd_is_tray_open, |
Markus Armbruster | f107639 | 2011-09-06 18:58:46 +0200 | [diff] [blame] | 2025 | .is_medium_locked = scsi_cd_is_medium_locked, |
Paolo Bonzini | aaebace | 2012-07-16 14:07:55 +0200 | [diff] [blame] | 2026 | |
| 2027 | .resize_cb = scsi_disk_resize_cb, |
| 2028 | }; |
| 2029 | |
| 2030 | static const BlockDevOps scsi_disk_block_ops = { |
| 2031 | .resize_cb = scsi_disk_resize_cb, |
Markus Armbruster | f107639 | 2011-09-06 18:58:46 +0200 | [diff] [blame] | 2032 | }; |
| 2033 | |
Paolo Bonzini | 8a9c16f | 2011-09-13 16:26:06 +0200 | [diff] [blame] | 2034 | static void scsi_disk_unit_attention_reported(SCSIDevice *dev) |
| 2035 | { |
| 2036 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
| 2037 | if (s->media_changed) { |
| 2038 | s->media_changed = false; |
Paolo Bonzini | e48e84e | 2012-07-16 14:18:58 +0200 | [diff] [blame] | 2039 | scsi_device_set_ua(&s->qdev, SENSE_CODE(MEDIUM_CHANGED)); |
Paolo Bonzini | 8a9c16f | 2011-09-13 16:26:06 +0200 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 2043 | static int scsi_initfn(SCSIDevice *dev) |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 2044 | { |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 2045 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 2046 | |
Markus Armbruster | f8b6cc0 | 2010-05-05 16:36:52 +0200 | [diff] [blame] | 2047 | if (!s->qdev.conf.bs) { |
Markus Armbruster | 6a84cb1 | 2011-12-21 11:37:57 +0100 | [diff] [blame] | 2048 | error_report("drive property not set"); |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 2049 | return -1; |
| 2050 | } |
| 2051 | |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 2052 | if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) && |
| 2053 | !bdrv_is_inserted(s->qdev.conf.bs)) { |
Markus Armbruster | 98f28ad | 2010-07-06 14:37:44 +0200 | [diff] [blame] | 2054 | error_report("Device needs media, but drive is empty"); |
| 2055 | return -1; |
| 2056 | } |
| 2057 | |
Markus Armbruster | 911525d | 2012-07-11 15:08:37 +0200 | [diff] [blame] | 2058 | blkconf_serial(&s->qdev.conf, &s->serial); |
Markus Armbruster | b2df431 | 2012-07-12 18:52:31 +0200 | [diff] [blame] | 2059 | if (dev->type == TYPE_DISK |
| 2060 | && blkconf_geometry(&dev->conf, NULL, 65535, 255, 255) < 0) { |
Markus Armbruster | b7eb0c9 | 2012-07-11 15:08:39 +0200 | [diff] [blame] | 2061 | return -1; |
| 2062 | } |
Markus Armbruster | a0fef65 | 2010-06-01 20:32:34 +0200 | [diff] [blame] | 2063 | |
Markus Armbruster | 552fee9 | 2010-06-01 20:32:35 +0200 | [diff] [blame] | 2064 | if (!s->version) { |
Crístian Viana | 93bfef4 | 2012-05-30 00:35:51 -0300 | [diff] [blame] | 2065 | s->version = g_strdup(qemu_get_version()); |
Markus Armbruster | 552fee9 | 2010-06-01 20:32:35 +0200 | [diff] [blame] | 2066 | } |
Dmitry Fleytman | 353815a | 2012-07-06 22:03:35 -0700 | [diff] [blame] | 2067 | if (!s->vendor) { |
| 2068 | s->vendor = g_strdup("QEMU"); |
| 2069 | } |
Markus Armbruster | 552fee9 | 2010-06-01 20:32:35 +0200 | [diff] [blame] | 2070 | |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 2071 | if (bdrv_is_sg(s->qdev.conf.bs)) { |
Markus Armbruster | 6a84cb1 | 2011-12-21 11:37:57 +0100 | [diff] [blame] | 2072 | error_report("unwanted /dev/sg*"); |
Markus Armbruster | 32bb404 | 2010-02-25 11:23:52 +0100 | [diff] [blame] | 2073 | return -1; |
| 2074 | } |
| 2075 | |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 2076 | if (s->features & (1 << SCSI_DISK_F_REMOVABLE)) { |
Paolo Bonzini | aaebace | 2012-07-16 14:07:55 +0200 | [diff] [blame] | 2077 | bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_disk_removable_block_ops, s); |
| 2078 | } else { |
| 2079 | bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_disk_block_ops, s); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 2080 | } |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 2081 | bdrv_set_buffer_alignment(s->qdev.conf.bs, s->qdev.blocksize); |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 2082 | |
Paolo Bonzini | 44740c3 | 2011-10-12 12:54:16 +0200 | [diff] [blame] | 2083 | bdrv_iostatus_enable(s->qdev.conf.bs); |
Paolo Bonzini | 7082826 | 2011-11-15 17:36:38 +0100 | [diff] [blame] | 2084 | add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, NULL); |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 2085 | return 0; |
ths | 8ccc2ac | 2007-12-10 02:58:34 +0000 | [diff] [blame] | 2086 | } |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 2087 | |
Markus Armbruster | b443ae6 | 2011-05-16 15:04:53 +0200 | [diff] [blame] | 2088 | static int scsi_hd_initfn(SCSIDevice *dev) |
| 2089 | { |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 2090 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
| 2091 | s->qdev.blocksize = s->qdev.conf.logical_block_size; |
| 2092 | s->qdev.type = TYPE_DISK; |
Dmitry Fleytman | 353815a | 2012-07-06 22:03:35 -0700 | [diff] [blame] | 2093 | if (!s->product) { |
| 2094 | s->product = g_strdup("QEMU HARDDISK"); |
| 2095 | } |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 2096 | return scsi_initfn(&s->qdev); |
Markus Armbruster | b443ae6 | 2011-05-16 15:04:53 +0200 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | static int scsi_cd_initfn(SCSIDevice *dev) |
| 2100 | { |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 2101 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
| 2102 | s->qdev.blocksize = 2048; |
| 2103 | s->qdev.type = TYPE_ROM; |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 2104 | s->features |= 1 << SCSI_DISK_F_REMOVABLE; |
Dmitry Fleytman | 353815a | 2012-07-06 22:03:35 -0700 | [diff] [blame] | 2105 | if (!s->product) { |
| 2106 | s->product = g_strdup("QEMU CD-ROM"); |
| 2107 | } |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 2108 | return scsi_initfn(&s->qdev); |
Markus Armbruster | b443ae6 | 2011-05-16 15:04:53 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | static int scsi_disk_initfn(SCSIDevice *dev) |
| 2112 | { |
Markus Armbruster | 95b5edc | 2011-05-16 15:04:56 +0200 | [diff] [blame] | 2113 | DriveInfo *dinfo; |
Markus Armbruster | b443ae6 | 2011-05-16 15:04:53 +0200 | [diff] [blame] | 2114 | |
| 2115 | if (!dev->conf.bs) { |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 2116 | return scsi_initfn(dev); /* ... and die there */ |
Markus Armbruster | b443ae6 | 2011-05-16 15:04:53 +0200 | [diff] [blame] | 2117 | } |
| 2118 | |
Paolo Bonzini | e39be48 | 2011-10-12 14:27:40 +0200 | [diff] [blame] | 2119 | dinfo = drive_get_by_blockdev(dev->conf.bs); |
| 2120 | if (dinfo->media_cd) { |
| 2121 | return scsi_cd_initfn(dev); |
| 2122 | } else { |
| 2123 | return scsi_hd_initfn(dev); |
| 2124 | } |
Markus Armbruster | b443ae6 | 2011-05-16 15:04:53 +0200 | [diff] [blame] | 2125 | } |
| 2126 | |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2127 | static const SCSIReqOps scsi_disk_emulate_reqops = { |
Paolo Bonzini | 8dbd457 | 2011-08-03 10:49:08 +0200 | [diff] [blame] | 2128 | .size = sizeof(SCSIDiskReq), |
Paolo Bonzini | 12010e7 | 2011-08-03 10:49:09 +0200 | [diff] [blame] | 2129 | .free_req = scsi_free_request, |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2130 | .send_command = scsi_disk_emulate_command, |
Paolo Bonzini | 314a329 | 2012-02-08 10:26:21 +0100 | [diff] [blame] | 2131 | .read_data = scsi_disk_emulate_read_data, |
| 2132 | .write_data = scsi_disk_emulate_write_data, |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2133 | .get_buf = scsi_get_buf, |
| 2134 | }; |
| 2135 | |
| 2136 | static const SCSIReqOps scsi_disk_dma_reqops = { |
| 2137 | .size = sizeof(SCSIDiskReq), |
| 2138 | .free_req = scsi_free_request, |
| 2139 | .send_command = scsi_disk_dma_command, |
Paolo Bonzini | 12010e7 | 2011-08-03 10:49:09 +0200 | [diff] [blame] | 2140 | .read_data = scsi_read_data, |
| 2141 | .write_data = scsi_write_data, |
| 2142 | .cancel_io = scsi_cancel_io, |
| 2143 | .get_buf = scsi_get_buf, |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 2144 | .load_request = scsi_disk_load_request, |
| 2145 | .save_request = scsi_disk_save_request, |
Paolo Bonzini | 8dbd457 | 2011-08-03 10:49:08 +0200 | [diff] [blame] | 2146 | }; |
| 2147 | |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2148 | static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = { |
| 2149 | [TEST_UNIT_READY] = &scsi_disk_emulate_reqops, |
| 2150 | [INQUIRY] = &scsi_disk_emulate_reqops, |
| 2151 | [MODE_SENSE] = &scsi_disk_emulate_reqops, |
| 2152 | [MODE_SENSE_10] = &scsi_disk_emulate_reqops, |
| 2153 | [START_STOP] = &scsi_disk_emulate_reqops, |
| 2154 | [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops, |
| 2155 | [READ_CAPACITY_10] = &scsi_disk_emulate_reqops, |
| 2156 | [READ_TOC] = &scsi_disk_emulate_reqops, |
| 2157 | [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops, |
| 2158 | [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops, |
| 2159 | [GET_CONFIGURATION] = &scsi_disk_emulate_reqops, |
| 2160 | [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops, |
| 2161 | [MECHANISM_STATUS] = &scsi_disk_emulate_reqops, |
| 2162 | [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops, |
| 2163 | [REQUEST_SENSE] = &scsi_disk_emulate_reqops, |
| 2164 | [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops, |
| 2165 | [SEEK_10] = &scsi_disk_emulate_reqops, |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2166 | [MODE_SELECT] = &scsi_disk_emulate_reqops, |
| 2167 | [MODE_SELECT_10] = &scsi_disk_emulate_reqops, |
Paolo Bonzini | 5222aaf | 2012-08-09 11:00:22 +0200 | [diff] [blame] | 2168 | [UNMAP] = &scsi_disk_emulate_reqops, |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2169 | [WRITE_SAME_10] = &scsi_disk_emulate_reqops, |
| 2170 | [WRITE_SAME_16] = &scsi_disk_emulate_reqops, |
| 2171 | |
| 2172 | [READ_6] = &scsi_disk_dma_reqops, |
| 2173 | [READ_10] = &scsi_disk_dma_reqops, |
| 2174 | [READ_12] = &scsi_disk_dma_reqops, |
| 2175 | [READ_16] = &scsi_disk_dma_reqops, |
| 2176 | [VERIFY_10] = &scsi_disk_dma_reqops, |
| 2177 | [VERIFY_12] = &scsi_disk_dma_reqops, |
| 2178 | [VERIFY_16] = &scsi_disk_dma_reqops, |
| 2179 | [WRITE_6] = &scsi_disk_dma_reqops, |
| 2180 | [WRITE_10] = &scsi_disk_dma_reqops, |
| 2181 | [WRITE_12] = &scsi_disk_dma_reqops, |
| 2182 | [WRITE_16] = &scsi_disk_dma_reqops, |
| 2183 | [WRITE_VERIFY_10] = &scsi_disk_dma_reqops, |
| 2184 | [WRITE_VERIFY_12] = &scsi_disk_dma_reqops, |
| 2185 | [WRITE_VERIFY_16] = &scsi_disk_dma_reqops, |
| 2186 | }; |
| 2187 | |
Paolo Bonzini | 63db0f0 | 2011-10-12 12:58:31 +0200 | [diff] [blame] | 2188 | static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, |
| 2189 | uint8_t *buf, void *hba_private) |
Paolo Bonzini | 8dbd457 | 2011-08-03 10:49:08 +0200 | [diff] [blame] | 2190 | { |
| 2191 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
| 2192 | SCSIRequest *req; |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2193 | const SCSIReqOps *ops; |
| 2194 | uint8_t command; |
Paolo Bonzini | 8dbd457 | 2011-08-03 10:49:08 +0200 | [diff] [blame] | 2195 | |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2196 | command = buf[0]; |
| 2197 | ops = scsi_disk_reqops_dispatch[command]; |
| 2198 | if (!ops) { |
| 2199 | ops = &scsi_disk_emulate_reqops; |
| 2200 | } |
| 2201 | req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private); |
Paolo Bonzini | 79fb50b | 2012-07-31 16:10:23 +0200 | [diff] [blame] | 2202 | |
| 2203 | #ifdef DEBUG_SCSI |
| 2204 | DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]); |
| 2205 | { |
| 2206 | int i; |
| 2207 | for (i = 1; i < req->cmd.len; i++) { |
| 2208 | printf(" 0x%02x", buf[i]); |
| 2209 | } |
| 2210 | printf("\n"); |
| 2211 | } |
| 2212 | #endif |
| 2213 | |
Paolo Bonzini | 8dbd457 | 2011-08-03 10:49:08 +0200 | [diff] [blame] | 2214 | return req; |
| 2215 | } |
| 2216 | |
Paolo Bonzini | 336a691 | 2011-10-25 12:53:37 +0200 | [diff] [blame] | 2217 | #ifdef __linux__ |
| 2218 | static int get_device_type(SCSIDiskState *s) |
| 2219 | { |
| 2220 | BlockDriverState *bdrv = s->qdev.conf.bs; |
| 2221 | uint8_t cmd[16]; |
| 2222 | uint8_t buf[36]; |
| 2223 | uint8_t sensebuf[8]; |
| 2224 | sg_io_hdr_t io_header; |
| 2225 | int ret; |
| 2226 | |
| 2227 | memset(cmd, 0, sizeof(cmd)); |
| 2228 | memset(buf, 0, sizeof(buf)); |
| 2229 | cmd[0] = INQUIRY; |
| 2230 | cmd[4] = sizeof(buf); |
| 2231 | |
| 2232 | memset(&io_header, 0, sizeof(io_header)); |
| 2233 | io_header.interface_id = 'S'; |
| 2234 | io_header.dxfer_direction = SG_DXFER_FROM_DEV; |
| 2235 | io_header.dxfer_len = sizeof(buf); |
| 2236 | io_header.dxferp = buf; |
| 2237 | io_header.cmdp = cmd; |
| 2238 | io_header.cmd_len = sizeof(cmd); |
| 2239 | io_header.mx_sb_len = sizeof(sensebuf); |
| 2240 | io_header.sbp = sensebuf; |
| 2241 | io_header.timeout = 6000; /* XXX */ |
| 2242 | |
| 2243 | ret = bdrv_ioctl(bdrv, SG_IO, &io_header); |
| 2244 | if (ret < 0 || io_header.driver_status || io_header.host_status) { |
| 2245 | return -1; |
| 2246 | } |
| 2247 | s->qdev.type = buf[0]; |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 2248 | if (buf[1] & 0x80) { |
| 2249 | s->features |= 1 << SCSI_DISK_F_REMOVABLE; |
| 2250 | } |
Paolo Bonzini | 336a691 | 2011-10-25 12:53:37 +0200 | [diff] [blame] | 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | static int scsi_block_initfn(SCSIDevice *dev) |
| 2255 | { |
| 2256 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
| 2257 | int sg_version; |
| 2258 | int rc; |
| 2259 | |
| 2260 | if (!s->qdev.conf.bs) { |
| 2261 | error_report("scsi-block: drive property not set"); |
| 2262 | return -1; |
| 2263 | } |
| 2264 | |
| 2265 | /* check we are using a driver managing SG_IO (version 3 and after) */ |
| 2266 | if (bdrv_ioctl(s->qdev.conf.bs, SG_GET_VERSION_NUM, &sg_version) < 0 || |
| 2267 | sg_version < 30000) { |
| 2268 | error_report("scsi-block: scsi generic interface too old"); |
| 2269 | return -1; |
| 2270 | } |
| 2271 | |
| 2272 | /* get device type from INQUIRY data */ |
| 2273 | rc = get_device_type(s); |
| 2274 | if (rc < 0) { |
| 2275 | error_report("scsi-block: INQUIRY failed"); |
| 2276 | return -1; |
| 2277 | } |
| 2278 | |
| 2279 | /* Make a guess for the block size, we'll fix it when the guest sends. |
| 2280 | * READ CAPACITY. If they don't, they likely would assume these sizes |
| 2281 | * anyway. (TODO: check in /sys). |
| 2282 | */ |
| 2283 | if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) { |
| 2284 | s->qdev.blocksize = 2048; |
| 2285 | } else { |
| 2286 | s->qdev.blocksize = 512; |
| 2287 | } |
| 2288 | return scsi_initfn(&s->qdev); |
| 2289 | } |
| 2290 | |
| 2291 | static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag, |
| 2292 | uint32_t lun, uint8_t *buf, |
| 2293 | void *hba_private) |
| 2294 | { |
| 2295 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
| 2296 | |
| 2297 | switch (buf[0]) { |
| 2298 | case READ_6: |
| 2299 | case READ_10: |
| 2300 | case READ_12: |
| 2301 | case READ_16: |
Paolo Bonzini | 7f64f8e | 2012-04-19 11:46:13 +0200 | [diff] [blame] | 2302 | case VERIFY_10: |
| 2303 | case VERIFY_12: |
| 2304 | case VERIFY_16: |
Paolo Bonzini | 336a691 | 2011-10-25 12:53:37 +0200 | [diff] [blame] | 2305 | case WRITE_6: |
| 2306 | case WRITE_10: |
| 2307 | case WRITE_12: |
| 2308 | case WRITE_16: |
| 2309 | case WRITE_VERIFY_10: |
| 2310 | case WRITE_VERIFY_12: |
| 2311 | case WRITE_VERIFY_16: |
Paolo Bonzini | eaccf49 | 2012-02-15 14:11:22 +0100 | [diff] [blame] | 2312 | /* If we are not using O_DIRECT, we might read stale data from the |
| 2313 | * host cache if writes were made using other commands than these |
| 2314 | * ones (such as WRITE SAME or EXTENDED COPY, etc.). So, without |
| 2315 | * O_DIRECT everything must go through SG_IO. |
| 2316 | */ |
Markus Armbruster | 137745c | 2012-06-05 16:49:25 +0200 | [diff] [blame] | 2317 | if (bdrv_get_flags(s->qdev.conf.bs) & BDRV_O_NOCACHE) { |
Paolo Bonzini | eaccf49 | 2012-02-15 14:11:22 +0100 | [diff] [blame] | 2318 | break; |
| 2319 | } |
| 2320 | |
Paolo Bonzini | 33ebad1 | 2011-11-14 14:31:52 +0100 | [diff] [blame] | 2321 | /* MMC writing cannot be done via pread/pwrite, because it sometimes |
| 2322 | * involves writing beyond the maximum LBA or to negative LBA (lead-in). |
| 2323 | * And once you do these writes, reading from the block device is |
| 2324 | * unreliable, too. It is even possible that reads deliver random data |
| 2325 | * from the host page cache (this is probably a Linux bug). |
| 2326 | * |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2327 | * We might use scsi_disk_dma_reqops as long as no writing commands are |
Paolo Bonzini | 33ebad1 | 2011-11-14 14:31:52 +0100 | [diff] [blame] | 2328 | * seen, but performance usually isn't paramount on optical media. So, |
| 2329 | * just make scsi-block operate the same as scsi-generic for them. |
| 2330 | */ |
Paolo Bonzini | b08d0ea | 2012-02-08 09:53:42 +0100 | [diff] [blame] | 2331 | if (s->qdev.type != TYPE_ROM) { |
| 2332 | return scsi_req_alloc(&scsi_disk_dma_reqops, &s->qdev, tag, lun, |
| 2333 | hba_private); |
| 2334 | } |
Paolo Bonzini | 336a691 | 2011-10-25 12:53:37 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun, |
| 2338 | hba_private); |
| 2339 | } |
| 2340 | #endif |
| 2341 | |
Dmitry Fleytman | 353815a | 2012-07-06 22:03:35 -0700 | [diff] [blame] | 2342 | #define DEFINE_SCSI_DISK_PROPERTIES() \ |
| 2343 | DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \ |
| 2344 | DEFINE_PROP_STRING("ver", SCSIDiskState, version), \ |
| 2345 | DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \ |
| 2346 | DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \ |
| 2347 | DEFINE_PROP_STRING("product", SCSIDiskState, product) |
Markus Armbruster | b443ae6 | 2011-05-16 15:04:53 +0200 | [diff] [blame] | 2348 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2349 | static Property scsi_hd_properties[] = { |
| 2350 | DEFINE_SCSI_DISK_PROPERTIES(), |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 2351 | DEFINE_PROP_BIT("removable", SCSIDiskState, features, |
| 2352 | SCSI_DISK_F_REMOVABLE, false), |
Paolo Bonzini | da8365d | 2012-05-01 10:25:16 +0200 | [diff] [blame] | 2353 | DEFINE_PROP_BIT("dpofua", SCSIDiskState, features, |
| 2354 | SCSI_DISK_F_DPOFUA, false), |
Paolo Bonzini | 27395ad | 2012-05-15 12:46:09 +0200 | [diff] [blame] | 2355 | DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0), |
Markus Armbruster | d252df4 | 2012-07-10 11:12:42 +0200 | [diff] [blame] | 2356 | DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2357 | DEFINE_PROP_END_OF_LIST(), |
| 2358 | }; |
| 2359 | |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 2360 | static const VMStateDescription vmstate_scsi_disk_state = { |
| 2361 | .name = "scsi-disk", |
| 2362 | .version_id = 1, |
| 2363 | .minimum_version_id = 1, |
| 2364 | .minimum_version_id_old = 1, |
| 2365 | .fields = (VMStateField[]) { |
| 2366 | VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState), |
| 2367 | VMSTATE_BOOL(media_changed, SCSIDiskState), |
| 2368 | VMSTATE_BOOL(media_event, SCSIDiskState), |
| 2369 | VMSTATE_BOOL(eject_request, SCSIDiskState), |
| 2370 | VMSTATE_BOOL(tray_open, SCSIDiskState), |
| 2371 | VMSTATE_BOOL(tray_locked, SCSIDiskState), |
| 2372 | VMSTATE_END_OF_LIST() |
| 2373 | } |
| 2374 | }; |
| 2375 | |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2376 | static void scsi_hd_class_initfn(ObjectClass *klass, void *data) |
| 2377 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2378 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2379 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
| 2380 | |
| 2381 | sc->init = scsi_hd_initfn; |
| 2382 | sc->destroy = scsi_destroy; |
| 2383 | sc->alloc_req = scsi_new_request; |
| 2384 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2385 | dc->fw_name = "disk"; |
| 2386 | dc->desc = "virtual SCSI disk"; |
| 2387 | dc->reset = scsi_disk_reset; |
| 2388 | dc->props = scsi_hd_properties; |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 2389 | dc->vmsd = &vmstate_scsi_disk_state; |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2390 | } |
| 2391 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2392 | static TypeInfo scsi_hd_info = { |
| 2393 | .name = "scsi-hd", |
| 2394 | .parent = TYPE_SCSI_DEVICE, |
| 2395 | .instance_size = sizeof(SCSIDiskState), |
| 2396 | .class_init = scsi_hd_class_initfn, |
| 2397 | }; |
| 2398 | |
| 2399 | static Property scsi_cd_properties[] = { |
| 2400 | DEFINE_SCSI_DISK_PROPERTIES(), |
Paolo Bonzini | 27395ad | 2012-05-15 12:46:09 +0200 | [diff] [blame] | 2401 | DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2402 | DEFINE_PROP_END_OF_LIST(), |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2403 | }; |
| 2404 | |
| 2405 | static void scsi_cd_class_initfn(ObjectClass *klass, void *data) |
| 2406 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2407 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2408 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
| 2409 | |
| 2410 | sc->init = scsi_cd_initfn; |
| 2411 | sc->destroy = scsi_destroy; |
| 2412 | sc->alloc_req = scsi_new_request; |
| 2413 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2414 | dc->fw_name = "disk"; |
| 2415 | dc->desc = "virtual SCSI CD-ROM"; |
| 2416 | dc->reset = scsi_disk_reset; |
| 2417 | dc->props = scsi_cd_properties; |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 2418 | dc->vmsd = &vmstate_scsi_disk_state; |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2419 | } |
| 2420 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2421 | static TypeInfo scsi_cd_info = { |
| 2422 | .name = "scsi-cd", |
| 2423 | .parent = TYPE_SCSI_DEVICE, |
| 2424 | .instance_size = sizeof(SCSIDiskState), |
| 2425 | .class_init = scsi_cd_class_initfn, |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2426 | }; |
| 2427 | |
Paolo Bonzini | 336a691 | 2011-10-25 12:53:37 +0200 | [diff] [blame] | 2428 | #ifdef __linux__ |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2429 | static Property scsi_block_properties[] = { |
Paolo Bonzini | 0384783 | 2012-07-09 12:11:04 +0200 | [diff] [blame] | 2430 | DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.bs), |
Paolo Bonzini | 0f1da44 | 2012-08-28 12:46:18 +0200 | [diff] [blame] | 2431 | DEFINE_PROP_INT32("bootindex", SCSIDiskState, qdev.conf.bootindex, -1), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2432 | DEFINE_PROP_END_OF_LIST(), |
| 2433 | }; |
| 2434 | |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2435 | static void scsi_block_class_initfn(ObjectClass *klass, void *data) |
| 2436 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2437 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2438 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
| 2439 | |
| 2440 | sc->init = scsi_block_initfn; |
| 2441 | sc->destroy = scsi_destroy; |
| 2442 | sc->alloc_req = scsi_block_new_request; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2443 | dc->fw_name = "disk"; |
| 2444 | dc->desc = "SCSI block device passthrough"; |
| 2445 | dc->reset = scsi_disk_reset; |
| 2446 | dc->props = scsi_block_properties; |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 2447 | dc->vmsd = &vmstate_scsi_disk_state; |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2448 | } |
| 2449 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2450 | static TypeInfo scsi_block_info = { |
| 2451 | .name = "scsi-block", |
| 2452 | .parent = TYPE_SCSI_DEVICE, |
| 2453 | .instance_size = sizeof(SCSIDiskState), |
| 2454 | .class_init = scsi_block_class_initfn, |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2455 | }; |
Paolo Bonzini | 336a691 | 2011-10-25 12:53:37 +0200 | [diff] [blame] | 2456 | #endif |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2457 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2458 | static Property scsi_disk_properties[] = { |
| 2459 | DEFINE_SCSI_DISK_PROPERTIES(), |
Paolo Bonzini | bfe3d7a | 2012-05-01 10:23:54 +0200 | [diff] [blame] | 2460 | DEFINE_PROP_BIT("removable", SCSIDiskState, features, |
| 2461 | SCSI_DISK_F_REMOVABLE, false), |
Paolo Bonzini | da8365d | 2012-05-01 10:25:16 +0200 | [diff] [blame] | 2462 | DEFINE_PROP_BIT("dpofua", SCSIDiskState, features, |
| 2463 | SCSI_DISK_F_DPOFUA, false), |
Paolo Bonzini | 27395ad | 2012-05-15 12:46:09 +0200 | [diff] [blame] | 2464 | DEFINE_PROP_HEX64("wwn", SCSIDiskState, wwn, 0), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2465 | DEFINE_PROP_END_OF_LIST(), |
| 2466 | }; |
| 2467 | |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2468 | static void scsi_disk_class_initfn(ObjectClass *klass, void *data) |
| 2469 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2470 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2471 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
| 2472 | |
| 2473 | sc->init = scsi_disk_initfn; |
| 2474 | sc->destroy = scsi_destroy; |
| 2475 | sc->alloc_req = scsi_new_request; |
| 2476 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2477 | dc->fw_name = "disk"; |
| 2478 | dc->desc = "virtual SCSI disk or CD-ROM (legacy)"; |
| 2479 | dc->reset = scsi_disk_reset; |
| 2480 | dc->props = scsi_disk_properties; |
Paolo Bonzini | 43b978b | 2011-12-15 07:24:47 -0500 | [diff] [blame] | 2481 | dc->vmsd = &vmstate_scsi_disk_state; |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2482 | } |
| 2483 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2484 | static TypeInfo scsi_disk_info = { |
| 2485 | .name = "scsi-disk", |
| 2486 | .parent = TYPE_SCSI_DEVICE, |
| 2487 | .instance_size = sizeof(SCSIDiskState), |
| 2488 | .class_init = scsi_disk_class_initfn, |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 2489 | }; |
| 2490 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 2491 | static void scsi_disk_register_types(void) |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 2492 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2493 | type_register_static(&scsi_hd_info); |
| 2494 | type_register_static(&scsi_cd_info); |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2495 | #ifdef __linux__ |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2496 | type_register_static(&scsi_block_info); |
Anthony Liguori | b9eea3e | 2011-12-15 14:50:08 -0600 | [diff] [blame] | 2497 | #endif |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 2498 | type_register_static(&scsi_disk_info); |
Gerd Hoffmann | d52affa | 2009-08-31 14:24:04 +0200 | [diff] [blame] | 2499 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 2500 | |
| 2501 | type_init(scsi_disk_register_types) |