]> git.proxmox.com Git - mirror_qemu.git/blame - hw/scsi-disk.c
Merge remote-tracking branch 'pm-arm/for-upstream' into pm
[mirror_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 {
5dba48a8 217 if (type == SCSI_REQ_STATUS_RETRY_READ) {
ab9adc88 218 scsi_req_data(&r->req, 0);
5dba48a8 219 }
efb9ee02
HR
220 switch (error) {
221 case ENOMEM:
a1f0cce2
HR
222 scsi_command_complete(r, CHECK_CONDITION,
223 SENSE_CODE(TARGET_FAILURE));
efb9ee02
HR
224 break;
225 case EINVAL:
226 scsi_command_complete(r, CHECK_CONDITION,
227 SENSE_CODE(INVALID_FIELD));
228 break;
229 default:
a1f0cce2
HR
230 scsi_command_complete(r, CHECK_CONDITION,
231 SENSE_CODE(IO_ERROR));
efb9ee02 232 break;
a1f0cce2 233 }
5dba48a8 234 bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
ea8a5d7f 235 }
ea8a5d7f
AL
236 return 1;
237}
238
4d611c9a
PB
239static void scsi_write_complete(void * opaque, int ret)
240{
4c41d2ef 241 SCSIDiskReq *r = (SCSIDiskReq *)opaque;
a917d384 242 uint32_t len;
ea8a5d7f
AL
243 uint32_t n;
244
4c41d2ef 245 r->req.aiocb = NULL;
4d611c9a
PB
246
247 if (ret) {
5dba48a8 248 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
ea8a5d7f 249 return;
5dba48a8 250 }
4d611c9a
PB
251 }
252
c87c0672 253 n = r->iov.iov_len / 512;
ea8a5d7f
AL
254 r->sector += n;
255 r->sector_count -= n;
a917d384 256 if (r->sector_count == 0) {
a1f0cce2 257 scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
a917d384
PB
258 } else {
259 len = r->sector_count * 512;
260 if (len > SCSI_DMA_BUF_SIZE) {
261 len = SCSI_DMA_BUF_SIZE;
262 }
c87c0672 263 r->iov.iov_len = len;
4c41d2ef 264 DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
ab9adc88 265 scsi_req_data(&r->req, len);
4d611c9a 266 }
4d611c9a
PB
267}
268
42741212 269static void scsi_write_data(SCSIRequest *req)
ea8a5d7f 270{
5c6c0e51 271 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
4c41d2ef 272 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
ea8a5d7f
AL
273 uint32_t n;
274
6fa2c95f
SH
275 /* No data transfer may already be in progress */
276 assert(r->req.aiocb == NULL);
277
efb9ee02
HR
278 if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
279 DPRINTF("Data transfer direction invalid\n");
280 scsi_write_complete(r, -EINVAL);
42741212 281 return;
efb9ee02
HR
282 }
283
c87c0672 284 n = r->iov.iov_len / 512;
ea8a5d7f 285 if (n) {
c87c0672 286 qemu_iovec_init_external(&r->qiov, &r->iov, 1);
428c149b 287 r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
c87c0672 288 scsi_write_complete, r);
d33ea50a 289 if (r->req.aiocb == NULL) {
a1f0cce2 290 scsi_write_complete(r, -ENOMEM);
d33ea50a 291 }
ea8a5d7f
AL
292 } else {
293 /* Invoke completion routine to fetch data from host. */
294 scsi_write_complete(r, 0);
295 }
a917d384 296}
2e5d83bb 297
213189ab 298static void scsi_dma_restart_bh(void *opaque)
ea8a5d7f 299{
d52affa7 300 SCSIDiskState *s = opaque;
9af99d98
GH
301 SCSIRequest *req;
302 SCSIDiskReq *r;
213189ab
MA
303
304 qemu_bh_delete(s->bh);
305 s->bh = NULL;
ea8a5d7f 306
9af99d98
GH
307 QTAILQ_FOREACH(req, &s->qdev.requests, next) {
308 r = DO_UPCAST(SCSIDiskReq, req, req);
ea8a5d7f 309 if (r->status & SCSI_REQ_STATUS_RETRY) {
5dba48a8 310 int status = r->status;
78ced65e
KW
311 int ret;
312
5dba48a8
KW
313 r->status &=
314 ~(SCSI_REQ_STATUS_RETRY | SCSI_REQ_STATUS_RETRY_TYPE_MASK);
315
316 switch (status & SCSI_REQ_STATUS_RETRY_TYPE_MASK) {
317 case SCSI_REQ_STATUS_RETRY_READ:
5c6c0e51 318 scsi_read_data(&r->req);
5dba48a8
KW
319 break;
320 case SCSI_REQ_STATUS_RETRY_WRITE:
5c6c0e51 321 scsi_write_data(&r->req);
5dba48a8 322 break;
78ced65e
KW
323 case SCSI_REQ_STATUS_RETRY_FLUSH:
324 ret = scsi_disk_emulate_command(r, r->iov.iov_base);
325 if (ret == 0) {
a1f0cce2 326 scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
78ced65e 327 }
5dba48a8 328 }
ea8a5d7f 329 }
ea8a5d7f
AL
330 }
331}
332
213189ab
MA
333static void scsi_dma_restart_cb(void *opaque, int running, int reason)
334{
d52affa7 335 SCSIDiskState *s = opaque;
213189ab
MA
336
337 if (!running)
338 return;
339
340 if (!s->bh) {
341 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
342 qemu_bh_schedule(s->bh);
343 }
344}
345
a917d384 346/* Return a pointer to the data buffer. */
5c6c0e51 347static uint8_t *scsi_get_buf(SCSIRequest *req)
a917d384 348{
5c6c0e51 349 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
2e5d83bb 350
3f4cb3d3 351 return (uint8_t *)r->iov.iov_base;
2e5d83bb
PB
352}
353
74382217
HR
354/* Copy sense information into the provided buffer */
355static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
356{
357 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
358
359 return scsi_build_sense(s->sense, outbuf, len, len > 14);
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
1455084e 525 if (req->lun) {
5f71d32f 526 outbuf[0] = 0x7f; /* LUN not supported */
0b06c059
GH
527 return buflen;
528 }
529
f37bd73b
HR
530 outbuf[0] = s->qdev.type & 0x1f;
531 if (s->qdev.type == TYPE_ROM) {
0b06c059 532 outbuf[1] = 0x80;
550fe6c6 533 memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
0b06c059 534 } else {
419e691f 535 outbuf[1] = s->removable ? 0x80 : 0;
550fe6c6 536 memcpy(&outbuf[16], "QEMU HARDDISK ", 16);
0b06c059 537 }
550fe6c6 538 memcpy(&outbuf[8], "QEMU ", 8);
314b1811 539 memset(&outbuf[32], 0, 4);
552fee93 540 memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
99aba0c4
CH
541 /*
542 * We claim conformance to SPC-3, which is required for guests
543 * to ask for modern features like READ CAPACITY(16) or the
544 * block characteristics VPD page by default. Not all of SPC-3
545 * is actually implemented, but we're good enough.
546 */
ee3659e3 547 outbuf[2] = 5;
0b06c059 548 outbuf[3] = 2; /* Format 2 */
ad3cea42
AT
549
550 if (buflen > 36) {
551 outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
552 } else {
553 /* If the allocation length of CDB is too small,
554 the additional length is not adjusted */
555 outbuf[4] = 36 - 5;
556 }
557
0b06c059
GH
558 /* Sync data transfer and TCQ. */
559 outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
560 return buflen;
561}
562
282ab04e
BK
563static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p,
564 int page_control)
ebddfcbe
GH
565{
566 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
428c149b 567 BlockDriverState *bdrv = s->bs;
ebddfcbe
GH
568 int cylinders, heads, secs;
569
282ab04e
BK
570 /*
571 * If Changeable Values are requested, a mask denoting those mode parameters
572 * that are changeable shall be returned. As we currently don't support
573 * parameter changes via MODE_SELECT all bits are returned set to zero.
574 * The buffer was already menset to zero by the caller of this function.
575 */
ebddfcbe
GH
576 switch (page) {
577 case 4: /* Rigid disk device geometry page. */
578 p[0] = 4;
579 p[1] = 0x16;
282ab04e
BK
580 if (page_control == 1) { /* Changeable Values */
581 return p[1] + 2;
582 }
ebddfcbe
GH
583 /* if a geometry hint is available, use it */
584 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
585 p[2] = (cylinders >> 16) & 0xff;
586 p[3] = (cylinders >> 8) & 0xff;
587 p[4] = cylinders & 0xff;
588 p[5] = heads & 0xff;
589 /* Write precomp start cylinder, disabled */
590 p[6] = (cylinders >> 16) & 0xff;
591 p[7] = (cylinders >> 8) & 0xff;
592 p[8] = cylinders & 0xff;
593 /* Reduced current start cylinder, disabled */
594 p[9] = (cylinders >> 16) & 0xff;
595 p[10] = (cylinders >> 8) & 0xff;
596 p[11] = cylinders & 0xff;
597 /* Device step rate [ns], 200ns */
598 p[12] = 0;
599 p[13] = 200;
600 /* Landing zone cylinder */
601 p[14] = 0xff;
602 p[15] = 0xff;
603 p[16] = 0xff;
604 /* Medium rotation rate [rpm], 5400 rpm */
605 p[20] = (5400 >> 8) & 0xff;
606 p[21] = 5400 & 0xff;
282ab04e 607 return p[1] + 2;
ebddfcbe
GH
608
609 case 5: /* Flexible disk device geometry page. */
610 p[0] = 5;
611 p[1] = 0x1e;
282ab04e
BK
612 if (page_control == 1) { /* Changeable Values */
613 return p[1] + 2;
614 }
ebddfcbe
GH
615 /* Transfer rate [kbit/s], 5Mbit/s */
616 p[2] = 5000 >> 8;
617 p[3] = 5000 & 0xff;
618 /* if a geometry hint is available, use it */
619 bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
620 p[4] = heads & 0xff;
621 p[5] = secs & 0xff;
622 p[6] = s->cluster_size * 2;
623 p[8] = (cylinders >> 8) & 0xff;
624 p[9] = cylinders & 0xff;
625 /* Write precomp start cylinder, disabled */
626 p[10] = (cylinders >> 8) & 0xff;
627 p[11] = cylinders & 0xff;
628 /* Reduced current start cylinder, disabled */
629 p[12] = (cylinders >> 8) & 0xff;
630 p[13] = cylinders & 0xff;
631 /* Device step rate [100us], 100us */
632 p[14] = 0;
633 p[15] = 1;
634 /* Device step pulse width [us], 1us */
635 p[16] = 1;
636 /* Device head settle delay [100us], 100us */
637 p[17] = 0;
638 p[18] = 1;
639 /* Motor on delay [0.1s], 0.1s */
640 p[19] = 1;
641 /* Motor off delay [0.1s], 0.1s */
642 p[20] = 1;
643 /* Medium rotation rate [rpm], 5400 rpm */
644 p[28] = (5400 >> 8) & 0xff;
645 p[29] = 5400 & 0xff;
282ab04e 646 return p[1] + 2;
ebddfcbe
GH
647
648 case 8: /* Caching page. */
649 p[0] = 8;
650 p[1] = 0x12;
282ab04e
BK
651 if (page_control == 1) { /* Changeable Values */
652 return p[1] + 2;
653 }
428c149b 654 if (bdrv_enable_write_cache(s->bs)) {
ebddfcbe
GH
655 p[2] = 4; /* WCE */
656 }
282ab04e 657 return p[1] + 2;
ebddfcbe
GH
658
659 case 0x2a: /* CD Capabilities and Mechanical Status page. */
f37bd73b 660 if (s->qdev.type != TYPE_ROM)
ebddfcbe
GH
661 return 0;
662 p[0] = 0x2a;
663 p[1] = 0x14;
282ab04e
BK
664 if (page_control == 1) { /* Changeable Values */
665 return p[1] + 2;
666 }
ebddfcbe
GH
667 p[2] = 3; // CD-R & CD-RW read
668 p[3] = 0; // Writing not supported
669 p[4] = 0x7f; /* Audio, composite, digital out,
670 mode 2 form 1&2, multi session */
671 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
672 RW corrected, C2 errors, ISRC,
673 UPC, Bar code */
428c149b 674 p[6] = 0x2d | (bdrv_is_locked(s->bs)? 2 : 0);
ebddfcbe
GH
675 /* Locking supported, jumper present, eject, tray */
676 p[7] = 0; /* no volume & mute control, no
677 changer */
678 p[8] = (50 * 176) >> 8; // 50x read speed
679 p[9] = (50 * 176) & 0xff;
680 p[10] = 0 >> 8; // No volume
681 p[11] = 0 & 0xff;
682 p[12] = 2048 >> 8; // 2M buffer
683 p[13] = 2048 & 0xff;
684 p[14] = (16 * 176) >> 8; // 16x read speed current
685 p[15] = (16 * 176) & 0xff;
686 p[18] = (16 * 176) >> 8; // 16x write speed
687 p[19] = (16 * 176) & 0xff;
688 p[20] = (16 * 176) >> 8; // 16x write speed current
689 p[21] = (16 * 176) & 0xff;
282ab04e 690 return p[1] + 2;
ebddfcbe
GH
691
692 default:
693 return 0;
694 }
695}
696
697static int scsi_disk_emulate_mode_sense(SCSIRequest *req, uint8_t *outbuf)
698{
699 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
ebddfcbe 700 uint64_t nb_sectors;
282ab04e 701 int page, dbd, buflen, page_control;
ebddfcbe 702 uint8_t *p;
ce512ee1 703 uint8_t dev_specific_param;
ebddfcbe
GH
704
705 dbd = req->cmd.buf[1] & 0x8;
706 page = req->cmd.buf[2] & 0x3f;
282ab04e 707 page_control = (req->cmd.buf[2] & 0xc0) >> 6;
aa2b1e89
BK
708 DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
709 (req->cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, req->cmd.xfer, page_control);
ebddfcbe
GH
710 memset(outbuf, 0, req->cmd.xfer);
711 p = outbuf;
712
0056dcc1 713 if (bdrv_is_read_only(s->bs)) {
ce512ee1
BK
714 dev_specific_param = 0x80; /* Readonly. */
715 } else {
716 dev_specific_param = 0x00;
717 }
718
719 if (req->cmd.buf[0] == MODE_SENSE) {
720 p[1] = 0; /* Default media type. */
721 p[2] = dev_specific_param;
722 p[3] = 0; /* Block descriptor length. */
723 p += 4;
724 } else { /* MODE_SENSE_10 */
725 p[2] = 0; /* Default media type. */
726 p[3] = dev_specific_param;
727 p[6] = p[7] = 0; /* Block descriptor length. */
728 p += 8;
ebddfcbe 729 }
ebddfcbe 730
428c149b 731 bdrv_get_geometry(s->bs, &nb_sectors);
333d50fe 732 if (!dbd && nb_sectors) {
ce512ee1
BK
733 if (req->cmd.buf[0] == MODE_SENSE) {
734 outbuf[3] = 8; /* Block descriptor length */
735 } else { /* MODE_SENSE_10 */
736 outbuf[7] = 8; /* Block descriptor length */
737 }
ebddfcbe 738 nb_sectors /= s->cluster_size;
ebddfcbe 739 if (nb_sectors > 0xffffff)
2488b740 740 nb_sectors = 0;
ebddfcbe
GH
741 p[0] = 0; /* media density code */
742 p[1] = (nb_sectors >> 16) & 0xff;
743 p[2] = (nb_sectors >> 8) & 0xff;
744 p[3] = nb_sectors & 0xff;
745 p[4] = 0; /* reserved */
746 p[5] = 0; /* bytes 5-7 are the sector size in bytes */
747 p[6] = s->cluster_size * 2;
748 p[7] = 0;
749 p += 8;
750 }
751
282ab04e
BK
752 if (page_control == 3) { /* Saved Values */
753 return -1; /* ILLEGAL_REQUEST */
754 }
755
ebddfcbe
GH
756 switch (page) {
757 case 0x04:
758 case 0x05:
759 case 0x08:
760 case 0x2a:
282ab04e 761 p += mode_sense_page(req, page, p, page_control);
ebddfcbe
GH
762 break;
763 case 0x3f:
282ab04e
BK
764 p += mode_sense_page(req, 0x08, p, page_control);
765 p += mode_sense_page(req, 0x2a, p, page_control);
ebddfcbe 766 break;
a9c17b2b
BK
767 default:
768 return -1; /* ILLEGAL_REQUEST */
ebddfcbe
GH
769 }
770
771 buflen = p - outbuf;
ce512ee1
BK
772 /*
773 * The mode data length field specifies the length in bytes of the
774 * following data that is available to be transferred. The mode data
775 * length does not include itself.
776 */
777 if (req->cmd.buf[0] == MODE_SENSE) {
778 outbuf[0] = buflen - 1;
779 } else { /* MODE_SENSE_10 */
780 outbuf[0] = ((buflen - 2) >> 8) & 0xff;
781 outbuf[1] = (buflen - 2) & 0xff;
782 }
ebddfcbe
GH
783 if (buflen > req->cmd.xfer)
784 buflen = req->cmd.xfer;
785 return buflen;
786}
787
02880f43
GH
788static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
789{
790 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
02880f43
GH
791 int start_track, format, msf, toclen;
792 uint64_t nb_sectors;
793
794 msf = req->cmd.buf[1] & 2;
795 format = req->cmd.buf[2] & 0xf;
796 start_track = req->cmd.buf[6];
428c149b 797 bdrv_get_geometry(s->bs, &nb_sectors);
02880f43
GH
798 DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
799 nb_sectors /= s->cluster_size;
800 switch (format) {
801 case 0:
802 toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
803 break;
804 case 1:
805 /* multi session : only a single session defined */
806 toclen = 12;
807 memset(outbuf, 0, 12);
808 outbuf[1] = 0x0a;
809 outbuf[2] = 0x01;
810 outbuf[3] = 0x01;
811 break;
812 case 2:
813 toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
814 break;
815 default:
816 return -1;
817 }
818 if (toclen > req->cmd.xfer)
819 toclen = req->cmd.xfer;
820 return toclen;
821}
822
8af7a3ab 823static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
aa5dbdc1 824{
8af7a3ab 825 SCSIRequest *req = &r->req;
e7e25e32 826 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
e7e25e32 827 uint64_t nb_sectors;
aa5dbdc1 828 int buflen = 0;
78ced65e 829 int ret;
aa5dbdc1
GH
830
831 switch (req->cmd.buf[0]) {
832 case TEST_UNIT_READY:
428c149b 833 if (!bdrv_is_inserted(s->bs))
aa5dbdc1 834 goto not_ready;
5f71d32f 835 break;
51ad87c9
GH
836 case REQUEST_SENSE:
837 if (req->cmd.xfer < 4)
838 goto illegal_request;
a1f0cce2
HR
839 buflen = scsi_build_sense(s->sense, outbuf, req->cmd.xfer,
840 req->cmd.xfer > 13);
a6d96eb7 841 scsi_disk_clear_sense(s);
51ad87c9 842 break;
0b06c059
GH
843 case INQUIRY:
844 buflen = scsi_disk_emulate_inquiry(req, outbuf);
845 if (buflen < 0)
846 goto illegal_request;
5f71d32f 847 break;
ebddfcbe
GH
848 case MODE_SENSE:
849 case MODE_SENSE_10:
850 buflen = scsi_disk_emulate_mode_sense(req, outbuf);
851 if (buflen < 0)
852 goto illegal_request;
853 break;
02880f43
GH
854 case READ_TOC:
855 buflen = scsi_disk_emulate_read_toc(req, outbuf);
856 if (buflen < 0)
857 goto illegal_request;
858 break;
3d53ba18
GH
859 case RESERVE:
860 if (req->cmd.buf[1] & 1)
861 goto illegal_request;
862 break;
863 case RESERVE_10:
864 if (req->cmd.buf[1] & 3)
865 goto illegal_request;
866 break;
867 case RELEASE:
868 if (req->cmd.buf[1] & 1)
869 goto illegal_request;
870 break;
871 case RELEASE_10:
872 if (req->cmd.buf[1] & 3)
873 goto illegal_request;
874 break;
8d3628ff 875 case START_STOP:
f37bd73b 876 if (s->qdev.type == TYPE_ROM && (req->cmd.buf[4] & 2)) {
8d3628ff 877 /* load/eject medium */
428c149b 878 bdrv_eject(s->bs, !(req->cmd.buf[4] & 1));
8d3628ff 879 }
5f71d32f 880 break;
c68b9f34 881 case ALLOW_MEDIUM_REMOVAL:
428c149b 882 bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
5f71d32f 883 break;
5e30a07d 884 case READ_CAPACITY_10:
e7e25e32 885 /* The normal LEN field for this command is zero. */
5f71d32f
HR
886 memset(outbuf, 0, 8);
887 bdrv_get_geometry(s->bs, &nb_sectors);
e7e25e32
GH
888 if (!nb_sectors)
889 goto not_ready;
890 nb_sectors /= s->cluster_size;
891 /* Returned value is the address of the last sector. */
892 nb_sectors--;
893 /* Remember the new size for read/write sanity checking. */
894 s->max_lba = nb_sectors;
895 /* Clip to 2TB, instead of returning capacity modulo 2TB. */
896 if (nb_sectors > UINT32_MAX)
897 nb_sectors = UINT32_MAX;
898 outbuf[0] = (nb_sectors >> 24) & 0xff;
899 outbuf[1] = (nb_sectors >> 16) & 0xff;
900 outbuf[2] = (nb_sectors >> 8) & 0xff;
901 outbuf[3] = nb_sectors & 0xff;
902 outbuf[4] = 0;
903 outbuf[5] = 0;
904 outbuf[6] = s->cluster_size * 2;
905 outbuf[7] = 0;
906 buflen = 8;
5f71d32f 907 break;
fc903943 908 case SYNCHRONIZE_CACHE:
78ced65e
KW
909 ret = bdrv_flush(s->bs);
910 if (ret < 0) {
911 if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_FLUSH)) {
912 return -1;
913 }
914 }
fc903943 915 break;
38215553
GH
916 case GET_CONFIGURATION:
917 memset(outbuf, 0, 8);
918 /* ??? This should probably return much more information. For now
919 just return the basic header indicating the CD-ROM profile. */
920 outbuf[7] = 8; // CD-ROM
921 buflen = 8;
922 break;
5dd90e2a
GH
923 case SERVICE_ACTION_IN:
924 /* Service Action In subcommands. */
925 if ((req->cmd.buf[1] & 31) == 0x10) {
926 DPRINTF("SAI READ CAPACITY(16)\n");
927 memset(outbuf, 0, req->cmd.xfer);
428c149b 928 bdrv_get_geometry(s->bs, &nb_sectors);
5dd90e2a
GH
929 if (!nb_sectors)
930 goto not_ready;
931 nb_sectors /= s->cluster_size;
932 /* Returned value is the address of the last sector. */
933 nb_sectors--;
934 /* Remember the new size for read/write sanity checking. */
935 s->max_lba = nb_sectors;
936 outbuf[0] = (nb_sectors >> 56) & 0xff;
937 outbuf[1] = (nb_sectors >> 48) & 0xff;
938 outbuf[2] = (nb_sectors >> 40) & 0xff;
939 outbuf[3] = (nb_sectors >> 32) & 0xff;
940 outbuf[4] = (nb_sectors >> 24) & 0xff;
941 outbuf[5] = (nb_sectors >> 16) & 0xff;
942 outbuf[6] = (nb_sectors >> 8) & 0xff;
943 outbuf[7] = nb_sectors & 0xff;
944 outbuf[8] = 0;
945 outbuf[9] = 0;
946 outbuf[10] = s->cluster_size * 2;
947 outbuf[11] = 0;
ee3659e3
CH
948 outbuf[12] = 0;
949 outbuf[13] = get_physical_block_exp(&s->qdev.conf);
ea3bd56f
CH
950
951 /* set TPE bit if the format supports discard */
952 if (s->qdev.conf.discard_granularity) {
953 outbuf[14] = 0x80;
954 }
955
5dd90e2a
GH
956 /* Protection, exponent and lowest lba field left blank. */
957 buflen = req->cmd.xfer;
958 break;
959 }
960 DPRINTF("Unsupported Service Action In\n");
961 goto illegal_request;
39ec9a50
GH
962 case REPORT_LUNS:
963 if (req->cmd.xfer < 16)
964 goto illegal_request;
965 memset(outbuf, 0, 16);
966 outbuf[3] = 8;
967 buflen = 16;
968 break;
5e30a07d 969 case VERIFY_10:
88f8a5ed 970 break;
aa5dbdc1 971 default:
a1f0cce2
HR
972 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
973 return -1;
aa5dbdc1 974 }
a1f0cce2 975 scsi_req_set_status(r, GOOD, SENSE_CODE(NO_SENSE));
aa5dbdc1
GH
976 return buflen;
977
978not_ready:
a1f0cce2
HR
979 if (!bdrv_is_inserted(s->bs)) {
980 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(NO_MEDIUM));
981 } else {
982 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(LUN_NOT_READY));
983 }
8af7a3ab 984 return -1;
aa5dbdc1
GH
985
986illegal_request:
a1f0cce2 987 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_FIELD));
8af7a3ab 988 return -1;
aa5dbdc1
GH
989}
990
2e5d83bb
PB
991/* Execute a scsi command. Returns the length of the data expected by the
992 command. This will be Positive for data transfers from the device
993 (eg. disk reads), negative for transfers to the device (eg. disk writes),
994 and zero if the command does not transfer any data. */
995
5c6c0e51 996static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
2e5d83bb 997{
5c6c0e51
HR
998 SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
999 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
ad2d30f7 1000 int32_t len;
a917d384
PB
1001 uint8_t command;
1002 uint8_t *outbuf;
aa5dbdc1 1003 int rc;
a917d384
PB
1004
1005 command = buf[0];
3f4cb3d3 1006 outbuf = (uint8_t *)r->iov.iov_base;
653c1c3f 1007 DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
2dd791b6
HR
1008
1009 if (scsi_req_parse(&r->req, buf) != 0) {
a917d384 1010 BADF("Unsupported command length, command %x\n", command);
a1f0cce2
HR
1011 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
1012 return 0;
2e5d83bb
PB
1013 }
1014#ifdef DEBUG_SCSI
1015 {
1016 int i;
2dd791b6 1017 for (i = 1; i < r->req.cmd.len; i++) {
2e5d83bb
PB
1018 printf(" 0x%02x", buf[i]);
1019 }
1020 printf("\n");
1021 }
1022#endif
aa5dbdc1 1023
1455084e 1024 if (req->lun) {
2e5d83bb 1025 /* Only LUN 0 supported. */
1455084e 1026 DPRINTF("Unimplemented LUN %d\n", req->lun);
a1f0cce2
HR
1027 if (command != REQUEST_SENSE && command != INQUIRY) {
1028 scsi_command_complete(r, CHECK_CONDITION,
1029 SENSE_CODE(LUN_NOT_SUPPORTED));
1030 return 0;
1031 }
2e5d83bb 1032 }
a917d384 1033 switch (command) {
ebf46023 1034 case TEST_UNIT_READY:
51ad87c9 1035 case REQUEST_SENSE:
0b06c059 1036 case INQUIRY:
ebddfcbe
GH
1037 case MODE_SENSE:
1038 case MODE_SENSE_10:
3d53ba18
GH
1039 case RESERVE:
1040 case RESERVE_10:
1041 case RELEASE:
1042 case RELEASE_10:
8d3628ff 1043 case START_STOP:
c68b9f34 1044 case ALLOW_MEDIUM_REMOVAL:
5e30a07d 1045 case READ_CAPACITY_10:
fc903943 1046 case SYNCHRONIZE_CACHE:
02880f43 1047 case READ_TOC:
38215553 1048 case GET_CONFIGURATION:
5dd90e2a 1049 case SERVICE_ACTION_IN:
39ec9a50 1050 case REPORT_LUNS:
5e30a07d 1051 case VERIFY_10:
8af7a3ab
KW
1052 rc = scsi_disk_emulate_command(r, outbuf);
1053 if (rc < 0) {
0b06c059 1054 return 0;
aa5dbdc1 1055 }
8af7a3ab
KW
1056
1057 r->iov.iov_len = rc;
0b06c059 1058 break;
ebf46023
GH
1059 case READ_6:
1060 case READ_10:
bd536cf3
GH
1061 case READ_12:
1062 case READ_16:
5c6c0e51 1063 len = r->req.cmd.xfer / s->qdev.blocksize;
2dd791b6
HR
1064 DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1065 if (r->req.cmd.lba > s->max_lba)
274fb0e1 1066 goto illegal_lba;
2dd791b6 1067 r->sector = r->req.cmd.lba * s->cluster_size;
a917d384 1068 r->sector_count = len * s->cluster_size;
2e5d83bb 1069 break;
ebf46023
GH
1070 case WRITE_6:
1071 case WRITE_10:
bd536cf3
GH
1072 case WRITE_12:
1073 case WRITE_16:
5e30a07d 1074 case WRITE_VERIFY_10:
ebef0bbb
BK
1075 case WRITE_VERIFY_12:
1076 case WRITE_VERIFY_16:
5c6c0e51 1077 len = r->req.cmd.xfer / s->qdev.blocksize;
ebef0bbb 1078 DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
2dd791b6
HR
1079 (command & 0xe) == 0xe ? "And Verify " : "",
1080 r->req.cmd.lba, len);
1081 if (r->req.cmd.lba > s->max_lba)
274fb0e1 1082 goto illegal_lba;
2dd791b6 1083 r->sector = r->req.cmd.lba * s->cluster_size;
a917d384 1084 r->sector_count = len * s->cluster_size;
2e5d83bb 1085 break;
ebef0bbb 1086 case MODE_SELECT:
2dd791b6 1087 DPRINTF("Mode Select(6) (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 > 12) {
ebef0bbb
BK
1091 goto fail;
1092 }
1093 break;
1094 case MODE_SELECT_10:
2dd791b6 1095 DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
ebef0bbb
BK
1096 /* We don't support mode parameter changes.
1097 Allow the mode parameter header + block descriptors only. */
2dd791b6 1098 if (r->req.cmd.xfer > 16) {
ebef0bbb
BK
1099 goto fail;
1100 }
1101 break;
1102 case SEEK_6:
1103 case SEEK_10:
2dd791b6
HR
1104 DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
1105 r->req.cmd.lba);
1106 if (r->req.cmd.lba > s->max_lba) {
ebef0bbb
BK
1107 goto illegal_lba;
1108 }
ea3bd56f
CH
1109 break;
1110 case WRITE_SAME_16:
5c6c0e51 1111 len = r->req.cmd.xfer / s->qdev.blocksize;
ea3bd56f
CH
1112
1113 DPRINTF("WRITE SAME(16) (sector %" PRId64 ", count %d)\n",
1114 r->req.cmd.lba, len);
1115
1116 if (r->req.cmd.lba > s->max_lba) {
1117 goto illegal_lba;
1118 }
1119
1120 /*
1121 * We only support WRITE SAME with the unmap bit set for now.
1122 */
1123 if (!(buf[1] & 0x8)) {
1124 goto fail;
1125 }
1126
1127 rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1128 len * s->cluster_size);
1129 if (rc < 0) {
1130 /* XXX: better error code ?*/
1131 goto fail;
1132 }
1133
ebef0bbb 1134 break;
2e5d83bb 1135 default:
2dd791b6 1136 DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
a1f0cce2
HR
1137 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
1138 return 0;
2e5d83bb 1139 fail:
a1f0cce2 1140 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_FIELD));
2dd791b6 1141 return 0;
274fb0e1 1142 illegal_lba:
a1f0cce2 1143 scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(LBA_OUT_OF_RANGE));
274fb0e1 1144 return 0;
2e5d83bb 1145 }
c87c0672 1146 if (r->sector_count == 0 && r->iov.iov_len == 0) {
a1f0cce2 1147 scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
a917d384 1148 }
c87c0672 1149 len = r->sector_count * 512 + r->iov.iov_len;
efb9ee02
HR
1150 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1151 return -len;
a917d384
PB
1152 } else {
1153 if (!r->sector_count)
1154 r->sector_count = -1;
efb9ee02 1155 return len;
2e5d83bb 1156 }
2e5d83bb
PB
1157}
1158
e9447f35
JK
1159static void scsi_disk_reset(DeviceState *dev)
1160{
1161 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1162 uint64_t nb_sectors;
1163
c557e889 1164 scsi_device_purge_requests(&s->qdev);
e9447f35
JK
1165
1166 bdrv_get_geometry(s->bs, &nb_sectors);
1167 nb_sectors /= s->cluster_size;
1168 if (nb_sectors) {
1169 nb_sectors--;
1170 }
1171 s->max_lba = nb_sectors;
1172}
1173
1174static void scsi_destroy(SCSIDevice *dev)
1175{
1176 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1177
c557e889 1178 scsi_device_purge_requests(&s->qdev);
f8b6cc00 1179 blockdev_mark_auto_del(s->qdev.conf.bs);
56a14938
GH
1180}
1181
f37bd73b 1182static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
2e5d83bb 1183{
d52affa7 1184 SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
f8b6cc00 1185 DriveInfo *dinfo;
2e5d83bb 1186
f8b6cc00 1187 if (!s->qdev.conf.bs) {
1ecda02b 1188 error_report("scsi-disk: drive property not set");
d52affa7
GH
1189 return -1;
1190 }
f8b6cc00 1191 s->bs = s->qdev.conf.bs;
d52affa7 1192
f37bd73b 1193 if (scsi_type == TYPE_DISK && !bdrv_is_inserted(s->bs)) {
98f28ad7
MA
1194 error_report("Device needs media, but drive is empty");
1195 return -1;
1196 }
1197
a0fef654 1198 if (!s->serial) {
f8b6cc00
MA
1199 /* try to fall back to value set with legacy -drive serial=... */
1200 dinfo = drive_get_by_blockdev(s->bs);
3e1c0c9a
HR
1201 if (*dinfo->serial) {
1202 s->serial = qemu_strdup(dinfo->serial);
1203 }
a0fef654
MA
1204 }
1205
552fee93
MA
1206 if (!s->version) {
1207 s->version = qemu_strdup(QEMU_VERSION);
1208 }
1209
32bb404a 1210 if (bdrv_is_sg(s->bs)) {
1ecda02b 1211 error_report("scsi-disk: unwanted /dev/sg*");
32bb404a
MA
1212 return -1;
1213 }
1214
f37bd73b 1215 if (scsi_type == TYPE_ROM) {
8cfacf07 1216 s->qdev.blocksize = 2048;
f37bd73b 1217 } else if (scsi_type == TYPE_DISK) {
8cfacf07 1218 s->qdev.blocksize = s->qdev.conf.logical_block_size;
f37bd73b
HR
1219 } else {
1220 error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
1221 return -1;
2e5d83bb 1222 }
8cfacf07 1223 s->cluster_size = s->qdev.blocksize / 512;
73fdb1e1 1224 s->bs->buffer_alignment = s->qdev.blocksize;
8cfacf07 1225
f37bd73b 1226 s->qdev.type = scsi_type;
ea8a5d7f 1227 qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
f37bd73b 1228 bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
1ca4d09a 1229 add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
d52affa7
GH
1230 return 0;
1231}
1232
b443ae67
MA
1233static int scsi_hd_initfn(SCSIDevice *dev)
1234{
f37bd73b 1235 return scsi_initfn(dev, TYPE_DISK);
b443ae67
MA
1236}
1237
1238static int scsi_cd_initfn(SCSIDevice *dev)
1239{
f37bd73b 1240 return scsi_initfn(dev, TYPE_ROM);
b443ae67
MA
1241}
1242
1243static int scsi_disk_initfn(SCSIDevice *dev)
1244{
95b5edcd 1245 DriveInfo *dinfo;
f37bd73b 1246 uint8_t scsi_type;
b443ae67
MA
1247
1248 if (!dev->conf.bs) {
f37bd73b 1249 scsi_type = TYPE_DISK; /* will die in scsi_initfn() */
b443ae67 1250 } else {
95b5edcd 1251 dinfo = drive_get_by_blockdev(dev->conf.bs);
f37bd73b 1252 scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
b443ae67
MA
1253 }
1254
f37bd73b 1255 return scsi_initfn(dev, scsi_type);
b443ae67
MA
1256}
1257
1258#define DEFINE_SCSI_DISK_PROPERTIES() \
1259 DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf), \
1260 DEFINE_PROP_STRING("ver", SCSIDiskState, version), \
1261 DEFINE_PROP_STRING("serial", SCSIDiskState, serial)
1262
1263static SCSIDeviceInfo scsi_disk_info[] = {
1264 {
1265 .qdev.name = "scsi-hd",
1266 .qdev.fw_name = "disk",
1267 .qdev.desc = "virtual SCSI disk",
1268 .qdev.size = sizeof(SCSIDiskState),
1269 .qdev.reset = scsi_disk_reset,
1270 .init = scsi_hd_initfn,
1271 .destroy = scsi_destroy,
5c6c0e51 1272 .alloc_req = scsi_new_request,
ad2d30f7 1273 .free_req = scsi_free_request,
b443ae67
MA
1274 .send_command = scsi_send_command,
1275 .read_data = scsi_read_data,
1276 .write_data = scsi_write_data,
1277 .cancel_io = scsi_cancel_io,
1278 .get_buf = scsi_get_buf,
74382217 1279 .get_sense = scsi_get_sense,
b443ae67
MA
1280 .qdev.props = (Property[]) {
1281 DEFINE_SCSI_DISK_PROPERTIES(),
1282 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1283 DEFINE_PROP_END_OF_LIST(),
1284 }
1285 },{
1286 .qdev.name = "scsi-cd",
1287 .qdev.fw_name = "disk",
1288 .qdev.desc = "virtual SCSI CD-ROM",
1289 .qdev.size = sizeof(SCSIDiskState),
1290 .qdev.reset = scsi_disk_reset,
1291 .init = scsi_cd_initfn,
1292 .destroy = scsi_destroy,
5c6c0e51 1293 .alloc_req = scsi_new_request,
ad2d30f7 1294 .free_req = scsi_free_request,
b443ae67
MA
1295 .send_command = scsi_send_command,
1296 .read_data = scsi_read_data,
1297 .write_data = scsi_write_data,
1298 .cancel_io = scsi_cancel_io,
1299 .get_buf = scsi_get_buf,
74382217 1300 .get_sense = scsi_get_sense,
b443ae67
MA
1301 .qdev.props = (Property[]) {
1302 DEFINE_SCSI_DISK_PROPERTIES(),
1303 DEFINE_PROP_END_OF_LIST(),
1304 },
1305 },{
1306 .qdev.name = "scsi-disk", /* legacy -device scsi-disk */
1307 .qdev.fw_name = "disk",
1308 .qdev.desc = "virtual SCSI disk or CD-ROM (legacy)",
1309 .qdev.size = sizeof(SCSIDiskState),
1310 .qdev.reset = scsi_disk_reset,
1311 .init = scsi_disk_initfn,
1312 .destroy = scsi_destroy,
5c6c0e51 1313 .alloc_req = scsi_new_request,
ad2d30f7 1314 .free_req = scsi_free_request,
b443ae67
MA
1315 .send_command = scsi_send_command,
1316 .read_data = scsi_read_data,
1317 .write_data = scsi_write_data,
1318 .cancel_io = scsi_cancel_io,
1319 .get_buf = scsi_get_buf,
74382217 1320 .get_sense = scsi_get_sense,
b443ae67
MA
1321 .qdev.props = (Property[]) {
1322 DEFINE_SCSI_DISK_PROPERTIES(),
1323 DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1324 DEFINE_PROP_END_OF_LIST(),
1325 }
1326 }
d52affa7
GH
1327};
1328
1329static void scsi_disk_register_devices(void)
1330{
b443ae67
MA
1331 int i;
1332
1333 for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1334 scsi_qdev_register(&scsi_disk_info[i]);
1335 }
8ccc2ace 1336}
d52affa7 1337device_init(scsi_disk_register_devices)