Statistics
| Branch: | Revision:

root / hw / scsi-disk.c @ c557e889

History | View | Annotate | Download (39.4 kB)

1
/*
2
 * SCSI Device emulation
3
 *
4
 * Copyright (c) 2006 CodeSourcery.
5
 * Based on code by Fabrice Bellard
6
 *
7
 * Written by Paul Brook
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.
14
 *
15
 * This code is licenced under the LGPL.
16
 *
17
 * Note that this file only handles the SCSI architecture model and device
18
 * commands.  Emulation of interface/link layer protocols is handled by
19
 * the host adapter emulator.
20
 */
21

    
22
//#define DEBUG_SCSI
23

    
24
#ifdef DEBUG_SCSI
25
#define DPRINTF(fmt, ...) \
26
do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
27
#else
28
#define DPRINTF(fmt, ...) do {} while(0)
29
#endif
30

    
31
#define BADF(fmt, ...) \
32
do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
33

    
34
#include "qemu-common.h"
35
#include "qemu-error.h"
36
#include "scsi.h"
37
#include "scsi-defs.h"
38
#include "sysemu.h"
39
#include "blockdev.h"
40

    
41
#define SCSI_DMA_BUF_SIZE    131072
42
#define SCSI_MAX_INQUIRY_LEN 256
43

    
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
48
#define SCSI_REQ_STATUS_RETRY_FLUSH     0x04
49

    
50
typedef struct SCSIDiskState SCSIDiskState;
51

    
52
typedef struct SCSISense {
53
    uint8_t key;
54
} SCSISense;
55

    
56
typedef struct SCSIDiskReq {
57
    SCSIRequest req;
58
    /* ??? We should probably keep track of whether the data transfer is
59
       a read or a write.  Currently we rely on the host getting it right.  */
60
    /* Both sector and sector_count are in terms of qemu 512 byte blocks.  */
61
    uint64_t sector;
62
    uint32_t sector_count;
63
    struct iovec iov;
64
    QEMUIOVector qiov;
65
    uint32_t status;
66
} SCSIDiskReq;
67

    
68
typedef enum { SCSI_HD, SCSI_CD } SCSIDriveKind;
69

    
70
struct SCSIDiskState
71
{
72
    SCSIDevice qdev;
73
    BlockDriverState *bs;
74
    /* The qemu block layer uses a fixed 512 byte sector size.
75
       This is the number of 512 byte blocks in a single scsi sector.  */
76
    int cluster_size;
77
    uint32_t removable;
78
    uint64_t max_lba;
79
    QEMUBH *bh;
80
    char *version;
81
    char *serial;
82
    SCSISense sense;
83
    SCSIDriveKind drive_kind;
84
};
85

    
86
static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
87
static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf);
88

    
89
static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
90
        uint32_t lun)
91
{
92
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
93
    SCSIRequest *req;
94
    SCSIDiskReq *r;
95

    
96
    req = scsi_req_alloc(sizeof(SCSIDiskReq), &s->qdev, tag, lun);
97
    r = DO_UPCAST(SCSIDiskReq, req, req);
98
    r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
99
    return req;
100
}
101

    
102
static void scsi_free_request(SCSIRequest *req)
103
{
104
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
105

    
106
    qemu_vfree(r->iov.iov_base);
107
}
108

    
109
static void scsi_disk_clear_sense(SCSIDiskState *s)
110
{
111
    memset(&s->sense, 0, sizeof(s->sense));
112
}
113

    
114
static void scsi_disk_set_sense(SCSIDiskState *s, uint8_t key)
115
{
116
    s->sense.key = key;
117
}
118

    
119
static void scsi_req_set_status(SCSIDiskReq *r, int status, int sense_code)
120
{
121
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
122

    
123
    r->req.status = status;
124
    scsi_disk_set_sense(s, sense_code);
125
}
126

    
127
/* Helper function for command completion.  */
128
static void scsi_command_complete(SCSIDiskReq *r, int status, int sense)
129
{
130
    DPRINTF("Command complete tag=0x%x status=%d sense=%d\n",
131
            r->req.tag, status, sense);
132
    scsi_req_set_status(r, status, sense);
133
    scsi_req_complete(&r->req);
134
}
135

    
136
/* Cancel a pending data transfer.  */
137
static void scsi_cancel_io(SCSIRequest *req)
138
{
139
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
140

    
141
    DPRINTF("Cancel tag=0x%x\n", req->tag);
142
    if (r->req.aiocb) {
143
        bdrv_aio_cancel(r->req.aiocb);
144
    }
145
    r->req.aiocb = NULL;
146
    scsi_req_dequeue(&r->req);
147
}
148

    
149
static void scsi_read_complete(void * opaque, int ret)
150
{
151
    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
152
    int n;
153

    
154
    r->req.aiocb = NULL;
155

    
156
    if (ret) {
157
        if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_READ)) {
158
            return;
159
        }
160
    }
161

    
162
    DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->iov.iov_len);
163

    
164
    n = r->iov.iov_len / 512;
165
    r->sector += n;
166
    r->sector_count -= n;
167
    scsi_req_data(&r->req, r->iov.iov_len);
168
}
169

    
170

    
171
/* Read more data from scsi device into buffer.  */
172
static void scsi_read_data(SCSIRequest *req)
173
{
174
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
175
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
176
    uint32_t n;
177

    
178
    if (r->sector_count == (uint32_t)-1) {
179
        DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
180
        r->sector_count = 0;
181
        scsi_req_data(&r->req, r->iov.iov_len);
182
        return;
183
    }
184
    DPRINTF("Read sector_count=%d\n", r->sector_count);
185
    if (r->sector_count == 0) {
186
        scsi_command_complete(r, GOOD, NO_SENSE);
187
        return;
188
    }
189

    
190
    /* No data transfer may already be in progress */
191
    assert(r->req.aiocb == NULL);
192

    
193
    n = r->sector_count;
194
    if (n > SCSI_DMA_BUF_SIZE / 512)
195
        n = SCSI_DMA_BUF_SIZE / 512;
196

    
197
    r->iov.iov_len = n * 512;
198
    qemu_iovec_init_external(&r->qiov, &r->iov, 1);
199
    r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
200
                              scsi_read_complete, r);
201
    if (r->req.aiocb == NULL) {
202
        scsi_read_complete(r, -EIO);
203
    }
204
}
205

    
206
static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
207
{
208
    int is_read = (type == SCSI_REQ_STATUS_RETRY_READ);
209
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
210
    BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
211

    
212
    if (action == BLOCK_ERR_IGNORE) {
213
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
214
        return 0;
215
    }
216

    
217
    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
218
            || action == BLOCK_ERR_STOP_ANY) {
219

    
220
        type &= SCSI_REQ_STATUS_RETRY_TYPE_MASK;
221
        r->status |= SCSI_REQ_STATUS_RETRY | type;
222

    
223
        bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
224
        vm_stop(VMSTOP_DISKFULL);
225
    } else {
226
        if (type == SCSI_REQ_STATUS_RETRY_READ) {
227
            scsi_req_data(&r->req, 0);
228
        }
229
        scsi_command_complete(r, CHECK_CONDITION,
230
                HARDWARE_ERROR);
231
        bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
232
    }
233

    
234
    return 1;
235
}
236

    
237
static void scsi_write_complete(void * opaque, int ret)
238
{
239
    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
240
    uint32_t len;
241
    uint32_t n;
242

    
243
    r->req.aiocb = NULL;
244

    
245
    if (ret) {
246
        if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
247
            return;
248
        }
249
    }
250

    
251
    n = r->iov.iov_len / 512;
252
    r->sector += n;
253
    r->sector_count -= n;
254
    if (r->sector_count == 0) {
255
        scsi_command_complete(r, GOOD, NO_SENSE);
256
    } else {
257
        len = r->sector_count * 512;
258
        if (len > SCSI_DMA_BUF_SIZE) {
259
            len = SCSI_DMA_BUF_SIZE;
260
        }
261
        r->iov.iov_len = len;
262
        DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
263
        scsi_req_data(&r->req, len);
264
    }
265
}
266

    
267
static int scsi_write_data(SCSIRequest *req)
268
{
269
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
270
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
271
    uint32_t n;
272

    
273
    /* No data transfer may already be in progress */
274
    assert(r->req.aiocb == NULL);
275

    
276
    n = r->iov.iov_len / 512;
277
    if (n) {
278
        qemu_iovec_init_external(&r->qiov, &r->iov, 1);
279
        r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
280
                                   scsi_write_complete, r);
281
        if (r->req.aiocb == NULL) {
282
            scsi_write_complete(r, -EIO);
283
        }
284
    } else {
285
        /* Invoke completion routine to fetch data from host.  */
286
        scsi_write_complete(r, 0);
287
    }
288

    
289
    return 0;
290
}
291

    
292
static void scsi_dma_restart_bh(void *opaque)
293
{
294
    SCSIDiskState *s = opaque;
295
    SCSIRequest *req;
296
    SCSIDiskReq *r;
297

    
298
    qemu_bh_delete(s->bh);
299
    s->bh = NULL;
300

    
301
    QTAILQ_FOREACH(req, &s->qdev.requests, next) {
302
        r = DO_UPCAST(SCSIDiskReq, req, req);
303
        if (r->status & SCSI_REQ_STATUS_RETRY) {
304
            int status = r->status;
305
            int ret;
306

    
307
            r->status &=
308
                ~(SCSI_REQ_STATUS_RETRY | SCSI_REQ_STATUS_RETRY_TYPE_MASK);
309

    
310
            switch (status & SCSI_REQ_STATUS_RETRY_TYPE_MASK) {
311
            case SCSI_REQ_STATUS_RETRY_READ:
312
                scsi_read_data(&r->req);
313
                break;
314
            case SCSI_REQ_STATUS_RETRY_WRITE:
315
                scsi_write_data(&r->req);
316
                break;
317
            case SCSI_REQ_STATUS_RETRY_FLUSH:
318
                ret = scsi_disk_emulate_command(r, r->iov.iov_base);
319
                if (ret == 0) {
320
                    scsi_command_complete(r, GOOD, NO_SENSE);
321
                }
322
            }
323
        }
324
    }
325
}
326

    
327
static void scsi_dma_restart_cb(void *opaque, int running, int reason)
328
{
329
    SCSIDiskState *s = opaque;
330

    
331
    if (!running)
332
        return;
333

    
334
    if (!s->bh) {
335
        s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
336
        qemu_bh_schedule(s->bh);
337
    }
338
}
339

    
340
/* Return a pointer to the data buffer.  */
341
static uint8_t *scsi_get_buf(SCSIRequest *req)
342
{
343
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
344

    
345
    return (uint8_t *)r->iov.iov_base;
346
}
347

    
348
static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
349
{
350
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
351
    int buflen = 0;
352

    
353
    if (req->cmd.buf[1] & 0x2) {
354
        /* Command support data - optional, not implemented */
355
        BADF("optional INQUIRY command support request not implemented\n");
356
        return -1;
357
    }
358

    
359
    if (req->cmd.buf[1] & 0x1) {
360
        /* Vital product data */
361
        uint8_t page_code = req->cmd.buf[2];
362
        if (req->cmd.xfer < 4) {
363
            BADF("Error: Inquiry (EVPD[%02X]) buffer size %zd is "
364
                 "less than 4\n", page_code, req->cmd.xfer);
365
            return -1;
366
        }
367

    
368
        if (s->drive_kind == SCSI_CD) {
369
            outbuf[buflen++] = 5;
370
        } else {
371
            outbuf[buflen++] = 0;
372
        }
373
        outbuf[buflen++] = page_code ; // this page
374
        outbuf[buflen++] = 0x00;
375

    
376
        switch (page_code) {
377
        case 0x00: /* Supported page codes, mandatory */
378
        {
379
            int pages;
380
            DPRINTF("Inquiry EVPD[Supported pages] "
381
                    "buffer size %zd\n", req->cmd.xfer);
382
            pages = buflen++;
383
            outbuf[buflen++] = 0x00; // list of supported pages (this page)
384
            outbuf[buflen++] = 0x80; // unit serial number
385
            outbuf[buflen++] = 0x83; // device identification
386
            if (s->drive_kind == SCSI_HD) {
387
                outbuf[buflen++] = 0xb0; // block limits
388
                outbuf[buflen++] = 0xb2; // thin provisioning
389
            }
390
            outbuf[pages] = buflen - pages - 1; // number of pages
391
            break;
392
        }
393
        case 0x80: /* Device serial number, optional */
394
        {
395
            int l = strlen(s->serial);
396

    
397
            if (l > req->cmd.xfer)
398
                l = req->cmd.xfer;
399
            if (l > 20)
400
                l = 20;
401

    
402
            DPRINTF("Inquiry EVPD[Serial number] "
403
                    "buffer size %zd\n", req->cmd.xfer);
404
            outbuf[buflen++] = l;
405
            memcpy(outbuf+buflen, s->serial, l);
406
            buflen += l;
407
            break;
408
        }
409

    
410
        case 0x83: /* Device identification page, mandatory */
411
        {
412
            int max_len = 255 - 8;
413
            int id_len = strlen(bdrv_get_device_name(s->bs));
414

    
415
            if (id_len > max_len)
416
                id_len = max_len;
417
            DPRINTF("Inquiry EVPD[Device identification] "
418
                    "buffer size %zd\n", req->cmd.xfer);
419

    
420
            outbuf[buflen++] = 4 + id_len;
421
            outbuf[buflen++] = 0x2; // ASCII
422
            outbuf[buflen++] = 0;   // not officially assigned
423
            outbuf[buflen++] = 0;   // reserved
424
            outbuf[buflen++] = id_len; // length of data following
425

    
426
            memcpy(outbuf+buflen, bdrv_get_device_name(s->bs), id_len);
427
            buflen += id_len;
428
            break;
429
        }
430
        case 0xb0: /* block limits */
431
        {
432
            unsigned int unmap_sectors =
433
                    s->qdev.conf.discard_granularity / s->qdev.blocksize;
434
            unsigned int min_io_size =
435
                    s->qdev.conf.min_io_size / s->qdev.blocksize;
436
            unsigned int opt_io_size =
437
                    s->qdev.conf.opt_io_size / s->qdev.blocksize;
438

    
439
            if (s->drive_kind == SCSI_CD) {
440
                DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
441
                        page_code);
442
                return -1;
443
            }
444
            /* required VPD size with unmap support */
445
            outbuf[3] = buflen = 0x3c;
446

    
447
            memset(outbuf + 4, 0, buflen - 4);
448

    
449
            /* optimal transfer length granularity */
450
            outbuf[6] = (min_io_size >> 8) & 0xff;
451
            outbuf[7] = min_io_size & 0xff;
452

    
453
            /* optimal transfer length */
454
            outbuf[12] = (opt_io_size >> 24) & 0xff;
455
            outbuf[13] = (opt_io_size >> 16) & 0xff;
456
            outbuf[14] = (opt_io_size >> 8) & 0xff;
457
            outbuf[15] = opt_io_size & 0xff;
458

    
459
            /* optimal unmap granularity */
460
            outbuf[28] = (unmap_sectors >> 24) & 0xff;
461
            outbuf[29] = (unmap_sectors >> 16) & 0xff;
462
            outbuf[30] = (unmap_sectors >> 8) & 0xff;
463
            outbuf[31] = unmap_sectors & 0xff;
464
            break;
465
        }
466
        case 0xb2: /* thin provisioning */
467
        {
468
            outbuf[3] = buflen = 8;
469
            outbuf[4] = 0;
470
            outbuf[5] = 0x40; /* write same with unmap supported */
471
            outbuf[6] = 0;
472
            outbuf[7] = 0;
473
            break;
474
        }
475
        default:
476
            BADF("Error: unsupported Inquiry (EVPD[%02X]) "
477
                 "buffer size %zd\n", page_code, req->cmd.xfer);
478
            return -1;
479
        }
480
        /* done with EVPD */
481
        return buflen;
482
    }
483

    
484
    /* Standard INQUIRY data */
485
    if (req->cmd.buf[2] != 0) {
486
        BADF("Error: Inquiry (STANDARD) page or code "
487
             "is non-zero [%02X]\n", req->cmd.buf[2]);
488
        return -1;
489
    }
490

    
491
    /* PAGE CODE == 0 */
492
    if (req->cmd.xfer < 5) {
493
        BADF("Error: Inquiry (STANDARD) buffer size %zd "
494
             "is less than 5\n", req->cmd.xfer);
495
        return -1;
496
    }
497

    
498
    buflen = req->cmd.xfer;
499
    if (buflen > SCSI_MAX_INQUIRY_LEN)
500
        buflen = SCSI_MAX_INQUIRY_LEN;
501

    
502
    memset(outbuf, 0, buflen);
503

    
504
    if (req->lun || req->cmd.buf[1] >> 5) {
505
        outbuf[0] = 0x7f;        /* LUN not supported */
506
        return buflen;
507
    }
508

    
509
    if (s->drive_kind == SCSI_CD) {
510
        outbuf[0] = 5;
511
        outbuf[1] = 0x80;
512
        memcpy(&outbuf[16], "QEMU CD-ROM     ", 16);
513
    } else {
514
        outbuf[0] = 0;
515
        outbuf[1] = s->removable ? 0x80 : 0;
516
        memcpy(&outbuf[16], "QEMU HARDDISK   ", 16);
517
    }
518
    memcpy(&outbuf[8], "QEMU    ", 8);
519
    memset(&outbuf[32], 0, 4);
520
    memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version)));
521
    /*
522
     * We claim conformance to SPC-3, which is required for guests
523
     * to ask for modern features like READ CAPACITY(16) or the
524
     * block characteristics VPD page by default.  Not all of SPC-3
525
     * is actually implemented, but we're good enough.
526
     */
527
    outbuf[2] = 5;
528
    outbuf[3] = 2; /* Format 2 */
529

    
530
    if (buflen > 36) {
531
        outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
532
    } else {
533
        /* If the allocation length of CDB is too small,
534
               the additional length is not adjusted */
535
        outbuf[4] = 36 - 5;
536
    }
537

    
538
    /* Sync data transfer and TCQ.  */
539
    outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
540
    return buflen;
541
}
542

    
543
static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p,
544
                           int page_control)
545
{
546
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
547
    BlockDriverState *bdrv = s->bs;
548
    int cylinders, heads, secs;
549

    
550
    /*
551
     * If Changeable Values are requested, a mask denoting those mode parameters
552
     * that are changeable shall be returned. As we currently don't support
553
     * parameter changes via MODE_SELECT all bits are returned set to zero.
554
     * The buffer was already menset to zero by the caller of this function.
555
     */
556
    switch (page) {
557
    case 4: /* Rigid disk device geometry page. */
558
        p[0] = 4;
559
        p[1] = 0x16;
560
        if (page_control == 1) { /* Changeable Values */
561
            return p[1] + 2;
562
        }
563
        /* if a geometry hint is available, use it */
564
        bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
565
        p[2] = (cylinders >> 16) & 0xff;
566
        p[3] = (cylinders >> 8) & 0xff;
567
        p[4] = cylinders & 0xff;
568
        p[5] = heads & 0xff;
569
        /* Write precomp start cylinder, disabled */
570
        p[6] = (cylinders >> 16) & 0xff;
571
        p[7] = (cylinders >> 8) & 0xff;
572
        p[8] = cylinders & 0xff;
573
        /* Reduced current start cylinder, disabled */
574
        p[9] = (cylinders >> 16) & 0xff;
575
        p[10] = (cylinders >> 8) & 0xff;
576
        p[11] = cylinders & 0xff;
577
        /* Device step rate [ns], 200ns */
578
        p[12] = 0;
579
        p[13] = 200;
580
        /* Landing zone cylinder */
581
        p[14] = 0xff;
582
        p[15] =  0xff;
583
        p[16] = 0xff;
584
        /* Medium rotation rate [rpm], 5400 rpm */
585
        p[20] = (5400 >> 8) & 0xff;
586
        p[21] = 5400 & 0xff;
587
        return p[1] + 2;
588

    
589
    case 5: /* Flexible disk device geometry page. */
590
        p[0] = 5;
591
        p[1] = 0x1e;
592
        if (page_control == 1) { /* Changeable Values */
593
            return p[1] + 2;
594
        }
595
        /* Transfer rate [kbit/s], 5Mbit/s */
596
        p[2] = 5000 >> 8;
597
        p[3] = 5000 & 0xff;
598
        /* if a geometry hint is available, use it */
599
        bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
600
        p[4] = heads & 0xff;
601
        p[5] = secs & 0xff;
602
        p[6] = s->cluster_size * 2;
603
        p[8] = (cylinders >> 8) & 0xff;
604
        p[9] = cylinders & 0xff;
605
        /* Write precomp start cylinder, disabled */
606
        p[10] = (cylinders >> 8) & 0xff;
607
        p[11] = cylinders & 0xff;
608
        /* Reduced current start cylinder, disabled */
609
        p[12] = (cylinders >> 8) & 0xff;
610
        p[13] = cylinders & 0xff;
611
        /* Device step rate [100us], 100us */
612
        p[14] = 0;
613
        p[15] = 1;
614
        /* Device step pulse width [us], 1us */
615
        p[16] = 1;
616
        /* Device head settle delay [100us], 100us */
617
        p[17] = 0;
618
        p[18] = 1;
619
        /* Motor on delay [0.1s], 0.1s */
620
        p[19] = 1;
621
        /* Motor off delay [0.1s], 0.1s */
622
        p[20] = 1;
623
        /* Medium rotation rate [rpm], 5400 rpm */
624
        p[28] = (5400 >> 8) & 0xff;
625
        p[29] = 5400 & 0xff;
626
        return p[1] + 2;
627

    
628
    case 8: /* Caching page.  */
629
        p[0] = 8;
630
        p[1] = 0x12;
631
        if (page_control == 1) { /* Changeable Values */
632
            return p[1] + 2;
633
        }
634
        if (bdrv_enable_write_cache(s->bs)) {
635
            p[2] = 4; /* WCE */
636
        }
637
        return p[1] + 2;
638

    
639
    case 0x2a: /* CD Capabilities and Mechanical Status page. */
640
        if (s->drive_kind != SCSI_CD)
641
            return 0;
642
        p[0] = 0x2a;
643
        p[1] = 0x14;
644
        if (page_control == 1) { /* Changeable Values */
645
            return p[1] + 2;
646
        }
647
        p[2] = 3; // CD-R & CD-RW read
648
        p[3] = 0; // Writing not supported
649
        p[4] = 0x7f; /* Audio, composite, digital out,
650
                        mode 2 form 1&2, multi session */
651
        p[5] = 0xff; /* CD DA, DA accurate, RW supported,
652
                        RW corrected, C2 errors, ISRC,
653
                        UPC, Bar code */
654
        p[6] = 0x2d | (bdrv_is_locked(s->bs)? 2 : 0);
655
        /* Locking supported, jumper present, eject, tray */
656
        p[7] = 0; /* no volume & mute control, no
657
                     changer */
658
        p[8] = (50 * 176) >> 8; // 50x read speed
659
        p[9] = (50 * 176) & 0xff;
660
        p[10] = 0 >> 8; // No volume
661
        p[11] = 0 & 0xff;
662
        p[12] = 2048 >> 8; // 2M buffer
663
        p[13] = 2048 & 0xff;
664
        p[14] = (16 * 176) >> 8; // 16x read speed current
665
        p[15] = (16 * 176) & 0xff;
666
        p[18] = (16 * 176) >> 8; // 16x write speed
667
        p[19] = (16 * 176) & 0xff;
668
        p[20] = (16 * 176) >> 8; // 16x write speed current
669
        p[21] = (16 * 176) & 0xff;
670
        return p[1] + 2;
671

    
672
    default:
673
        return 0;
674
    }
675
}
676

    
677
static int scsi_disk_emulate_mode_sense(SCSIRequest *req, uint8_t *outbuf)
678
{
679
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
680
    uint64_t nb_sectors;
681
    int page, dbd, buflen, page_control;
682
    uint8_t *p;
683
    uint8_t dev_specific_param;
684

    
685
    dbd = req->cmd.buf[1]  & 0x8;
686
    page = req->cmd.buf[2] & 0x3f;
687
    page_control = (req->cmd.buf[2] & 0xc0) >> 6;
688
    DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
689
        (req->cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, req->cmd.xfer, page_control);
690
    memset(outbuf, 0, req->cmd.xfer);
691
    p = outbuf;
692

    
693
    if (bdrv_is_read_only(s->bs)) {
694
        dev_specific_param = 0x80; /* Readonly.  */
695
    } else {
696
        dev_specific_param = 0x00;
697
    }
698

    
699
    if (req->cmd.buf[0] == MODE_SENSE) {
700
        p[1] = 0; /* Default media type.  */
701
        p[2] = dev_specific_param;
702
        p[3] = 0; /* Block descriptor length.  */
703
        p += 4;
704
    } else { /* MODE_SENSE_10 */
705
        p[2] = 0; /* Default media type.  */
706
        p[3] = dev_specific_param;
707
        p[6] = p[7] = 0; /* Block descriptor length.  */
708
        p += 8;
709
    }
710

    
711
    bdrv_get_geometry(s->bs, &nb_sectors);
712
    if (!dbd && nb_sectors) {
713
        if (req->cmd.buf[0] == MODE_SENSE) {
714
            outbuf[3] = 8; /* Block descriptor length  */
715
        } else { /* MODE_SENSE_10 */
716
            outbuf[7] = 8; /* Block descriptor length  */
717
        }
718
        nb_sectors /= s->cluster_size;
719
        if (nb_sectors > 0xffffff)
720
            nb_sectors = 0;
721
        p[0] = 0; /* media density code */
722
        p[1] = (nb_sectors >> 16) & 0xff;
723
        p[2] = (nb_sectors >> 8) & 0xff;
724
        p[3] = nb_sectors & 0xff;
725
        p[4] = 0; /* reserved */
726
        p[5] = 0; /* bytes 5-7 are the sector size in bytes */
727
        p[6] = s->cluster_size * 2;
728
        p[7] = 0;
729
        p += 8;
730
    }
731

    
732
    if (page_control == 3) { /* Saved Values */
733
        return -1; /* ILLEGAL_REQUEST */
734
    }
735

    
736
    switch (page) {
737
    case 0x04:
738
    case 0x05:
739
    case 0x08:
740
    case 0x2a:
741
        p += mode_sense_page(req, page, p, page_control);
742
        break;
743
    case 0x3f:
744
        p += mode_sense_page(req, 0x08, p, page_control);
745
        p += mode_sense_page(req, 0x2a, p, page_control);
746
        break;
747
    default:
748
        return -1; /* ILLEGAL_REQUEST */
749
    }
750

    
751
    buflen = p - outbuf;
752
    /*
753
     * The mode data length field specifies the length in bytes of the
754
     * following data that is available to be transferred. The mode data
755
     * length does not include itself.
756
     */
757
    if (req->cmd.buf[0] == MODE_SENSE) {
758
        outbuf[0] = buflen - 1;
759
    } else { /* MODE_SENSE_10 */
760
        outbuf[0] = ((buflen - 2) >> 8) & 0xff;
761
        outbuf[1] = (buflen - 2) & 0xff;
762
    }
763
    if (buflen > req->cmd.xfer)
764
        buflen = req->cmd.xfer;
765
    return buflen;
766
}
767

    
768
static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
769
{
770
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
771
    int start_track, format, msf, toclen;
772
    uint64_t nb_sectors;
773

    
774
    msf = req->cmd.buf[1] & 2;
775
    format = req->cmd.buf[2] & 0xf;
776
    start_track = req->cmd.buf[6];
777
    bdrv_get_geometry(s->bs, &nb_sectors);
778
    DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
779
    nb_sectors /= s->cluster_size;
780
    switch (format) {
781
    case 0:
782
        toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
783
        break;
784
    case 1:
785
        /* multi session : only a single session defined */
786
        toclen = 12;
787
        memset(outbuf, 0, 12);
788
        outbuf[1] = 0x0a;
789
        outbuf[2] = 0x01;
790
        outbuf[3] = 0x01;
791
        break;
792
    case 2:
793
        toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
794
        break;
795
    default:
796
        return -1;
797
    }
798
    if (toclen > req->cmd.xfer)
799
        toclen = req->cmd.xfer;
800
    return toclen;
801
}
802

    
803
static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
804
{
805
    SCSIRequest *req = &r->req;
806
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
807
    uint64_t nb_sectors;
808
    int buflen = 0;
809
    int ret;
810

    
811
    switch (req->cmd.buf[0]) {
812
    case TEST_UNIT_READY:
813
        if (!bdrv_is_inserted(s->bs))
814
            goto not_ready;
815
        break;
816
    case REQUEST_SENSE:
817
        if (req->cmd.xfer < 4)
818
            goto illegal_request;
819
        memset(outbuf, 0, 4);
820
        buflen = 4;
821
        if (s->sense.key == NOT_READY && req->cmd.xfer >= 18) {
822
            memset(outbuf, 0, 18);
823
            buflen = 18;
824
            outbuf[7] = 10;
825
            /* asc 0x3a, ascq 0: Medium not present */
826
            outbuf[12] = 0x3a;
827
            outbuf[13] = 0;
828
        }
829
        outbuf[0] = 0xf0;
830
        outbuf[1] = 0;
831
        outbuf[2] = s->sense.key;
832
        scsi_disk_clear_sense(s);
833
        break;
834
    case INQUIRY:
835
        buflen = scsi_disk_emulate_inquiry(req, outbuf);
836
        if (buflen < 0)
837
            goto illegal_request;
838
        break;
839
    case MODE_SENSE:
840
    case MODE_SENSE_10:
841
        buflen = scsi_disk_emulate_mode_sense(req, outbuf);
842
        if (buflen < 0)
843
            goto illegal_request;
844
        break;
845
    case READ_TOC:
846
        buflen = scsi_disk_emulate_read_toc(req, outbuf);
847
        if (buflen < 0)
848
            goto illegal_request;
849
        break;
850
    case RESERVE:
851
        if (req->cmd.buf[1] & 1)
852
            goto illegal_request;
853
        break;
854
    case RESERVE_10:
855
        if (req->cmd.buf[1] & 3)
856
            goto illegal_request;
857
        break;
858
    case RELEASE:
859
        if (req->cmd.buf[1] & 1)
860
            goto illegal_request;
861
        break;
862
    case RELEASE_10:
863
        if (req->cmd.buf[1] & 3)
864
            goto illegal_request;
865
        break;
866
    case START_STOP:
867
        if (s->drive_kind == SCSI_CD && (req->cmd.buf[4] & 2)) {
868
            /* load/eject medium */
869
            bdrv_eject(s->bs, !(req->cmd.buf[4] & 1));
870
        }
871
        break;
872
    case ALLOW_MEDIUM_REMOVAL:
873
        bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
874
        break;
875
    case READ_CAPACITY:
876
        /* The normal LEN field for this command is zero.  */
877
        memset(outbuf, 0, 8);
878
        bdrv_get_geometry(s->bs, &nb_sectors);
879
        if (!nb_sectors)
880
            goto not_ready;
881
        nb_sectors /= s->cluster_size;
882
        /* Returned value is the address of the last sector.  */
883
        nb_sectors--;
884
        /* Remember the new size for read/write sanity checking. */
885
        s->max_lba = nb_sectors;
886
        /* Clip to 2TB, instead of returning capacity modulo 2TB. */
887
        if (nb_sectors > UINT32_MAX)
888
            nb_sectors = UINT32_MAX;
889
        outbuf[0] = (nb_sectors >> 24) & 0xff;
890
        outbuf[1] = (nb_sectors >> 16) & 0xff;
891
        outbuf[2] = (nb_sectors >> 8) & 0xff;
892
        outbuf[3] = nb_sectors & 0xff;
893
        outbuf[4] = 0;
894
        outbuf[5] = 0;
895
        outbuf[6] = s->cluster_size * 2;
896
        outbuf[7] = 0;
897
        buflen = 8;
898
        break;
899
    case SYNCHRONIZE_CACHE:
900
        ret = bdrv_flush(s->bs);
901
        if (ret < 0) {
902
            if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_FLUSH)) {
903
                return -1;
904
            }
905
        }
906
        break;
907
    case GET_CONFIGURATION:
908
        memset(outbuf, 0, 8);
909
        /* ??? This should probably return much more information.  For now
910
           just return the basic header indicating the CD-ROM profile.  */
911
        outbuf[7] = 8; // CD-ROM
912
        buflen = 8;
913
        break;
914
    case SERVICE_ACTION_IN:
915
        /* Service Action In subcommands. */
916
        if ((req->cmd.buf[1] & 31) == 0x10) {
917
            DPRINTF("SAI READ CAPACITY(16)\n");
918
            memset(outbuf, 0, req->cmd.xfer);
919
            bdrv_get_geometry(s->bs, &nb_sectors);
920
            if (!nb_sectors)
921
                goto not_ready;
922
            nb_sectors /= s->cluster_size;
923
            /* Returned value is the address of the last sector.  */
924
            nb_sectors--;
925
            /* Remember the new size for read/write sanity checking. */
926
            s->max_lba = nb_sectors;
927
            outbuf[0] = (nb_sectors >> 56) & 0xff;
928
            outbuf[1] = (nb_sectors >> 48) & 0xff;
929
            outbuf[2] = (nb_sectors >> 40) & 0xff;
930
            outbuf[3] = (nb_sectors >> 32) & 0xff;
931
            outbuf[4] = (nb_sectors >> 24) & 0xff;
932
            outbuf[5] = (nb_sectors >> 16) & 0xff;
933
            outbuf[6] = (nb_sectors >> 8) & 0xff;
934
            outbuf[7] = nb_sectors & 0xff;
935
            outbuf[8] = 0;
936
            outbuf[9] = 0;
937
            outbuf[10] = s->cluster_size * 2;
938
            outbuf[11] = 0;
939
            outbuf[12] = 0;
940
            outbuf[13] = get_physical_block_exp(&s->qdev.conf);
941

    
942
            /* set TPE bit if the format supports discard */
943
            if (s->qdev.conf.discard_granularity) {
944
                outbuf[14] = 0x80;
945
            }
946

    
947
            /* Protection, exponent and lowest lba field left blank. */
948
            buflen = req->cmd.xfer;
949
            break;
950
        }
951
        DPRINTF("Unsupported Service Action In\n");
952
        goto illegal_request;
953
    case REPORT_LUNS:
954
        if (req->cmd.xfer < 16)
955
            goto illegal_request;
956
        memset(outbuf, 0, 16);
957
        outbuf[3] = 8;
958
        buflen = 16;
959
        break;
960
    case VERIFY:
961
        break;
962
    case REZERO_UNIT:
963
        DPRINTF("Rezero Unit\n");
964
        if (!bdrv_is_inserted(s->bs)) {
965
            goto not_ready;
966
        }
967
        break;
968
    default:
969
        goto illegal_request;
970
    }
971
    scsi_req_set_status(r, GOOD, NO_SENSE);
972
    return buflen;
973

    
974
not_ready:
975
    scsi_command_complete(r, CHECK_CONDITION, NOT_READY);
976
    return -1;
977

    
978
illegal_request:
979
    scsi_command_complete(r, CHECK_CONDITION, ILLEGAL_REQUEST);
980
    return -1;
981
}
982

    
983
/* Execute a scsi command.  Returns the length of the data expected by the
984
   command.  This will be Positive for data transfers from the device
985
   (eg. disk reads), negative for transfers to the device (eg. disk writes),
986
   and zero if the command does not transfer any data.  */
987

    
988
static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
989
{
990
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
991
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
992
    int32_t len;
993
    int is_write;
994
    uint8_t command;
995
    uint8_t *outbuf;
996
    int rc;
997

    
998
    scsi_req_enqueue(req);
999
    command = buf[0];
1000
    outbuf = (uint8_t *)r->iov.iov_base;
1001
    is_write = 0;
1002
    DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
1003

    
1004
    if (scsi_req_parse(&r->req, buf) != 0) {
1005
        BADF("Unsupported command length, command %x\n", command);
1006
        goto fail;
1007
    }
1008
#ifdef DEBUG_SCSI
1009
    {
1010
        int i;
1011
        for (i = 1; i < r->req.cmd.len; i++) {
1012
            printf(" 0x%02x", buf[i]);
1013
        }
1014
        printf("\n");
1015
    }
1016
#endif
1017

    
1018
    if (req->lun || buf[1] >> 5) {
1019
        /* Only LUN 0 supported.  */
1020
        DPRINTF("Unimplemented LUN %d\n", req->lun ? req->lun : buf[1] >> 5);
1021
        if (command != REQUEST_SENSE && command != INQUIRY)
1022
            goto fail;
1023
    }
1024
    switch (command) {
1025
    case TEST_UNIT_READY:
1026
    case REQUEST_SENSE:
1027
    case INQUIRY:
1028
    case MODE_SENSE:
1029
    case MODE_SENSE_10:
1030
    case RESERVE:
1031
    case RESERVE_10:
1032
    case RELEASE:
1033
    case RELEASE_10:
1034
    case START_STOP:
1035
    case ALLOW_MEDIUM_REMOVAL:
1036
    case READ_CAPACITY:
1037
    case SYNCHRONIZE_CACHE:
1038
    case READ_TOC:
1039
    case GET_CONFIGURATION:
1040
    case SERVICE_ACTION_IN:
1041
    case REPORT_LUNS:
1042
    case VERIFY:
1043
    case REZERO_UNIT:
1044
        rc = scsi_disk_emulate_command(r, outbuf);
1045
        if (rc < 0) {
1046
            return 0;
1047
        }
1048

    
1049
        r->iov.iov_len = rc;
1050
        break;
1051
    case READ_6:
1052
    case READ_10:
1053
    case READ_12:
1054
    case READ_16:
1055
        len = r->req.cmd.xfer / s->qdev.blocksize;
1056
        DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1057
        if (r->req.cmd.lba > s->max_lba)
1058
            goto illegal_lba;
1059
        r->sector = r->req.cmd.lba * s->cluster_size;
1060
        r->sector_count = len * s->cluster_size;
1061
        break;
1062
    case WRITE_6:
1063
    case WRITE_10:
1064
    case WRITE_12:
1065
    case WRITE_16:
1066
    case WRITE_VERIFY:
1067
    case WRITE_VERIFY_12:
1068
    case WRITE_VERIFY_16:
1069
        len = r->req.cmd.xfer / s->qdev.blocksize;
1070
        DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
1071
                (command & 0xe) == 0xe ? "And Verify " : "",
1072
                r->req.cmd.lba, len);
1073
        if (r->req.cmd.lba > s->max_lba)
1074
            goto illegal_lba;
1075
        r->sector = r->req.cmd.lba * s->cluster_size;
1076
        r->sector_count = len * s->cluster_size;
1077
        is_write = 1;
1078
        break;
1079
    case MODE_SELECT:
1080
        DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1081
        /* We don't support mode parameter changes.
1082
           Allow the mode parameter header + block descriptors only. */
1083
        if (r->req.cmd.xfer > 12) {
1084
            goto fail;
1085
        }
1086
        break;
1087
    case MODE_SELECT_10:
1088
        DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1089
        /* We don't support mode parameter changes.
1090
           Allow the mode parameter header + block descriptors only. */
1091
        if (r->req.cmd.xfer > 16) {
1092
            goto fail;
1093
        }
1094
        break;
1095
    case SEEK_6:
1096
    case SEEK_10:
1097
        DPRINTF("Seek(%d) (sector %" PRId64 ")\n", command == SEEK_6 ? 6 : 10,
1098
                r->req.cmd.lba);
1099
        if (r->req.cmd.lba > s->max_lba) {
1100
            goto illegal_lba;
1101
        }
1102
        break;
1103
    case WRITE_SAME_16:
1104
        len = r->req.cmd.xfer / s->qdev.blocksize;
1105

    
1106
        DPRINTF("WRITE SAME(16) (sector %" PRId64 ", count %d)\n",
1107
                r->req.cmd.lba, len);
1108

    
1109
        if (r->req.cmd.lba > s->max_lba) {
1110
            goto illegal_lba;
1111
        }
1112

    
1113
        /*
1114
         * We only support WRITE SAME with the unmap bit set for now.
1115
         */
1116
        if (!(buf[1] & 0x8)) {
1117
            goto fail;
1118
        }
1119

    
1120
        rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1121
                          len * s->cluster_size);
1122
        if (rc < 0) {
1123
            /* XXX: better error code ?*/
1124
            goto fail;
1125
        }
1126

    
1127
        break;
1128
    default:
1129
        DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1130
    fail:
1131
        scsi_command_complete(r, CHECK_CONDITION, ILLEGAL_REQUEST);
1132
        return 0;
1133
    illegal_lba:
1134
        scsi_command_complete(r, CHECK_CONDITION, HARDWARE_ERROR);
1135
        return 0;
1136
    }
1137
    if (r->sector_count == 0 && r->iov.iov_len == 0) {
1138
        scsi_command_complete(r, GOOD, NO_SENSE);
1139
    }
1140
    len = r->sector_count * 512 + r->iov.iov_len;
1141
    if (is_write) {
1142
        len = -len;
1143
    } else {
1144
        if (!r->sector_count)
1145
            r->sector_count = -1;
1146
    }
1147
    return len;
1148
}
1149

    
1150
static void scsi_disk_reset(DeviceState *dev)
1151
{
1152
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1153
    uint64_t nb_sectors;
1154

    
1155
    scsi_device_purge_requests(&s->qdev);
1156

    
1157
    bdrv_get_geometry(s->bs, &nb_sectors);
1158
    nb_sectors /= s->cluster_size;
1159
    if (nb_sectors) {
1160
        nb_sectors--;
1161
    }
1162
    s->max_lba = nb_sectors;
1163
}
1164

    
1165
static void scsi_destroy(SCSIDevice *dev)
1166
{
1167
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1168

    
1169
    scsi_device_purge_requests(&s->qdev);
1170
    blockdev_mark_auto_del(s->qdev.conf.bs);
1171
}
1172

    
1173
static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
1174
{
1175
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1176
    DriveInfo *dinfo;
1177

    
1178
    if (!s->qdev.conf.bs) {
1179
        error_report("scsi-disk: drive property not set");
1180
        return -1;
1181
    }
1182
    s->bs = s->qdev.conf.bs;
1183
    s->drive_kind = kind;
1184

    
1185
    if (kind == SCSI_HD && !bdrv_is_inserted(s->bs)) {
1186
        error_report("Device needs media, but drive is empty");
1187
        return -1;
1188
    }
1189

    
1190
    if (!s->serial) {
1191
        /* try to fall back to value set with legacy -drive serial=... */
1192
        dinfo = drive_get_by_blockdev(s->bs);
1193
        s->serial = qemu_strdup(*dinfo->serial ? dinfo->serial : "0");
1194
    }
1195

    
1196
    if (!s->version) {
1197
        s->version = qemu_strdup(QEMU_VERSION);
1198
    }
1199

    
1200
    if (bdrv_is_sg(s->bs)) {
1201
        error_report("scsi-disk: unwanted /dev/sg*");
1202
        return -1;
1203
    }
1204

    
1205
    if (kind == SCSI_CD) {
1206
        s->qdev.blocksize = 2048;
1207
    } else {
1208
        s->qdev.blocksize = s->qdev.conf.logical_block_size;
1209
    }
1210
    s->cluster_size = s->qdev.blocksize / 512;
1211
    s->bs->buffer_alignment = s->qdev.blocksize;
1212

    
1213
    s->qdev.type = TYPE_DISK;
1214
    qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
1215
    bdrv_set_removable(s->bs, kind == SCSI_CD);
1216
    add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
1217
    return 0;
1218
}
1219

    
1220
static int scsi_hd_initfn(SCSIDevice *dev)
1221
{
1222
    return scsi_initfn(dev, SCSI_HD);
1223
}
1224

    
1225
static int scsi_cd_initfn(SCSIDevice *dev)
1226
{
1227
    return scsi_initfn(dev, SCSI_CD);
1228
}
1229

    
1230
static int scsi_disk_initfn(SCSIDevice *dev)
1231
{
1232
    SCSIDriveKind kind;
1233
    DriveInfo *dinfo;
1234

    
1235
    if (!dev->conf.bs) {
1236
        kind = SCSI_HD;         /* will die in scsi_initfn() */
1237
    } else {
1238
        dinfo = drive_get_by_blockdev(dev->conf.bs);
1239
        kind = dinfo->media_cd ? SCSI_CD : SCSI_HD;
1240
    }
1241

    
1242
    return scsi_initfn(dev, kind);
1243
}
1244

    
1245
#define DEFINE_SCSI_DISK_PROPERTIES()                           \
1246
    DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf),          \
1247
    DEFINE_PROP_STRING("ver",  SCSIDiskState, version),         \
1248
    DEFINE_PROP_STRING("serial",  SCSIDiskState, serial)
1249

    
1250
static SCSIDeviceInfo scsi_disk_info[] = {
1251
    {
1252
        .qdev.name    = "scsi-hd",
1253
        .qdev.fw_name = "disk",
1254
        .qdev.desc    = "virtual SCSI disk",
1255
        .qdev.size    = sizeof(SCSIDiskState),
1256
        .qdev.reset   = scsi_disk_reset,
1257
        .init         = scsi_hd_initfn,
1258
        .destroy      = scsi_destroy,
1259
        .alloc_req    = scsi_new_request,
1260
        .free_req     = scsi_free_request,
1261
        .send_command = scsi_send_command,
1262
        .read_data    = scsi_read_data,
1263
        .write_data   = scsi_write_data,
1264
        .cancel_io    = scsi_cancel_io,
1265
        .get_buf      = scsi_get_buf,
1266
        .qdev.props   = (Property[]) {
1267
            DEFINE_SCSI_DISK_PROPERTIES(),
1268
            DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1269
            DEFINE_PROP_END_OF_LIST(),
1270
        }
1271
    },{
1272
        .qdev.name    = "scsi-cd",
1273
        .qdev.fw_name = "disk",
1274
        .qdev.desc    = "virtual SCSI CD-ROM",
1275
        .qdev.size    = sizeof(SCSIDiskState),
1276
        .qdev.reset   = scsi_disk_reset,
1277
        .init         = scsi_cd_initfn,
1278
        .destroy      = scsi_destroy,
1279
        .alloc_req    = scsi_new_request,
1280
        .free_req     = scsi_free_request,
1281
        .send_command = scsi_send_command,
1282
        .read_data    = scsi_read_data,
1283
        .write_data   = scsi_write_data,
1284
        .cancel_io    = scsi_cancel_io,
1285
        .get_buf      = scsi_get_buf,
1286
        .qdev.props   = (Property[]) {
1287
            DEFINE_SCSI_DISK_PROPERTIES(),
1288
            DEFINE_PROP_END_OF_LIST(),
1289
        },
1290
    },{
1291
        .qdev.name    = "scsi-disk", /* legacy -device scsi-disk */
1292
        .qdev.fw_name = "disk",
1293
        .qdev.desc    = "virtual SCSI disk or CD-ROM (legacy)",
1294
        .qdev.size    = sizeof(SCSIDiskState),
1295
        .qdev.reset   = scsi_disk_reset,
1296
        .init         = scsi_disk_initfn,
1297
        .destroy      = scsi_destroy,
1298
        .alloc_req    = scsi_new_request,
1299
        .free_req     = scsi_free_request,
1300
        .send_command = scsi_send_command,
1301
        .read_data    = scsi_read_data,
1302
        .write_data   = scsi_write_data,
1303
        .cancel_io    = scsi_cancel_io,
1304
        .get_buf      = scsi_get_buf,
1305
        .qdev.props   = (Property[]) {
1306
            DEFINE_SCSI_DISK_PROPERTIES(),
1307
            DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1308
            DEFINE_PROP_END_OF_LIST(),
1309
        }
1310
    }
1311
};
1312

    
1313
static void scsi_disk_register_devices(void)
1314
{
1315
    int i;
1316

    
1317
    for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1318
        scsi_qdev_register(&scsi_disk_info[i]);
1319
    }
1320
}
1321
device_init(scsi_disk_register_devices)