Statistics
| Branch: | Revision:

root / hw / scsi-disk.c @ 0fd76ff4

History | View | Annotate | Download (53 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 licensed 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
#include "block_int.h"
41

    
42
#ifdef __linux
43
#include <scsi/sg.h>
44
#endif
45

    
46
#define SCSI_DMA_BUF_SIZE    131072
47
#define SCSI_MAX_INQUIRY_LEN 256
48

    
49
typedef struct SCSIDiskState SCSIDiskState;
50

    
51
typedef struct SCSIDiskReq {
52
    SCSIRequest req;
53
    /* Both sector and sector_count are in terms of qemu 512 byte blocks.  */
54
    uint64_t sector;
55
    uint32_t sector_count;
56
    uint32_t buflen;
57
    struct iovec iov;
58
    QEMUIOVector qiov;
59
    BlockAcctCookie acct;
60
} SCSIDiskReq;
61

    
62
struct SCSIDiskState
63
{
64
    SCSIDevice qdev;
65
    uint32_t removable;
66
    bool media_changed;
67
    bool media_event;
68
    bool eject_request;
69
    QEMUBH *bh;
70
    char *version;
71
    char *serial;
72
    bool tray_open;
73
    bool tray_locked;
74
};
75

    
76
static int scsi_handle_rw_error(SCSIDiskReq *r, int error);
77

    
78
static void scsi_free_request(SCSIRequest *req)
79
{
80
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
81

    
82
    if (r->iov.iov_base) {
83
        qemu_vfree(r->iov.iov_base);
84
    }
85
}
86

    
87
/* Helper function for command completion with sense.  */
88
static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
89
{
90
    DPRINTF("Command complete tag=0x%x sense=%d/%d/%d\n",
91
            r->req.tag, sense.key, sense.asc, sense.ascq);
92
    scsi_req_build_sense(&r->req, sense);
93
    scsi_req_complete(&r->req, CHECK_CONDITION);
94
}
95

    
96
/* Cancel a pending data transfer.  */
97
static void scsi_cancel_io(SCSIRequest *req)
98
{
99
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
100

    
101
    DPRINTF("Cancel tag=0x%x\n", req->tag);
102
    if (r->req.aiocb) {
103
        bdrv_aio_cancel(r->req.aiocb);
104

    
105
        /* This reference was left in by scsi_*_data.  We take ownership of
106
         * it the moment scsi_req_cancel is called, independent of whether
107
         * bdrv_aio_cancel completes the request or not.  */
108
        scsi_req_unref(&r->req);
109
    }
110
    r->req.aiocb = NULL;
111
}
112

    
113
static uint32_t scsi_init_iovec(SCSIDiskReq *r)
114
{
115
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
116

    
117
    if (!r->iov.iov_base) {
118
        r->buflen = SCSI_DMA_BUF_SIZE;
119
        r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
120
    }
121
    r->iov.iov_len = MIN(r->sector_count * 512, r->buflen);
122
    qemu_iovec_init_external(&r->qiov, &r->iov, 1);
123
    return r->qiov.size / 512;
124
}
125

    
126
static void scsi_read_complete(void * opaque, int ret)
127
{
128
    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
129
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
130
    int n;
131

    
132
    if (r->req.aiocb != NULL) {
133
        r->req.aiocb = NULL;
134
        bdrv_acct_done(s->qdev.conf.bs, &r->acct);
135
    }
136

    
137
    if (ret) {
138
        if (scsi_handle_rw_error(r, -ret)) {
139
            goto done;
140
        }
141
    }
142

    
143
    DPRINTF("Data ready tag=0x%x len=%zd\n", r->req.tag, r->qiov.size);
144

    
145
    n = r->qiov.size / 512;
146
    r->sector += n;
147
    r->sector_count -= n;
148
    scsi_req_data(&r->req, r->qiov.size);
149

    
150
done:
151
    if (!r->req.io_canceled) {
152
        scsi_req_unref(&r->req);
153
    }
154
}
155

    
156
static void scsi_flush_complete(void * opaque, int ret)
157
{
158
    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
159
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
160

    
161
    if (r->req.aiocb != NULL) {
162
        r->req.aiocb = NULL;
163
        bdrv_acct_done(s->qdev.conf.bs, &r->acct);
164
    }
165

    
166
    if (ret < 0) {
167
        if (scsi_handle_rw_error(r, -ret)) {
168
            goto done;
169
        }
170
    }
171

    
172
    scsi_req_complete(&r->req, GOOD);
173

    
174
done:
175
    if (!r->req.io_canceled) {
176
        scsi_req_unref(&r->req);
177
    }
178
}
179

    
180
/* Read more data from scsi device into buffer.  */
181
static void scsi_read_data(SCSIRequest *req)
182
{
183
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
184
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
185
    uint32_t n;
186

    
187
    if (r->sector_count == (uint32_t)-1) {
188
        DPRINTF("Read buf_len=%zd\n", r->iov.iov_len);
189
        r->sector_count = 0;
190
        scsi_req_data(&r->req, r->iov.iov_len);
191
        return;
192
    }
193
    DPRINTF("Read sector_count=%d\n", r->sector_count);
194
    if (r->sector_count == 0) {
195
        /* This also clears the sense buffer for REQUEST SENSE.  */
196
        scsi_req_complete(&r->req, GOOD);
197
        return;
198
    }
199

    
200
    /* No data transfer may already be in progress */
201
    assert(r->req.aiocb == NULL);
202

    
203
    /* The request is used as the AIO opaque value, so add a ref.  */
204
    scsi_req_ref(&r->req);
205
    if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
206
        DPRINTF("Data transfer direction invalid\n");
207
        scsi_read_complete(r, -EINVAL);
208
        return;
209
    }
210

    
211
    if (s->tray_open) {
212
        scsi_read_complete(r, -ENOMEDIUM);
213
        return;
214
    }
215

    
216
    n = scsi_init_iovec(r);
217
    bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
218
    r->req.aiocb = bdrv_aio_readv(s->qdev.conf.bs, r->sector, &r->qiov, n,
219
                              scsi_read_complete, r);
220
    if (r->req.aiocb == NULL) {
221
        scsi_read_complete(r, -EIO);
222
    }
223
}
224

    
225
/*
226
 * scsi_handle_rw_error has two return values.  0 means that the error
227
 * must be ignored, 1 means that the error has been processed and the
228
 * caller should not do anything else for this request.  Note that
229
 * scsi_handle_rw_error always manages its reference counts, independent
230
 * of the return value.
231
 */
232
static int scsi_handle_rw_error(SCSIDiskReq *r, int error)
233
{
234
    int is_read = (r->req.cmd.xfer == SCSI_XFER_FROM_DEV);
235
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
236
    BlockErrorAction action = bdrv_get_on_error(s->qdev.conf.bs, is_read);
237

    
238
    if (action == BLOCK_ERR_IGNORE) {
239
        bdrv_mon_event(s->qdev.conf.bs, BDRV_ACTION_IGNORE, is_read);
240
        return 0;
241
    }
242

    
243
    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
244
            || action == BLOCK_ERR_STOP_ANY) {
245

    
246
        bdrv_mon_event(s->qdev.conf.bs, BDRV_ACTION_STOP, is_read);
247
        vm_stop(RUN_STATE_IO_ERROR);
248
        bdrv_iostatus_set_err(s->qdev.conf.bs, error);
249
        scsi_req_retry(&r->req);
250
    } else {
251
        switch (error) {
252
        case ENOMEDIUM:
253
            scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
254
            break;
255
        case ENOMEM:
256
            scsi_check_condition(r, SENSE_CODE(TARGET_FAILURE));
257
            break;
258
        case EINVAL:
259
            scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
260
            break;
261
        default:
262
            scsi_check_condition(r, SENSE_CODE(IO_ERROR));
263
            break;
264
        }
265
        bdrv_mon_event(s->qdev.conf.bs, BDRV_ACTION_REPORT, is_read);
266
    }
267
    return 1;
268
}
269

    
270
static void scsi_write_complete(void * opaque, int ret)
271
{
272
    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
273
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
274
    uint32_t n;
275

    
276
    if (r->req.aiocb != NULL) {
277
        r->req.aiocb = NULL;
278
        bdrv_acct_done(s->qdev.conf.bs, &r->acct);
279
    }
280

    
281
    if (ret) {
282
        if (scsi_handle_rw_error(r, -ret)) {
283
            goto done;
284
        }
285
    }
286

    
287
    n = r->qiov.size / 512;
288
    r->sector += n;
289
    r->sector_count -= n;
290
    if (r->sector_count == 0) {
291
        scsi_req_complete(&r->req, GOOD);
292
    } else {
293
        scsi_init_iovec(r);
294
        DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, r->qiov.size);
295
        scsi_req_data(&r->req, r->qiov.size);
296
    }
297

    
298
done:
299
    if (!r->req.io_canceled) {
300
        scsi_req_unref(&r->req);
301
    }
302
}
303

    
304
static void scsi_write_data(SCSIRequest *req)
305
{
306
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
307
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
308
    uint32_t n;
309

    
310
    /* No data transfer may already be in progress */
311
    assert(r->req.aiocb == NULL);
312

    
313
    /* The request is used as the AIO opaque value, so add a ref.  */
314
    scsi_req_ref(&r->req);
315
    if (r->req.cmd.mode != SCSI_XFER_TO_DEV) {
316
        DPRINTF("Data transfer direction invalid\n");
317
        scsi_write_complete(r, -EINVAL);
318
        return;
319
    }
320

    
321
    n = r->qiov.size / 512;
322
    if (n) {
323
        if (s->tray_open) {
324
            scsi_write_complete(r, -ENOMEDIUM);
325
            return;
326
        }
327
        bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE);
328
        r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, r->sector, &r->qiov, n,
329
                                       scsi_write_complete, r);
330
        if (r->req.aiocb == NULL) {
331
            scsi_write_complete(r, -ENOMEM);
332
        }
333
    } else {
334
        /* Called for the first time.  Ask the driver to send us more data.  */
335
        scsi_write_complete(r, 0);
336
    }
337
}
338

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

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

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

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

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

    
367
        outbuf[buflen++] = s->qdev.type & 0x1f;
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
            if (s->serial) {
380
                outbuf[buflen++] = 0x80; // unit serial number
381
            }
382
            outbuf[buflen++] = 0x83; // device identification
383
            if (s->qdev.type == TYPE_DISK) {
384
                outbuf[buflen++] = 0xb0; // block limits
385
                outbuf[buflen++] = 0xb2; // thin provisioning
386
            }
387
            outbuf[pages] = buflen - pages - 1; // number of pages
388
            break;
389
        }
390
        case 0x80: /* Device serial number, optional */
391
        {
392
            int l;
393

    
394
            if (!s->serial) {
395
                DPRINTF("Inquiry (EVPD[Serial number] not supported\n");
396
                return -1;
397
            }
398

    
399
            l = strlen(s->serial);
400
            if (l > req->cmd.xfer) {
401
                l = req->cmd.xfer;
402
            }
403
            if (l > 20) {
404
                l = 20;
405
            }
406

    
407
            DPRINTF("Inquiry EVPD[Serial number] "
408
                    "buffer size %zd\n", req->cmd.xfer);
409
            outbuf[buflen++] = l;
410
            memcpy(outbuf+buflen, s->serial, l);
411
            buflen += l;
412
            break;
413
        }
414

    
415
        case 0x83: /* Device identification page, mandatory */
416
        {
417
            int max_len = 255 - 8;
418
            int id_len = strlen(bdrv_get_device_name(s->qdev.conf.bs));
419

    
420
            if (id_len > max_len) {
421
                id_len = max_len;
422
            }
423
            DPRINTF("Inquiry EVPD[Device identification] "
424
                    "buffer size %zd\n", req->cmd.xfer);
425

    
426
            outbuf[buflen++] = 4 + id_len;
427
            outbuf[buflen++] = 0x2; // ASCII
428
            outbuf[buflen++] = 0;   // not officially assigned
429
            outbuf[buflen++] = 0;   // reserved
430
            outbuf[buflen++] = id_len; // length of data following
431

    
432
            memcpy(outbuf+buflen, bdrv_get_device_name(s->qdev.conf.bs), id_len);
433
            buflen += id_len;
434
            break;
435
        }
436
        case 0xb0: /* block limits */
437
        {
438
            unsigned int unmap_sectors =
439
                    s->qdev.conf.discard_granularity / s->qdev.blocksize;
440
            unsigned int min_io_size =
441
                    s->qdev.conf.min_io_size / s->qdev.blocksize;
442
            unsigned int opt_io_size =
443
                    s->qdev.conf.opt_io_size / s->qdev.blocksize;
444

    
445
            if (s->qdev.type == TYPE_ROM) {
446
                DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
447
                        page_code);
448
                return -1;
449
            }
450
            /* required VPD size with unmap support */
451
            outbuf[3] = buflen = 0x3c;
452

    
453
            memset(outbuf + 4, 0, buflen - 4);
454

    
455
            /* optimal transfer length granularity */
456
            outbuf[6] = (min_io_size >> 8) & 0xff;
457
            outbuf[7] = min_io_size & 0xff;
458

    
459
            /* optimal transfer length */
460
            outbuf[12] = (opt_io_size >> 24) & 0xff;
461
            outbuf[13] = (opt_io_size >> 16) & 0xff;
462
            outbuf[14] = (opt_io_size >> 8) & 0xff;
463
            outbuf[15] = opt_io_size & 0xff;
464

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

    
490
    /* Standard INQUIRY data */
491
    if (req->cmd.buf[2] != 0) {
492
        BADF("Error: Inquiry (STANDARD) page or code "
493
             "is non-zero [%02X]\n", req->cmd.buf[2]);
494
        return -1;
495
    }
496

    
497
    /* PAGE CODE == 0 */
498
    if (req->cmd.xfer < 5) {
499
        BADF("Error: Inquiry (STANDARD) buffer size %zd "
500
             "is less than 5\n", req->cmd.xfer);
501
        return -1;
502
    }
503

    
504
    buflen = req->cmd.xfer;
505
    if (buflen > SCSI_MAX_INQUIRY_LEN) {
506
        buflen = SCSI_MAX_INQUIRY_LEN;
507
    }
508
    memset(outbuf, 0, buflen);
509

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

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

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

    
542
static inline bool media_is_dvd(SCSIDiskState *s)
543
{
544
    uint64_t nb_sectors;
545
    if (s->qdev.type != TYPE_ROM) {
546
        return false;
547
    }
548
    if (!bdrv_is_inserted(s->qdev.conf.bs)) {
549
        return false;
550
    }
551
    bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
552
    return nb_sectors > CD_MAX_SECTORS;
553
}
554

    
555
static inline bool media_is_cd(SCSIDiskState *s)
556
{
557
    uint64_t nb_sectors;
558
    if (s->qdev.type != TYPE_ROM) {
559
        return false;
560
    }
561
    if (!bdrv_is_inserted(s->qdev.conf.bs)) {
562
        return false;
563
    }
564
    bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
565
    return nb_sectors <= CD_MAX_SECTORS;
566
}
567

    
568
static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r,
569
                                   uint8_t *outbuf)
570
{
571
    static const int rds_caps_size[5] = {
572
        [0] = 2048 + 4,
573
        [1] = 4 + 4,
574
        [3] = 188 + 4,
575
        [4] = 2048 + 4,
576
    };
577

    
578
    uint8_t media = r->req.cmd.buf[1];
579
    uint8_t layer = r->req.cmd.buf[6];
580
    uint8_t format = r->req.cmd.buf[7];
581
    int size = -1;
582

    
583
    if (s->qdev.type != TYPE_ROM) {
584
        return -1;
585
    }
586
    if (media != 0) {
587
        scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
588
        return -1;
589
    }
590

    
591
    if (format != 0xff) {
592
        if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
593
            scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
594
            return -1;
595
        }
596
        if (media_is_cd(s)) {
597
            scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT));
598
            return -1;
599
        }
600
        if (format >= ARRAY_SIZE(rds_caps_size)) {
601
            return -1;
602
        }
603
        size = rds_caps_size[format];
604
        memset(outbuf, 0, size);
605
    }
606

    
607
    switch (format) {
608
    case 0x00: {
609
        /* Physical format information */
610
        uint64_t nb_sectors;
611
        if (layer != 0) {
612
            goto fail;
613
        }
614
        bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
615

    
616
        outbuf[4] = 1;   /* DVD-ROM, part version 1 */
617
        outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
618
        outbuf[6] = 1;   /* one layer, read-only (per MMC-2 spec) */
619
        outbuf[7] = 0;   /* default densities */
620

    
621
        stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */
622
        stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */
623
        break;
624
    }
625

    
626
    case 0x01: /* DVD copyright information, all zeros */
627
        break;
628

    
629
    case 0x03: /* BCA information - invalid field for no BCA info */
630
        return -1;
631

    
632
    case 0x04: /* DVD disc manufacturing information, all zeros */
633
        break;
634

    
635
    case 0xff: { /* List capabilities */
636
        int i;
637
        size = 4;
638
        for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) {
639
            if (!rds_caps_size[i]) {
640
                continue;
641
            }
642
            outbuf[size] = i;
643
            outbuf[size + 1] = 0x40; /* Not writable, readable */
644
            stw_be_p(&outbuf[size + 2], rds_caps_size[i]);
645
            size += 4;
646
        }
647
        break;
648
     }
649

    
650
    default:
651
        return -1;
652
    }
653

    
654
    /* Size of buffer, not including 2 byte size field */
655
    stw_be_p(outbuf, size - 2);
656
    return size;
657

    
658
fail:
659
    return -1;
660
}
661

    
662
static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf)
663
{
664
    uint8_t event_code, media_status;
665

    
666
    media_status = 0;
667
    if (s->tray_open) {
668
        media_status = MS_TRAY_OPEN;
669
    } else if (bdrv_is_inserted(s->qdev.conf.bs)) {
670
        media_status = MS_MEDIA_PRESENT;
671
    }
672

    
673
    /* Event notification descriptor */
674
    event_code = MEC_NO_CHANGE;
675
    if (media_status != MS_TRAY_OPEN) {
676
        if (s->media_event) {
677
            event_code = MEC_NEW_MEDIA;
678
            s->media_event = false;
679
        } else if (s->eject_request) {
680
            event_code = MEC_EJECT_REQUESTED;
681
            s->eject_request = false;
682
        }
683
    }
684

    
685
    outbuf[0] = event_code;
686
    outbuf[1] = media_status;
687

    
688
    /* These fields are reserved, just clear them. */
689
    outbuf[2] = 0;
690
    outbuf[3] = 0;
691
    return 4;
692
}
693

    
694
static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r,
695
                                              uint8_t *outbuf)
696
{
697
    int size;
698
    uint8_t *buf = r->req.cmd.buf;
699
    uint8_t notification_class_request = buf[4];
700
    if (s->qdev.type != TYPE_ROM) {
701
        return -1;
702
    }
703
    if ((buf[1] & 1) == 0) {
704
        /* asynchronous */
705
        return -1;
706
    }
707

    
708
    size = 4;
709
    outbuf[0] = outbuf[1] = 0;
710
    outbuf[3] = 1 << GESN_MEDIA; /* supported events */
711
    if (notification_class_request & (1 << GESN_MEDIA)) {
712
        outbuf[2] = GESN_MEDIA;
713
        size += scsi_event_status_media(s, &outbuf[size]);
714
    } else {
715
        outbuf[2] = 0x80;
716
    }
717
    stw_be_p(outbuf, size - 4);
718
    return size;
719
}
720

    
721
static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf)
722
{
723
    int current;
724

    
725
    if (s->qdev.type != TYPE_ROM) {
726
        return -1;
727
    }
728
    current = media_is_dvd(s) ? MMC_PROFILE_DVD_ROM : MMC_PROFILE_CD_ROM;
729
    memset(outbuf, 0, 40);
730
    stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */
731
    stw_be_p(&outbuf[6], current);
732
    /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */
733
    outbuf[10] = 0x03; /* persistent, current */
734
    outbuf[11] = 8; /* two profiles */
735
    stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM);
736
    outbuf[14] = (current == MMC_PROFILE_DVD_ROM);
737
    stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM);
738
    outbuf[18] = (current == MMC_PROFILE_CD_ROM);
739
    /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */
740
    stw_be_p(&outbuf[20], 1);
741
    outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */
742
    outbuf[23] = 8;
743
    stl_be_p(&outbuf[24], 1); /* SCSI */
744
    outbuf[28] = 1; /* DBE = 1, mandatory */
745
    /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */
746
    stw_be_p(&outbuf[32], 3);
747
    outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */
748
    outbuf[35] = 4;
749
    outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */
750
    /* TODO: Random readable, CD read, DVD read, drive serial number,
751
       power management */
752
    return 40;
753
}
754

    
755
static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf)
756
{
757
    if (s->qdev.type != TYPE_ROM) {
758
        return -1;
759
    }
760
    memset(outbuf, 0, 8);
761
    outbuf[5] = 1; /* CD-ROM */
762
    return 8;
763
}
764

    
765
static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
766
                           int page_control)
767
{
768
    static const int mode_sense_valid[0x3f] = {
769
        [MODE_PAGE_HD_GEOMETRY]            = (1 << TYPE_DISK),
770
        [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK),
771
        [MODE_PAGE_CACHING]                = (1 << TYPE_DISK) | (1 << TYPE_ROM),
772
        [MODE_PAGE_R_W_ERROR]              = (1 << TYPE_DISK) | (1 << TYPE_ROM),
773
        [MODE_PAGE_AUDIO_CTL]              = (1 << TYPE_ROM),
774
        [MODE_PAGE_CAPABILITIES]           = (1 << TYPE_ROM),
775
    };
776

    
777
    BlockDriverState *bdrv = s->qdev.conf.bs;
778
    int cylinders, heads, secs;
779
    uint8_t *p = *p_outbuf;
780

    
781
    if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) {
782
        return -1;
783
    }
784

    
785
    p[0] = page;
786

    
787
    /*
788
     * If Changeable Values are requested, a mask denoting those mode parameters
789
     * that are changeable shall be returned. As we currently don't support
790
     * parameter changes via MODE_SELECT all bits are returned set to zero.
791
     * The buffer was already menset to zero by the caller of this function.
792
     */
793
    switch (page) {
794
    case MODE_PAGE_HD_GEOMETRY:
795
        p[1] = 0x16;
796
        if (page_control == 1) { /* Changeable Values */
797
            break;
798
        }
799
        /* if a geometry hint is available, use it */
800
        bdrv_guess_geometry(bdrv, &cylinders, &heads, &secs);
801
        p[2] = (cylinders >> 16) & 0xff;
802
        p[3] = (cylinders >> 8) & 0xff;
803
        p[4] = cylinders & 0xff;
804
        p[5] = heads & 0xff;
805
        /* Write precomp start cylinder, disabled */
806
        p[6] = (cylinders >> 16) & 0xff;
807
        p[7] = (cylinders >> 8) & 0xff;
808
        p[8] = cylinders & 0xff;
809
        /* Reduced current start cylinder, disabled */
810
        p[9] = (cylinders >> 16) & 0xff;
811
        p[10] = (cylinders >> 8) & 0xff;
812
        p[11] = cylinders & 0xff;
813
        /* Device step rate [ns], 200ns */
814
        p[12] = 0;
815
        p[13] = 200;
816
        /* Landing zone cylinder */
817
        p[14] = 0xff;
818
        p[15] =  0xff;
819
        p[16] = 0xff;
820
        /* Medium rotation rate [rpm], 5400 rpm */
821
        p[20] = (5400 >> 8) & 0xff;
822
        p[21] = 5400 & 0xff;
823
        break;
824

    
825
    case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY:
826
        p[1] = 0x1e;
827
        if (page_control == 1) { /* Changeable Values */
828
            break;
829
        }
830
        /* Transfer rate [kbit/s], 5Mbit/s */
831
        p[2] = 5000 >> 8;
832
        p[3] = 5000 & 0xff;
833
        /* if a geometry hint is available, use it */
834
        bdrv_guess_geometry(bdrv, &cylinders, &heads, &secs);
835
        p[4] = heads & 0xff;
836
        p[5] = secs & 0xff;
837
        p[6] = s->qdev.blocksize >> 8;
838
        p[8] = (cylinders >> 8) & 0xff;
839
        p[9] = cylinders & 0xff;
840
        /* Write precomp start cylinder, disabled */
841
        p[10] = (cylinders >> 8) & 0xff;
842
        p[11] = cylinders & 0xff;
843
        /* Reduced current start cylinder, disabled */
844
        p[12] = (cylinders >> 8) & 0xff;
845
        p[13] = cylinders & 0xff;
846
        /* Device step rate [100us], 100us */
847
        p[14] = 0;
848
        p[15] = 1;
849
        /* Device step pulse width [us], 1us */
850
        p[16] = 1;
851
        /* Device head settle delay [100us], 100us */
852
        p[17] = 0;
853
        p[18] = 1;
854
        /* Motor on delay [0.1s], 0.1s */
855
        p[19] = 1;
856
        /* Motor off delay [0.1s], 0.1s */
857
        p[20] = 1;
858
        /* Medium rotation rate [rpm], 5400 rpm */
859
        p[28] = (5400 >> 8) & 0xff;
860
        p[29] = 5400 & 0xff;
861
        break;
862

    
863
    case MODE_PAGE_CACHING:
864
        p[0] = 8;
865
        p[1] = 0x12;
866
        if (page_control == 1) { /* Changeable Values */
867
            break;
868
        }
869
        if (bdrv_enable_write_cache(s->qdev.conf.bs)) {
870
            p[2] = 4; /* WCE */
871
        }
872
        break;
873

    
874
    case MODE_PAGE_R_W_ERROR:
875
        p[1] = 10;
876
        p[2] = 0x80; /* Automatic Write Reallocation Enabled */
877
        if (s->qdev.type == TYPE_ROM) {
878
            p[3] = 0x20; /* Read Retry Count */
879
        }
880
        break;
881

    
882
    case MODE_PAGE_AUDIO_CTL:
883
        p[1] = 14;
884
        break;
885

    
886
    case MODE_PAGE_CAPABILITIES:
887
        p[1] = 0x14;
888
        if (page_control == 1) { /* Changeable Values */
889
            break;
890
        }
891

    
892
        p[2] = 0x3b; /* CD-R & CD-RW read */
893
        p[3] = 0; /* Writing not supported */
894
        p[4] = 0x7f; /* Audio, composite, digital out,
895
                        mode 2 form 1&2, multi session */
896
        p[5] = 0xff; /* CD DA, DA accurate, RW supported,
897
                        RW corrected, C2 errors, ISRC,
898
                        UPC, Bar code */
899
        p[6] = 0x2d | (s->tray_locked ? 2 : 0);
900
        /* Locking supported, jumper present, eject, tray */
901
        p[7] = 0; /* no volume & mute control, no
902
                     changer */
903
        p[8] = (50 * 176) >> 8; /* 50x read speed */
904
        p[9] = (50 * 176) & 0xff;
905
        p[10] = 2 >> 8; /* Two volume levels */
906
        p[11] = 2 & 0xff;
907
        p[12] = 2048 >> 8; /* 2M buffer */
908
        p[13] = 2048 & 0xff;
909
        p[14] = (16 * 176) >> 8; /* 16x read speed current */
910
        p[15] = (16 * 176) & 0xff;
911
        p[18] = (16 * 176) >> 8; /* 16x write speed */
912
        p[19] = (16 * 176) & 0xff;
913
        p[20] = (16 * 176) >> 8; /* 16x write speed current */
914
        p[21] = (16 * 176) & 0xff;
915
        break;
916

    
917
    default:
918
        return -1;
919
    }
920

    
921
    *p_outbuf += p[1] + 2;
922
    return p[1] + 2;
923
}
924

    
925
static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf)
926
{
927
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
928
    uint64_t nb_sectors;
929
    int page, dbd, buflen, ret, page_control;
930
    uint8_t *p;
931
    uint8_t dev_specific_param;
932

    
933
    dbd = r->req.cmd.buf[1]  & 0x8;
934
    page = r->req.cmd.buf[2] & 0x3f;
935
    page_control = (r->req.cmd.buf[2] & 0xc0) >> 6;
936
    DPRINTF("Mode Sense(%d) (page %d, xfer %zd, page_control %d)\n",
937
        (r->req.cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, r->req.cmd.xfer, page_control);
938
    memset(outbuf, 0, r->req.cmd.xfer);
939
    p = outbuf;
940

    
941
    if (bdrv_is_read_only(s->qdev.conf.bs)) {
942
        dev_specific_param = 0x80; /* Readonly.  */
943
    } else {
944
        dev_specific_param = 0x00;
945
    }
946

    
947
    if (r->req.cmd.buf[0] == MODE_SENSE) {
948
        p[1] = 0; /* Default media type.  */
949
        p[2] = dev_specific_param;
950
        p[3] = 0; /* Block descriptor length.  */
951
        p += 4;
952
    } else { /* MODE_SENSE_10 */
953
        p[2] = 0; /* Default media type.  */
954
        p[3] = dev_specific_param;
955
        p[6] = p[7] = 0; /* Block descriptor length.  */
956
        p += 8;
957
    }
958

    
959
    /* MMC prescribes that CD/DVD drives have no block descriptors.  */
960
    bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
961
    if (!dbd && s->qdev.type == TYPE_DISK && nb_sectors) {
962
        if (r->req.cmd.buf[0] == MODE_SENSE) {
963
            outbuf[3] = 8; /* Block descriptor length  */
964
        } else { /* MODE_SENSE_10 */
965
            outbuf[7] = 8; /* Block descriptor length  */
966
        }
967
        nb_sectors /= (s->qdev.blocksize / 512);
968
        if (nb_sectors > 0xffffff) {
969
            nb_sectors = 0;
970
        }
971
        p[0] = 0; /* media density code */
972
        p[1] = (nb_sectors >> 16) & 0xff;
973
        p[2] = (nb_sectors >> 8) & 0xff;
974
        p[3] = nb_sectors & 0xff;
975
        p[4] = 0; /* reserved */
976
        p[5] = 0; /* bytes 5-7 are the sector size in bytes */
977
        p[6] = s->qdev.blocksize >> 8;
978
        p[7] = 0;
979
        p += 8;
980
    }
981

    
982
    if (page_control == 3) {
983
        /* Saved Values */
984
        scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED));
985
        return -1;
986
    }
987

    
988
    if (page == 0x3f) {
989
        for (page = 0; page <= 0x3e; page++) {
990
            mode_sense_page(s, page, &p, page_control);
991
        }
992
    } else {
993
        ret = mode_sense_page(s, page, &p, page_control);
994
        if (ret == -1) {
995
            return -1;
996
        }
997
    }
998

    
999
    buflen = p - outbuf;
1000
    /*
1001
     * The mode data length field specifies the length in bytes of the
1002
     * following data that is available to be transferred. The mode data
1003
     * length does not include itself.
1004
     */
1005
    if (r->req.cmd.buf[0] == MODE_SENSE) {
1006
        outbuf[0] = buflen - 1;
1007
    } else { /* MODE_SENSE_10 */
1008
        outbuf[0] = ((buflen - 2) >> 8) & 0xff;
1009
        outbuf[1] = (buflen - 2) & 0xff;
1010
    }
1011
    if (buflen > r->req.cmd.xfer) {
1012
        buflen = r->req.cmd.xfer;
1013
    }
1014
    return buflen;
1015
}
1016

    
1017
static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
1018
{
1019
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1020
    int start_track, format, msf, toclen;
1021
    uint64_t nb_sectors;
1022

    
1023
    msf = req->cmd.buf[1] & 2;
1024
    format = req->cmd.buf[2] & 0xf;
1025
    start_track = req->cmd.buf[6];
1026
    bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1027
    DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
1028
    nb_sectors /= s->qdev.blocksize / 512;
1029
    switch (format) {
1030
    case 0:
1031
        toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track);
1032
        break;
1033
    case 1:
1034
        /* multi session : only a single session defined */
1035
        toclen = 12;
1036
        memset(outbuf, 0, 12);
1037
        outbuf[1] = 0x0a;
1038
        outbuf[2] = 0x01;
1039
        outbuf[3] = 0x01;
1040
        break;
1041
    case 2:
1042
        toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track);
1043
        break;
1044
    default:
1045
        return -1;
1046
    }
1047
    if (toclen > req->cmd.xfer) {
1048
        toclen = req->cmd.xfer;
1049
    }
1050
    return toclen;
1051
}
1052

    
1053
static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
1054
{
1055
    SCSIRequest *req = &r->req;
1056
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1057
    bool start = req->cmd.buf[4] & 1;
1058
    bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */
1059

    
1060
    if (s->qdev.type == TYPE_ROM && loej) {
1061
        if (!start && !s->tray_open && s->tray_locked) {
1062
            scsi_check_condition(r,
1063
                                 bdrv_is_inserted(s->qdev.conf.bs)
1064
                                 ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED)
1065
                                 : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED));
1066
            return -1;
1067
        }
1068
        bdrv_eject(s->qdev.conf.bs, !start);
1069
        s->tray_open = !start;
1070
    }
1071
    return 0;
1072
}
1073

    
1074
static int scsi_disk_emulate_command(SCSIDiskReq *r)
1075
{
1076
    SCSIRequest *req = &r->req;
1077
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1078
    uint64_t nb_sectors;
1079
    uint8_t *outbuf;
1080
    int buflen = 0;
1081

    
1082
    if (!r->iov.iov_base) {
1083
        /*
1084
         * FIXME: we shouldn't return anything bigger than 4k, but the code
1085
         * requires the buffer to be as big as req->cmd.xfer in several
1086
         * places.  So, do not allow CDBs with a very large ALLOCATION
1087
         * LENGTH.  The real fix would be to modify scsi_read_data and
1088
         * dma_buf_read, so that they return data beyond the buflen
1089
         * as all zeros.
1090
         */
1091
        if (req->cmd.xfer > 65536) {
1092
            goto illegal_request;
1093
        }
1094
        r->buflen = MAX(4096, req->cmd.xfer);
1095
        r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
1096
    }
1097

    
1098
    outbuf = r->iov.iov_base;
1099
    switch (req->cmd.buf[0]) {
1100
    case TEST_UNIT_READY:
1101
        if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
1102
            goto not_ready;
1103
        }
1104
        break;
1105
    case INQUIRY:
1106
        buflen = scsi_disk_emulate_inquiry(req, outbuf);
1107
        if (buflen < 0) {
1108
            goto illegal_request;
1109
        }
1110
        break;
1111
    case MODE_SENSE:
1112
    case MODE_SENSE_10:
1113
        buflen = scsi_disk_emulate_mode_sense(r, outbuf);
1114
        if (buflen < 0) {
1115
            goto illegal_request;
1116
        }
1117
        break;
1118
    case READ_TOC:
1119
        buflen = scsi_disk_emulate_read_toc(req, outbuf);
1120
        if (buflen < 0) {
1121
            goto illegal_request;
1122
        }
1123
        break;
1124
    case RESERVE:
1125
        if (req->cmd.buf[1] & 1) {
1126
            goto illegal_request;
1127
        }
1128
        break;
1129
    case RESERVE_10:
1130
        if (req->cmd.buf[1] & 3) {
1131
            goto illegal_request;
1132
        }
1133
        break;
1134
    case RELEASE:
1135
        if (req->cmd.buf[1] & 1) {
1136
            goto illegal_request;
1137
        }
1138
        break;
1139
    case RELEASE_10:
1140
        if (req->cmd.buf[1] & 3) {
1141
            goto illegal_request;
1142
        }
1143
        break;
1144
    case START_STOP:
1145
        if (scsi_disk_emulate_start_stop(r) < 0) {
1146
            return -1;
1147
        }
1148
        break;
1149
    case ALLOW_MEDIUM_REMOVAL:
1150
        s->tray_locked = req->cmd.buf[4] & 1;
1151
        bdrv_lock_medium(s->qdev.conf.bs, req->cmd.buf[4] & 1);
1152
        break;
1153
    case READ_CAPACITY_10:
1154
        /* The normal LEN field for this command is zero.  */
1155
        memset(outbuf, 0, 8);
1156
        bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1157
        if (!nb_sectors) {
1158
            goto not_ready;
1159
        }
1160
        if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) {
1161
            goto illegal_request;
1162
        }
1163
        nb_sectors /= s->qdev.blocksize / 512;
1164
        /* Returned value is the address of the last sector.  */
1165
        nb_sectors--;
1166
        /* Remember the new size for read/write sanity checking. */
1167
        s->qdev.max_lba = nb_sectors;
1168
        /* Clip to 2TB, instead of returning capacity modulo 2TB. */
1169
        if (nb_sectors > UINT32_MAX) {
1170
            nb_sectors = UINT32_MAX;
1171
        }
1172
        outbuf[0] = (nb_sectors >> 24) & 0xff;
1173
        outbuf[1] = (nb_sectors >> 16) & 0xff;
1174
        outbuf[2] = (nb_sectors >> 8) & 0xff;
1175
        outbuf[3] = nb_sectors & 0xff;
1176
        outbuf[4] = 0;
1177
        outbuf[5] = 0;
1178
        outbuf[6] = s->qdev.blocksize >> 8;
1179
        outbuf[7] = 0;
1180
        buflen = 8;
1181
        break;
1182
    case MECHANISM_STATUS:
1183
        buflen = scsi_emulate_mechanism_status(s, outbuf);
1184
        if (buflen < 0) {
1185
            goto illegal_request;
1186
        }
1187
        break;
1188
    case GET_CONFIGURATION:
1189
        buflen = scsi_get_configuration(s, outbuf);
1190
        if (buflen < 0) {
1191
            goto illegal_request;
1192
        }
1193
        break;
1194
    case GET_EVENT_STATUS_NOTIFICATION:
1195
        buflen = scsi_get_event_status_notification(s, r, outbuf);
1196
        if (buflen < 0) {
1197
            goto illegal_request;
1198
        }
1199
        break;
1200
    case READ_DVD_STRUCTURE:
1201
        buflen = scsi_read_dvd_structure(s, r, outbuf);
1202
        if (buflen < 0) {
1203
            goto illegal_request;
1204
        }
1205
        break;
1206
    case SERVICE_ACTION_IN_16:
1207
        /* Service Action In subcommands. */
1208
        if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
1209
            DPRINTF("SAI READ CAPACITY(16)\n");
1210
            memset(outbuf, 0, req->cmd.xfer);
1211
            bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1212
            if (!nb_sectors) {
1213
                goto not_ready;
1214
            }
1215
            if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) {
1216
                goto illegal_request;
1217
            }
1218
            nb_sectors /= s->qdev.blocksize / 512;
1219
            /* Returned value is the address of the last sector.  */
1220
            nb_sectors--;
1221
            /* Remember the new size for read/write sanity checking. */
1222
            s->qdev.max_lba = nb_sectors;
1223
            outbuf[0] = (nb_sectors >> 56) & 0xff;
1224
            outbuf[1] = (nb_sectors >> 48) & 0xff;
1225
            outbuf[2] = (nb_sectors >> 40) & 0xff;
1226
            outbuf[3] = (nb_sectors >> 32) & 0xff;
1227
            outbuf[4] = (nb_sectors >> 24) & 0xff;
1228
            outbuf[5] = (nb_sectors >> 16) & 0xff;
1229
            outbuf[6] = (nb_sectors >> 8) & 0xff;
1230
            outbuf[7] = nb_sectors & 0xff;
1231
            outbuf[8] = 0;
1232
            outbuf[9] = 0;
1233
            outbuf[10] = s->qdev.blocksize >> 8;
1234
            outbuf[11] = 0;
1235
            outbuf[12] = 0;
1236
            outbuf[13] = get_physical_block_exp(&s->qdev.conf);
1237

    
1238
            /* set TPE bit if the format supports discard */
1239
            if (s->qdev.conf.discard_granularity) {
1240
                outbuf[14] = 0x80;
1241
            }
1242

    
1243
            /* Protection, exponent and lowest lba field left blank. */
1244
            buflen = req->cmd.xfer;
1245
            break;
1246
        }
1247
        DPRINTF("Unsupported Service Action In\n");
1248
        goto illegal_request;
1249
    case VERIFY_10:
1250
        break;
1251
    default:
1252
        scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
1253
        return -1;
1254
    }
1255
    return buflen;
1256

    
1257
not_ready:
1258
    if (s->tray_open || !bdrv_is_inserted(s->qdev.conf.bs)) {
1259
        scsi_check_condition(r, SENSE_CODE(NO_MEDIUM));
1260
    } else {
1261
        scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY));
1262
    }
1263
    return -1;
1264

    
1265
illegal_request:
1266
    if (r->req.status == -1) {
1267
        scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1268
    }
1269
    return -1;
1270
}
1271

    
1272
/* Execute a scsi command.  Returns the length of the data expected by the
1273
   command.  This will be Positive for data transfers from the device
1274
   (eg. disk reads), negative for transfers to the device (eg. disk writes),
1275
   and zero if the command does not transfer any data.  */
1276

    
1277
static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
1278
{
1279
    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
1280
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
1281
    int32_t len;
1282
    uint8_t command;
1283
    int rc;
1284

    
1285
    command = buf[0];
1286
    DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
1287

    
1288
#ifdef DEBUG_SCSI
1289
    {
1290
        int i;
1291
        for (i = 1; i < r->req.cmd.len; i++) {
1292
            printf(" 0x%02x", buf[i]);
1293
        }
1294
        printf("\n");
1295
    }
1296
#endif
1297

    
1298
    switch (command) {
1299
    case TEST_UNIT_READY:
1300
    case INQUIRY:
1301
    case MODE_SENSE:
1302
    case MODE_SENSE_10:
1303
    case RESERVE:
1304
    case RESERVE_10:
1305
    case RELEASE:
1306
    case RELEASE_10:
1307
    case START_STOP:
1308
    case ALLOW_MEDIUM_REMOVAL:
1309
    case READ_CAPACITY_10:
1310
    case READ_TOC:
1311
    case READ_DVD_STRUCTURE:
1312
    case GET_CONFIGURATION:
1313
    case GET_EVENT_STATUS_NOTIFICATION:
1314
    case MECHANISM_STATUS:
1315
    case SERVICE_ACTION_IN_16:
1316
    case VERIFY_10:
1317
        rc = scsi_disk_emulate_command(r);
1318
        if (rc < 0) {
1319
            return 0;
1320
        }
1321

    
1322
        r->iov.iov_len = rc;
1323
        break;
1324
    case SYNCHRONIZE_CACHE:
1325
        /* The request is used as the AIO opaque value, so add a ref.  */
1326
        scsi_req_ref(&r->req);
1327
        bdrv_acct_start(s->qdev.conf.bs, &r->acct, 0, BDRV_ACCT_FLUSH);
1328
        r->req.aiocb = bdrv_aio_flush(s->qdev.conf.bs, scsi_flush_complete, r);
1329
        if (r->req.aiocb == NULL) {
1330
            scsi_flush_complete(r, -EIO);
1331
        }
1332
        return 0;
1333
    case READ_6:
1334
    case READ_10:
1335
    case READ_12:
1336
    case READ_16:
1337
        len = r->req.cmd.xfer / s->qdev.blocksize;
1338
        DPRINTF("Read (sector %" PRId64 ", count %d)\n", r->req.cmd.lba, len);
1339
        if (r->req.cmd.lba > s->qdev.max_lba) {
1340
            goto illegal_lba;
1341
        }
1342
        r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1343
        r->sector_count = len * (s->qdev.blocksize / 512);
1344
        break;
1345
    case WRITE_6:
1346
    case WRITE_10:
1347
    case WRITE_12:
1348
    case WRITE_16:
1349
    case WRITE_VERIFY_10:
1350
    case WRITE_VERIFY_12:
1351
    case WRITE_VERIFY_16:
1352
        len = r->req.cmd.xfer / s->qdev.blocksize;
1353
        DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
1354
                (command & 0xe) == 0xe ? "And Verify " : "",
1355
                r->req.cmd.lba, len);
1356
        if (r->req.cmd.lba > s->qdev.max_lba) {
1357
            goto illegal_lba;
1358
        }
1359
        r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
1360
        r->sector_count = len * (s->qdev.blocksize / 512);
1361
        break;
1362
    case MODE_SELECT:
1363
        DPRINTF("Mode Select(6) (len %lu)\n", (long)r->req.cmd.xfer);
1364
        /* We don't support mode parameter changes.
1365
           Allow the mode parameter header + block descriptors only. */
1366
        if (r->req.cmd.xfer > 12) {
1367
            goto fail;
1368
        }
1369
        break;
1370
    case MODE_SELECT_10:
1371
        DPRINTF("Mode Select(10) (len %lu)\n", (long)r->req.cmd.xfer);
1372
        /* We don't support mode parameter changes.
1373
           Allow the mode parameter header + block descriptors only. */
1374
        if (r->req.cmd.xfer > 16) {
1375
            goto fail;
1376
        }
1377
        break;
1378
    case SEEK_10:
1379
        DPRINTF("Seek(10) (sector %" PRId64 ")\n", r->req.cmd.lba);
1380
        if (r->req.cmd.lba > s->qdev.max_lba) {
1381
            goto illegal_lba;
1382
        }
1383
        break;
1384
    case WRITE_SAME_16:
1385
        len = r->req.cmd.xfer / s->qdev.blocksize;
1386

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

    
1390
        if (r->req.cmd.lba > s->qdev.max_lba) {
1391
            goto illegal_lba;
1392
        }
1393

    
1394
        /*
1395
         * We only support WRITE SAME with the unmap bit set for now.
1396
         */
1397
        if (!(buf[1] & 0x8)) {
1398
            goto fail;
1399
        }
1400

    
1401
        rc = bdrv_discard(s->qdev.conf.bs,
1402
                          r->req.cmd.lba * (s->qdev.blocksize / 512),
1403
                          len * (s->qdev.blocksize / 512));
1404
        if (rc < 0) {
1405
            /* XXX: better error code ?*/
1406
            goto fail;
1407
        }
1408

    
1409
        break;
1410
    case REQUEST_SENSE:
1411
        abort();
1412
    default:
1413
        DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
1414
        scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
1415
        return 0;
1416
    fail:
1417
        scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
1418
        return 0;
1419
    illegal_lba:
1420
        scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
1421
        return 0;
1422
    }
1423
    if (r->sector_count == 0 && r->iov.iov_len == 0) {
1424
        scsi_req_complete(&r->req, GOOD);
1425
    }
1426
    len = r->sector_count * 512 + r->iov.iov_len;
1427
    if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
1428
        return -len;
1429
    } else {
1430
        if (!r->sector_count) {
1431
            r->sector_count = -1;
1432
        }
1433
        return len;
1434
    }
1435
}
1436

    
1437
static void scsi_disk_reset(DeviceState *dev)
1438
{
1439
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev);
1440
    uint64_t nb_sectors;
1441

    
1442
    scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET));
1443

    
1444
    bdrv_get_geometry(s->qdev.conf.bs, &nb_sectors);
1445
    nb_sectors /= s->qdev.blocksize / 512;
1446
    if (nb_sectors) {
1447
        nb_sectors--;
1448
    }
1449
    s->qdev.max_lba = nb_sectors;
1450
}
1451

    
1452
static void scsi_destroy(SCSIDevice *dev)
1453
{
1454
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1455

    
1456
    scsi_device_purge_requests(&s->qdev, SENSE_CODE(NO_SENSE));
1457
    blockdev_mark_auto_del(s->qdev.conf.bs);
1458
}
1459

    
1460
static void scsi_cd_change_media_cb(void *opaque, bool load)
1461
{
1462
    SCSIDiskState *s = opaque;
1463

    
1464
    /*
1465
     * When a CD gets changed, we have to report an ejected state and
1466
     * then a loaded state to guests so that they detect tray
1467
     * open/close and media change events.  Guests that do not use
1468
     * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1469
     * states rely on this behavior.
1470
     *
1471
     * media_changed governs the state machine used for unit attention
1472
     * report.  media_event is used by GET EVENT STATUS NOTIFICATION.
1473
     */
1474
    s->media_changed = load;
1475
    s->tray_open = !load;
1476
    s->qdev.unit_attention = SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM);
1477
    s->media_event = true;
1478
    s->eject_request = false;
1479
}
1480

    
1481
static void scsi_cd_eject_request_cb(void *opaque, bool force)
1482
{
1483
    SCSIDiskState *s = opaque;
1484

    
1485
    s->eject_request = true;
1486
    if (force) {
1487
        s->tray_locked = false;
1488
    }
1489
}
1490

    
1491
static bool scsi_cd_is_tray_open(void *opaque)
1492
{
1493
    return ((SCSIDiskState *)opaque)->tray_open;
1494
}
1495

    
1496
static bool scsi_cd_is_medium_locked(void *opaque)
1497
{
1498
    return ((SCSIDiskState *)opaque)->tray_locked;
1499
}
1500

    
1501
static const BlockDevOps scsi_cd_block_ops = {
1502
    .change_media_cb = scsi_cd_change_media_cb,
1503
    .eject_request_cb = scsi_cd_eject_request_cb,
1504
    .is_tray_open = scsi_cd_is_tray_open,
1505
    .is_medium_locked = scsi_cd_is_medium_locked,
1506
};
1507

    
1508
static void scsi_disk_unit_attention_reported(SCSIDevice *dev)
1509
{
1510
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1511
    if (s->media_changed) {
1512
        s->media_changed = false;
1513
        s->qdev.unit_attention = SENSE_CODE(MEDIUM_CHANGED);
1514
    }
1515
}
1516

    
1517
static int scsi_initfn(SCSIDevice *dev)
1518
{
1519
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1520
    DriveInfo *dinfo;
1521

    
1522
    if (!s->qdev.conf.bs) {
1523
        error_report("scsi-disk: drive property not set");
1524
        return -1;
1525
    }
1526

    
1527
    if (!s->removable && !bdrv_is_inserted(s->qdev.conf.bs)) {
1528
        error_report("Device needs media, but drive is empty");
1529
        return -1;
1530
    }
1531

    
1532
    if (!s->serial) {
1533
        /* try to fall back to value set with legacy -drive serial=... */
1534
        dinfo = drive_get_by_blockdev(s->qdev.conf.bs);
1535
        if (*dinfo->serial) {
1536
            s->serial = g_strdup(dinfo->serial);
1537
        }
1538
    }
1539

    
1540
    if (!s->version) {
1541
        s->version = g_strdup(QEMU_VERSION);
1542
    }
1543

    
1544
    if (bdrv_is_sg(s->qdev.conf.bs)) {
1545
        error_report("scsi-disk: unwanted /dev/sg*");
1546
        return -1;
1547
    }
1548

    
1549
    if (s->removable) {
1550
        bdrv_set_dev_ops(s->qdev.conf.bs, &scsi_cd_block_ops, s);
1551
    }
1552
    bdrv_set_buffer_alignment(s->qdev.conf.bs, s->qdev.blocksize);
1553

    
1554
    bdrv_iostatus_enable(s->qdev.conf.bs);
1555
    add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, NULL);
1556
    return 0;
1557
}
1558

    
1559
static int scsi_hd_initfn(SCSIDevice *dev)
1560
{
1561
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1562
    s->qdev.blocksize = s->qdev.conf.logical_block_size;
1563
    s->qdev.type = TYPE_DISK;
1564
    return scsi_initfn(&s->qdev);
1565
}
1566

    
1567
static int scsi_cd_initfn(SCSIDevice *dev)
1568
{
1569
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1570
    s->qdev.blocksize = 2048;
1571
    s->qdev.type = TYPE_ROM;
1572
    s->removable = true;
1573
    return scsi_initfn(&s->qdev);
1574
}
1575

    
1576
static int scsi_disk_initfn(SCSIDevice *dev)
1577
{
1578
    DriveInfo *dinfo;
1579

    
1580
    if (!dev->conf.bs) {
1581
        return scsi_initfn(dev);  /* ... and die there */
1582
    }
1583

    
1584
    dinfo = drive_get_by_blockdev(dev->conf.bs);
1585
    if (dinfo->media_cd) {
1586
        return scsi_cd_initfn(dev);
1587
    } else {
1588
        return scsi_hd_initfn(dev);
1589
    }
1590
}
1591

    
1592
static const SCSIReqOps scsi_disk_reqops = {
1593
    .size         = sizeof(SCSIDiskReq),
1594
    .free_req     = scsi_free_request,
1595
    .send_command = scsi_send_command,
1596
    .read_data    = scsi_read_data,
1597
    .write_data   = scsi_write_data,
1598
    .cancel_io    = scsi_cancel_io,
1599
    .get_buf      = scsi_get_buf,
1600
};
1601

    
1602
static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
1603
                                     uint8_t *buf, void *hba_private)
1604
{
1605
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1606
    SCSIRequest *req;
1607

    
1608
    req = scsi_req_alloc(&scsi_disk_reqops, &s->qdev, tag, lun, hba_private);
1609
    return req;
1610
}
1611

    
1612
#ifdef __linux__
1613
static int get_device_type(SCSIDiskState *s)
1614
{
1615
    BlockDriverState *bdrv = s->qdev.conf.bs;
1616
    uint8_t cmd[16];
1617
    uint8_t buf[36];
1618
    uint8_t sensebuf[8];
1619
    sg_io_hdr_t io_header;
1620
    int ret;
1621

    
1622
    memset(cmd, 0, sizeof(cmd));
1623
    memset(buf, 0, sizeof(buf));
1624
    cmd[0] = INQUIRY;
1625
    cmd[4] = sizeof(buf);
1626

    
1627
    memset(&io_header, 0, sizeof(io_header));
1628
    io_header.interface_id = 'S';
1629
    io_header.dxfer_direction = SG_DXFER_FROM_DEV;
1630
    io_header.dxfer_len = sizeof(buf);
1631
    io_header.dxferp = buf;
1632
    io_header.cmdp = cmd;
1633
    io_header.cmd_len = sizeof(cmd);
1634
    io_header.mx_sb_len = sizeof(sensebuf);
1635
    io_header.sbp = sensebuf;
1636
    io_header.timeout = 6000; /* XXX */
1637

    
1638
    ret = bdrv_ioctl(bdrv, SG_IO, &io_header);
1639
    if (ret < 0 || io_header.driver_status || io_header.host_status) {
1640
        return -1;
1641
    }
1642
    s->qdev.type = buf[0];
1643
    s->removable = (buf[1] & 0x80) != 0;
1644
    return 0;
1645
}
1646

    
1647
static int scsi_block_initfn(SCSIDevice *dev)
1648
{
1649
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
1650
    int sg_version;
1651
    int rc;
1652

    
1653
    if (!s->qdev.conf.bs) {
1654
        error_report("scsi-block: drive property not set");
1655
        return -1;
1656
    }
1657

    
1658
    /* check we are using a driver managing SG_IO (version 3 and after) */
1659
    if (bdrv_ioctl(s->qdev.conf.bs, SG_GET_VERSION_NUM, &sg_version) < 0 ||
1660
        sg_version < 30000) {
1661
        error_report("scsi-block: scsi generic interface too old");
1662
        return -1;
1663
    }
1664

    
1665
    /* get device type from INQUIRY data */
1666
    rc = get_device_type(s);
1667
    if (rc < 0) {
1668
        error_report("scsi-block: INQUIRY failed");
1669
        return -1;
1670
    }
1671

    
1672
    /* Make a guess for the block size, we'll fix it when the guest sends.
1673
     * READ CAPACITY.  If they don't, they likely would assume these sizes
1674
     * anyway. (TODO: check in /sys).
1675
     */
1676
    if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) {
1677
        s->qdev.blocksize = 2048;
1678
    } else {
1679
        s->qdev.blocksize = 512;
1680
    }
1681
    return scsi_initfn(&s->qdev);
1682
}
1683

    
1684
static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag,
1685
                                           uint32_t lun, uint8_t *buf,
1686
                                           void *hba_private)
1687
{
1688
    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
1689

    
1690
    switch (buf[0]) {
1691
    case READ_6:
1692
    case READ_10:
1693
    case READ_12:
1694
    case READ_16:
1695
    case WRITE_6:
1696
    case WRITE_10:
1697
    case WRITE_12:
1698
    case WRITE_16:
1699
    case WRITE_VERIFY_10:
1700
    case WRITE_VERIFY_12:
1701
    case WRITE_VERIFY_16:
1702
        return scsi_req_alloc(&scsi_disk_reqops, &s->qdev, tag, lun,
1703
                              hba_private);
1704
    }
1705

    
1706
    return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun,
1707
                          hba_private);
1708
}
1709
#endif
1710

    
1711
#define DEFINE_SCSI_DISK_PROPERTIES()                           \
1712
    DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf),          \
1713
    DEFINE_PROP_STRING("ver",  SCSIDiskState, version),         \
1714
    DEFINE_PROP_STRING("serial",  SCSIDiskState, serial)
1715

    
1716
static SCSIDeviceInfo scsi_disk_info[] = {
1717
    {
1718
        .qdev.name    = "scsi-hd",
1719
        .qdev.fw_name = "disk",
1720
        .qdev.desc    = "virtual SCSI disk",
1721
        .qdev.size    = sizeof(SCSIDiskState),
1722
        .qdev.reset   = scsi_disk_reset,
1723
        .init         = scsi_hd_initfn,
1724
        .destroy      = scsi_destroy,
1725
        .alloc_req    = scsi_new_request,
1726
        .unit_attention_reported = scsi_disk_unit_attention_reported,
1727
        .qdev.props   = (Property[]) {
1728
            DEFINE_SCSI_DISK_PROPERTIES(),
1729
            DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1730
            DEFINE_PROP_END_OF_LIST(),
1731
        }
1732
    },{
1733
        .qdev.name    = "scsi-cd",
1734
        .qdev.fw_name = "disk",
1735
        .qdev.desc    = "virtual SCSI CD-ROM",
1736
        .qdev.size    = sizeof(SCSIDiskState),
1737
        .qdev.reset   = scsi_disk_reset,
1738
        .init         = scsi_cd_initfn,
1739
        .destroy      = scsi_destroy,
1740
        .alloc_req    = scsi_new_request,
1741
        .unit_attention_reported = scsi_disk_unit_attention_reported,
1742
        .qdev.props   = (Property[]) {
1743
            DEFINE_SCSI_DISK_PROPERTIES(),
1744
            DEFINE_PROP_END_OF_LIST(),
1745
        },
1746
#ifdef __linux__
1747
    },{
1748
        .qdev.name    = "scsi-block",
1749
        .qdev.fw_name = "disk",
1750
        .qdev.desc    = "SCSI block device passthrough",
1751
        .qdev.size    = sizeof(SCSIDiskState),
1752
        .qdev.reset   = scsi_disk_reset,
1753
        .init         = scsi_block_initfn,
1754
        .destroy      = scsi_destroy,
1755
        .alloc_req    = scsi_block_new_request,
1756
        .qdev.props   = (Property[]) {
1757
            DEFINE_SCSI_DISK_PROPERTIES(),
1758
            DEFINE_PROP_END_OF_LIST(),
1759
        },
1760
#endif
1761
    },{
1762
        .qdev.name    = "scsi-disk", /* legacy -device scsi-disk */
1763
        .qdev.fw_name = "disk",
1764
        .qdev.desc    = "virtual SCSI disk or CD-ROM (legacy)",
1765
        .qdev.size    = sizeof(SCSIDiskState),
1766
        .qdev.reset   = scsi_disk_reset,
1767
        .init         = scsi_disk_initfn,
1768
        .destroy      = scsi_destroy,
1769
        .alloc_req    = scsi_new_request,
1770
        .unit_attention_reported = scsi_disk_unit_attention_reported,
1771
        .qdev.props   = (Property[]) {
1772
            DEFINE_SCSI_DISK_PROPERTIES(),
1773
            DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),
1774
            DEFINE_PROP_END_OF_LIST(),
1775
        }
1776
    }
1777
};
1778

    
1779
static void scsi_disk_register_devices(void)
1780
{
1781
    int i;
1782

    
1783
    for (i = 0; i < ARRAY_SIZE(scsi_disk_info); i++) {
1784
        scsi_qdev_register(&scsi_disk_info[i]);
1785
    }
1786
}
1787
device_init(scsi_disk_register_devices)