]> git.proxmox.com Git - qemu.git/blame - hw/scsi-disk.c
block: New change_media_cb() parameter load
[qemu.git] / hw / scsi-disk.c
CommitLineData
2e5d83bb
PB
1/*
2 * SCSI Device emulation
3 *
4 * Copyright (c) 2006 CodeSourcery.
5 * Based on code by Fabrice Bellard
6 *
7 * Written by Paul Brook
ad3cea42
AT
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.
2e5d83bb 14 *
8e31bf38 15 * This code is licensed under the LGPL.
a917d384
PB
16 *
17 * Note that this file only handles the SCSI architecture model and device
1d4db89c
AZ
18 * commands. Emulation of interface/link layer protocols is handled by
19 * the host adapter emulator.
2e5d83bb
PB
20 */
21
22//#define DEBUG_SCSI
23
24#ifdef DEBUG_SCSI
001faf32
BS
25#define DPRINTF(fmt, ...) \
26do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
2e5d83bb 27#else
001faf32 28#define DPRINTF(fmt, ...) do {} while(0)
2e5d83bb
PB
29#endif
30
001faf32
BS
31#define BADF(fmt, ...) \
32do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
2e5d83bb 33
87ecb68b 34#include "qemu-common.h"
2f792016 35#include "qemu-error.h"
43b443b6 36#include "scsi.h"
0d65e1f8 37#include "scsi-defs.h"
666daa68 38#include "sysemu.h"
2446333c 39#include "blockdev.h"
d1a0739d 40#include "block_int.h"
22864256 41
f0f72ffe 42#define SCSI_DMA_BUF_SIZE 131072
57575058 43#define SCSI_MAX_INQUIRY_LEN 256
a917d384 44
5dba48a8
KW
45#define SCSI_REQ_STATUS_RETRY 0x01
46#define SCSI_REQ_STATUS_RETRY_TYPE_MASK 0x06
47#define SCSI_REQ_STATUS_RETRY_READ 0x00
48#define SCSI_REQ_STATUS_RETRY_WRITE 0x02
78ced65e 49#define SCSI_REQ_STATUS_RETRY_FLUSH 0x04
ea8a5d7f 50
d52affa7
GH
51typedef struct SCSIDiskState SCSIDiskState;
52
4c41d2ef
GH
53typedef struct SCSIDiskReq {
54 SCSIRequest req;
a917d384 55 /* Both sector and sector_count are in terms of qemu 512 byte blocks. */
e035b43d
AL
56 uint64_t sector;
57 uint32_t sector_count;
c87c0672
AL
58 struct iovec iov;
59 QEMUIOVector qiov;
ea8a5d7f 60 uint32_t status;
a597e79c 61 BlockAcctCookie acct;
4c41d2ef 62} SCSIDiskReq;
a917d384 63
d52affa7 64struct SCSIDiskState
a917d384 65{
d52affa7 66 SCSIDevice qdev;
428c149b 67 BlockDriverState *bs;
a917d384
PB
68 /* The qemu block layer uses a fixed 512 byte sector size.
69 This is the number of 512 byte blocks in a single scsi sector. */
70 int cluster_size;
419e691f 71 uint32_t removable;
274fb0e1 72 uint64_t max_lba;
213189ab 73 QEMUBH *bh;
383b4d9b 74 char *version;
a0fef654 75 char *serial;
ece0d5e9 76 bool tray_open;
81b1008d 77 bool tray_locked;
2e5d83bb
PB
78};
79
5dba48a8 80static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
78ced65e 81static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf);
5dba48a8 82
ad2d30f7 83static void scsi_free_request(SCSIRequest *req)
4d611c9a 84{
ad2d30f7
PB
85 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
86
f8a83245 87 qemu_vfree(r->iov.iov_base);
4d611c9a
PB
88}
89
b45ef674
PB
90/* Helper function for command completion with sense. */
91static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
ed3a34a3 92{
02fa69b6
BS
93 DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
94 r->req.tag, sense.key, sense.asc, sense.ascq);
b45ef674
PB
95 scsi_req_build_sense(&r->req, sense);
96 scsi_req_complete(&r->req, CHECK_CONDITION);
4d611c9a
PB
97}
98
99/* Cancel a pending data transfer. */
5c6c0e51 100static void scsi_cancel_io(SCSIRequest *req)
4d611c9a 101{
5c6c0e51
HR
102 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
103
104 DPRINTF("Cancel tag=0x%x\n", req->tag);
105 if (r->req.aiocb) {
106 bdrv_aio_cancel(r->req.aiocb);
a917d384 107 }
5c6c0e51 108 r->req.aiocb = NULL;
a917d384
PB
109}
110
111static void scsi_read_complete(void * opaque, int ret)
112{
4c41d2ef 113 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
a597e79c 114 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
5dba48a8 115 int n;
a917d384 116
8e321cc6
PB
117 if (r->req.aiocb != NULL) {
118 r->req.aiocb = NULL;
119 bdrv_acct_done(s->bs, &r->acct);
120 }
a597e79c 121
a917d384 122 if (ret) {
5dba48a8
KW
123 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_READ)) {
124 return;
125 }
4d611c9a 126 }
5dba48a8 127
aa2b1e89 128 DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->iov.iov_len);
a917d384 129
5dba48a8
KW
130 n = r->iov.iov_len / 512;
131 r->sector += n;
132 r->sector_count -= n;
ab9adc88 133 scsi_req_data(&r->req, r->iov.iov_len);
4d611c9a
PB
134}
135
0a4ac106
PB
136static void scsi_flush_complete(void * opaque, int ret)
137{
138 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
139 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
140
141 if (r->req.aiocb != NULL) {
142 r->req.aiocb = NULL;
143 bdrv_acct_done(s->bs, &r->acct);
144 }
145
146 if (ret < 0) {
147 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_FLUSH)) {
148 return;
149 }
150 }
151
152 scsi_req_complete(&r->req, GOOD);
153}
5dba48a8 154
5c6c0e51
HR
155/* Read more data from scsi device into buffer. */
156static void scsi_read_data(SCSIRequest *req)
2e5d83bb 157{
5c6c0e51 158 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
5dba48a8 159 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
2e5d83bb
PB
160 uint32_t n;
161
a917d384 162 if (r->sector_count == (uint32_t)-1) {
aa2b1e89 163 DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
a917d384 164 r->sector_count = 0;
ab9adc88 165 scsi_req_data(&r->req, r->iov.iov_len);
a917d384 166 return;
2e5d83bb 167 }
a917d384
PB
168 DPRINTF("Read sector_count=%d\n", r->sector_count);
169 if (r->sector_count == 0) {
b45ef674
PB
170 /* This also clears the sense buffer for REQUEST SENSE. */
171 scsi_req_complete(&r->req, GOOD);
a917d384 172 return;
2e5d83bb
PB
173 }
174
6fa2c95f
SH
175 /* No data transfer may already be in progress */
176 assert(r->req.aiocb == NULL);
177
efb9ee02
HR
178 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
179 DPRINTF("Data transfer direction invalid\n");
180 scsi_read_complete(r, -EINVAL);
181 return;
182 }
183
a917d384
PB
184 n = r->sector_count;
185 if (n > SCSI_DMA_BUF_SIZE / 512)
186 n = SCSI_DMA_BUF_SIZE / 512;
187
a1aff5bf
MA
188 if (s->tray_open) {
189 scsi_read_complete(r, -ENOMEDIUM);
190 }
c87c0672
AL
191 r->iov.iov_len = n * 512;
192 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
a597e79c
CH
193
194 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
428c149b 195 r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
c87c0672 196 scsi_read_complete, r);
d33ea50a
KW
197 if (r->req.aiocb == NULL) {
198 scsi_read_complete(r, -EIO);
199 }
2e5d83bb
PB
200}
201
5dba48a8
KW
202static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
203{
204 int is_read = (type == SCSI_REQ_STATUS_RETRY_READ);
4c41d2ef 205 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
5dba48a8 206 BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
ea8a5d7f 207
380f640f 208 if (action == BLOCK_ERR_IGNORE) {
5dba48a8 209 bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
ea8a5d7f 210 return 0;
380f640f 211 }
ea8a5d7f
AL
212
213 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
214 || action == BLOCK_ERR_STOP_ANY) {
5dba48a8
KW
215
216 type &= SCSI_REQ_STATUS_RETRY_TYPE_MASK;
217 r->status |= SCSI_REQ_STATUS_RETRY | type;
218
219 bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
e07bbac5 220 vm_stop(VMSTOP_DISKFULL);
ea8a5d7f 221 } else {
efb9ee02
HR
222 switch (error) {
223 case ENOMEM:
b45ef674 224 scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
efb9ee02
HR
225 break;
226 case EINVAL:
b45ef674 227 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
efb9ee02
HR
228 break;
229 default:
b45ef674 230 scsi_check_condition(r, SENSE_CODE(IO_ERROR));
efb9ee02 231 break;
a1f0cce2 232 }
5dba48a8 233 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
ea8a5d7f 234 }
ea8a5d7f
AL
235 return 1;
236}
237
4d611c9a
PB
238static void scsi_write_complete(void * opaque, int ret)
239{
4c41d2ef 240 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
a597e79c 241 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
a917d384 242 uint32_t len;
ea8a5d7f
AL
243 uint32_t n;
244
8e321cc6
PB
245 if (r->req.aiocb != NULL) {
246 r->req.aiocb = NULL;
247 bdrv_acct_done(s->bs, &r->acct);
248 }
a597e79c 249
4d611c9a 250 if (ret) {
5dba48a8 251 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
ea8a5d7f 252 return;
5dba48a8 253 }
4d611c9a
PB
254 }
255
c87c0672 256 n = r->iov.iov_len / 512;
ea8a5d7f
AL
257 r->sector += n;
258 r->sector_count -= n;
a917d384 259 if (r->sector_count == 0) {
b45ef674 260 scsi_req_complete(&r->req, GOOD);
a917d384
PB
261 } else {
262 len = r->sector_count * 512;
263 if (len > SCSI_DMA_BUF_SIZE) {
264 len = SCSI_DMA_BUF_SIZE;
265 }
c87c0672 266 r->iov.iov_len = len;
4c41d2ef 267 DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
ab9adc88 268 scsi_req_data(&r->req, len);
4d611c9a 269 }
4d611c9a
PB
270}
271
42741212 272static void scsi_write_data(SCSIRequest *req)
ea8a5d7f 273{
5c6c0e51 274 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
4c41d2ef 275 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
ea8a5d7f
AL
276 uint32_t n;
277
6fa2c95f
SH
278 /* No data transfer may already be in progress */
279 assert(r->req.aiocb == NULL);
280
efb9ee02
HR
281 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
282 DPRINTF("Data transfer direction invalid\n");
283 scsi_write_complete(r, -EINVAL);
42741212 284 return;
efb9ee02
HR
285 }
286
c87c0672 287 n = r->iov.iov_len / 512;
ea8a5d7f 288 if (n) {
a1aff5bf
MA
289 if (s->tray_open) {
290 scsi_write_complete(r, -ENOMEDIUM);
291 }
c87c0672 292 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
a597e79c
CH
293
294 bdrv_acct_start(s->bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE);
428c149b 295 r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
c87c0672 296 scsi_write_complete, r);
d33ea50a 297 if (r->req.aiocb == NULL) {
a1f0cce2 298 scsi_write_complete(r, -ENOMEM);
d33ea50a 299 }
ea8a5d7f
AL
300 } else {
301 /* Invoke completion routine to fetch data from host. */
302 scsi_write_complete(r, 0);
303 }
a917d384 304}
2e5d83bb 305
213189ab 306static void scsi_dma_restart_bh(void *opaque)
ea8a5d7f 307{
d52affa7 308 SCSIDiskState *s = opaque;
9af99d98
GH
309 SCSIRequest *req;
310 SCSIDiskReq *r;
213189ab
MA
311
312 qemu_bh_delete(s->bh);
313 s->bh = NULL;
ea8a5d7f 314
9af99d98
GH
315 QTAILQ_FOREACH(req, &s->qdev.requests, next) {
316 r = DO_UPCAST(SCSIDiskReq, req, req);
ea8a5d7f 317 if (r->status & SCSI_REQ_STATUS_RETRY) {
5dba48a8 318 int status = r->status;
78ced65e
KW
319 int ret;
320
5dba48a8
KW
321 r->status &=
322 ~(SCSI_REQ_STATUS_RETRY | SCSI_REQ_STATUS_RETRY_TYPE_MASK);
323
324 switch (status & SCSI_REQ_STATUS_RETRY_TYPE_MASK) {
325 case SCSI_REQ_STATUS_RETRY_READ:
5c6c0e51 326 scsi_read_data(&r->req);
5dba48a8
KW
327 break;
328 case SCSI_REQ_STATUS_RETRY_WRITE:
5c6c0e51 329 scsi_write_data(&r->req);
5dba48a8 330 break;
78ced65e
KW
331 case SCSI_REQ_STATUS_RETRY_FLUSH:
332 ret = scsi_disk_emulate_command(r, r->iov.iov_base);
333 if (ret == 0) {
b45ef674 334 scsi_req_complete(&r->req, GOOD);
78ced65e 335 }
5dba48a8 336 }
ea8a5d7f 337 }
ea8a5d7f
AL
338 }
339}
340
213189ab
MA
341static void scsi_dma_restart_cb(void *opaque, int running, int reason)
342{
d52affa7 343 SCSIDiskState *s = opaque;
213189ab
MA
344
345 if (!running)
346 return;
347
348 if (!s->bh) {
349 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
350 qemu_bh_schedule(s->bh);
351 }
352}
353
a917d384 354/* Return a pointer to the data buffer. */
5c6c0e51 355static uint8_t *scsi_get_buf(SCSIRequest *req)
a917d384 356{
5c6c0e51 357 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
2e5d83bb 358
3f4cb3d3 359 return (uint8_t *)r->iov.iov_base;
2e5d83bb
PB
360}
361
0b06c059
GH
362static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
363{
383b4d9b 364 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
0b06c059
GH
365 int buflen = 0;
366
367 if (req->cmd.buf[1] & 0x2) {
368 /* Command support data - optional, not implemented */
369 BADF("optional INQUIRY command support request not implemented\n");
370 return -1;
371 }
372
373 if (req->cmd.buf[1] & 0x1) {
374 /* Vital product data */
375 uint8_t page_code = req->cmd.buf[2];
376 if (req->cmd.xfer < 4) {
377 BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
378 "less than 4\n", page_code, req->cmd.xfer);
379 return -1;
380 }
381
f37bd73b 382 if (s->qdev.type == TYPE_ROM) {
0b06c059
GH
383 outbuf[buflen++] = 5;
384 } else {
385 outbuf[buflen++] = 0;
386 }
387 outbuf[buflen++] = page_code ; // this page
388 outbuf[buflen++] = 0x00;
389
390 switch (page_code) {
391 case 0x00: /* Supported page codes, mandatory */
39d98982
HR
392 {
393 int pages;
0b06c059
GH
394 DPRINTF("Inquiry EVPD[Supported pages] "
395 "buffer size %zd\n", req->cmd.xfer);
39d98982 396 pages = buflen++;
0b06c059 397 outbuf[buflen++] = 0x00; // list of supported pages (this page)
3e1c0c9a
HR
398 if (s->serial)
399 outbuf[buflen++] = 0x80; // unit serial number
0b06c059 400 outbuf[buflen++] = 0x83; // device identification
f37bd73b 401 if (s->qdev.type == TYPE_DISK) {
ea3bd56f
CH
402 outbuf[buflen++] = 0xb0; // block limits
403 outbuf[buflen++] = 0xb2; // thin provisioning
39d98982
HR
404 }
405 outbuf[pages] = buflen - pages - 1; // number of pages
0b06c059 406 break;
39d98982 407 }
0b06c059
GH
408 case 0x80: /* Device serial number, optional */
409 {
3e1c0c9a 410 int l;
0b06c059 411
3e1c0c9a
HR
412 if (!s->serial) {
413 DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
414 return -1;
415 }
416
417 l = strlen(s->serial);
0b06c059
GH
418 if (l > req->cmd.xfer)
419 l = req->cmd.xfer;
420 if (l > 20)
421 l = 20;
422
423 DPRINTF("Inquiry EVPD[Serial number] "
424 "buffer size %zd\n", req->cmd.xfer);
425 outbuf[buflen++] = l;
a0fef654 426 memcpy(outbuf+buflen, s->serial, l);
0b06c059
GH
427 buflen += l;
428 break;
429 }
430
431 case 0x83: /* Device identification page, mandatory */
432 {
433 int max_len = 255 - 8;
428c149b 434 int id_len = strlen(bdrv_get_device_name(s->bs));
0b06c059
GH
435
436 if (id_len > max_len)
437 id_len = max_len;
438 DPRINTF("Inquiry EVPD[Device identification] "
439 "buffer size %zd\n", req->cmd.xfer);
440
39d98982 441 outbuf[buflen++] = 4 + id_len;
0b06c059
GH
442 outbuf[buflen++] = 0x2; // ASCII
443 outbuf[buflen++] = 0; // not officially assigned
444 outbuf[buflen++] = 0; // reserved
445 outbuf[buflen++] = id_len; // length of data following
446
428c149b 447 memcpy(outbuf+buflen, bdrv_get_device_name(s->bs), id_len);
0b06c059
GH
448 buflen += id_len;
449 break;
450 }
ea3bd56f 451 case 0xb0: /* block limits */
ee3659e3 452 {
ea3bd56f
CH
453 unsigned int unmap_sectors =
454 s->qdev.conf.discard_granularity / s->qdev.blocksize;
8cfacf07
CH
455 unsigned int min_io_size =
456 s->qdev.conf.min_io_size / s->qdev.blocksize;
457 unsigned int opt_io_size =
458 s->qdev.conf.opt_io_size / s->qdev.blocksize;
ee3659e3 459
f37bd73b 460 if (s->qdev.type == TYPE_ROM) {
39d98982
HR
461 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
462 page_code);
463 return -1;
464 }
ee3659e3
CH
465 /* required VPD size with unmap support */
466 outbuf[3] = buflen = 0x3c;
467
468 memset(outbuf + 4, 0, buflen - 4);
469
470 /* optimal transfer length granularity */
471 outbuf[6] = (min_io_size >> 8) & 0xff;
472 outbuf[7] = min_io_size & 0xff;
473
474 /* optimal transfer length */
475 outbuf[12] = (opt_io_size >> 24) & 0xff;
476 outbuf[13] = (opt_io_size >> 16) & 0xff;
477 outbuf[14] = (opt_io_size >> 8) & 0xff;
478 outbuf[15] = opt_io_size & 0xff;
ea3bd56f
CH
479
480 /* optimal unmap granularity */
481 outbuf[28] = (unmap_sectors >> 24) & 0xff;
482 outbuf[29] = (unmap_sectors >> 16) & 0xff;
483 outbuf[30] = (unmap_sectors >> 8) & 0xff;
484 outbuf[31] = unmap_sectors & 0xff;
485 break;
486 }
487 case 0xb2: /* thin provisioning */
488 {
489 outbuf[3] = buflen = 8;
490 outbuf[4] = 0;
491 outbuf[5] = 0x40; /* write same with unmap supported */
492 outbuf[6] = 0;
493 outbuf[7] = 0;
ee3659e3
CH
494 break;
495 }
0b06c059
GH
496 default:
497 BADF("Error: unsupported Inquiry (EVPD[%02X]) "
498 "buffer size %zd\n", page_code, req->cmd.xfer);
499 return -1;
500 }
501 /* done with EVPD */
502 return buflen;
503 }
504
505 /* Standard INQUIRY data */
506 if (req->cmd.buf[2] != 0) {
507 BADF("Error: Inquiry (STANDARD) page or code "
508 "is non-zero [%02X]\n", req->cmd.buf[2]);
509 return -1;
510 }
511
512 /* PAGE CODE == 0 */
513 if (req->cmd.xfer < 5) {
514 BADF("Error: Inquiry (STANDARD) buffer size %zd "
515 "is less than 5\n", req->cmd.xfer);
516 return -1;
517 }
518
0b06c059
GH
519 buflen = req->cmd.xfer;
520 if (buflen > SCSI_MAX_INQUIRY_LEN)
521 buflen = SCSI_MAX_INQUIRY_LEN;
522
523 memset(outbuf, 0, buflen);
524
f37bd73b
HR
525 outbuf[0] = s->qdev.type & 0x1f;
526 if (s->qdev.type == TYPE_ROM) {
0b06c059 527 outbuf[1] = 0x80;
550fe6c6 528 memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
0b06c059 529 } else {
419e691f 530 outbuf[1] = s->removable ? 0x80 : 0;
550fe6c6 531 memcpy(&outbuf[16], "QEMU HARDDISK ", 16);
0b06c059 532 }
550fe6c6 533 memcpy(&outbuf[8], "QEMU ", 8);
314b1811 534 memset(&outbuf[32], 0, 4);
552fee93 535 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
99aba0c4
CH
536 /*
537 * We claim conformance to SPC-3, which is required for guests
538 * to ask for modern features like READ CAPACITY(16) or the
539 * block characteristics VPD page by default. Not all of SPC-3
540 * is actually implemented, but we're good enough.
541 */
ee3659e3 542 outbuf[2] = 5;
0b06c059 543 outbuf[3] = 2; /* Format 2 */
ad3cea42
AT
544
545 if (buflen > 36) {
546 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
547 } else {
548 /* If the allocation length of CDB is too small,
549 the additional length is not adjusted */
550 outbuf[4] = 36 - 5;
551 }
552
0b06c059
GH
553 /* Sync data transfer and TCQ. */
554 outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
555 return buflen;
556}
557
cfc606da 558static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
282ab04e 559 int page_control)
ebddfcbe 560{
428c149b 561 BlockDriverState *bdrv = s->bs;
ebddfcbe 562 int cylinders, heads, secs;
cfc606da 563 uint8_t *p = *p_outbuf;
ebddfcbe 564
282ab04e
BK
565 /*
566 * If Changeable Values are requested, a mask denoting those mode parameters
567 * that are changeable shall be returned. As we currently don't support
568 * parameter changes via MODE_SELECT all bits are returned set to zero.
569 * The buffer was already menset to zero by the caller of this function.
570 */
ebddfcbe
GH
571 switch (page) {
572 case 4: /* Rigid disk device geometry page. */
cfc606da
PB
573 if (s->qdev.type == TYPE_ROM) {
574 return -1;
575 }
ebddfcbe
GH
576 p[0] = 4;
577 p[1] = 0x16;
282ab04e 578 if (page_control == 1) { /* Changeable Values */
cfc606da 579 break;
282ab04e 580 }
ebddfcbe
GH
581 /* if a geometry hint is available, use it */
582 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
583 p[2] = (cylinders >> 16) & 0xff;
584 p[3] = (cylinders >> 8) & 0xff;
585 p[4] = cylinders & 0xff;
586 p[5] = heads & 0xff;
587 /* Write precomp start cylinder, disabled */
588 p[6] = (cylinders >> 16) & 0xff;
589 p[7] = (cylinders >> 8) & 0xff;
590 p[8] = cylinders & 0xff;
591 /* Reduced current start cylinder, disabled */
592 p[9] = (cylinders >> 16) & 0xff;
593 p[10] = (cylinders >> 8) & 0xff;
594 p[11] = cylinders & 0xff;
595 /* Device step rate [ns], 200ns */
596 p[12] = 0;
597 p[13] = 200;
598 /* Landing zone cylinder */
599 p[14] = 0xff;
600 p[15] = 0xff;
601 p[16] = 0xff;
602 /* Medium rotation rate [rpm], 5400 rpm */
603 p[20] = (5400 >> 8) & 0xff;
604 p[21] = 5400 & 0xff;
cfc606da 605 break;
ebddfcbe
GH
606
607 case 5: /* Flexible disk device geometry page. */
cfc606da
PB
608 if (s->qdev.type == TYPE_ROM) {
609 return -1;
610 }
ebddfcbe
GH
611 p[0] = 5;
612 p[1] = 0x1e;
282ab04e 613 if (page_control == 1) { /* Changeable Values */
cfc606da 614 break;
282ab04e 615 }
ebddfcbe
GH
616 /* Transfer rate [kbit/s], 5Mbit/s */
617 p[2] = 5000 >> 8;
618 p[3] = 5000 & 0xff;
619 /* if a geometry hint is available, use it */
620 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
621 p[4] = heads & 0xff;
622 p[5] = secs & 0xff;
623 p[6] = s->cluster_size * 2;
624 p[8] = (cylinders >> 8) & 0xff;
625 p[9] = cylinders & 0xff;
626 /* Write precomp start cylinder, disabled */
627 p[10] = (cylinders >> 8) & 0xff;
628 p[11] = cylinders & 0xff;
629 /* Reduced current start cylinder, disabled */
630 p[12] = (cylinders >> 8) & 0xff;
631 p[13] = cylinders & 0xff;
632 /* Device step rate [100us], 100us */
633 p[14] = 0;
634 p[15] = 1;
635 /* Device step pulse width [us], 1us */
636 p[16] = 1;
637 /* Device head settle delay [100us], 100us */
638 p[17] = 0;
639 p[18] = 1;
640 /* Motor on delay [0.1s], 0.1s */
641 p[19] = 1;
642 /* Motor off delay [0.1s], 0.1s */
643 p[20] = 1;
644 /* Medium rotation rate [rpm], 5400 rpm */
645 p[28] = (5400 >> 8) & 0xff;
646 p[29] = 5400 & 0xff;
cfc606da 647 break;
ebddfcbe
GH
648
649 case 8: /* Caching page. */
650 p[0] = 8;
651 p[1] = 0x12;
282ab04e 652 if (page_control == 1) { /* Changeable Values */
cfc606da 653 break;
282ab04e 654 }
428c149b 655 if (bdrv_enable_write_cache(s->bs)) {
ebddfcbe
GH
656 p[2] = 4; /* WCE */
657 }
cfc606da 658 break;
ebddfcbe
GH
659
660 case 0x2a: /* CD Capabilities and Mechanical Status page. */
cfc606da
PB
661 if (s->qdev.type != TYPE_ROM) {
662 return -1;
663 }
ebddfcbe
GH
664 p[0] = 0x2a;
665 p[1] = 0x14;
282ab04e 666 if (page_control == 1) { /* Changeable Values */
cfc606da 667 break;
282ab04e 668 }
ebddfcbe
GH
669 p[2] = 3; // CD-R & CD-RW read
670 p[3] = 0; // Writing not supported
671 p[4] = 0x7f; /* Audio, composite, digital out,
672 mode 2 form 1&2, multi session */
673 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
674 RW corrected, C2 errors, ISRC,
675 UPC, Bar code */
81b1008d 676 p[6] = 0x2d | (s->tray_locked ? 2 : 0);
ebddfcbe
GH
677 /* Locking supported, jumper present, eject, tray */
678 p[7] = 0; /* no volume & mute control, no
679 changer */
680 p[8] = (50 * 176) >> 8; // 50x read speed
681 p[9] = (50 * 176) & 0xff;
682 p[10] = 0 >> 8; // No volume
683 p[11] = 0 & 0xff;
684 p[12] = 2048 >> 8; // 2M buffer
685 p[13] = 2048 & 0xff;
686 p[14] = (16 * 176) >> 8; // 16x read speed current
687 p[15] = (16 * 176) & 0xff;
688 p[18] = (16 * 176) >> 8; // 16x write speed
689 p[19] = (16 * 176) & 0xff;
690 p[20] = (16 * 176) >> 8; // 16x write speed current
691 p[21] = (16 * 176) & 0xff;
cfc606da 692 break;
ebddfcbe
GH
693
694 default:
cfc606da 695 return -1;
ebddfcbe 696 }
cfc606da
PB
697
698 *p_outbuf += p[1] + 2;
699 return p[1] + 2;
ebddfcbe
GH
700}
701
cfc606da 702static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
ebddfcbe 703{
cfc606da 704 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
ebddfcbe 705 uint64_t nb_sectors;
cfc606da 706 int page, dbd, buflen, ret, page_control;
ebddfcbe 707 uint8_t *p;
ce512ee1 708 uint8_t dev_specific_param;
ebddfcbe 709
cfc606da
PB
710 dbd = r->req.cmd.buf[1] & 0x8;
711 page = r->req.cmd.buf[2] & 0x3f;
712 page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
aa2b1e89 713 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
cfc606da
PB
714 (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
715 memset(outbuf, 0, r->req.cmd.xfer);
ebddfcbe
GH
716 p = outbuf;
717
0056dcc1 718 if (bdrv_is_read_only(s->bs)) {
ce512ee1
BK
719 dev_specific_param = 0x80; /* Readonly. */
720 } else {
721 dev_specific_param = 0x00;
722 }
723
cfc606da 724 if (r->req.cmd.buf[0] == MODE_SENSE) {
ce512ee1
BK
725 p[1] = 0; /* Default media type. */
726 p[2] = dev_specific_param;
727 p[3] = 0; /* Block descriptor length. */
728 p += 4;
729 } else { /* MODE_SENSE_10 */
730 p[2] = 0; /* Default media type. */
731 p[3] = dev_specific_param;
732 p[6] = p[7] = 0; /* Block descriptor length. */
733 p += 8;
ebddfcbe 734 }
ebddfcbe 735
428c149b 736 bdrv_get_geometry(s->bs, &nb_sectors);
333d50fe 737 if (!dbd && nb_sectors) {
cfc606da 738 if (r->req.cmd.buf[0] == MODE_SENSE) {
ce512ee1
BK
739 outbuf[3] = 8; /* Block descriptor length */
740 } else { /* MODE_SENSE_10 */
741 outbuf[7] = 8; /* Block descriptor length */
742 }
ebddfcbe 743 nb_sectors /= s->cluster_size;
ebddfcbe 744 if (nb_sectors > 0xffffff)
2488b740 745 nb_sectors = 0;
ebddfcbe
GH
746 p[0] = 0; /* media density code */
747 p[1] = (nb_sectors >> 16) & 0xff;
748 p[2] = (nb_sectors >> 8) & 0xff;
749 p[3] = nb_sectors & 0xff;
750 p[4] = 0; /* reserved */
751 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
752 p[6] = s->cluster_size * 2;
753 p[7] = 0;
754 p += 8;
755 }
756
cfc606da
PB
757 if (page_control == 3) {
758 /* Saved Values */
759 scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
760 return -1;
282ab04e
BK
761 }
762
cfc606da
PB
763 if (page == 0x3f) {
764 for (page = 0; page <= 0x3e; page++) {
765 mode_sense_page(s, page, &p, page_control);
766 }
767 } else {
768 ret = mode_sense_page(s, page, &p, page_control);
769 if (ret == -1) {
770 return -1;
771 }
ebddfcbe
GH
772 }
773
774 buflen = p - outbuf;
ce512ee1
BK
775 /*
776 * The mode data length field specifies the length in bytes of the
777 * following data that is available to be transferred. The mode data
778 * length does not include itself.
779 */
cfc606da 780 if (r->req.cmd.buf[0] == MODE_SENSE) {
ce512ee1
BK
781 outbuf[0] = buflen - 1;
782 } else { /* MODE_SENSE_10 */
783 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
784 outbuf[1] = (buflen - 2) & 0xff;
785 }
cfc606da
PB
786 if (buflen > r->req.cmd.xfer)
787 buflen = r->req.cmd.xfer;
ebddfcbe
GH
788 return buflen;
789}
790
02880f43
GH
791static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
792{
793 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
02880f43
GH
794 int start_track, format, msf, toclen;
795 uint64_t nb_sectors;
796
797 msf = req->cmd.buf[1] & 2;
798 format = req->cmd.buf[2] & 0xf;
799 start_track = req->cmd.buf[6];
428c149b 800 bdrv_get_geometry(s->bs, &nb_sectors);
02880f43
GH
801 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
802 nb_sectors /= s->cluster_size;
803 switch (format) {
804 case 0:
805 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
806 break;
807 case 1:
808 /* multi session : only a single session defined */
809 toclen = 12;
810 memset(outbuf, 0, 12);
811 outbuf[1] = 0x0a;
812 outbuf[2] = 0x01;
813 outbuf[3] = 0x01;
814 break;
815 case 2:
816 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
817 break;
818 default:
819 return -1;
820 }
821 if (toclen > req->cmd.xfer)
822 toclen = req->cmd.xfer;
823 return toclen;
824}
825
68bb01f3 826static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
bfd52647
MA
827{
828 SCSIRequest *req = &r->req;
829 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
830 bool start = req->cmd.buf[4] & 1;
831 bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
832
833 if (s->qdev.type == TYPE_ROM && loej) {
68bb01f3
MA
834 if (!start && !s->tray_open && s->tray_locked) {
835 scsi_check_condition(r,
836 bdrv_is_inserted(s->bs)
837 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED)
838 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED));
839 return -1;
fdec4404 840 }
bfd52647 841 bdrv_eject(s->bs, !start);
ece0d5e9 842 s->tray_open = !start;
bfd52647 843 }
68bb01f3 844 return 0;
bfd52647
MA
845}
846
8af7a3ab 847static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
aa5dbdc1 848{
8af7a3ab 849 SCSIRequest *req = &r->req;
e7e25e32 850 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
e7e25e32 851 uint64_t nb_sectors;
aa5dbdc1
GH
852 int buflen = 0;
853
854 switch (req->cmd.buf[0]) {
855 case TEST_UNIT_READY:
a1aff5bf 856 if (s->tray_open || !bdrv_is_inserted(s->bs))
aa5dbdc1 857 goto not_ready;
5f71d32f 858 break;
0b06c059
GH
859 case INQUIRY:
860 buflen = scsi_disk_emulate_inquiry(req, outbuf);
861 if (buflen < 0)
862 goto illegal_request;
5f71d32f 863 break;
ebddfcbe
GH
864 case MODE_SENSE:
865 case MODE_SENSE_10:
cfc606da 866 buflen = scsi_disk_emulate_mode_sense(r, outbuf);
ebddfcbe
GH
867 if (buflen < 0)
868 goto illegal_request;
869 break;
02880f43
GH
870 case READ_TOC:
871 buflen = scsi_disk_emulate_read_toc(req, outbuf);
872 if (buflen < 0)
873 goto illegal_request;
874 break;
3d53ba18
GH
875 case RESERVE:
876 if (req->cmd.buf[1] & 1)
877 goto illegal_request;
878 break;
879 case RESERVE_10:
880 if (req->cmd.buf[1] & 3)
881 goto illegal_request;
882 break;
883 case RELEASE:
884 if (req->cmd.buf[1] & 1)
885 goto illegal_request;
886 break;
887 case RELEASE_10:
888 if (req->cmd.buf[1] & 3)
889 goto illegal_request;
890 break;
8d3628ff 891 case START_STOP:
68bb01f3
MA
892 if (scsi_disk_emulate_start_stop(r) < 0) {
893 return -1;
894 }
5f71d32f 895 break;
c68b9f34 896 case ALLOW_MEDIUM_REMOVAL:
81b1008d 897 s->tray_locked = req->cmd.buf[4] & 1;
025e849a 898 bdrv_lock_medium(s->bs, req->cmd.buf[4] & 1);
5f71d32f 899 break;
5e30a07d 900 case READ_CAPACITY_10:
e7e25e32 901 /* The normal LEN field for this command is zero. */
5f71d32f
HR
902 memset(outbuf, 0, 8);
903 bdrv_get_geometry(s->bs, &nb_sectors);
e7e25e32
GH
904 if (!nb_sectors)
905 goto not_ready;
906 nb_sectors /= s->cluster_size;
907 /* Returned value is the address of the last sector. */
908 nb_sectors--;
909 /* Remember the new size for read/write sanity checking. */
910 s->max_lba = nb_sectors;
911 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
912 if (nb_sectors > UINT32_MAX)
913 nb_sectors = UINT32_MAX;
914 outbuf[0] = (nb_sectors >> 24) & 0xff;
915 outbuf[1] = (nb_sectors >> 16) & 0xff;
916 outbuf[2] = (nb_sectors >> 8) & 0xff;
917 outbuf[3] = nb_sectors & 0xff;
918 outbuf[4] = 0;
919 outbuf[5] = 0;
920 outbuf[6] = s->cluster_size * 2;
921 outbuf[7] = 0;
922 buflen = 8;
5f71d32f 923 break;
38215553
GH
924 case GET_CONFIGURATION:
925 memset(outbuf, 0, 8);
926 /* ??? This should probably return much more information. For now
927 just return the basic header indicating the CD-ROM profile. */
928 outbuf[7] = 8; // CD-ROM
929 buflen = 8;
930 break;
f6515262 931 case SERVICE_ACTION_IN_16:
5dd90e2a 932 /* Service Action In subcommands. */
f6515262 933 if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
5dd90e2a
GH
934 DPRINTF("SAI READ CAPACITY(16)\n");
935 memset(outbuf, 0, req->cmd.xfer);
428c149b 936 bdrv_get_geometry(s->bs, &nb_sectors);
5dd90e2a
GH
937 if (!nb_sectors)
938 goto not_ready;
939 nb_sectors /= s->cluster_size;
940 /* Returned value is the address of the last sector. */
941 nb_sectors--;
942 /* Remember the new size for read/write sanity checking. */
943 s->max_lba = nb_sectors;
944 outbuf[0] = (nb_sectors >> 56) & 0xff;
945 outbuf[1] = (nb_sectors >> 48) & 0xff;
946 outbuf[2] = (nb_sectors >> 40) & 0xff;
947 outbuf[3] = (nb_sectors >> 32) & 0xff;
948 outbuf[4] = (nb_sectors >> 24) & 0xff;
949 outbuf[5] = (nb_sectors >> 16) & 0xff;
950 outbuf[6] = (nb_sectors >> 8) & 0xff;
951 outbuf[7] = nb_sectors & 0xff;
952 outbuf[8] = 0;
953 outbuf[9] = 0;
954 outbuf[10] = s->cluster_size * 2;
955 outbuf[11] = 0;
ee3659e3
CH
956 outbuf[12] = 0;
957 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
ea3bd56f
CH
958
959 /* set TPE bit if the format supports discard */
960 if (s->qdev.conf.discard_granularity) {
961 outbuf[14] = 0x80;
962 }
963
5dd90e2a
GH
964 /* Protection, exponent and lowest lba field left blank. */
965 buflen = req->cmd.xfer;
966 break;
967 }
968 DPRINTF("Unsupported Service Action In\n");
969 goto illegal_request;
5e30a07d 970 case VERIFY_10:
88f8a5ed 971 break;
aa5dbdc1 972 default:
b45ef674 973 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
a1f0cce2 974 return -1;
aa5dbdc1 975 }
aa5dbdc1
GH
976 return buflen;
977
978not_ready:
a1aff5bf 979 if (s->tray_open || !bdrv_is_inserted(s->bs)) {
b45ef674 980 scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
a1f0cce2 981 } else {
b45ef674 982 scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
a1f0cce2 983 }
8af7a3ab 984 return -1;
aa5dbdc1
GH
985
986illegal_request:
cfc606da
PB
987 if (r->req.status == -1) {
988 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
989 }
8af7a3ab 990 return -1;
aa5dbdc1
GH
991}
992
2e5d83bb
PB
993/* Execute a scsi command. Returns the length of the data expected by the
994 command. This will be Positive for data transfers from the device
995 (eg. disk reads), negative for transfers to the device (eg. disk writes),
996 and zero if the command does not transfer any data. */
997
5c6c0e51 998static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
2e5d83bb 999{
5c6c0e51
HR
1000 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1001 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
ad2d30f7 1002 int32_t len;
a917d384
PB
1003 uint8_t command;
1004 uint8_t *outbuf;
aa5dbdc1 1005 int rc;
a917d384
PB
1006
1007 command = buf[0];
3f4cb3d3 1008 outbuf = (uint8_t *)r->iov.iov_base;
653c1c3f 1009 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
2dd791b6 1010
2e5d83bb
PB
1011#ifdef DEBUG_SCSI
1012 {
1013 int i;
2dd791b6 1014 for (i = 1; i < r->req.cmd.len; i++) {
2e5d83bb
PB
1015 printf(" 0x%02x", buf[i]);
1016 }
1017 printf("\n");
1018 }
1019#endif
aa5dbdc1 1020
a917d384 1021 switch (command) {
ebf46023 1022 case TEST_UNIT_READY:
0b06c059 1023 case INQUIRY:
ebddfcbe
GH
1024 case MODE_SENSE:
1025 case MODE_SENSE_10:
3d53ba18
GH
1026 case RESERVE:
1027 case RESERVE_10:
1028 case RELEASE:
1029 case RELEASE_10:
8d3628ff 1030 case START_STOP:
c68b9f34 1031 case ALLOW_MEDIUM_REMOVAL:
5e30a07d 1032 case READ_CAPACITY_10:
02880f43 1033 case READ_TOC:
38215553 1034 case GET_CONFIGURATION:
f6515262 1035 case SERVICE_ACTION_IN_16:
5e30a07d 1036 case VERIFY_10:
8af7a3ab
KW
1037 rc = scsi_disk_emulate_command(r, outbuf);
1038 if (rc < 0) {
0b06c059 1039 return 0;
aa5dbdc1 1040 }
8af7a3ab
KW
1041
1042 r->iov.iov_len = rc;
0b06c059 1043 break;
0a4ac106
PB
1044 case SYNCHRONIZE_CACHE:
1045 bdrv_acct_start(s->bs, &r->acct, 0, BDRV_ACCT_FLUSH);
1046 r->req.aiocb = bdrv_aio_flush(s->bs, scsi_flush_complete, r);
1047 if (r->req.aiocb == NULL) {
1048 scsi_flush_complete(r, -EIO);
1049 }
1050 return 0;
ebf46023
GH
1051 case READ_6:
1052 case READ_10:
bd536cf3
GH
1053 case READ_12:
1054 case READ_16:
5c6c0e51 1055 len = r->req.cmd.xfer / s->qdev.blocksize;
2dd791b6
HR
1056 DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1057 if (r->req.cmd.lba > s->max_lba)
274fb0e1 1058 goto illegal_lba;
2dd791b6 1059 r->sector = r->req.cmd.lba * s->cluster_size;
a917d384 1060 r->sector_count = len * s->cluster_size;
2e5d83bb 1061 break;
ebf46023
GH
1062 case WRITE_6:
1063 case WRITE_10:
bd536cf3
GH
1064 case WRITE_12:
1065 case WRITE_16:
5e30a07d 1066 case WRITE_VERIFY_10:
ebef0bbb
BK
1067 case WRITE_VERIFY_12:
1068 case WRITE_VERIFY_16:
5c6c0e51 1069 len = r->req.cmd.xfer / s->qdev.blocksize;
ebef0bbb 1070 DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
2dd791b6
HR
1071 (command & 0xe) == 0xe ? "And Verify " : "",
1072 r->req.cmd.lba, len);
1073 if (r->req.cmd.lba > s->max_lba)
274fb0e1 1074 goto illegal_lba;
2dd791b6 1075 r->sector = r->req.cmd.lba * s->cluster_size;
a917d384 1076 r->sector_count = len * s->cluster_size;
2e5d83bb 1077 break;
ebef0bbb 1078 case MODE_SELECT:
2dd791b6 1079 DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
ebef0bbb
BK
1080 /* We don't support mode parameter changes.
1081 Allow the mode parameter header + block descriptors only. */
2dd791b6 1082 if (r->req.cmd.xfer > 12) {
ebef0bbb
BK
1083 goto fail;
1084 }
1085 break;
1086 case MODE_SELECT_10:
2dd791b6 1087 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
ebef0bbb
BK
1088 /* We don't support mode parameter changes.
1089 Allow the mode parameter header + block descriptors only. */
2dd791b6 1090 if (r->req.cmd.xfer > 16) {
ebef0bbb
BK
1091 goto fail;
1092 }
1093 break;
1094 case SEEK_6:
1095 case SEEK_10:
2dd791b6
HR
1096 DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
1097 r->req.cmd.lba);
1098 if (r->req.cmd.lba > s->max_lba) {
ebef0bbb
BK
1099 goto illegal_lba;
1100 }
ea3bd56f
CH
1101 break;
1102 case WRITE_SAME_16:
5c6c0e51 1103 len = r->req.cmd.xfer / s->qdev.blocksize;
ea3bd56f
CH
1104
1105 DPRINTF("WRITE SAME(16) (sector %" PRId64 ", count %d)\n",
1106 r->req.cmd.lba, len);
1107
1108 if (r->req.cmd.lba > s->max_lba) {
1109 goto illegal_lba;
1110 }
1111
1112 /*
1113 * We only support WRITE SAME with the unmap bit set for now.
1114 */
1115 if (!(buf[1] & 0x8)) {
1116 goto fail;
1117 }
1118
1119 rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1120 len * s->cluster_size);
1121 if (rc < 0) {
1122 /* XXX: better error code ?*/
1123 goto fail;
1124 }
1125
ebef0bbb 1126 break;
739df215
PB
1127 case REQUEST_SENSE:
1128 abort();
2e5d83bb 1129 default:
2dd791b6 1130 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
b45ef674 1131 scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
a1f0cce2 1132 return 0;
2e5d83bb 1133 fail:
b45ef674 1134 scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
2dd791b6 1135 return 0;
274fb0e1 1136 illegal_lba:
b45ef674 1137 scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
274fb0e1 1138 return 0;
2e5d83bb 1139 }
c87c0672 1140 if (r->sector_count == 0 && r->iov.iov_len == 0) {
b45ef674 1141 scsi_req_complete(&r->req, GOOD);
a917d384 1142 }
c87c0672 1143 len = r->sector_count * 512 + r->iov.iov_len;
efb9ee02
HR
1144 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1145 return -len;
a917d384
PB
1146 } else {
1147 if (!r->sector_count)
1148 r->sector_count = -1;
efb9ee02 1149 return len;
2e5d83bb 1150 }
2e5d83bb
PB
1151}
1152
e9447f35
JK
1153static void scsi_disk_reset(DeviceState *dev)
1154{
1155 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1156 uint64_t nb_sectors;
1157
c7b48872 1158 scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
e9447f35
JK
1159
1160 bdrv_get_geometry(s->bs, &nb_sectors);
1161 nb_sectors /= s->cluster_size;
1162 if (nb_sectors) {
1163 nb_sectors--;
1164 }
1165 s->max_lba = nb_sectors;
1166}
1167
1168static void scsi_destroy(SCSIDevice *dev)
1169{
1170 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1171
c7b48872 1172 scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
f8b6cc00 1173 blockdev_mark_auto_del(s->qdev.conf.bs);
56a14938
GH
1174}
1175
7d4b4ba5 1176static void scsi_cd_change_media_cb(void *opaque, bool load)
2c6942fa
MA
1177{
1178}
1179
e4def80b
MA
1180static bool scsi_cd_is_tray_open(void *opaque)
1181{
1182 return ((SCSIDiskState *)opaque)->tray_open;
1183}
1184
f107639a
MA
1185static bool scsi_cd_is_medium_locked(void *opaque)
1186{
1187 return ((SCSIDiskState *)opaque)->tray_locked;
1188}
1189
1190static const BlockDevOps scsi_cd_block_ops = {
2c6942fa 1191 .change_media_cb = scsi_cd_change_media_cb,
e4def80b 1192 .is_tray_open = scsi_cd_is_tray_open,
f107639a
MA
1193 .is_medium_locked = scsi_cd_is_medium_locked,
1194};
1195
f37bd73b 1196static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
2e5d83bb 1197{
d52affa7 1198 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
f8b6cc00 1199 DriveInfo *dinfo;
2e5d83bb 1200
f8b6cc00 1201 if (!s->qdev.conf.bs) {
1ecda02b 1202 error_report("scsi-disk: drive property not set");
d52affa7
GH
1203 return -1;
1204 }
f8b6cc00 1205 s->bs = s->qdev.conf.bs;
d52affa7 1206
f37bd73b 1207 if (scsi_type == TYPE_DISK && !bdrv_is_inserted(s->bs)) {
98f28ad7
MA
1208 error_report("Device needs media, but drive is empty");
1209 return -1;
1210 }
1211
a0fef654 1212 if (!s->serial) {
f8b6cc00
MA
1213 /* try to fall back to value set with legacy -drive serial=... */
1214 dinfo = drive_get_by_blockdev(s->bs);
3e1c0c9a 1215 if (*dinfo->serial) {
7267c094 1216 s->serial = g_strdup(dinfo->serial);
3e1c0c9a 1217 }
a0fef654
MA
1218 }
1219
552fee93 1220 if (!s->version) {
7267c094 1221 s->version = g_strdup(QEMU_VERSION);
552fee93
MA
1222 }
1223
32bb404a 1224 if (bdrv_is_sg(s->bs)) {
1ecda02b 1225 error_report("scsi-disk: unwanted /dev/sg*");
32bb404a
MA
1226 return -1;
1227 }
1228
f37bd73b 1229 if (scsi_type == TYPE_ROM) {
f107639a 1230 bdrv_set_dev_ops(s->bs, &scsi_cd_block_ops, s);
8cfacf07 1231 s->qdev.blocksize = 2048;
f37bd73b 1232 } else if (scsi_type == TYPE_DISK) {
8cfacf07 1233 s->qdev.blocksize = s->qdev.conf.logical_block_size;
f37bd73b
HR
1234 } else {
1235 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
1236 return -1;
2e5d83bb 1237 }
8cfacf07 1238 s->cluster_size = s->qdev.blocksize / 512;
7b6f9300 1239 bdrv_set_buffer_alignment(s->bs, s->qdev.blocksize);
8cfacf07 1240
f37bd73b 1241 s->qdev.type = scsi_type;
ea8a5d7f 1242 qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
1ca4d09a 1243 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
d52affa7
GH
1244 return 0;
1245}
1246
b443ae67
MA
1247static int scsi_hd_initfn(SCSIDevice *dev)
1248{
f37bd73b 1249 return scsi_initfn(dev, TYPE_DISK);
b443ae67
MA
1250}
1251
1252static int scsi_cd_initfn(SCSIDevice *dev)
1253{
f37bd73b 1254 return scsi_initfn(dev, TYPE_ROM);
b443ae67
MA
1255}
1256
1257static int scsi_disk_initfn(SCSIDevice *dev)
1258{
95b5edcd 1259 DriveInfo *dinfo;
f37bd73b 1260 uint8_t scsi_type;
b443ae67
MA
1261
1262 if (!dev->conf.bs) {
f37bd73b 1263 scsi_type = TYPE_DISK; /* will die in scsi_initfn() */
b443ae67 1264 } else {
95b5edcd 1265 dinfo = drive_get_by_blockdev(dev->conf.bs);
f37bd73b 1266 scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
b443ae67
MA
1267 }
1268
f37bd73b 1269 return scsi_initfn(dev, scsi_type);
b443ae67
MA
1270}
1271
8dbd4574
PB
1272static SCSIReqOps scsi_disk_reqops = {
1273 .size = sizeof(SCSIDiskReq),
12010e7b
PB
1274 .free_req = scsi_free_request,
1275 .send_command = scsi_send_command,
1276 .read_data = scsi_read_data,
1277 .write_data = scsi_write_data,
1278 .cancel_io = scsi_cancel_io,
1279 .get_buf = scsi_get_buf,
8dbd4574
PB
1280};
1281
1282static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
1283 uint32_t lun, void *hba_private)
1284{
1285 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1286 SCSIRequest *req;
1287 SCSIDiskReq *r;
1288
1289 req = scsi_req_alloc(&scsi_disk_reqops, &s->qdev, tag, lun, hba_private);
1290 r = DO_UPCAST(SCSIDiskReq, req, req);
1291 r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
1292 return req;
1293}
1294
b443ae67
MA
1295#define DEFINE_SCSI_DISK_PROPERTIES() \
1296 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1297 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1298 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1299
1300static SCSIDeviceInfo scsi_disk_info[] = {
1301 {
1302 .qdev.name = "scsi-hd",
1303 .qdev.fw_name = "disk",
1304 .qdev.desc = "virtual SCSI disk",
1305 .qdev.size = sizeof(SCSIDiskState),
1306 .qdev.reset = scsi_disk_reset,
1307 .init = scsi_hd_initfn,
1308 .destroy = scsi_destroy,
5c6c0e51 1309 .alloc_req = scsi_new_request,
b443ae67
MA
1310 .qdev.props = (Property[]) {
1311 DEFINE_SCSI_DISK_PROPERTIES(),
1312 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1313 DEFINE_PROP_END_OF_LIST(),
1314 }
1315 },{
1316 .qdev.name = "scsi-cd",
1317 .qdev.fw_name = "disk",
1318 .qdev.desc = "virtual SCSI CD-ROM",
1319 .qdev.size = sizeof(SCSIDiskState),
1320 .qdev.reset = scsi_disk_reset,
1321 .init = scsi_cd_initfn,
1322 .destroy = scsi_destroy,
5c6c0e51 1323 .alloc_req = scsi_new_request,
b443ae67
MA
1324 .qdev.props = (Property[]) {
1325 DEFINE_SCSI_DISK_PROPERTIES(),
1326 DEFINE_PROP_END_OF_LIST(),
1327 },
1328 },{
1329 .qdev.name = "scsi-disk", /* legacy -device scsi-disk */
1330 .qdev.fw_name = "disk",
1331 .qdev.desc = "virtual SCSI disk or CD-ROM (legacy)",
1332 .qdev.size = sizeof(SCSIDiskState),
1333 .qdev.reset = scsi_disk_reset,
1334 .init = scsi_disk_initfn,
1335 .destroy = scsi_destroy,
5c6c0e51 1336 .alloc_req = scsi_new_request,
b443ae67
MA
1337 .qdev.props = (Property[]) {
1338 DEFINE_SCSI_DISK_PROPERTIES(),
1339 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1340 DEFINE_PROP_END_OF_LIST(),
1341 }
1342 }
d52affa7
GH
1343};
1344
1345static void scsi_disk_register_devices(void)
1346{
b443ae67
MA
1347 int i;
1348
1349 for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1350 scsi_qdev_register(&scsi_disk_info[i]);
1351 }
8ccc2ace 1352}
d52affa7 1353device_init(scsi_disk_register_devices)