scsi-bus.c 22.7 KB
Newer Older
1
#include "hw.h"
2
#include "qemu-error.h"
G
Gerd Hoffmann 已提交
3
#include "scsi.h"
4
#include "scsi-defs.h"
5
#include "qdev.h"
B
Blue Swirl 已提交
6
#include "blockdev.h"
7
#include "trace.h"
8

9
static char *scsibus_get_fw_dev_path(DeviceState *dev);
10 11
static int scsi_build_sense(uint8_t *in_buf, int in_len,
                            uint8_t *buf, int len, bool fixed);
12

13 14 15
static struct BusInfo scsi_bus_info = {
    .name  = "SCSI",
    .size  = sizeof(SCSIBus),
16
    .get_fw_dev_path = scsibus_get_fw_dev_path,
17 18 19 20 21 22 23 24
    .props = (Property[]) {
        DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
        DEFINE_PROP_END_OF_LIST(),
    },
};
static int next_scsi_bus;

/* Create a scsi bus, and attach devices to it.  */
25
void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
P
Paolo Bonzini 已提交
26
                  const SCSIBusOps *ops)
27
{
28
    qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
29 30 31
    bus->busnr = next_scsi_bus++;
    bus->tcq = tcq;
    bus->ndev = ndev;
P
Paolo Bonzini 已提交
32
    bus->ops = ops;
G
Gerd Hoffmann 已提交
33
    bus->qbus.allow_hotplug = 1;
34 35 36 37 38 39 40
}

static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
{
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
    SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
G
Gerd Hoffmann 已提交
41
    int rc = -1;
42 43 44 45 46 47 48 49

    if (dev->id == -1) {
        for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
            if (bus->devs[dev->id] == NULL)
                break;
        }
    }
    if (dev->id >= bus->ndev) {
50
        error_report("bad scsi device id: %d", dev->id);
51 52 53 54
        goto err;
    }

    if (bus->devs[dev->id]) {
G
Gerd Hoffmann 已提交
55
        qdev_free(&bus->devs[dev->id]->qdev);
56 57 58 59
    }
    bus->devs[dev->id] = dev;

    dev->info = info;
60
    QTAILQ_INIT(&dev->requests);
G
Gerd Hoffmann 已提交
61 62 63 64
    rc = dev->info->init(dev);
    if (rc != 0) {
        bus->devs[dev->id] = NULL;
    }
65 66

err:
G
Gerd Hoffmann 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
    return rc;
}

static int scsi_qdev_exit(DeviceState *qdev)
{
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);

    assert(bus->devs[dev->id] != NULL);
    if (bus->devs[dev->id]->info->destroy) {
        bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
    }
    bus->devs[dev->id] = NULL;
    return 0;
81 82 83 84 85 86
}

void scsi_qdev_register(SCSIDeviceInfo *info)
{
    info->qdev.bus_info = &scsi_bus_info;
    info->qdev.init     = scsi_qdev_init;
G
Gerd Hoffmann 已提交
87
    info->qdev.unplug   = qdev_simple_unplug_cb;
G
Gerd Hoffmann 已提交
88
    info->qdev.exit     = scsi_qdev_exit;
89 90 91 92
    qdev_register(&info->qdev);
}

/* handle legacy '-drive if=scsi,...' cmd line args */
93 94
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
                                      int unit, bool removable)
95 96 97 98
{
    const char *driver;
    DeviceState *dev;

99
    driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
100 101
    dev = qdev_create(&bus->qbus, driver);
    qdev_prop_set_uint32(dev, "scsi-id", unit);
102 103 104
    if (qdev_prop_exists(dev, "removable")) {
        qdev_prop_set_bit(dev, "removable", removable);
    }
105 106 107 108
    if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
        qdev_free(dev);
        return NULL;
    }
109 110
    if (qdev_init(dev) < 0)
        return NULL;
111 112 113
    return DO_UPCAST(SCSIDevice, qdev, dev);
}

114
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
115
{
116
    Location loc;
117
    DriveInfo *dinfo;
118
    int res = 0, unit;
119

120
    loc_push_none(&loc);
121
    for (unit = 0; unit < bus->ndev; unit++) {
122 123 124 125
        dinfo = drive_get(IF_SCSI, bus->busnr, unit);
        if (dinfo == NULL) {
            continue;
        }
126
        qemu_opts_loc_restore(dinfo->opts);
127
        if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false)) {
128 129 130
            res = -1;
            break;
        }
131
    }
132
    loc_pop(&loc);
133
    return res;
134
}
135

P
Paolo Bonzini 已提交
136
SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
137
                            uint32_t lun, void *hba_private)
138 139 140
{
    SCSIRequest *req;

P
Paolo Bonzini 已提交
141
    req = qemu_mallocz(reqops->size);
142
    req->refcount = 1;
143 144 145 146
    req->bus = scsi_bus_from_device(d);
    req->dev = d;
    req->tag = tag;
    req->lun = lun;
147
    req->hba_private = hba_private;
G
Gerd Hoffmann 已提交
148
    req->status = -1;
149
    req->sense_len = 0;
P
Paolo Bonzini 已提交
150
    req->ops = reqops;
151
    trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
152 153 154
    return req;
}

155 156
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
                          void *hba_private)
P
Paolo Bonzini 已提交
157
{
158
    return d->info->alloc_req(d, tag, lun, hba_private);
P
Paolo Bonzini 已提交
159 160
}

P
Paolo Bonzini 已提交
161 162
uint8_t *scsi_req_get_buf(SCSIRequest *req)
{
163
    return req->ops->get_buf(req);
P
Paolo Bonzini 已提交
164 165
}

166 167
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
{
168 169
    assert(len >= 14);
    if (!req->sense_len) {
170 171
        return 0;
    }
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    return scsi_build_sense(req->sense, req->sense_len, buf, len, true);
}

int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
{
    return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
}

void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
{
    trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
                               sense.key, sense.asc, sense.ascq);
    memset(req->sense, 0, 18);
    req->sense[0] = 0xf0;
    req->sense[2] = sense.key;
    req->sense[12] = sense.asc;
    req->sense[13] = sense.ascq;
    req->sense_len = 18;
190 191
}

192
int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
193
{
194 195
    int32_t rc;

196 197 198 199
    assert(!req->enqueued);
    scsi_req_ref(req);
    req->enqueued = true;
    QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
200 201

    scsi_req_ref(req);
202
    rc = req->ops->send_command(req, buf);
203 204
    scsi_req_unref(req);
    return rc;
205 206
}

207
static void scsi_req_dequeue(SCSIRequest *req)
208
{
209
    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
210 211 212
    if (req->enqueued) {
        QTAILQ_REMOVE(&req->dev->requests, req, next);
        req->enqueued = false;
P
Paolo Bonzini 已提交
213
        scsi_req_unref(req);
214 215 216
    }
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
{
    switch (cmd[0] >> 5) {
    case 0:
        req->cmd.xfer = cmd[4];
        req->cmd.len = 6;
        /* length 0 means 256 blocks */
        if (req->cmd.xfer == 0)
            req->cmd.xfer = 256;
        break;
    case 1:
    case 2:
        req->cmd.xfer = cmd[8] | (cmd[7] << 8);
        req->cmd.len = 10;
        break;
    case 4:
        req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
        req->cmd.len = 16;
        break;
    case 5:
        req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
        req->cmd.len = 12;
        break;
    default:
241
        trace_scsi_req_parse_bad(req->dev->id, req->lun, req->tag, cmd[0]);
242 243 244 245 246
        return -1;
    }

    switch(cmd[0]) {
    case TEST_UNIT_READY:
247
    case REWIND:
248 249 250 251
    case START_STOP:
    case SEEK_6:
    case WRITE_FILEMARKS:
    case SPACE:
252 253
    case RESERVE:
    case RELEASE:
254 255
    case ERASE:
    case ALLOW_MEDIUM_REMOVAL:
256
    case VERIFY_10:
257 258 259 260 261 262
    case SEEK_10:
    case SYNCHRONIZE_CACHE:
    case LOCK_UNLOCK_CACHE:
    case LOAD_UNLOAD:
    case SET_CD_SPEED:
    case SET_LIMITS:
263
    case WRITE_LONG_10:
264 265 266 267 268 269
    case MOVE_MEDIUM:
    case UPDATE_BLOCK:
        req->cmd.xfer = 0;
        break;
    case MODE_SENSE:
        break;
270
    case WRITE_SAME_10:
271 272
        req->cmd.xfer = 1;
        break;
273
    case READ_CAPACITY_10:
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        req->cmd.xfer = 8;
        break;
    case READ_BLOCK_LIMITS:
        req->cmd.xfer = 6;
        break;
    case READ_POSITION:
        req->cmd.xfer = 20;
        break;
    case SEND_VOLUME_TAG:
        req->cmd.xfer *= 40;
        break;
    case MEDIUM_SCAN:
        req->cmd.xfer *= 8;
        break;
    case WRITE_10:
289
    case WRITE_VERIFY_10:
290 291 292
    case WRITE_6:
    case WRITE_12:
    case WRITE_VERIFY_12:
G
Gerd Hoffmann 已提交
293 294
    case WRITE_16:
    case WRITE_VERIFY_16:
295 296 297 298 299 300 301
        req->cmd.xfer *= req->dev->blocksize;
        break;
    case READ_10:
    case READ_6:
    case READ_REVERSE:
    case RECOVER_BUFFERED_DATA:
    case READ_12:
G
Gerd Hoffmann 已提交
302
    case READ_16:
303 304 305 306 307
        req->cmd.xfer *= req->dev->blocksize;
        break;
    case INQUIRY:
        req->cmd.xfer = cmd[4] | (cmd[3] << 8);
        break;
308 309 310 311 312 313 314
    case MAINTENANCE_OUT:
    case MAINTENANCE_IN:
        if (req->dev->type == TYPE_ROM) {
            /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
            req->cmd.xfer = cmd[9] | (cmd[8] << 8);
        }
        break;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    }
    return 0;
}

static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
{
    switch(cmd[0]) {
    /* stream commands */
    case READ_6:
    case READ_REVERSE:
    case RECOVER_BUFFERED_DATA:
    case WRITE_6:
        req->cmd.len = 6;
        req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
        if (cmd[1] & 0x01) /* fixed */
            req->cmd.xfer *= req->dev->blocksize;
        break;
    case REWIND:
    case START_STOP:
        req->cmd.len = 6;
        req->cmd.xfer = 0;
        break;
    /* generic commands */
    default:
        return scsi_req_length(req, cmd);
    }
    return 0;
}

G
Gerd Hoffmann 已提交
344 345 346 347 348
static void scsi_req_xfer_mode(SCSIRequest *req)
{
    switch (req->cmd.buf[0]) {
    case WRITE_6:
    case WRITE_10:
349
    case WRITE_VERIFY_10:
G
Gerd Hoffmann 已提交
350 351
    case WRITE_12:
    case WRITE_VERIFY_12:
G
Gerd Hoffmann 已提交
352 353
    case WRITE_16:
    case WRITE_VERIFY_16:
G
Gerd Hoffmann 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    case COPY:
    case COPY_VERIFY:
    case COMPARE:
    case CHANGE_DEFINITION:
    case LOG_SELECT:
    case MODE_SELECT:
    case MODE_SELECT_10:
    case SEND_DIAGNOSTIC:
    case WRITE_BUFFER:
    case FORMAT_UNIT:
    case REASSIGN_BLOCKS:
    case SEARCH_EQUAL:
    case SEARCH_HIGH:
    case SEARCH_LOW:
    case UPDATE_BLOCK:
369 370
    case WRITE_LONG_10:
    case WRITE_SAME_10:
G
Gerd Hoffmann 已提交
371 372 373 374 375
    case SEARCH_HIGH_12:
    case SEARCH_EQUAL_12:
    case SEARCH_LOW_12:
    case MEDIUM_SCAN:
    case SEND_VOLUME_TAG:
376
    case PERSISTENT_RESERVE_OUT:
377
    case MAINTENANCE_OUT:
G
Gerd Hoffmann 已提交
378 379 380 381 382 383 384 385 386 387 388 389
        req->cmd.mode = SCSI_XFER_TO_DEV;
        break;
    default:
        if (req->cmd.xfer)
            req->cmd.mode = SCSI_XFER_FROM_DEV;
        else {
            req->cmd.mode = SCSI_XFER_NONE;
        }
        break;
    }
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
static uint64_t scsi_req_lba(SCSIRequest *req)
{
    uint8_t *buf = req->cmd.buf;
    uint64_t lba;

    switch (buf[0] >> 5) {
    case 0:
        lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
              (((uint64_t) buf[1] & 0x1f) << 16);
        break;
    case 1:
    case 2:
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
        break;
    case 4:
        lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
              ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
              ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
              ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
        break;
    case 5:
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
        break;
    default:
        lba = -1;

    }
    return lba;
}

int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
{
    int rc;

    if (req->dev->type == TYPE_TAPE) {
        rc = scsi_req_stream_length(req, buf);
    } else {
        rc = scsi_req_length(req, buf);
    }
    if (rc != 0)
        return rc;

    memcpy(req->cmd.buf, buf, req->cmd.len);
G
Gerd Hoffmann 已提交
435
    scsi_req_xfer_mode(req);
436
    req->cmd.lba = scsi_req_lba(req);
437
    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
438 439 440 441 442
                          req->cmd.mode, req->cmd.xfer);
    if (req->cmd.lba != -1) {
        trace_scsi_req_parsed_lba(req->dev->id, req->lun, req->tag, buf[0],
                              req->cmd.lba);
    }
443 444
    return 0;
}
G
Gerd Hoffmann 已提交
445

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
/*
 * Predefined sense codes
 */

/* No sense data available */
const struct SCSISense sense_code_NO_SENSE = {
    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
};

/* LUN not ready, Manual intervention required */
const struct SCSISense sense_code_LUN_NOT_READY = {
    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
};

/* LUN not ready, Medium not present */
const struct SCSISense sense_code_NO_MEDIUM = {
    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
};

/* Hardware error, internal target failure */
const struct SCSISense sense_code_TARGET_FAILURE = {
    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
};

/* Illegal request, invalid command operation code */
const struct SCSISense sense_code_INVALID_OPCODE = {
    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
};

/* Illegal request, LBA out of range */
const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
};

/* Illegal request, Invalid field in CDB */
const struct SCSISense sense_code_INVALID_FIELD = {
    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
};

/* Illegal request, LUN not supported */
const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
};

/* Command aborted, I/O process terminated */
const struct SCSISense sense_code_IO_ERROR = {
    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
};

/* Command aborted, I_T Nexus loss occurred */
const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
};

/* Command aborted, Logical Unit failure */
const struct SCSISense sense_code_LUN_FAILURE = {
    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
};

/*
 * scsi_build_sense
 *
508
 * Convert between fixed and descriptor sense buffers
509
 */
510 511
int scsi_build_sense(uint8_t *in_buf, int in_len,
                     uint8_t *buf, int len, bool fixed)
512
{
513 514
    bool fixed_in;
    SCSISense sense;
515 516 517 518
    if (!fixed && len < 8) {
        return 0;
    }

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    if (in_len == 0) {
        sense.key = NO_SENSE;
        sense.asc = 0;
        sense.ascq = 0;
    } else {
        fixed_in = (in_buf[0] & 2) == 0;

        if (fixed == fixed_in) {
            memcpy(buf, in_buf, MIN(len, in_len));
            return MIN(len, in_len);
        }

        if (fixed_in) {
            sense.key = in_buf[2];
            sense.asc = in_buf[12];
            sense.ascq = in_buf[13];
        } else {
            sense.key = in_buf[1];
            sense.asc = in_buf[2];
            sense.ascq = in_buf[3];
        }
    }

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    memset(buf, 0, len);
    if (fixed) {
        /* Return fixed format sense buffer */
        buf[0] = 0xf0;
        buf[2] = sense.key;
        buf[7] = 7;
        buf[12] = sense.asc;
        buf[13] = sense.ascq;
        return MIN(len, 18);
    } else {
        /* Return descriptor format sense buffer */
        buf[0] = 0x72;
        buf[1] = sense.key;
        buf[2] = sense.asc;
        buf[3] = sense.ascq;
        return 8;
    }
}

G
Gerd Hoffmann 已提交
561 562 563 564
static const char *scsi_command_name(uint8_t cmd)
{
    static const char *names[] = {
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
565
        [ REWIND                   ] = "REWIND",
G
Gerd Hoffmann 已提交
566 567 568 569 570 571 572 573 574 575 576 577
        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS",
        [ READ_6                   ] = "READ_6",
        [ WRITE_6                  ] = "WRITE_6",
        [ SEEK_6                   ] = "SEEK_6",
        [ READ_REVERSE             ] = "READ_REVERSE",
        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
        [ SPACE                    ] = "SPACE",
        [ INQUIRY                  ] = "INQUIRY",
        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
578 579
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
G
Gerd Hoffmann 已提交
580 581 582 583 584 585 586 587 588 589
        [ MODE_SELECT              ] = "MODE_SELECT",
        [ RESERVE                  ] = "RESERVE",
        [ RELEASE                  ] = "RELEASE",
        [ COPY                     ] = "COPY",
        [ ERASE                    ] = "ERASE",
        [ MODE_SENSE               ] = "MODE_SENSE",
        [ START_STOP               ] = "START_STOP",
        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
590
        [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
G
Gerd Hoffmann 已提交
591 592 593
        [ READ_10                  ] = "READ_10",
        [ WRITE_10                 ] = "WRITE_10",
        [ SEEK_10                  ] = "SEEK_10",
594 595
        [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
        [ VERIFY_10                ] = "VERIFY_10",
G
Gerd Hoffmann 已提交
596 597 598 599 600
        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
        [ SEARCH_LOW               ] = "SEARCH_LOW",
        [ SET_LIMITS               ] = "SET_LIMITS",
        [ PRE_FETCH                ] = "PRE_FETCH",
601
        /* READ_POSITION and PRE_FETCH use the same operation code */
G
Gerd Hoffmann 已提交
602 603 604 605 606 607 608 609 610
        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA",
        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
        [ COMPARE                  ] = "COMPARE",
        [ COPY_VERIFY              ] = "COPY_VERIFY",
        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
        [ READ_BUFFER              ] = "READ_BUFFER",
        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
611 612
        [ READ_LONG_10             ] = "READ_LONG_10",
        [ WRITE_LONG_10            ] = "WRITE_LONG_10",
G
Gerd Hoffmann 已提交
613
        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
614 615
        [ WRITE_SAME_10            ] = "WRITE_SAME_10",
        [ UNMAP                    ] = "UNMAP",
G
Gerd Hoffmann 已提交
616
        [ READ_TOC                 ] = "READ_TOC",
617 618
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
G
Gerd Hoffmann 已提交
619 620 621 622 623 624 625 626
        [ LOG_SELECT               ] = "LOG_SELECT",
        [ LOG_SENSE                ] = "LOG_SENSE",
        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
        [ RESERVE_10               ] = "RESERVE_10",
        [ RELEASE_10               ] = "RELEASE_10",
        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
        [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
        [ EXTENDED_COPY            ] = "EXTENDED_COPY",
        [ ATA_PASSTHROUGH          ] = "ATA_PASSTHROUGH",
        [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
        [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
        [ READ_16                  ] = "READ_16",
        [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
        [ WRITE_16                 ] = "WRITE_16",
        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
        [ VERIFY_16                ] = "VERIFY_16",
        [ SYNCHRONIZE_CACHE_16     ] = "SYNCHRONIZE_CACHE_16",
        [ LOCATE_16                ] = "LOCATE_16",
        [ WRITE_SAME_16            ] = "WRITE_SAME_16",
        [ ERASE_16                 ] = "ERASE_16",
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
        [ WRITE_LONG_16            ] = "WRITE_LONG_16",
        [ REPORT_LUNS              ] = "REPORT_LUNS",
        [ BLANK                    ] = "BLANK",
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
G
Gerd Hoffmann 已提交
647
        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
648
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
G
Gerd Hoffmann 已提交
649 650 651
        [ READ_12                  ] = "READ_12",
        [ WRITE_12                 ] = "WRITE_12",
        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
652
        [ VERIFY_12                ] = "VERIFY_12",
G
Gerd Hoffmann 已提交
653 654 655 656 657
        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG",
658
        [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
G
Gerd Hoffmann 已提交
659 660 661 662 663 664 665 666
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
    };

    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
        return "*UNKNOWN*";
    return names[cmd];
}

P
Paolo Bonzini 已提交
667 668 669 670 671 672 673 674 675
SCSIRequest *scsi_req_ref(SCSIRequest *req)
{
    req->refcount++;
    return req;
}

void scsi_req_unref(SCSIRequest *req)
{
    if (--req->refcount == 0) {
676 677
        if (req->ops->free_req) {
            req->ops->free_req(req);
P
Paolo Bonzini 已提交
678 679 680 681 682
        }
        qemu_free(req);
    }
}

683 684 685 686 687 688
/* Tell the device that we finished processing this chunk of I/O.  It
   will start the next chunk or complete the command.  */
void scsi_req_continue(SCSIRequest *req)
{
    trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
    if (req->cmd.mode == SCSI_XFER_TO_DEV) {
689
        req->ops->write_data(req);
690
    } else {
691
        req->ops->read_data(req);
692 693 694
    }
}

P
Paolo Bonzini 已提交
695 696
/* Called by the devices when data is ready for the HBA.  The HBA should
   start a DMA operation to read or fill the device's data buffer.
697
   Once it completes, calling scsi_req_continue will restart I/O.  */
P
Paolo Bonzini 已提交
698 699 700
void scsi_req_data(SCSIRequest *req, int len)
{
    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
701
    req->bus->ops->transfer_data(req, len);
P
Paolo Bonzini 已提交
702 703
}

G
Gerd Hoffmann 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
void scsi_req_print(SCSIRequest *req)
{
    FILE *fp = stderr;
    int i;

    fprintf(fp, "[%s id=%d] %s",
            req->dev->qdev.parent_bus->name,
            req->dev->id,
            scsi_command_name(req->cmd.buf[0]));
    for (i = 1; i < req->cmd.len; i++) {
        fprintf(fp, " 0x%02x", req->cmd.buf[i]);
    }
    switch (req->cmd.mode) {
    case SCSI_XFER_NONE:
        fprintf(fp, " - none\n");
        break;
    case SCSI_XFER_FROM_DEV:
        fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
        break;
    case SCSI_XFER_TO_DEV:
        fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
        break;
    default:
        fprintf(fp, " - Oops\n");
        break;
    }
}

732
void scsi_req_complete(SCSIRequest *req, int status)
G
Gerd Hoffmann 已提交
733
{
734 735
    assert(req->status == -1);
    req->status = status;
736 737 738 739 740 741 742 743 744 745 746

    assert(req->sense_len < sizeof(req->sense));
    if (status == GOOD) {
        req->sense_len = 0;
    }

    if (req->sense_len) {
        memcpy(req->dev->sense, req->sense, req->sense_len);
    }
    req->dev->sense_len = req->sense_len;

P
Paolo Bonzini 已提交
747
    scsi_req_ref(req);
748
    scsi_req_dequeue(req);
749
    req->bus->ops->complete(req, req->status);
P
Paolo Bonzini 已提交
750
    scsi_req_unref(req);
G
Gerd Hoffmann 已提交
751
}
752

P
Paolo Bonzini 已提交
753 754
void scsi_req_cancel(SCSIRequest *req)
{
755 756
    if (req->ops->cancel_io) {
        req->ops->cancel_io(req);
P
Paolo Bonzini 已提交
757 758 759 760 761 762 763 764 765
    }
    scsi_req_ref(req);
    scsi_req_dequeue(req);
    if (req->bus->ops->cancel) {
        req->bus->ops->cancel(req);
    }
    scsi_req_unref(req);
}

P
Paolo Bonzini 已提交
766 767
void scsi_req_abort(SCSIRequest *req, int status)
{
768 769
    if (req->ops->cancel_io) {
        req->ops->cancel_io(req);
P
Paolo Bonzini 已提交
770
    }
771
    scsi_req_complete(req, status);
P
Paolo Bonzini 已提交
772 773
}

P
Paolo Bonzini 已提交
774 775 776 777 778 779
void scsi_device_purge_requests(SCSIDevice *sdev)
{
    SCSIRequest *req;

    while (!QTAILQ_EMPTY(&sdev->requests)) {
        req = QTAILQ_FIRST(&sdev->requests);
P
Paolo Bonzini 已提交
780
        scsi_req_cancel(req);
P
Paolo Bonzini 已提交
781 782 783
    }
}

784 785
static char *scsibus_get_fw_dev_path(DeviceState *dev)
{
Z
Zhi Yong Wu 已提交
786
    SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    SCSIBus *bus = scsi_bus_from_device(d);
    char path[100];
    int i;

    for (i = 0; i < bus->ndev; i++) {
        if (bus->devs[i] == d) {
            break;
        }
    }

    assert(i != bus->ndev);

    snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);

    return strdup(path);
}