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