34
36
#define SENSE_HARDWARE_ERROR 4
35
37
#define SENSE_ILLEGAL_REQUEST 5
37
#define SCSI_DMA_BUF_SIZE 65536
40
#define STATUS_CHECK_CONDITION 2
42
#define SCSI_DMA_BUF_SIZE 131072
43
#define SCSI_MAX_INQUIRY_LEN 256
45
#define SCSI_REQ_STATUS_RETRY 0x01
39
47
typedef struct SCSIRequest {
40
48
SCSIDeviceState *dev;
42
/* ??? We should probably keep track of whether the data trasfer is
50
/* ??? We should probably keep track of whether the data transfer is
43
51
a read or a write. Currently we rely on the host getting it right. */
44
52
/* Both sector and sector_count are in terms of qemu 512 byte blocks. */
54
uint32_t sector_count;
47
55
/* The amounnt of data in the buffer. */
50
58
BlockDriverAIOCB *aiocb;
51
59
struct SCSIRequest *next;
54
63
struct SCSIDeviceState
126
138
/* Helper function for command completion. */
127
static void scsi_command_complete(SCSIRequest *r, int sense)
139
static void scsi_command_complete(SCSIRequest *r, int status, int sense)
129
141
SCSIDeviceState *s = r->dev;
131
DPRINTF("Command complete tag=0x%x sense=%d\n", r->tag, sense);
143
DPRINTF("Command complete tag=0x%x status=%d sense=%d\n", r->tag, status, sense);
132
144
s->sense = sense;
134
146
scsi_remove_request(r);
135
s->completion(s->opaque, SCSI_REASON_DONE, tag, sense);
147
s->completion(s->opaque, SCSI_REASON_DONE, tag, status);
138
150
/* Cancel a pending data transfer. */
199
212
r->aiocb = bdrv_aio_read(s->bdrv, r->sector, r->dma_buf, n,
200
213
scsi_read_complete, r);
201
214
if (r->aiocb == NULL)
202
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
215
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_HARDWARE_ERROR);
204
217
r->sector_count -= n;
220
static int scsi_handle_write_error(SCSIRequest *r, int error)
222
BlockInterfaceErrorAction action = drive_get_onerror(r->dev->bdrv);
224
if (action == BLOCK_ERR_IGNORE)
227
if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
228
|| action == BLOCK_ERR_STOP_ANY) {
229
r->status |= SCSI_REQ_STATUS_RETRY;
232
scsi_command_complete(r, STATUS_CHECK_CONDITION,
233
SENSE_HARDWARE_ERROR);
207
239
static void scsi_write_complete(void * opaque, int ret)
209
241
SCSIRequest *r = (SCSIRequest *)opaque;
210
242
SCSIDeviceState *s = r->dev;
214
fprintf(stderr, "scsi-disc: IO write error\n");
249
if (scsi_handle_write_error(r, -ret))
253
n = r->buf_len / 512;
255
r->sector_count -= n;
219
256
if (r->sector_count == 0) {
220
scsi_command_complete(r, SENSE_NO_SENSE);
257
scsi_command_complete(r, STATUS_GOOD, SENSE_NO_SENSE);
222
259
len = r->sector_count * 512;
223
260
if (len > SCSI_DMA_BUF_SIZE) {
269
static void scsi_write_request(SCSIRequest *r)
271
SCSIDeviceState *s = r->dev;
274
n = r->buf_len / 512;
276
r->aiocb = bdrv_aio_write(s->bdrv, r->sector, r->dma_buf, n,
277
scsi_write_complete, r);
278
if (r->aiocb == NULL)
279
scsi_command_complete(r, STATUS_CHECK_CONDITION,
280
SENSE_HARDWARE_ERROR);
282
/* Invoke completion routine to fetch data from host. */
283
scsi_write_complete(r, 0);
232
287
/* Write data to a scsi device. Returns nonzero on failure.
233
288
The transfer may complete asynchronously. */
234
289
static int scsi_write_data(SCSIDevice *d, uint32_t tag)
236
291
SCSIDeviceState *s = d->state;
240
294
DPRINTF("Write data tag=0x%x\n", tag);
241
295
r = scsi_find_request(s, tag);
243
297
BADF("Bad write tag 0x%x\n", tag);
244
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
298
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_HARDWARE_ERROR);
248
303
BADF("Data transfer already in progress\n");
249
n = r->buf_len / 512;
251
r->aiocb = bdrv_aio_write(s->bdrv, r->sector, r->dma_buf, n,
252
scsi_write_complete, r);
253
if (r->aiocb == NULL)
254
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
256
r->sector_count -= n;
258
/* Invoke completion routine to fetch data from host. */
259
scsi_write_complete(r, 0);
305
scsi_write_request(r);
310
static void scsi_dma_restart_cb(void *opaque, int running, int reason)
312
SCSIDeviceState *s = opaque;
313
SCSIRequest *r = s->requests;
318
if (r->status & SCSI_REQ_STATUS_RETRY) {
319
r->status &= ~SCSI_REQ_STATUS_RETRY;
320
scsi_write_request(r);
265
326
/* Return a pointer to the data buffer. */
266
327
static uint8_t *scsi_get_buf(SCSIDevice *d, uint32_t tag)
308
369
DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
309
370
switch (command >> 5) {
311
lba = buf[3] | (buf[2] << 8) | ((buf[1] & 0x1f) << 16);
372
lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
373
(((uint64_t) buf[1] & 0x1f) << 16);
317
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
379
lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
380
((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
318
381
len = buf[8] | (buf[7] << 8);
322
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
385
lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
386
((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
387
((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
388
((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
323
389
len = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24);
327
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
393
lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
394
((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
328
395
len = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24);
364
432
DPRINTF("Inquiry (len %d)\n", len);
366
BADF("Inquiry buffer too small (%d)\n", len);
368
memset(outbuf, 0, 36);
369
if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
434
/* Command support data - optional, not implemented */
435
BADF("optional INQUIRY command support request not implemented\n");
438
else if (buf[1] & 0x1) {
439
/* Vital product data */
440
uint8_t page_code = buf[2];
442
BADF("Error: Inquiry (EVPD[%02X]) buffer size %d is "
443
"less than 4\n", page_code, len);
450
/* Supported page codes, mandatory */
451
DPRINTF("Inquiry EVPD[Supported pages] "
452
"buffer size %d\n", len);
456
if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
457
outbuf[r->buf_len++] = 5;
459
outbuf[r->buf_len++] = 0;
462
outbuf[r->buf_len++] = 0x00; // this page
463
outbuf[r->buf_len++] = 0x00;
464
outbuf[r->buf_len++] = 3; // number of pages
465
outbuf[r->buf_len++] = 0x00; // list of supported pages (this page)
466
outbuf[r->buf_len++] = 0x80; // unit serial number
467
outbuf[r->buf_len++] = 0x83; // device identification
474
/* Device serial number, optional */
476
BADF("Error: EVPD[Serial number] Inquiry buffer "
477
"size %d too small, %d needed\n", len, 4);
481
DPRINTF("Inquiry EVPD[Serial number] buffer size %d\n", len);
482
l = MIN(len, strlen(s->drive_serial_str));
486
/* Supported page codes */
487
if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
488
outbuf[r->buf_len++] = 5;
490
outbuf[r->buf_len++] = 0;
493
outbuf[r->buf_len++] = 0x80; // this page
494
outbuf[r->buf_len++] = 0x00;
495
outbuf[r->buf_len++] = l;
496
memcpy(&outbuf[r->buf_len], s->drive_serial_str, l);
503
/* Device identification page, mandatory */
504
int max_len = 255 - 8;
505
int id_len = strlen(bdrv_get_device_name(s->bdrv));
506
if (id_len > max_len)
509
DPRINTF("Inquiry EVPD[Device identification] "
510
"buffer size %d\n", len);
512
if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
513
outbuf[r->buf_len++] = 5;
515
outbuf[r->buf_len++] = 0;
518
outbuf[r->buf_len++] = 0x83; // this page
519
outbuf[r->buf_len++] = 0x00;
520
outbuf[r->buf_len++] = 3 + id_len;
522
outbuf[r->buf_len++] = 0x2; // ASCII
523
outbuf[r->buf_len++] = 0; // not officially assigned
524
outbuf[r->buf_len++] = 0; // reserved
525
outbuf[r->buf_len++] = id_len; // length of data following
527
memcpy(&outbuf[r->buf_len],
528
bdrv_get_device_name(s->bdrv), id_len);
529
r->buf_len += id_len;
533
BADF("Error: unsupported Inquiry (EVPD[%02X]) "
534
"buffer size %d\n", page_code, len);
541
/* Standard INQUIRY data */
543
BADF("Error: Inquiry (STANDARD) page or code "
544
"is non-zero [%02X]\n", buf[2]);
550
BADF("Error: Inquiry (STANDARD) buffer size %d "
551
"is less than 5\n", len);
556
BADF("Error: Inquiry (STANDARD) buffer size %d "
557
"is less than 36 (TODO: only 5 required)\n", len);
561
if(len > SCSI_MAX_INQUIRY_LEN)
562
len = SCSI_MAX_INQUIRY_LEN;
564
memset(outbuf, 0, len);
566
if (lun || buf[1] >> 5) {
567
outbuf[0] = 0x7f; /* LUN not supported */
568
} else if (bdrv_get_type_hint(s->bdrv) == BDRV_TYPE_CDROM) {
371
570
outbuf[1] = 0x80;
372
571
memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
411
610
outbuf[2] = 0x80; /* Readonly. */
414
if ((page == 8 || page == 0x3f)) {
614
int cylinders, heads, secs;
616
/* Rigid disk device geometry page. */
619
/* if a geometry hint is available, use it */
620
bdrv_get_geometry_hint(s->bdrv, &cylinders, &heads, &secs);
621
p[2] = (cylinders >> 16) & 0xff;
622
p[3] = (cylinders >> 8) & 0xff;
623
p[4] = cylinders & 0xff;
625
/* Write precomp start cylinder, disabled */
626
p[6] = (cylinders >> 16) & 0xff;
627
p[7] = (cylinders >> 8) & 0xff;
628
p[8] = cylinders & 0xff;
629
/* Reduced current start cylinder, disabled */
630
p[9] = (cylinders >> 16) & 0xff;
631
p[10] = (cylinders >> 8) & 0xff;
632
p[11] = cylinders & 0xff;
633
/* Device step rate [ns], 200ns */
636
/* Landing zone cylinder */
640
/* Medium rotation rate [rpm], 5400 rpm */
641
p[20] = (5400 >> 8) & 0xff;
644
} else if (page == 5) {
645
int cylinders, heads, secs;
647
/* Flexible disk device geometry page. */
650
/* Transfer rate [kbit/s], 5Mbit/s */
653
/* if a geometry hint is available, use it */
654
bdrv_get_geometry_hint(s->bdrv, &cylinders, &heads, &secs);
657
p[6] = s->cluster_size * 2;
658
p[8] = (cylinders >> 8) & 0xff;
659
p[9] = cylinders & 0xff;
660
/* Write precomp start cylinder, disabled */
661
p[10] = (cylinders >> 8) & 0xff;
662
p[11] = cylinders & 0xff;
663
/* Reduced current start cylinder, disabled */
664
p[12] = (cylinders >> 8) & 0xff;
665
p[13] = cylinders & 0xff;
666
/* Device step rate [100us], 100us */
669
/* Device step pulse width [us], 1us */
671
/* Device head settle delay [100us], 100us */
674
/* Motor on delay [0.1s], 0.1s */
676
/* Motor off delay [0.1s], 0.1s */
678
/* Medium rotation rate [rpm], 5400 rpm */
679
p[28] = (5400 >> 8) & 0xff;
682
} else if ((page == 8 || page == 0x3f)) {
415
683
/* Caching page. */
483
scsi_command_complete(r, SENSE_NOT_READY);
757
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_NOT_READY);
489
DPRINTF("Read (sector %d, count %d)\n", lba, len);
764
DPRINTF("Read (sector %lld, count %d)\n", lba, len);
765
if (lba > s->max_lba)
490
767
r->sector = lba * s->cluster_size;
491
768
r->sector_count = len * s->cluster_size;
495
DPRINTF("Write (sector %d, count %d)\n", lba, len);
773
DPRINTF("Write (sector %lld, count %d)\n", lba, len);
774
if (lba > s->max_lba)
496
776
r->sector = lba * s->cluster_size;
497
777
r->sector_count = len * s->cluster_size;
840
/* Service Action In subcommands. */
841
if ((buf[1] & 31) == 0x10) {
842
DPRINTF("SAI READ CAPACITY(16)\n");
843
memset(outbuf, 0, len);
844
bdrv_get_geometry(s->bdrv, &nb_sectors);
845
nb_sectors /= s->cluster_size;
846
/* Returned value is the address of the last sector. */
849
/* Remember the new size for read/write sanity checking. */
850
s->max_lba = nb_sectors;
851
outbuf[0] = (nb_sectors >> 56) & 0xff;
852
outbuf[1] = (nb_sectors >> 48) & 0xff;
853
outbuf[2] = (nb_sectors >> 40) & 0xff;
854
outbuf[3] = (nb_sectors >> 32) & 0xff;
855
outbuf[4] = (nb_sectors >> 24) & 0xff;
856
outbuf[5] = (nb_sectors >> 16) & 0xff;
857
outbuf[6] = (nb_sectors >> 8) & 0xff;
858
outbuf[7] = nb_sectors & 0xff;
861
outbuf[10] = s->cluster_size * 2;
863
/* Protection, exponent and lowest lba field left blank. */
866
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_NOT_READY);
871
DPRINTF("Unsupported Service Action In\n");
560
874
DPRINTF("Report LUNs (len %d)\n", len);
882
DPRINTF("Verify (sector %d, count %d)\n", lba, len);
568
885
DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
570
scsi_command_complete(r, SENSE_ILLEGAL_REQUEST);
887
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_ILLEGAL_REQUEST);
890
scsi_command_complete(r, STATUS_CHECK_CONDITION, SENSE_HARDWARE_ERROR);
573
893
if (r->sector_count == 0 && r->buf_len == 0) {
574
scsi_command_complete(r, SENSE_NO_SENSE);
894
scsi_command_complete(r, STATUS_GOOD, SENSE_NO_SENSE);
576
896
len = r->sector_count * 512 + r->buf_len;