scsi-bus.c 33.1 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
static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
11 12
static int scsi_build_sense(uint8_t *in_buf, int in_len,
                            uint8_t *buf, int len, bool fixed);
13

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

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

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);
40
    SCSIDevice *olddev;
G
Gerd Hoffmann 已提交
41
    int rc = -1;
42 43

    if (dev->id == -1) {
44 45 46 47
        int id;
        for (id = 0; id < bus->info->ndev; id++) {
            if (!scsi_device_find(bus, id, 0)) {
                dev->id = id;
48
                break;
49
            }
50 51
        }
    }
52
    if (dev->id >= bus->info->ndev) {
53
        error_report("bad scsi device id: %d", dev->id);
54 55 56
        goto err;
    }

57 58 59
    olddev = scsi_device_find(bus, dev->id, dev->lun);
    if (olddev && dev->lun == olddev->lun) {
        qdev_free(&olddev->qdev);
60 61 62
    }

    dev->info = info;
63
    QTAILQ_INIT(&dev->requests);
G
Gerd Hoffmann 已提交
64
    rc = dev->info->init(dev);
65 66

err:
G
Gerd Hoffmann 已提交
67 68 69 70 71 72 73
    return rc;
}

static int scsi_qdev_exit(DeviceState *qdev)
{
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);

74 75
    if (dev->info->destroy) {
        dev->info->destroy(dev);
G
Gerd Hoffmann 已提交
76 77
    }
    return 0;
78 79 80 81 82 83
}

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

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

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

111
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
112
{
113
    Location loc;
114
    DriveInfo *dinfo;
115
    int res = 0, unit;
116

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

133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* SCSIReqOps implementation for invalid commands.  */

static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
{
    scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
    scsi_req_complete(req, CHECK_CONDITION);
    return 0;
}

struct SCSIReqOps reqops_invalid_opcode = {
    .size         = sizeof(SCSIRequest),
    .send_command = scsi_invalid_command
};

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/* SCSIReqOps implementation for unit attention conditions.  */

static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
{
    if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) {
        scsi_req_build_sense(req, req->dev->unit_attention);
    } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
        scsi_req_build_sense(req, req->bus->unit_attention);
    }
    scsi_req_complete(req, CHECK_CONDITION);
    return 0;
}

struct SCSIReqOps reqops_unit_attention = {
    .size         = sizeof(SCSIRequest),
    .send_command = scsi_unit_attention
};

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/* SCSIReqOps implementation for REPORT LUNS and for commands sent to
   an invalid LUN.  */

typedef struct SCSITargetReq SCSITargetReq;

struct SCSITargetReq {
    SCSIRequest req;
    int len;
    uint8_t buf[64];
};

static void store_lun(uint8_t *outbuf, int lun)
{
    if (lun < 256) {
        outbuf[1] = lun;
        return;
    }
    outbuf[1] = (lun & 255);
    outbuf[0] = (lun >> 8) | 0x40;
}

static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
{
    int len;
    if (r->req.cmd.xfer < 16) {
        return false;
    }
    if (r->req.cmd.buf[2] > 2) {
        return false;
    }
    len = MIN(sizeof r->buf, r->req.cmd.xfer);
    memset(r->buf, 0, len);
    if (r->req.dev->lun != 0) {
        r->buf[3] = 16;
        r->len = 24;
        store_lun(&r->buf[16], r->req.dev->lun);
    } else {
        r->buf[3] = 8;
        r->len = 16;
    }
    return true;
}

static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
{
    assert(r->req.dev->lun != r->req.lun);
    if (r->req.cmd.buf[1] & 0x2) {
        /* Command support data - optional, not implemented */
        return false;
    }

    if (r->req.cmd.buf[1] & 0x1) {
        /* Vital product data */
        uint8_t page_code = r->req.cmd.buf[2];
        if (r->req.cmd.xfer < 4) {
            return false;
        }

        r->buf[r->len++] = page_code ; /* this page */
        r->buf[r->len++] = 0x00;

        switch (page_code) {
        case 0x00: /* Supported page codes, mandatory */
        {
            int pages;
            pages = r->len++;
            r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
            r->buf[pages] = r->len - pages - 1; /* number of pages */
            break;
        }
        default:
            return false;
        }
        /* done with EVPD */
        assert(r->len < sizeof(r->buf));
        r->len = MIN(r->req.cmd.xfer, r->len);
        return true;
    }

    /* Standard INQUIRY data */
    if (r->req.cmd.buf[2] != 0) {
        return false;
    }

    /* PAGE CODE == 0 */
    if (r->req.cmd.xfer < 5) {
        return -1;
    }

    r->len = MIN(r->req.cmd.xfer, 36);
    memset(r->buf, 0, r->len);
    if (r->req.lun != 0) {
        r->buf[0] = TYPE_NO_LUN;
    } else {
        r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
        r->buf[2] = 5; /* Version */
        r->buf[3] = 2 | 0x10; /* HiSup, response data format */
        r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
263
        r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ.  */
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        memcpy(&r->buf[8], "QEMU    ", 8);
        memcpy(&r->buf[16], "QEMU TARGET     ", 16);
        strncpy((char *) &r->buf[32], QEMU_VERSION, 4);
    }
    return true;
}

static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
{
    SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);

    switch (buf[0]) {
    case REPORT_LUNS:
        if (!scsi_target_emulate_report_luns(r)) {
            goto illegal_request;
        }
        break;
    case INQUIRY:
        if (!scsi_target_emulate_inquiry(r)) {
            goto illegal_request;
        }
        break;
286 287 288 289
    case REQUEST_SENSE:
        if (req->cmd.xfer < 4) {
            goto illegal_request;
        }
290 291
        r->len = scsi_device_get_sense(r->req.dev, r->buf,
                                       MIN(req->cmd.xfer, sizeof r->buf),
292
                                       (req->cmd.buf[1] & 1) == 0);
293 294 295 296 297 298 299
        if (r->req.dev->sense_is_ua) {
            if (r->req.dev->info->unit_attention_reported) {
                r->req.dev->info->unit_attention_reported(req->dev);
            }
            r->req.dev->sense_len = 0;
            r->req.dev->sense_is_ua = false;
        }
300
        break;
301 302 303 304 305 306 307 308 309 310 311 312 313 314 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 344 345
    default:
        scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
        scsi_req_complete(req, CHECK_CONDITION);
        return 0;
    illegal_request:
        scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
        scsi_req_complete(req, CHECK_CONDITION);
        return 0;
    }

    if (!r->len) {
        scsi_req_complete(req, GOOD);
    }
    return r->len;
}

static void scsi_target_read_data(SCSIRequest *req)
{
    SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
    uint32_t n;

    n = r->len;
    if (n > 0) {
        r->len = 0;
        scsi_req_data(&r->req, n);
    } else {
        scsi_req_complete(&r->req, GOOD);
    }
}

static uint8_t *scsi_target_get_buf(SCSIRequest *req)
{
    SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);

    return r->buf;
}

struct SCSIReqOps reqops_target_command = {
    .size         = sizeof(SCSITargetReq),
    .send_command = scsi_target_send_command,
    .read_data    = scsi_target_read_data,
    .get_buf      = scsi_target_get_buf,
};


P
Paolo Bonzini 已提交
346
SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
347
                            uint32_t lun, void *hba_private)
348 349 350
{
    SCSIRequest *req;

351
    req = g_malloc0(reqops->size);
352
    req->refcount = 1;
353 354 355 356
    req->bus = scsi_bus_from_device(d);
    req->dev = d;
    req->tag = tag;
    req->lun = lun;
357
    req->hba_private = hba_private;
G
Gerd Hoffmann 已提交
358
    req->status = -1;
359
    req->sense_len = 0;
P
Paolo Bonzini 已提交
360
    req->ops = reqops;
361
    trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
362 363 364
    return req;
}

365
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
366
                          uint8_t *buf, void *hba_private)
P
Paolo Bonzini 已提交
367
{
368
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
369
    SCSIRequest *req;
370 371 372 373 374 375 376 377
    SCSICommand cmd;

    if (scsi_req_parse(&cmd, d, buf) != 0) {
        trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
        req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
    } else {
        trace_scsi_req_parsed(d->id, lun, tag, buf[0],
                              cmd.mode, cmd.xfer);
378
        if (cmd.lba != -1) {
379 380 381
            trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
                                      cmd.lba);
        }
382

383 384 385 386 387
        if ((d->unit_attention.key == UNIT_ATTENTION ||
             bus->unit_attention.key == UNIT_ATTENTION) &&
            (buf[0] != INQUIRY &&
             buf[0] != REPORT_LUNS &&
             buf[0] != GET_CONFIGURATION &&
388 389 390 391 392 393 394
             buf[0] != GET_EVENT_STATUS_NOTIFICATION &&

             /*
              * If we already have a pending unit attention condition,
              * report this one before triggering another one.
              */
             !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
395 396 397
            req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
                                 hba_private);
        } else if (lun != d->lun ||
398 399
            buf[0] == REPORT_LUNS ||
            buf[0] == REQUEST_SENSE) {
400 401 402 403 404
            req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
                                 hba_private);
        } else {
            req = d->info->alloc_req(d, tag, lun, hba_private);
        }
405 406 407
    }

    req->cmd = cmd;
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    switch (buf[0]) {
    case INQUIRY:
        trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
        break;
    case TEST_UNIT_READY:
        trace_scsi_test_unit_ready(d->id, lun, tag);
        break;
    case REPORT_LUNS:
        trace_scsi_report_luns(d->id, lun, tag);
        break;
    case REQUEST_SENSE:
        trace_scsi_request_sense(d->id, lun, tag);
        break;
    default:
        break;
    }

425
    return req;
P
Paolo Bonzini 已提交
426 427
}

P
Paolo Bonzini 已提交
428 429
uint8_t *scsi_req_get_buf(SCSIRequest *req)
{
430
    return req->ops->get_buf(req);
P
Paolo Bonzini 已提交
431 432
}

433 434 435 436 437 438 439 440 441 442 443 444 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
static void scsi_clear_unit_attention(SCSIRequest *req)
{
    SCSISense *ua;
    if (req->dev->unit_attention.key != UNIT_ATTENTION &&
        req->bus->unit_attention.key != UNIT_ATTENTION) {
        return;
    }

    /*
     * If an INQUIRY command enters the enabled command state,
     * the device server shall [not] clear any unit attention condition;
     * See also MMC-6, paragraphs 6.5 and 6.6.2.
     */
    if (req->cmd.buf[0] == INQUIRY ||
        req->cmd.buf[0] == GET_CONFIGURATION ||
        req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
        return;
    }

    if (req->dev->unit_attention.key == UNIT_ATTENTION) {
        ua = &req->dev->unit_attention;
    } else {
        ua = &req->bus->unit_attention;
    }

    /*
     * If a REPORT LUNS command enters the enabled command state, [...]
     * the device server shall clear any pending unit attention condition
     * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
     */
    if (req->cmd.buf[0] == REPORT_LUNS &&
        !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
          ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
        return;
    }

    *ua = SENSE_CODE(NO_SENSE);
}

472 473
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
{
474 475
    int ret;

476 477
    assert(len >= 14);
    if (!req->sense_len) {
478 479
        return 0;
    }
480 481 482 483 484 485 486 487 488 489

    ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);

    /*
     * FIXME: clearing unit attention conditions upon autosense should be done
     * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
     * (SAM-5, 5.14).
     *
     * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
     * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
490
     * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
491
     */
492 493 494 495 496 497 498
    if (req->dev->sense_is_ua) {
        if (req->dev->info->unit_attention_reported) {
            req->dev->info->unit_attention_reported(req->dev);
        }
        req->dev->sense_len = 0;
        req->dev->sense_is_ua = false;
    }
499
    return ret;
500 501 502 503 504 505 506 507 508 509 510 511 512 513
}

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;
514
    req->sense[7] = 10;
515 516 517
    req->sense[12] = sense.asc;
    req->sense[13] = sense.ascq;
    req->sense_len = 18;
518 519
}

520
int32_t scsi_req_enqueue(SCSIRequest *req)
521
{
522 523
    int32_t rc;

524 525 526 527
    assert(!req->enqueued);
    scsi_req_ref(req);
    req->enqueued = true;
    QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
528 529

    scsi_req_ref(req);
530
    rc = req->ops->send_command(req, req->cmd.buf);
531 532
    scsi_req_unref(req);
    return rc;
533 534
}

535
static void scsi_req_dequeue(SCSIRequest *req)
536
{
537
    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
538 539 540
    if (req->enqueued) {
        QTAILQ_REMOVE(&req->dev->requests, req, next);
        req->enqueued = false;
P
Paolo Bonzini 已提交
541
        scsi_req_unref(req);
542 543 544
    }
}

P
Paolo Bonzini 已提交
545
static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
546
{
P
Paolo Bonzini 已提交
547
    switch (buf[0] >> 5) {
548
    case 0:
P
Paolo Bonzini 已提交
549 550
        cmd->xfer = buf[4];
        cmd->len = 6;
551
        /* length 0 means 256 blocks */
P
Paolo Bonzini 已提交
552 553 554
        if (cmd->xfer == 0) {
            cmd->xfer = 256;
        }
555 556 557
        break;
    case 1:
    case 2:
558
        cmd->xfer = lduw_be_p(&buf[7]);
P
Paolo Bonzini 已提交
559
        cmd->len = 10;
560 561
        break;
    case 4:
562
        cmd->xfer = ldl_be_p(&buf[10]);
P
Paolo Bonzini 已提交
563
        cmd->len = 16;
564 565
        break;
    case 5:
566
        cmd->xfer = ldl_be_p(&buf[6]);
P
Paolo Bonzini 已提交
567
        cmd->len = 12;
568 569 570 571 572
        break;
    default:
        return -1;
    }

P
Paolo Bonzini 已提交
573
    switch (buf[0]) {
574
    case TEST_UNIT_READY:
575
    case REWIND:
576 577 578 579
    case START_STOP:
    case SEEK_6:
    case WRITE_FILEMARKS:
    case SPACE:
580 581
    case RESERVE:
    case RELEASE:
582 583
    case ERASE:
    case ALLOW_MEDIUM_REMOVAL:
584
    case VERIFY_10:
585 586 587 588 589 590
    case SEEK_10:
    case SYNCHRONIZE_CACHE:
    case LOCK_UNLOCK_CACHE:
    case LOAD_UNLOAD:
    case SET_CD_SPEED:
    case SET_LIMITS:
591
    case WRITE_LONG_10:
592 593
    case MOVE_MEDIUM:
    case UPDATE_BLOCK:
P
Paolo Bonzini 已提交
594
        cmd->xfer = 0;
595 596 597
        break;
    case MODE_SENSE:
        break;
598
    case WRITE_SAME_10:
P
Paolo Bonzini 已提交
599
        cmd->xfer = 1;
600
        break;
601
    case READ_CAPACITY_10:
P
Paolo Bonzini 已提交
602
        cmd->xfer = 8;
603 604
        break;
    case READ_BLOCK_LIMITS:
P
Paolo Bonzini 已提交
605
        cmd->xfer = 6;
606 607
        break;
    case READ_POSITION:
P
Paolo Bonzini 已提交
608
        cmd->xfer = 20;
609 610
        break;
    case SEND_VOLUME_TAG:
P
Paolo Bonzini 已提交
611
        cmd->xfer *= 40;
612 613
        break;
    case MEDIUM_SCAN:
P
Paolo Bonzini 已提交
614
        cmd->xfer *= 8;
615 616
        break;
    case WRITE_10:
617
    case WRITE_VERIFY_10:
618 619 620
    case WRITE_6:
    case WRITE_12:
    case WRITE_VERIFY_12:
G
Gerd Hoffmann 已提交
621 622
    case WRITE_16:
    case WRITE_VERIFY_16:
P
Paolo Bonzini 已提交
623
        cmd->xfer *= dev->blocksize;
624 625 626 627 628 629
        break;
    case READ_10:
    case READ_6:
    case READ_REVERSE:
    case RECOVER_BUFFERED_DATA:
    case READ_12:
G
Gerd Hoffmann 已提交
630
    case READ_16:
P
Paolo Bonzini 已提交
631
        cmd->xfer *= dev->blocksize;
632 633
        break;
    case INQUIRY:
P
Paolo Bonzini 已提交
634
        cmd->xfer = buf[4] | (buf[3] << 8);
635
        break;
636 637
    case MAINTENANCE_OUT:
    case MAINTENANCE_IN:
P
Paolo Bonzini 已提交
638
        if (dev->type == TYPE_ROM) {
639
            /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
P
Paolo Bonzini 已提交
640
            cmd->xfer = buf[9] | (buf[8] << 8);
641 642
        }
        break;
643 644 645 646
    }
    return 0;
}

P
Paolo Bonzini 已提交
647
static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
648
{
P
Paolo Bonzini 已提交
649
    switch (buf[0]) {
650 651 652 653 654
    /* stream commands */
    case READ_6:
    case READ_REVERSE:
    case RECOVER_BUFFERED_DATA:
    case WRITE_6:
P
Paolo Bonzini 已提交
655 656 657 658 659
        cmd->len = 6;
        cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
        if (buf[1] & 0x01) { /* fixed */
            cmd->xfer *= dev->blocksize;
        }
660 661 662
        break;
    case REWIND:
    case START_STOP:
P
Paolo Bonzini 已提交
663 664
        cmd->len = 6;
        cmd->xfer = 0;
665 666 667
        break;
    /* generic commands */
    default:
P
Paolo Bonzini 已提交
668
        return scsi_req_length(cmd, dev, buf);
669 670 671 672
    }
    return 0;
}

P
Paolo Bonzini 已提交
673
static void scsi_cmd_xfer_mode(SCSICommand *cmd)
G
Gerd Hoffmann 已提交
674
{
P
Paolo Bonzini 已提交
675
    switch (cmd->buf[0]) {
G
Gerd Hoffmann 已提交
676 677
    case WRITE_6:
    case WRITE_10:
678
    case WRITE_VERIFY_10:
G
Gerd Hoffmann 已提交
679 680
    case WRITE_12:
    case WRITE_VERIFY_12:
G
Gerd Hoffmann 已提交
681 682
    case WRITE_16:
    case WRITE_VERIFY_16:
G
Gerd Hoffmann 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    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:
698 699
    case WRITE_LONG_10:
    case WRITE_SAME_10:
G
Gerd Hoffmann 已提交
700 701 702 703 704
    case SEARCH_HIGH_12:
    case SEARCH_EQUAL_12:
    case SEARCH_LOW_12:
    case MEDIUM_SCAN:
    case SEND_VOLUME_TAG:
705
    case PERSISTENT_RESERVE_OUT:
706
    case MAINTENANCE_OUT:
P
Paolo Bonzini 已提交
707
        cmd->mode = SCSI_XFER_TO_DEV;
G
Gerd Hoffmann 已提交
708 709
        break;
    default:
P
Paolo Bonzini 已提交
710 711
        if (cmd->xfer)
            cmd->mode = SCSI_XFER_FROM_DEV;
G
Gerd Hoffmann 已提交
712
        else {
P
Paolo Bonzini 已提交
713
            cmd->mode = SCSI_XFER_NONE;
G
Gerd Hoffmann 已提交
714 715 716 717 718
        }
        break;
    }
}

P
Paolo Bonzini 已提交
719
static uint64_t scsi_cmd_lba(SCSICommand *cmd)
720
{
P
Paolo Bonzini 已提交
721
    uint8_t *buf = cmd->buf;
722 723 724 725
    uint64_t lba;

    switch (buf[0] >> 5) {
    case 0:
726
        lba = ldl_be_p(&buf[0]) & 0x1fffff;
727 728 729
        break;
    case 1:
    case 2:
730 731
    case 5:
        lba = ldl_be_p(&buf[2]);
732 733
        break;
    case 4:
734
        lba = ldq_be_p(&buf[2]);
735 736 737 738 739 740 741 742
        break;
    default:
        lba = -1;

    }
    return lba;
}

743
int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
744 745 746
{
    int rc;

747 748
    if (dev->type == TYPE_TAPE) {
        rc = scsi_req_stream_length(cmd, dev, buf);
749
    } else {
750
        rc = scsi_req_length(cmd, dev, buf);
751 752 753 754
    }
    if (rc != 0)
        return rc;

755 756 757
    memcpy(cmd->buf, buf, cmd->len);
    scsi_cmd_xfer_mode(cmd);
    cmd->lba = scsi_cmd_lba(cmd);
758 759
    return 0;
}
G
Gerd Hoffmann 已提交
760

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
/*
 * 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
};

780 781 782 783 784
/* LUN not ready, medium removal prevented */
const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
    .key = NOT_READY, .asc = 0x53, .ascq = 0x00
};

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
/* 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
};

810 811 812 813 814 815
/* Illegal request, Saving parameters not supported */
const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
    .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
};

/* Illegal request, Incompatible medium installed */
816
const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
817 818 819
    .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
};

820 821 822 823 824
/* Illegal request, medium removal prevented */
const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
    .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x00
};

825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/* 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
};

840 841 842 843 844
/* Unit attention, Power on, reset or bus device reset occurred */
const struct SCSISense sense_code_RESET = {
    .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
};

845 846 847 848 849
/* Unit attention, No medium */
const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
    .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
};

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
/* Unit attention, Medium may have changed */
const struct SCSISense sense_code_MEDIUM_CHANGED = {
    .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
};

/* Unit attention, Reported LUNs data has changed */
const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
    .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
};

/* Unit attention, Device internal reset */
const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
    .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
};

865 866 867
/*
 * scsi_build_sense
 *
868
 * Convert between fixed and descriptor sense buffers
869
 */
870 871
int scsi_build_sense(uint8_t *in_buf, int in_len,
                     uint8_t *buf, int len, bool fixed)
872
{
873 874
    bool fixed_in;
    SCSISense sense;
875 876 877 878
    if (!fixed && len < 8) {
        return 0;
    }

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
    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];
        }
    }

902 903 904 905 906
    memset(buf, 0, len);
    if (fixed) {
        /* Return fixed format sense buffer */
        buf[0] = 0xf0;
        buf[2] = sense.key;
907
        buf[7] = 10;
908 909 910 911 912 913 914 915 916 917 918 919 920
        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 已提交
921 922 923 924
static const char *scsi_command_name(uint8_t cmd)
{
    static const char *names[] = {
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
925
        [ REWIND                   ] = "REWIND",
G
Gerd Hoffmann 已提交
926 927 928 929 930 931 932 933 934 935 936 937
        [ 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",
938 939
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
G
Gerd Hoffmann 已提交
940 941 942 943 944 945 946 947 948 949
        [ 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",
950
        [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
G
Gerd Hoffmann 已提交
951 952 953
        [ READ_10                  ] = "READ_10",
        [ WRITE_10                 ] = "WRITE_10",
        [ SEEK_10                  ] = "SEEK_10",
954 955
        [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
        [ VERIFY_10                ] = "VERIFY_10",
G
Gerd Hoffmann 已提交
956 957 958 959 960
        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
        [ SEARCH_LOW               ] = "SEARCH_LOW",
        [ SET_LIMITS               ] = "SET_LIMITS",
        [ PRE_FETCH                ] = "PRE_FETCH",
961
        /* READ_POSITION and PRE_FETCH use the same operation code */
G
Gerd Hoffmann 已提交
962 963 964 965 966 967 968 969 970
        [ 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",
971 972
        [ READ_LONG_10             ] = "READ_LONG_10",
        [ WRITE_LONG_10            ] = "WRITE_LONG_10",
G
Gerd Hoffmann 已提交
973
        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
974 975
        [ WRITE_SAME_10            ] = "WRITE_SAME_10",
        [ UNMAP                    ] = "UNMAP",
G
Gerd Hoffmann 已提交
976
        [ READ_TOC                 ] = "READ_TOC",
977 978
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
G
Gerd Hoffmann 已提交
979 980 981 982 983 984 985 986
        [ 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",
987 988 989 990 991 992 993 994 995 996 997 998 999
        [ 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",
1000
        /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1001
        [ SERVICE_ACTION_IN_16     ] = "SERVICE_ACTION_IN_16",
1002 1003 1004
        [ WRITE_LONG_16            ] = "WRITE_LONG_16",
        [ REPORT_LUNS              ] = "REPORT_LUNS",
        [ BLANK                    ] = "BLANK",
G
Gerd Hoffmann 已提交
1005
        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
1006
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
G
Gerd Hoffmann 已提交
1007 1008
        [ READ_12                  ] = "READ_12",
        [ WRITE_12                 ] = "WRITE_12",
1009
        [ SERVICE_ACTION_IN_12     ] = "SERVICE_ACTION_IN_12",
G
Gerd Hoffmann 已提交
1010
        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
1011
        [ VERIFY_12                ] = "VERIFY_12",
G
Gerd Hoffmann 已提交
1012 1013 1014 1015 1016
        [ 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",
1017
        [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
G
Gerd Hoffmann 已提交
1018 1019 1020 1021 1022 1023 1024 1025
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
    };

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

P
Paolo Bonzini 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034
SCSIRequest *scsi_req_ref(SCSIRequest *req)
{
    req->refcount++;
    return req;
}

void scsi_req_unref(SCSIRequest *req)
{
    if (--req->refcount == 0) {
1035 1036
        if (req->ops->free_req) {
            req->ops->free_req(req);
P
Paolo Bonzini 已提交
1037
        }
1038
        g_free(req);
P
Paolo Bonzini 已提交
1039 1040 1041
    }
}

1042 1043 1044 1045 1046 1047
/* 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) {
1048
        req->ops->write_data(req);
1049
    } else {
1050
        req->ops->read_data(req);
1051 1052 1053
    }
}

P
Paolo Bonzini 已提交
1054 1055
/* 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.
1056
   Once it completes, calling scsi_req_continue will restart I/O.  */
P
Paolo Bonzini 已提交
1057 1058 1059
void scsi_req_data(SCSIRequest *req, int len)
{
    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1060
    req->bus->info->transfer_data(req, len);
P
Paolo Bonzini 已提交
1061 1062
}

G
Gerd Hoffmann 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
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;
    }
}

1091
void scsi_req_complete(SCSIRequest *req, int status)
G
Gerd Hoffmann 已提交
1092
{
1093 1094
    assert(req->status == -1);
    req->status = status;
1095 1096 1097 1098 1099 1100 1101 1102

    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);
1103 1104 1105 1106 1107
        req->dev->sense_len = req->sense_len;
        req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
    } else {
        req->dev->sense_len = 0;
        req->dev->sense_is_ua = false;
1108 1109
    }

1110 1111 1112 1113 1114 1115 1116
    /*
     * Unit attention state is now stored in the device's sense buffer
     * if the HBA didn't do autosense.  Clear the pending unit attention
     * flags.
     */
    scsi_clear_unit_attention(req);

P
Paolo Bonzini 已提交
1117
    scsi_req_ref(req);
1118
    scsi_req_dequeue(req);
1119
    req->bus->info->complete(req, req->status);
P
Paolo Bonzini 已提交
1120
    scsi_req_unref(req);
G
Gerd Hoffmann 已提交
1121
}
1122

P
Paolo Bonzini 已提交
1123 1124
void scsi_req_cancel(SCSIRequest *req)
{
1125 1126
    if (req->ops->cancel_io) {
        req->ops->cancel_io(req);
P
Paolo Bonzini 已提交
1127 1128 1129
    }
    scsi_req_ref(req);
    scsi_req_dequeue(req);
1130 1131
    if (req->bus->info->cancel) {
        req->bus->info->cancel(req);
P
Paolo Bonzini 已提交
1132 1133 1134 1135
    }
    scsi_req_unref(req);
}

P
Paolo Bonzini 已提交
1136 1137
void scsi_req_abort(SCSIRequest *req, int status)
{
1138 1139
    if (req->ops->cancel_io) {
        req->ops->cancel_io(req);
P
Paolo Bonzini 已提交
1140
    }
1141
    scsi_req_complete(req, status);
P
Paolo Bonzini 已提交
1142 1143
}

1144
void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
P
Paolo Bonzini 已提交
1145 1146 1147 1148 1149
{
    SCSIRequest *req;

    while (!QTAILQ_EMPTY(&sdev->requests)) {
        req = QTAILQ_FIRST(&sdev->requests);
P
Paolo Bonzini 已提交
1150
        scsi_req_cancel(req);
P
Paolo Bonzini 已提交
1151
    }
1152
    sdev->unit_attention = sense;
P
Paolo Bonzini 已提交
1153 1154
}

1155 1156
static char *scsibus_get_fw_dev_path(DeviceState *dev)
{
Z
Zhi Yong Wu 已提交
1157
    SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
1158 1159
    char path[100];

1160 1161
    snprintf(path, sizeof(path), "%s@%d:%d:%d", qdev_fw_name(dev),
             0, d->id, d->lun);
1162

1163 1164 1165 1166 1167 1168 1169
    return strdup(path);
}

SCSIDevice *scsi_device_find(SCSIBus *bus, int id, int lun)
{
    DeviceState *qdev;
    SCSIDevice *target_dev = NULL;
1170

1171 1172
    QTAILQ_FOREACH_REVERSE(qdev, &bus->qbus.children, ChildrenHead, sibling) {
        SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
1173

1174 1175 1176 1177 1178 1179 1180 1181
        if (dev->id == id) {
            if (dev->lun == lun) {
                return dev;
            }
            target_dev = dev;
        }
    }
    return target_dev;
1182
}