scsi-generic.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Generic SCSI Device support
 *
 * Copyright (c) 2007 Bull S.A.S.
 * Based on code by Paul Brook
 * Based on code by Fabrice Bellard
 *
 * Written by Laurent Vivier <Laurent.Vivier@bull.net>
 *
M
Matthew Fernandez 已提交
10
 * This code is licensed under the LGPL.
11 12 13
 *
 */

P
Peter Maydell 已提交
14
#include "qemu/osdep.h"
15
#include "qemu-common.h"
16
#include "qemu/error-report.h"
P
Paolo Bonzini 已提交
17
#include "hw/scsi/scsi.h"
18
#include "sysemu/block-backend.h"
19
#include "sysemu/blockdev.h"
20

21
#ifdef __linux__
22 23 24 25

//#define DEBUG_SCSI

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

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

#include <scsi/sg.h>
P
Paolo Bonzini 已提交
36
#include "block/scsi.h"
37

P
Paolo Bonzini 已提交
38 39 40 41 42 43 44
#define SG_ERR_DRIVER_TIMEOUT  0x06
#define SG_ERR_DRIVER_SENSE    0x08

#define SG_ERR_DID_OK          0x00
#define SG_ERR_DID_NO_CONNECT  0x01
#define SG_ERR_DID_BUS_BUSY    0x02
#define SG_ERR_DID_TIME_OUT    0x03
45 46 47 48 49

#ifndef MAX_UINT
#define MAX_UINT ((unsigned int)-1)
#endif

50 51
typedef struct SCSIGenericReq {
    SCSIRequest req;
52 53 54 55
    uint8_t *buf;
    int buflen;
    int len;
    sg_io_hdr_t io_header;
56
} SCSIGenericReq;
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static void scsi_generic_save_request(QEMUFile *f, SCSIRequest *req)
{
    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);

    qemu_put_sbe32s(f, &r->buflen);
    if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) {
        assert(!r->req.sg);
        qemu_put_buffer(f, r->buf, r->req.cmd.xfer);
    }
}

static void scsi_generic_load_request(QEMUFile *f, SCSIRequest *req)
{
    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);

    qemu_get_sbe32s(f, &r->buflen);
    if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) {
        assert(!r->req.sg);
        qemu_get_buffer(f, r->buf, r->req.cmd.xfer);
    }
}

P
Paolo Bonzini 已提交
80
static void scsi_free_request(SCSIRequest *req)
81
{
P
Paolo Bonzini 已提交
82 83
    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);

84
    g_free(r->buf);
85 86 87
}

/* Helper function for command completion.  */
88
static void scsi_command_complete_noio(SCSIGenericReq *r, int ret)
89
{
90
    int status;
91

92 93
    assert(r->req.aiocb == NULL);

94
    if (r->req.io_canceled) {
95
        scsi_req_cancel_complete(&r->req);
96 97
        goto done;
    }
P
Paolo Bonzini 已提交
98
    if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) {
99
        r->req.sense_len = r->io_header.sb_len_wr;
P
Paolo Bonzini 已提交
100
    }
A
aurel32 已提交
101

102 103
    if (ret != 0) {
        switch (ret) {
P
Paolo Bonzini 已提交
104
        case -EDOM:
105
            status = TASK_SET_FULL;
P
Paolo Bonzini 已提交
106
            break;
107
        case -ENOMEM:
108
            status = CHECK_CONDITION;
109
            scsi_req_build_sense(&r->req, SENSE_CODE(TARGET_FAILURE));
110 111
            break;
        default:
112
            status = CHECK_CONDITION;
113
            scsi_req_build_sense(&r->req, SENSE_CODE(IO_ERROR));
114 115 116
            break;
        }
    } else {
P
Paolo Bonzini 已提交
117 118 119 120
        if (r->io_header.host_status == SG_ERR_DID_NO_CONNECT ||
            r->io_header.host_status == SG_ERR_DID_BUS_BUSY ||
            r->io_header.host_status == SG_ERR_DID_TIME_OUT ||
            (r->io_header.driver_status & SG_ERR_DRIVER_TIMEOUT)) {
121
            status = BUSY;
122
            BADF("Driver Timeout\n");
P
Paolo Bonzini 已提交
123 124 125
        } else if (r->io_header.host_status) {
            status = CHECK_CONDITION;
            scsi_req_build_sense(&r->req, SENSE_CODE(I_T_NEXUS_LOSS));
126 127
        } else if (r->io_header.status) {
            status = r->io_header.status;
128
        } else if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) {
129 130 131 132
            status = CHECK_CONDITION;
        } else {
            status = GOOD;
        }
133
    }
A
aurel32 已提交
134
    DPRINTF("Command complete 0x%p tag=0x%x status=%d\n",
135
            r, r->req.tag, status);
G
Gerd Hoffmann 已提交
136

137
    scsi_req_complete(&r->req, status);
138
done:
139
    scsi_req_unref(&r->req);
140 141
}

142 143 144 145 146 147 148 149 150
static void scsi_command_complete(void *opaque, int ret)
{
    SCSIGenericReq *r = (SCSIGenericReq *)opaque;

    assert(r->req.aiocb != NULL);
    r->req.aiocb = NULL;
    scsi_command_complete_noio(r, ret);
}

151
static int execute_command(BlockBackend *blk,
152
                           SCSIGenericReq *r, int direction,
153
                           BlockCompletionFunc *complete)
154 155 156 157 158
{
    r->io_header.interface_id = 'S';
    r->io_header.dxfer_direction = direction;
    r->io_header.dxferp = r->buf;
    r->io_header.dxfer_len = r->buflen;
159 160
    r->io_header.cmdp = r->req.cmd.buf;
    r->io_header.cmd_len = r->req.cmd.len;
161 162
    r->io_header.mx_sb_len = sizeof(r->req.sense);
    r->io_header.sbp = r->req.sense;
163 164 165 166
    r->io_header.timeout = MAX_UINT;
    r->io_header.usr_ptr = r;
    r->io_header.flags |= SG_FLAG_DIRECT_IO;

167
    r->req.aiocb = blk_aio_ioctl(blk, SG_IO, &r->io_header, complete, r);
168 169 170
    if (r->req.aiocb == NULL) {
        return -EIO;
    }
171 172 173 174 175 176

    return 0;
}

static void scsi_read_complete(void * opaque, int ret)
{
177
    SCSIGenericReq *r = (SCSIGenericReq *)opaque;
178
    SCSIDevice *s = r->req.dev;
179 180
    int len;

181
    assert(r->req.aiocb != NULL);
182
    r->req.aiocb = NULL;
183

184
    if (ret || r->req.io_canceled) {
185
        scsi_command_complete_noio(r, ret);
186 187
        return;
    }
188

189
    len = r->io_header.dxfer_len - r->io_header.resid;
190
    DPRINTF("Data ready tag=0x%x len=%d\n", r->req.tag, len);
191 192

    r->len = -1;
193
    if (len == 0) {
194 195 196
        scsi_command_complete_noio(r, 0);
        return;
    }
197

198 199 200 201 202 203 204 205 206
    /* Snoop READ CAPACITY output to set the blocksize.  */
    if (r->req.cmd.buf[0] == READ_CAPACITY_10 &&
        (ldl_be_p(&r->buf[0]) != 0xffffffffU || s->max_lba == 0)) {
        s->blocksize = ldl_be_p(&r->buf[4]);
        s->max_lba = ldl_be_p(&r->buf[0]) & 0xffffffffULL;
    } else if (r->req.cmd.buf[0] == SERVICE_ACTION_IN_16 &&
               (r->req.cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) {
        s->blocksize = ldl_be_p(&r->buf[8]);
        s->max_lba = ldq_be_p(&r->buf[0]);
207
    }
208 209
    blk_set_guest_block_size(s->conf.blk, s->blocksize);

210 211 212 213 214 215 216 217 218 219 220 221 222 223
    /* Patch MODE SENSE device specific parameters if the BDS is opened
     * readonly.
     */
    if ((s->type == TYPE_DISK || s->type == TYPE_TAPE) &&
        blk_is_read_only(s->conf.blk) &&
        (r->req.cmd.buf[0] == MODE_SENSE ||
         r->req.cmd.buf[0] == MODE_SENSE_10) &&
        (r->req.cmd.buf[1] & 0x8) == 0) {
        if (r->req.cmd.buf[0] == MODE_SENSE) {
            r->buf[2] |= 0x80;
        } else  {
            r->buf[3] |= 0x80;
        }
    }
224 225
    scsi_req_data(&r->req, len);
    scsi_req_unref(&r->req);
226 227 228
}

/* Read more data from scsi device into buffer.  */
229
static void scsi_read_data(SCSIRequest *req)
230
{
231
    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);
232
    SCSIDevice *s = r->req.dev;
233 234
    int ret;

235
    DPRINTF("scsi_read_data 0x%x\n", req->tag);
236 237 238

    /* The request is used as the AIO opaque value, so add a ref.  */
    scsi_req_ref(&r->req);
239
    if (r->len == -1) {
240
        scsi_command_complete_noio(r, 0);
241 242 243
        return;
    }

244 245
    ret = execute_command(s->conf.blk, r, SG_DXFER_FROM_DEV,
                          scsi_read_complete);
246
    if (ret < 0) {
247
        scsi_command_complete_noio(r, ret);
248 249 250 251 252
    }
}

static void scsi_write_complete(void * opaque, int ret)
{
253
    SCSIGenericReq *r = (SCSIGenericReq *)opaque;
254
    SCSIDevice *s = r->req.dev;
255 256

    DPRINTF("scsi_write_complete() ret = %d\n", ret);
257 258

    assert(r->req.aiocb != NULL);
259
    r->req.aiocb = NULL;
260

261
    if (ret || r->req.io_canceled) {
262
        scsi_command_complete_noio(r, ret);
263 264 265
        return;
    }

266
    if (r->req.cmd.buf[0] == MODE_SELECT && r->req.cmd.buf[4] == 12 &&
267 268 269
        s->type == TYPE_TAPE) {
        s->blocksize = (r->buf[9] << 16) | (r->buf[10] << 8) | r->buf[11];
        DPRINTF("block size %d\n", s->blocksize);
A
aurel32 已提交
270 271
    }

272
    scsi_command_complete_noio(r, ret);
273 274 275 276
}

/* Write data to a scsi device.  Returns nonzero on failure.
   The transfer may complete asynchronously.  */
277
static void scsi_write_data(SCSIRequest *req)
278
{
279
    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);
280
    SCSIDevice *s = r->req.dev;
281 282
    int ret;

283
    DPRINTF("scsi_write_data 0x%x\n", req->tag);
284 285
    if (r->len == 0) {
        r->len = r->buflen;
P
Paolo Bonzini 已提交
286
        scsi_req_data(&r->req, r->len);
287
        return;
288 289
    }

290 291
    /* The request is used as the AIO opaque value, so add a ref.  */
    scsi_req_ref(&r->req);
292
    ret = execute_command(s->conf.blk, r, SG_DXFER_TO_DEV, scsi_write_complete);
293
    if (ret < 0) {
294
        scsi_command_complete_noio(r, ret);
295 296 297 298
    }
}

/* Return a pointer to the data buffer.  */
299
static uint8_t *scsi_get_buf(SCSIRequest *req)
300
{
301 302
    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);

303 304 305 306 307 308 309 310
    return r->buf;
}

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

311
static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd)
312
{
313
    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);
314
    SCSIDevice *s = r->req.dev;
315 316
    int ret;

317 318 319 320 321 322 323 324 325
#ifdef DEBUG_SCSI
    {
        int i;
        for (i = 1; i < r->req.cmd.len; i++) {
            printf(" 0x%02x", cmd[i]);
        }
        printf("\n");
    }
#endif
326

327
    if (r->req.cmd.xfer == 0) {
328
        g_free(r->buf);
329 330
        r->buflen = 0;
        r->buf = NULL;
331 332
        /* The request is used as the AIO opaque value, so add a ref.  */
        scsi_req_ref(&r->req);
333 334
        ret = execute_command(s->conf.blk, r, SG_DXFER_NONE,
                              scsi_command_complete);
335
        if (ret < 0) {
336
            scsi_command_complete_noio(r, ret);
337
            return 0;
338 339 340 341
        }
        return 0;
    }

342
    if (r->buflen != r->req.cmd.xfer) {
343
        g_free(r->buf);
344
        r->buf = g_malloc(r->req.cmd.xfer);
345
        r->buflen = r->req.cmd.xfer;
346 347 348
    }

    memset(r->buf, 0, r->buflen);
349
    r->len = r->req.cmd.xfer;
G
Gerd Hoffmann 已提交
350
    if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
351
        r->len = 0;
352
        return -r->req.cmd.xfer;
P
Paolo Bonzini 已提交
353
    } else {
354
        return r->req.cmd.xfer;
355 356 357
    }
}

358
static int get_stream_blocksize(BlockBackend *blk)
A
aurel32 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
{
    uint8_t cmd[6];
    uint8_t buf[12];
    uint8_t sensebuf[8];
    sg_io_hdr_t io_header;
    int ret;

    memset(cmd, 0, sizeof(cmd));
    memset(buf, 0, sizeof(buf));
    cmd[0] = MODE_SENSE;
    cmd[4] = sizeof(buf);

    memset(&io_header, 0, sizeof(io_header));
    io_header.interface_id = 'S';
    io_header.dxfer_direction = SG_DXFER_FROM_DEV;
    io_header.dxfer_len = sizeof(buf);
    io_header.dxferp = buf;
    io_header.cmdp = cmd;
    io_header.cmd_len = sizeof(cmd);
    io_header.mx_sb_len = sizeof(sensebuf);
    io_header.sbp = sensebuf;
    io_header.timeout = 6000; /* XXX */

382
    ret = blk_ioctl(blk, SG_IO, &io_header);
383
    if (ret < 0 || io_header.driver_status || io_header.host_status) {
A
aurel32 已提交
384
        return -1;
385
    }
A
aurel32 已提交
386 387 388
    return (buf[9] << 16) | (buf[10] << 8) | buf[11];
}

389 390
static void scsi_generic_reset(DeviceState *dev)
{
391
    SCSIDevice *s = SCSI_DEVICE(dev);
392

393
    scsi_device_purge_requests(s, SENSE_CODE(RESET));
394 395
}

396
static void scsi_generic_realize(SCSIDevice *s, Error **errp)
397
{
398
    int rc;
399 400 401
    int sg_version;
    struct sg_scsi_id scsiid;

402
    if (!s->conf.blk) {
403 404
        error_setg(errp, "drive property not set");
        return;
405
    }
406

407
    if (blk_get_on_error(s->conf.blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC) {
408 409
        error_setg(errp, "Device doesn't support drive option werror");
        return;
410
    }
411
    if (blk_get_on_error(s->conf.blk, 1) != BLOCKDEV_ON_ERROR_REPORT) {
412 413
        error_setg(errp, "Device doesn't support drive option rerror");
        return;
414 415
    }

416
    /* check we are using a driver managing SG_IO (version 3 and after */
417
    rc = blk_ioctl(s->conf.blk, SG_GET_VERSION_NUM, &sg_version);
418
    if (rc < 0) {
419 420 421 422
        error_setg(errp, "cannot get SG_IO version number: %s.  "
                         "Is this a SCSI device?",
                         strerror(-rc));
        return;
423 424
    }
    if (sg_version < 30000) {
425 426
        error_setg(errp, "scsi generic interface too old");
        return;
427
    }
428 429

    /* get LUN of the /dev/sg? */
430
    if (blk_ioctl(s->conf.blk, SG_GET_SCSI_ID, &scsiid)) {
431 432
        error_setg(errp, "SG_GET_SCSI_ID ioctl failed");
        return;
433
    }
434 435

    /* define device state */
436 437
    s->type = scsiid.scsi_type;
    DPRINTF("device type %d\n", s->type);
P
Paolo Bonzini 已提交
438

439 440
    switch (s->type) {
    case TYPE_TAPE:
441
        s->blocksize = get_stream_blocksize(s->conf.blk);
442 443 444
        if (s->blocksize == -1) {
            s->blocksize = 0;
        }
445 446 447 448 449 450 451 452 453 454 455 456 457
        break;

        /* Make a guess for block devices, we'll fix it when the guest sends.
         * READ CAPACITY.  If they don't, they likely would assume these sizes
         * anyway. (TODO: they could also send MODE SENSE).
         */
    case TYPE_ROM:
    case TYPE_WORM:
        s->blocksize = 2048;
        break;
    default:
        s->blocksize = 512;
        break;
A
aurel32 已提交
458
    }
459 460

    DPRINTF("block size %d\n", s->blocksize);
461
}
462

P
Paolo Bonzini 已提交
463
const SCSIReqOps scsi_generic_req_ops = {
P
Paolo Bonzini 已提交
464
    .size         = sizeof(SCSIGenericReq),
465 466 467 468 469
    .free_req     = scsi_free_request,
    .send_command = scsi_send_command,
    .read_data    = scsi_read_data,
    .write_data   = scsi_write_data,
    .get_buf      = scsi_get_buf,
470 471
    .load_request = scsi_generic_load_request,
    .save_request = scsi_generic_save_request,
P
Paolo Bonzini 已提交
472 473 474
};

static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
P
Paolo Bonzini 已提交
475
                                     uint8_t *buf, void *hba_private)
P
Paolo Bonzini 已提交
476 477 478 479 480 481 482
{
    SCSIRequest *req;

    req = scsi_req_alloc(&scsi_generic_req_ops, d, tag, lun, hba_private);
    return req;
}

483
static Property scsi_generic_properties[] = {
484
    DEFINE_PROP_DRIVE("drive", SCSIDevice, conf.blk),
485 486 487
    DEFINE_PROP_END_OF_LIST(),
};

488 489 490 491 492 493
static int scsi_generic_parse_cdb(SCSIDevice *dev, SCSICommand *cmd,
                                  uint8_t *buf, void *hba_private)
{
    return scsi_bus_parse_cdb(dev, cmd, buf, hba_private);
}

494 495
static void scsi_generic_class_initfn(ObjectClass *klass, void *data)
{
496
    DeviceClass *dc = DEVICE_CLASS(klass);
497 498
    SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass);

499
    sc->realize      = scsi_generic_realize;
500
    sc->alloc_req    = scsi_new_request;
501
    sc->parse_cdb    = scsi_generic_parse_cdb;
502 503 504 505
    dc->fw_name = "disk";
    dc->desc = "pass through generic scsi device (/dev/sg*)";
    dc->reset = scsi_generic_reset;
    dc->props = scsi_generic_properties;
506
    dc->vmsd  = &vmstate_scsi_device;
507 508
}

509
static const TypeInfo scsi_generic_info = {
510 511 512 513
    .name          = "scsi-generic",
    .parent        = TYPE_SCSI_DEVICE,
    .instance_size = sizeof(SCSIDevice),
    .class_init    = scsi_generic_class_initfn,
514
};
515

A
Andreas Färber 已提交
516
static void scsi_generic_register_types(void)
517
{
518
    type_register_static(&scsi_generic_info);
519
}
A
Andreas Färber 已提交
520 521

type_init(scsi_generic_register_types)
522

523
#endif /* __linux__ */