Statistics
| Branch: | Revision:

root / hw / scsi-disk.c @ a1f0cce2

History | View | Annotate | Download (39.8 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 SCSIDiskReq {
53
    SCSIRequest req;
54
    /* ??? We should probably keep track of whether the data transfer is
55
       a read or a write.  Currently we rely on the host getting it right.  */
56
    /* Both sector and sector_count are in terms of qemu 512 byte blocks.  */
57
    uint64_t sector;
58
    uint32_t sector_count;
59
    struct iovec iov;
60
    QEMUIOVector qiov;
61
    uint32_t status;
62
} SCSIDiskReq;
63

    
64
typedef enum { SCSI_HD, SCSI_CD } SCSIDriveKind;
65

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

    
82
static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
83
static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf);
84

    
85
static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag,
86
        uint32_t lun)
87
{
88
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
89
    SCSIRequest *req;
90
    SCSIDiskReq *r;
91

    
92
    req = scsi_req_alloc(sizeof(SCSIDiskReq), &s->qdev, tag, lun);
93
    r = DO_UPCAST(SCSIDiskReq, req, req);
94
    r->iov.iov_base = qemu_blockalign(s->bs, SCSI_DMA_BUF_SIZE);
95
    return req;
96
}
97

    
98
static void scsi_free_request(SCSIRequest *req)
99
{
100
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
101

    
102
    qemu_vfree(r->iov.iov_base);
103
}
104

    
105
static void scsi_disk_clear_sense(SCSIDiskState *s)
106
{
107
    memset(&s->sense, 0, sizeof(s->sense));
108
}
109

    
110
static void scsi_req_set_status(SCSIDiskReq *r, int status, SCSISense sense)
111
{
112
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
113

    
114
    r->req.status = status;
115
    s->sense = sense;
116
}
117

    
118
/* Helper function for command completion.  */
119
static void scsi_command_complete(SCSIDiskReq *r, int status, SCSISense sense)
120
{
121
    DPRINTF("Command complete tag=0x%x status=%d sense=%d/%d/%d\n",
122
            r->req.tag, status, sense.key, sense.asc, sense.ascq);
123
    scsi_req_set_status(r, status, sense);
124
    scsi_req_complete(&r->req);
125
}
126

    
127
/* Cancel a pending data transfer.  */
128
static void scsi_cancel_io(SCSIRequest *req)
129
{
130
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
131

    
132
    DPRINTF("Cancel tag=0x%x\n", req->tag);
133
    if (r->req.aiocb) {
134
        bdrv_aio_cancel(r->req.aiocb);
135
    }
136
    r->req.aiocb = NULL;
137
}
138

    
139
static void scsi_read_complete(void * opaque, int ret)
140
{
141
    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
142
    int n;
143

    
144
    r->req.aiocb = NULL;
145

    
146
    if (ret) {
147
        if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_READ)) {
148
            return;
149
        }
150
    }
151

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

    
154
    n = r->iov.iov_len / 512;
155
    r->sector += n;
156
    r->sector_count -= n;
157
    scsi_req_data(&r->req, r->iov.iov_len);
158
}
159

    
160

    
161
/* Read more data from scsi device into buffer.  */
162
static void scsi_read_data(SCSIRequest *req)
163
{
164
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
165
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
166
    uint32_t n;
167

    
168
    if (r->sector_count == (uint32_t)-1) {
169
        DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
170
        r->sector_count = 0;
171
        scsi_req_data(&r->req, r->iov.iov_len);
172
        return;
173
    }
174
    DPRINTF("Read sector_count=%d\n", r->sector_count);
175
    if (r->sector_count == 0) {
176
        scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
177
        return;
178
    }
179

    
180
    /* No data transfer may already be in progress */
181
    assert(r->req.aiocb == NULL);
182

    
183
    n = r->sector_count;
184
    if (n > SCSI_DMA_BUF_SIZE / 512)
185
        n = SCSI_DMA_BUF_SIZE / 512;
186

    
187
    r->iov.iov_len = n * 512;
188
    qemu_iovec_init_external(&r->qiov, &r->iov, 1);
189
    r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
190
                              scsi_read_complete, r);
191
    if (r->req.aiocb == NULL) {
192
        scsi_read_complete(r, -EIO);
193
    }
194
}
195

    
196
static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
197
{
198
    int is_read = (type == SCSI_REQ_STATUS_RETRY_READ);
199
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
200
    BlockErrorAction action = bdrv_get_on_error(s->bs, is_read);
201

    
202
    if (action == BLOCK_ERR_IGNORE) {
203
        bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read);
204
        return 0;
205
    }
206

    
207
    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
208
            || action == BLOCK_ERR_STOP_ANY) {
209

    
210
        type &= SCSI_REQ_STATUS_RETRY_TYPE_MASK;
211
        r->status |= SCSI_REQ_STATUS_RETRY | type;
212

    
213
        bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
214
        vm_stop(VMSTOP_DISKFULL);
215
    } else {
216
        if (type == SCSI_REQ_STATUS_RETRY_READ) {
217
            scsi_req_data(&r->req, 0);
218
        }
219
        if (error == ENOMEM) {
220
            scsi_command_complete(r, CHECK_CONDITION,
221
                                  SENSE_CODE(TARGET_FAILURE));
222
        } else {
223
            scsi_command_complete(r, CHECK_CONDITION,
224
                                  SENSE_CODE(IO_ERROR));
225
        }
226
        bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read);
227
    }
228

    
229
    return 1;
230
}
231

    
232
static void scsi_write_complete(void * opaque, int ret)
233
{
234
    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
235
    uint32_t len;
236
    uint32_t n;
237

    
238
    r->req.aiocb = NULL;
239

    
240
    if (ret) {
241
        if (scsi_handle_rw_error(r, -ret, SCSI_REQ_STATUS_RETRY_WRITE)) {
242
            return;
243
        }
244
    }
245

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

    
262
static int scsi_write_data(SCSIRequest *req)
263
{
264
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
265
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
266
    uint32_t n;
267

    
268
    /* No data transfer may already be in progress */
269
    assert(r->req.aiocb == NULL);
270

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

    
284
    return 0;
285
}
286

    
287
static void scsi_dma_restart_bh(void *opaque)
288
{
289
    SCSIDiskState *s = opaque;
290
    SCSIRequest *req;
291
    SCSIDiskReq *r;
292

    
293
    qemu_bh_delete(s->bh);
294
    s->bh = NULL;
295

    
296
    QTAILQ_FOREACH(req, &s->qdev.requests, next) {
297
        r = DO_UPCAST(SCSIDiskReq, req, req);
298
        if (r->status & SCSI_REQ_STATUS_RETRY) {
299
            int status = r->status;
300
            int ret;
301

    
302
            r->status &=
303
                ~(SCSI_REQ_STATUS_RETRY | SCSI_REQ_STATUS_RETRY_TYPE_MASK);
304

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

    
322
static void scsi_dma_restart_cb(void *opaque, int running, int reason)
323
{
324
    SCSIDiskState *s = opaque;
325

    
326
    if (!running)
327
        return;
328

    
329
    if (!s->bh) {
330
        s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
331
        qemu_bh_schedule(s->bh);
332
    }
333
}
334

    
335
/* Return a pointer to the data buffer.  */
336
static uint8_t *scsi_get_buf(SCSIRequest *req)
337
{
338
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
339

    
340
    return (uint8_t *)r->iov.iov_base;
341
}
342

    
343
static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
344
{
345
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
346
    int buflen = 0;
347

    
348
    if (req->cmd.buf[1] & 0x2) {
349
        /* Command support data - optional, not implemented */
350
        BADF("optional INQUIRY command support request not implemented\n");
351
        return -1;
352
    }
353

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

    
363
        if (s->drive_kind == SCSI_CD) {
364
            outbuf[buflen++] = 5;
365
        } else {
366
            outbuf[buflen++] = 0;
367
        }
368
        outbuf[buflen++] = page_code ; // this page
369
        outbuf[buflen++] = 0x00;
370

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

    
392
            if (l > req->cmd.xfer)
393
                l = req->cmd.xfer;
394
            if (l > 20)
395
                l = 20;
396

    
397
            DPRINTF("Inquiry EVPD[Serial number] "
398
                    "buffer size %zd\n", req->cmd.xfer);
399
            outbuf[buflen++] = l;
400
            memcpy(outbuf+buflen, s->serial, l);
401
            buflen += l;
402
            break;
403
        }
404

    
405
        case 0x83: /* Device identification page, mandatory */
406
        {
407
            int max_len = 255 - 8;
408
            int id_len = strlen(bdrv_get_device_name(s->bs));
409

    
410
            if (id_len > max_len)
411
                id_len = max_len;
412
            DPRINTF("Inquiry EVPD[Device identification] "
413
                    "buffer size %zd\n", req->cmd.xfer);
414

    
415
            outbuf[buflen++] = 4 + id_len;
416
            outbuf[buflen++] = 0x2; // ASCII
417
            outbuf[buflen++] = 0;   // not officially assigned
418
            outbuf[buflen++] = 0;   // reserved
419
            outbuf[buflen++] = id_len; // length of data following
420

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

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

    
442
            memset(outbuf + 4, 0, buflen - 4);
443

    
444
            /* optimal transfer length granularity */
445
            outbuf[6] = (min_io_size >> 8) & 0xff;
446
            outbuf[7] = min_io_size & 0xff;
447

    
448
            /* optimal transfer length */
449
            outbuf[12] = (opt_io_size >> 24) & 0xff;
450
            outbuf[13] = (opt_io_size >> 16) & 0xff;
451
            outbuf[14] = (opt_io_size >> 8) & 0xff;
452
            outbuf[15] = opt_io_size & 0xff;
453

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

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

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

    
493
    buflen = req->cmd.xfer;
494
    if (buflen > SCSI_MAX_INQUIRY_LEN)
495
        buflen = SCSI_MAX_INQUIRY_LEN;
496

    
497
    memset(outbuf, 0, buflen);
498

    
499
    if (req->lun || req->cmd.buf[1] >> 5) {
500
        outbuf[0] = 0x7f;        /* LUN not supported */
501
        return buflen;
502
    }
503

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

    
525
    if (buflen > 36) {
526
        outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */
527
    } else {
528
        /* If the allocation length of CDB is too small,
529
               the additional length is not adjusted */
530
        outbuf[4] = 36 - 5;
531
    }
532

    
533
    /* Sync data transfer and TCQ.  */
534
    outbuf[7] = 0x10 | (req->bus->tcq ? 0x02 : 0);
535
    return buflen;
536
}
537

    
538
static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p,
539
                           int page_control)
540
{
541
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
542
    BlockDriverState *bdrv = s->bs;
543
    int cylinders, heads, secs;
544

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

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

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

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

    
667
    default:
668
        return 0;
669
    }
670
}
671

    
672
static int scsi_disk_emulate_mode_sense(SCSIRequest *req, uint8_t *outbuf)
673
{
674
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
675
    uint64_t nb_sectors;
676
    int page, dbd, buflen, page_control;
677
    uint8_t *p;
678
    uint8_t dev_specific_param;
679

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

    
688
    if (bdrv_is_read_only(s->bs)) {
689
        dev_specific_param = 0x80; /* Readonly.  */
690
    } else {
691
        dev_specific_param = 0x00;
692
    }
693

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

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

    
727
    if (page_control == 3) { /* Saved Values */
728
        return -1; /* ILLEGAL_REQUEST */
729
    }
730

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

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

    
763
static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
764
{
765
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
766
    int start_track, format, msf, toclen;
767
    uint64_t nb_sectors;
768

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

    
798
static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
799
{
800
    SCSIRequest *req = &r->req;
801
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
802
    uint64_t nb_sectors;
803
    int buflen = 0;
804
    int ret;
805

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

    
926
            /* set TPE bit if the format supports discard */
927
            if (s->qdev.conf.discard_granularity) {
928
                outbuf[14] = 0x80;
929
            }
930

    
931
            /* Protection, exponent and lowest lba field left blank. */
932
            buflen = req->cmd.xfer;
933
            break;
934
        }
935
        DPRINTF("Unsupported Service Action In\n");
936
        goto illegal_request;
937
    case REPORT_LUNS:
938
        if (req->cmd.xfer < 16)
939
            goto illegal_request;
940
        memset(outbuf, 0, 16);
941
        outbuf[3] = 8;
942
        buflen = 16;
943
        break;
944
    case VERIFY:
945
        break;
946
    case REZERO_UNIT:
947
        DPRINTF("Rezero Unit\n");
948
        if (!bdrv_is_inserted(s->bs)) {
949
            goto not_ready;
950
        }
951
        break;
952
    default:
953
        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
954
        return -1;
955
    }
956
    scsi_req_set_status(r, GOOD, SENSE_CODE(NO_SENSE));
957
    return buflen;
958

    
959
not_ready:
960
    if (!bdrv_is_inserted(s->bs)) {
961
        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(NO_MEDIUM));
962
    } else {
963
        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(LUN_NOT_READY));
964
    }
965
    return -1;
966

    
967
illegal_request:
968
    scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_FIELD));
969
    return -1;
970
}
971

    
972
/* Execute a scsi command.  Returns the length of the data expected by the
973
   command.  This will be Positive for data transfers from the device
974
   (eg. disk reads), negative for transfers to the device (eg. disk writes),
975
   and zero if the command does not transfer any data.  */
976

    
977
static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
978
{
979
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
980
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
981
    int32_t len;
982
    int is_write;
983
    uint8_t command;
984
    uint8_t *outbuf;
985
    int rc;
986

    
987
    scsi_req_enqueue(req);
988
    command = buf[0];
989
    outbuf = (uint8_t *)r->iov.iov_base;
990
    is_write = 0;
991
    DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]);
992

    
993
    if (scsi_req_parse(&r->req, buf) != 0) {
994
        BADF("Unsupported command length, command %x\n", command);
995
        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
996
        return 0;
997
    }
998
#ifdef DEBUG_SCSI
999
    {
1000
        int i;
1001
        for (i = 1; i < r->req.cmd.len; i++) {
1002
            printf(" 0x%02x", buf[i]);
1003
        }
1004
        printf("\n");
1005
    }
1006
#endif
1007

    
1008
    if (req->lun || buf[1] >> 5) {
1009
        /* Only LUN 0 supported.  */
1010
        DPRINTF("Unimplemented LUN %d\n", req->lun ? req->lun : buf[1] >> 5);
1011
        if (command != REQUEST_SENSE && command != INQUIRY) {
1012
            scsi_command_complete(r, CHECK_CONDITION,
1013
                                  SENSE_CODE(LUN_NOT_SUPPORTED));
1014
            return 0;
1015
        }
1016
    }
1017
    switch (command) {
1018
    case TEST_UNIT_READY:
1019
    case REQUEST_SENSE:
1020
    case INQUIRY:
1021
    case MODE_SENSE:
1022
    case MODE_SENSE_10:
1023
    case RESERVE:
1024
    case RESERVE_10:
1025
    case RELEASE:
1026
    case RELEASE_10:
1027
    case START_STOP:
1028
    case ALLOW_MEDIUM_REMOVAL:
1029
    case READ_CAPACITY:
1030
    case SYNCHRONIZE_CACHE:
1031
    case READ_TOC:
1032
    case GET_CONFIGURATION:
1033
    case SERVICE_ACTION_IN:
1034
    case REPORT_LUNS:
1035
    case VERIFY:
1036
    case REZERO_UNIT:
1037
        rc = scsi_disk_emulate_command(r, outbuf);
1038
        if (rc < 0) {
1039
            return 0;
1040
        }
1041

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

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

    
1102
        if (r->req.cmd.lba > s->max_lba) {
1103
            goto illegal_lba;
1104
        }
1105

    
1106
        /*
1107
         * We only support WRITE SAME with the unmap bit set for now.
1108
         */
1109
        if (!(buf[1] & 0x8)) {
1110
            goto fail;
1111
        }
1112

    
1113
        rc = bdrv_discard(s->bs, r->req.cmd.lba * s->cluster_size,
1114
                          len * s->cluster_size);
1115
        if (rc < 0) {
1116
            /* XXX: better error code ?*/
1117
            goto fail;
1118
        }
1119

    
1120
        break;
1121
    default:
1122
        DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1123
        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
1124
        return 0;
1125
    fail:
1126
        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_FIELD));
1127
        return 0;
1128
    illegal_lba:
1129
        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(LBA_OUT_OF_RANGE));
1130
        return 0;
1131
    }
1132
    if (r->sector_count == 0 && r->iov.iov_len == 0) {
1133
        scsi_command_complete(r, GOOD, SENSE_CODE(NO_SENSE));
1134
    }
1135
    len = r->sector_count * 512 + r->iov.iov_len;
1136
    if (is_write) {
1137
        len = -len;
1138
    } else {
1139
        if (!r->sector_count)
1140
            r->sector_count = -1;
1141
    }
1142
    return len;
1143
}
1144

    
1145
static void scsi_disk_reset(DeviceState *dev)
1146
{
1147
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1148
    uint64_t nb_sectors;
1149

    
1150
    scsi_device_purge_requests(&s->qdev);
1151

    
1152
    bdrv_get_geometry(s->bs, &nb_sectors);
1153
    nb_sectors /= s->cluster_size;
1154
    if (nb_sectors) {
1155
        nb_sectors--;
1156
    }
1157
    s->max_lba = nb_sectors;
1158
}
1159

    
1160
static void scsi_destroy(SCSIDevice *dev)
1161
{
1162
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1163

    
1164
    scsi_device_purge_requests(&s->qdev);
1165
    blockdev_mark_auto_del(s->qdev.conf.bs);
1166
}
1167

    
1168
static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
1169
{
1170
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1171
    DriveInfo *dinfo;
1172

    
1173
    if (!s->qdev.conf.bs) {
1174
        error_report("scsi-disk: drive property not set");
1175
        return -1;
1176
    }
1177
    s->bs = s->qdev.conf.bs;
1178
    s->drive_kind = kind;
1179

    
1180
    if (kind == SCSI_HD && !bdrv_is_inserted(s->bs)) {
1181
        error_report("Device needs media, but drive is empty");
1182
        return -1;
1183
    }
1184

    
1185
    if (!s->serial) {
1186
        /* try to fall back to value set with legacy -drive serial=... */
1187
        dinfo = drive_get_by_blockdev(s->bs);
1188
        s->serial = qemu_strdup(*dinfo->serial ? dinfo->serial : "0");
1189
    }
1190

    
1191
    if (!s->version) {
1192
        s->version = qemu_strdup(QEMU_VERSION);
1193
    }
1194

    
1195
    if (bdrv_is_sg(s->bs)) {
1196
        error_report("scsi-disk: unwanted /dev/sg*");
1197
        return -1;
1198
    }
1199

    
1200
    if (kind == SCSI_CD) {
1201
        s->qdev.blocksize = 2048;
1202
    } else {
1203
        s->qdev.blocksize = s->qdev.conf.logical_block_size;
1204
    }
1205
    s->cluster_size = s->qdev.blocksize / 512;
1206
    s->bs->buffer_alignment = s->qdev.blocksize;
1207

    
1208
    s->qdev.type = TYPE_DISK;
1209
    qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
1210
    bdrv_set_removable(s->bs, kind == SCSI_CD);
1211
    add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
1212
    return 0;
1213
}
1214

    
1215
static int scsi_hd_initfn(SCSIDevice *dev)
1216
{
1217
    return scsi_initfn(dev, SCSI_HD);
1218
}
1219

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

    
1225
static int scsi_disk_initfn(SCSIDevice *dev)
1226
{
1227
    SCSIDriveKind kind;
1228
    DriveInfo *dinfo;
1229

    
1230
    if (!dev->conf.bs) {
1231
        kind = SCSI_HD;         /* will die in scsi_initfn() */
1232
    } else {
1233
        dinfo = drive_get_by_blockdev(dev->conf.bs);
1234
        kind = dinfo->media_cd ? SCSI_CD : SCSI_HD;
1235
    }
1236

    
1237
    return scsi_initfn(dev, kind);
1238
}
1239

    
1240
#define DEFINE_SCSI_DISK_PROPERTIES()                           \
1241
    DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf),          \
1242
    DEFINE_PROP_STRING("ver",  SCSIDiskState, version),         \
1243
    DEFINE_PROP_STRING("serial",  SCSIDiskState, serial)
1244

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

    
1308
static void scsi_disk_register_devices(void)
1309
{
1310
    int i;
1311

    
1312
    for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1313
        scsi_qdev_register(&scsi_disk_info[i]);
1314
    }
1315
}
1316
device_init(scsi_disk_register_devices)