3
#include "qemu/error-report.h"
4
#include "qemu/module.h"
5
#include "qemu/option.h"
6
#include "qemu/hw-version.h"
7
#include "hw/qdev-properties.h"
8
#include "hw/scsi/scsi.h"
9
#include "migration/qemu-file-types.h"
10
#include "migration/vmstate.h"
11
#include "scsi/constants.h"
12
#include "sysemu/block-backend.h"
13
#include "sysemu/blockdev.h"
14
#include "sysemu/sysemu.h"
15
#include "sysemu/runstate.h"
17
#include "sysemu/dma.h"
18
#include "qemu/cutils.h"
20
static char *scsibus_get_dev_path(DeviceState *dev);
21
static char *scsibus_get_fw_dev_path(DeviceState *dev);
22
static void scsi_req_dequeue(SCSIRequest *req);
23
static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len);
24
static void scsi_target_free_buf(SCSIRequest *req);
25
static void scsi_clear_reported_luns_changed(SCSIRequest *req);
27
static int next_scsi_bus;
29
static SCSIDevice *do_scsi_device_find(SCSIBus *bus,
30
int channel, int id, int lun,
31
bool include_unrealized)
34
SCSIDevice *retval = NULL;
36
QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) {
37
DeviceState *qdev = kid->child;
38
SCSIDevice *dev = SCSI_DEVICE(qdev);
40
if (dev->channel == channel && dev->id == id) {
41
if (dev->lun == lun) {
64
if (retval && !include_unrealized && !qdev_is_realized(&retval->qdev)) {
71
SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
73
RCU_READ_LOCK_GUARD();
74
return do_scsi_device_find(bus, channel, id, lun, false);
77
SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun)
80
RCU_READ_LOCK_GUARD();
81
d = do_scsi_device_find(bus, channel, id, lun, false);
93
static void scsi_device_for_each_req_sync(SCSIDevice *s,
94
void (*fn)(SCSIRequest *, void *),
98
SCSIRequest *next_req;
100
assert(!runstate_is_running());
101
assert(qemu_in_main_thread());
103
QTAILQ_FOREACH_SAFE(req, &s->requests, next, next_req) {
110
void (*fn)(SCSIRequest *, void *);
112
} SCSIDeviceForEachReqAsyncData;
114
static void scsi_device_for_each_req_async_bh(void *opaque)
116
g_autofree SCSIDeviceForEachReqAsyncData *data = opaque;
117
SCSIDevice *s = data->s;
128
ctx = blk_get_aio_context(s->conf.blk);
129
assert(ctx == qemu_get_current_aio_context());
131
QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
132
data->fn(req, data->fn_opaque);
136
object_unref(OBJECT(s));
139
blk_dec_in_flight(s->conf.blk);
148
static void scsi_device_for_each_req_async(SCSIDevice *s,
149
void (*fn)(SCSIRequest *, void *),
152
assert(qemu_in_main_thread());
154
SCSIDeviceForEachReqAsyncData *data =
155
g_new(SCSIDeviceForEachReqAsyncData, 1);
159
data->fn_opaque = opaque;
165
object_ref(OBJECT(s));
168
blk_inc_in_flight(s->conf.blk);
169
aio_bh_schedule_oneshot(blk_get_aio_context(s->conf.blk),
170
scsi_device_for_each_req_async_bh,
174
static void scsi_device_realize(SCSIDevice *s, Error **errp)
176
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
178
sc->realize(s, errp);
182
static void scsi_device_unrealize(SCSIDevice *s)
184
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
190
int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
191
size_t buf_len, void *hba_private)
193
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
196
assert(cmd->len == 0);
197
rc = scsi_req_parse_cdb(dev, cmd, buf, buf_len);
198
if (bus->info->parse_cdb) {
199
rc = bus->info->parse_cdb(dev, cmd, buf, buf_len, hba_private);
204
static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
205
uint8_t *buf, void *hba_private)
207
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
209
return sc->alloc_req(s, tag, lun, buf, hba_private);
215
void scsi_device_unit_attention_reported(SCSIDevice *s)
217
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
218
if (sc->unit_attention_reported) {
219
sc->unit_attention_reported(s);
224
void scsi_bus_init_named(SCSIBus *bus, size_t bus_size, DeviceState *host,
225
const SCSIBusInfo *info, const char *bus_name)
227
qbus_init(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
228
bus->busnr = next_scsi_bus++;
230
qbus_set_bus_hotplug_handler(BUS(bus));
233
void scsi_req_retry(SCSIRequest *req)
239
static void scsi_dma_restart_req(SCSIRequest *req, void *opaque)
244
switch (req->cmd.mode) {
245
case SCSI_XFER_FROM_DEV:
246
case SCSI_XFER_TO_DEV:
247
scsi_req_continue(req);
250
scsi_req_dequeue(req);
251
scsi_req_enqueue(req);
258
static void scsi_dma_restart_cb(void *opaque, bool running, RunState state)
260
SCSIDevice *s = opaque;
262
assert(qemu_in_main_thread());
268
scsi_device_for_each_req_async(s, scsi_dma_restart_req, NULL);
271
static bool scsi_bus_is_address_free(SCSIBus *bus,
272
int channel, int target, int lun,
277
RCU_READ_LOCK_GUARD();
278
d = do_scsi_device_find(bus, channel, target, lun, true);
279
if (d && d->lun == lun) {
291
static bool scsi_bus_check_address(BusState *qbus, DeviceState *qdev, Error **errp)
293
SCSIDevice *dev = SCSI_DEVICE(qdev);
294
SCSIBus *bus = SCSI_BUS(qbus);
296
if (dev->channel > bus->info->max_channel) {
297
error_setg(errp, "bad scsi channel id: %d", dev->channel);
300
if (dev->id != -1 && dev->id > bus->info->max_target) {
301
error_setg(errp, "bad scsi device id: %d", dev->id);
304
if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
305
error_setg(errp, "bad scsi device lun: %d", dev->lun);
309
if (dev->id != -1 && dev->lun != -1) {
311
if (!scsi_bus_is_address_free(bus, dev->channel, dev->id, dev->lun, &d)) {
312
error_setg(errp, "lun already used by '%s'", d->qdev.id);
320
static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
322
SCSIDevice *dev = SCSI_DEVICE(qdev);
323
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
325
Error *local_err = NULL;
329
if (dev->lun == -1) {
333
is_free = scsi_bus_is_address_free(bus, dev->channel, ++id, dev->lun, NULL);
334
} while (!is_free && id < bus->info->max_target);
336
error_setg(errp, "no free target");
340
} else if (dev->lun == -1) {
343
is_free = scsi_bus_is_address_free(bus, dev->channel, dev->id, ++lun, NULL);
344
} while (!is_free && lun < bus->info->max_lun);
346
error_setg(errp, "no free lun");
352
QTAILQ_INIT(&dev->requests);
353
scsi_device_realize(dev, &local_err);
355
error_propagate(errp, local_err);
358
dev->vmsentry = qdev_add_vm_change_state_handler(DEVICE(dev),
359
scsi_dma_restart_cb, dev);
362
static void scsi_qdev_unrealize(DeviceState *qdev)
364
SCSIDevice *dev = SCSI_DEVICE(qdev);
367
qemu_del_vm_change_state_handler(dev->vmsentry);
370
scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE));
372
scsi_device_unrealize(dev);
374
blockdev_mark_auto_del(dev->conf.blk);
378
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
379
int unit, bool removable, BlockConf *conf,
380
const char *serial, Error **errp)
387
Error *local_err = NULL;
389
if (blk_is_sg(blk)) {
390
driver = "scsi-generic";
392
dinfo = blk_legacy_dinfo(blk);
393
if (dinfo && dinfo->media_cd) {
399
dev = qdev_new(driver);
400
name = g_strdup_printf("legacy[%d]", unit);
401
object_property_add_child(OBJECT(bus), name, OBJECT(dev));
404
s = SCSI_DEVICE(dev);
407
check_boot_index(conf->bootindex, &local_err);
409
object_unparent(OBJECT(dev));
410
error_propagate(errp, local_err);
413
add_boot_device_path(conf->bootindex, dev, NULL);
415
qdev_prop_set_uint32(dev, "scsi-id", unit);
416
if (object_property_find(OBJECT(dev), "removable")) {
417
qdev_prop_set_bit(dev, "removable", removable);
419
if (serial && object_property_find(OBJECT(dev), "serial")) {
420
qdev_prop_set_string(dev, "serial", serial);
422
if (!qdev_prop_set_drive_err(dev, "drive", blk, errp)) {
423
object_unparent(OBJECT(dev));
427
if (!qdev_realize_and_unref(dev, &bus->qbus, errp)) {
428
object_unparent(OBJECT(dev));
434
void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
442
.rerror = BLOCKDEV_ON_ERROR_AUTO,
443
.werror = BLOCKDEV_ON_ERROR_AUTO,
447
for (unit = 0; unit <= bus->info->max_target; unit++) {
448
dinfo = drive_get(IF_SCSI, bus->busnr, unit);
452
qemu_opts_loc_restore(dinfo->opts);
453
scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo),
454
unit, false, &conf, NULL, &error_fatal);
459
static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
461
scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
462
scsi_req_complete(req, CHECK_CONDITION);
466
static const struct SCSIReqOps reqops_invalid_field = {
467
.size = sizeof(SCSIRequest),
468
.send_command = scsi_invalid_field
473
static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
475
scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
476
scsi_req_complete(req, CHECK_CONDITION);
480
static const struct SCSIReqOps reqops_invalid_opcode = {
481
.size = sizeof(SCSIRequest),
482
.send_command = scsi_invalid_command
487
static void scsi_fetch_unit_attention_sense(SCSIRequest *req)
489
SCSISense *ua = NULL;
491
if (req->dev->unit_attention.key == UNIT_ATTENTION) {
492
ua = &req->dev->unit_attention;
493
} else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
494
ua = &req->bus->unit_attention;
502
scsi_req_build_sense(req, *ua);
503
*ua = SENSE_CODE(NO_SENSE);
507
static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
509
scsi_req_complete(req, CHECK_CONDITION);
513
static const struct SCSIReqOps reqops_unit_attention = {
514
.size = sizeof(SCSIRequest),
515
.init_req = scsi_fetch_unit_attention_sense,
516
.send_command = scsi_unit_attention
522
typedef struct SCSITargetReq SCSITargetReq;
524
struct SCSITargetReq {
531
static void store_lun(uint8_t *outbuf, int lun)
539
outbuf[0] = 0x40 | (lun >> 8);
540
outbuf[1] = (lun & 255);
544
static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
548
uint8_t tmp[8] = {0};
552
if (r->req.cmd.xfer < 16) {
555
if (r->req.cmd.buf[2] > 2) {
560
buf = g_byte_array_sized_new(512);
562
channel = r->req.dev->channel;
566
g_byte_array_append(buf, tmp, 8);
570
g_byte_array_append(buf, tmp, 8);
573
WITH_RCU_READ_LOCK_GUARD() {
574
QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) {
575
DeviceState *qdev = kid->child;
576
SCSIDevice *dev = SCSI_DEVICE(qdev);
578
if (dev->channel == channel && dev->id == id && dev->lun != 0 &&
579
qdev_is_realized(&dev->qdev)) {
580
store_lun(tmp, dev->lun);
581
g_byte_array_append(buf, tmp, 8);
588
r->buf = g_byte_array_free(buf, FALSE);
589
r->len = MIN(len, r->req.cmd.xfer & ~7);
592
stl_be_p(&r->buf[0], len - 8);
599
scsi_clear_reported_luns_changed(&r->req);
604
static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
606
assert(r->req.dev->lun != r->req.lun);
608
scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
610
if (r->req.cmd.buf[1] & 0x2) {
615
if (r->req.cmd.buf[1] & 0x1) {
617
uint8_t page_code = r->req.cmd.buf[2];
618
r->buf[r->len++] = page_code ;
619
r->buf[r->len++] = 0x00;
626
r->buf[r->len++] = 0x00;
627
r->buf[pages] = r->len - pages - 1;
634
assert(r->len < r->buf_len);
635
r->len = MIN(r->req.cmd.xfer, r->len);
640
if (r->req.cmd.buf[2] != 0) {
645
r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
646
memset(r->buf, 0, r->len);
647
if (r->req.lun != 0) {
648
r->buf[0] = TYPE_NO_LUN;
650
r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
652
r->buf[3] = 2 | 0x10;
653
r->buf[4] = r->len - 5;
654
r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0);
655
memcpy(&r->buf[8], "QEMU ", 8);
656
memcpy(&r->buf[16], "QEMU TARGET ", 16);
657
pstrcpy((char *) &r->buf[32], 4, qemu_hw_version());
662
static size_t scsi_sense_len(SCSIRequest *req)
664
if (req->dev->type == TYPE_SCANNER)
665
return SCSI_SENSE_LEN_SCANNER;
667
return SCSI_SENSE_LEN;
670
static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
672
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
673
int fixed_sense = (req->cmd.buf[1] & 1) == 0;
676
buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) {
677
scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
678
scsi_req_complete(req, CHECK_CONDITION);
683
if (!scsi_target_emulate_report_luns(r)) {
684
goto illegal_request;
688
if (!scsi_target_emulate_inquiry(r)) {
689
goto illegal_request;
693
scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
695
const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED);
697
r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer,
700
r->len = scsi_device_get_sense(r->req.dev, r->buf,
701
MIN(req->cmd.xfer, r->buf_len),
704
if (r->req.dev->sense_is_ua) {
705
scsi_device_unit_attention_reported(req->dev);
706
r->req.dev->sense_len = 0;
707
r->req.dev->sense_is_ua = false;
710
case TEST_UNIT_READY:
713
scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
714
scsi_req_complete(req, CHECK_CONDITION);
717
scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
718
scsi_req_complete(req, CHECK_CONDITION);
723
scsi_req_complete(req, GOOD);
728
static void scsi_target_read_data(SCSIRequest *req)
730
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
736
scsi_req_data(&r->req, n);
738
scsi_req_complete(&r->req, GOOD);
742
static uint8_t *scsi_target_get_buf(SCSIRequest *req)
744
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
749
static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
751
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
753
r->buf = g_malloc(len);
759
static void scsi_target_free_buf(SCSIRequest *req)
761
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
766
static const struct SCSIReqOps reqops_target_command = {
767
.size = sizeof(SCSITargetReq),
768
.send_command = scsi_target_send_command,
769
.read_data = scsi_target_read_data,
770
.get_buf = scsi_target_get_buf,
771
.free_req = scsi_target_free_buf,
775
SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
776
uint32_t tag, uint32_t lun, void *hba_private)
779
SCSIBus *bus = scsi_bus_from_device(d);
780
BusState *qbus = BUS(bus);
781
const int memset_off = offsetof(SCSIRequest, sense)
782
+ sizeof(req->sense);
784
req = g_malloc(reqops->size);
785
memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off);
791
req->hba_private = hba_private;
793
req->host_status = -1;
795
object_ref(OBJECT(d));
796
object_ref(OBJECT(qbus->parent));
797
notifier_list_init(&req->cancel_notifiers);
799
if (reqops->init_req) {
800
reqops->init_req(req);
803
trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
807
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
808
uint8_t *buf, size_t buf_len, void *hba_private)
810
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
811
const SCSIReqOps *ops;
812
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d);
814
SCSICommand cmd = { .len = 0 };
818
trace_scsi_req_parse_bad(d->id, lun, tag, 0);
822
if ((d->unit_attention.key == UNIT_ATTENTION ||
823
bus->unit_attention.key == UNIT_ATTENTION) &&
824
(buf[0] != INQUIRY &&
825
buf[0] != REPORT_LUNS &&
826
buf[0] != GET_CONFIGURATION &&
827
buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
833
!(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
834
ops = &reqops_unit_attention;
835
} else if (lun != d->lun ||
836
buf[0] == REPORT_LUNS ||
837
(buf[0] == REQUEST_SENSE && d->sense_len)) {
838
ops = &reqops_target_command;
843
if (ops != NULL || !sc->parse_cdb) {
844
ret = scsi_req_parse_cdb(d, &cmd, buf, buf_len);
846
ret = sc->parse_cdb(d, &cmd, buf, buf_len, hba_private);
850
trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
852
req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
854
assert(cmd.len != 0);
855
trace_scsi_req_parsed(d->id, lun, tag, buf[0],
858
trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
862
if (cmd.xfer > INT32_MAX) {
863
req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
865
req = scsi_req_alloc(ops, d, tag, lun, hba_private);
867
req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
872
req->residual = req->cmd.xfer;
876
trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
878
case TEST_UNIT_READY:
879
trace_scsi_test_unit_ready(d->id, lun, tag);
882
trace_scsi_report_luns(d->id, lun, tag);
885
trace_scsi_request_sense(d->id, lun, tag);
894
uint8_t *scsi_req_get_buf(SCSIRequest *req)
896
return req->ops->get_buf(req);
899
static void scsi_clear_reported_luns_changed(SCSIRequest *req)
903
if (req->dev->unit_attention.key == UNIT_ATTENTION) {
904
ua = &req->dev->unit_attention;
905
} else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
906
ua = &req->bus->unit_attention;
911
if (ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
912
ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq) {
913
*ua = SENSE_CODE(NO_SENSE);
917
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
922
if (!req->sense_len) {
926
ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true);
937
if (req->dev->sense_is_ua) {
938
scsi_device_unit_attention_reported(req->dev);
939
req->dev->sense_len = 0;
940
req->dev->sense_is_ua = false;
945
int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
947
return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed);
950
void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
952
trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
953
sense.key, sense.asc, sense.ascq);
954
req->sense_len = scsi_build_sense(req->sense, sense);
957
static void scsi_req_enqueue_internal(SCSIRequest *req)
959
assert(!req->enqueued);
961
if (req->bus->info->get_sg_list) {
962
req->sg = req->bus->info->get_sg_list(req);
966
req->enqueued = true;
967
QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
970
int32_t scsi_req_enqueue(SCSIRequest *req)
975
scsi_req_enqueue_internal(req);
977
rc = req->ops->send_command(req, req->cmd.buf);
982
static void scsi_req_dequeue(SCSIRequest *req)
984
trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
987
QTAILQ_REMOVE(&req->dev->requests, req, next);
988
req->enqueued = false;
993
static int scsi_get_performance_length(int num_desc, int type, int data_type)
998
if ((data_type & 3) == 0) {
1000
return 16 * num_desc + 8;
1003
return 6 * num_desc + 8;
1008
return 8 * num_desc + 8;
1010
return 2048 * num_desc + 8;
1012
return 16 * num_desc + 8;
1018
static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
1020
int byte_block = (buf[2] >> 2) & 0x1;
1021
int type = (buf[2] >> 4) & 0x1;
1026
xfer_unit = dev->blocksize;
1037
static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf)
1039
int length = buf[2] & 0x3;
1041
int unit = ata_passthrough_xfer_unit(dev, buf);
1060
static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
1062
int extend = buf[1] & 0x1;
1063
int length = buf[2] & 0x3;
1065
int unit = ata_passthrough_xfer_unit(dev, buf);
1075
xfer |= (extend ? buf[3] << 8 : 0);
1079
xfer |= (extend ? buf[5] << 8 : 0);
1086
static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1088
cmd->xfer = scsi_cdb_xfer(buf);
1090
case TEST_UNIT_READY:
1094
case WRITE_FILEMARKS:
1095
case WRITE_FILEMARKS_16:
1100
case ALLOW_MEDIUM_REMOVAL:
1102
case SYNCHRONIZE_CACHE:
1103
case SYNCHRONIZE_CACHE_16:
1105
case LOCK_UNLOCK_CACHE:
1111
case SET_READ_AHEAD:
1114
case ALLOW_OVERWRITE:
1120
if ((buf[1] & 2) == 0) {
1122
} else if ((buf[1] & 4) != 0) {
1125
cmd->xfer *= dev->blocksize;
1131
cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize;
1133
case READ_CAPACITY_10:
1136
case READ_BLOCK_LIMITS:
1139
case SEND_VOLUME_TAG:
1141
if (dev->type == TYPE_ROM) {
1142
cmd->xfer = buf[10] | (buf[9] << 8);
1144
cmd->xfer = buf[9] | (buf[8] << 8);
1149
if (cmd->xfer == 0) {
1154
case WRITE_VERIFY_10:
1156
case WRITE_VERIFY_12:
1158
case WRITE_VERIFY_16:
1159
cmd->xfer *= dev->blocksize;
1164
if (cmd->xfer == 0) {
1171
cmd->xfer *= dev->blocksize;
1176
if (dev->type == TYPE_ROM && (buf[1] & 16)) {
1179
cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
1183
case RECEIVE_DIAGNOSTIC:
1184
case SEND_DIAGNOSTIC:
1185
cmd->xfer = buf[4] | (buf[3] << 8);
1190
case SEND_CUE_SHEET:
1191
cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1193
case PERSISTENT_RESERVE_OUT:
1194
cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
1197
if (dev->type == TYPE_ROM) {
1199
cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
1200
buf[10], buf[1] & 0x1f);
1203
case MECHANISM_STATUS:
1204
case READ_DVD_STRUCTURE:
1205
case SEND_DVD_STRUCTURE:
1206
case MAINTENANCE_OUT:
1207
case MAINTENANCE_IN:
1208
if (dev->type == TYPE_ROM) {
1210
cmd->xfer = buf[9] | (buf[8] << 8);
1213
case ATA_PASSTHROUGH_12:
1214
if (dev->type == TYPE_ROM) {
1218
cmd->xfer = ata_passthrough_12_xfer(dev, buf);
1221
case ATA_PASSTHROUGH_16:
1222
cmd->xfer = ata_passthrough_16_xfer(dev, buf);
1228
static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1238
case RECOVER_BUFFERED_DATA:
1240
cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1241
if (buf[1] & 0x01) {
1242
cmd->xfer *= dev->blocksize;
1246
case READ_REVERSE_16:
1249
cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1250
if (buf[1] & 0x01) {
1251
cmd->xfer *= dev->blocksize;
1259
cmd->xfer = buf[13] | (buf[12] << 8);
1262
switch (buf[1] & 0x1f) {
1263
case SHORT_FORM_BLOCK_ID:
1264
case SHORT_FORM_VENDOR_SPECIFIC:
1271
cmd->xfer = buf[8] | (buf[7] << 8);
1279
cmd->xfer = buf[4] | (buf[3] << 8);
1283
return scsi_req_xfer(cmd, dev, buf);
1288
static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1292
case EXCHANGE_MEDIUM:
1293
case INITIALIZE_ELEMENT_STATUS:
1294
case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1296
case POSITION_TO_ELEMENT:
1299
case READ_ELEMENT_STATUS:
1300
cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1305
return scsi_req_xfer(cmd, dev, buf);
1310
static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1314
case OBJECT_POSITION:
1324
cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1328
return scsi_req_xfer(cmd, dev, buf);
1334
static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1337
cmd->mode = SCSI_XFER_NONE;
1340
switch (cmd->buf[0]) {
1343
case WRITE_VERIFY_10:
1345
case WRITE_VERIFY_12:
1347
case WRITE_VERIFY_16:
1354
case CHANGE_DEFINITION:
1357
case MODE_SELECT_10:
1358
case SEND_DIAGNOSTIC:
1361
case REASSIGN_BLOCKS:
1370
case SEARCH_HIGH_12:
1371
case SEARCH_EQUAL_12:
1374
case SEND_VOLUME_TAG:
1375
case SEND_CUE_SHEET:
1376
case SEND_DVD_STRUCTURE:
1377
case PERSISTENT_RESERVE_OUT:
1378
case MAINTENANCE_OUT:
1384
cmd->mode = SCSI_XFER_TO_DEV;
1386
case ATA_PASSTHROUGH_12:
1387
case ATA_PASSTHROUGH_16:
1389
cmd->mode = (cmd->buf[2] & 0x8) ?
1390
SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1393
cmd->mode = SCSI_XFER_FROM_DEV;
1398
int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
1405
len = scsi_cdb_length(buf);
1406
if (len < 0 || len > buf_len) {
1411
switch (dev->type) {
1413
rc = scsi_req_stream_xfer(cmd, dev, buf);
1415
case TYPE_MEDIUM_CHANGER:
1416
rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
1419
rc = scsi_req_scanner_length(cmd, dev, buf);
1422
rc = scsi_req_xfer(cmd, dev, buf);
1429
memcpy(cmd->buf, buf, cmd->len);
1430
scsi_cmd_xfer_mode(cmd);
1431
cmd->lba = scsi_cmd_lba(cmd);
1435
void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1437
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1439
scsi_device_set_ua(dev, sense);
1440
if (bus->info->change) {
1441
bus->info->change(bus, dev, sense);
1445
SCSIRequest *scsi_req_ref(SCSIRequest *req)
1447
assert(req->refcount > 0);
1452
void scsi_req_unref(SCSIRequest *req)
1454
assert(req->refcount > 0);
1455
if (--req->refcount == 0) {
1456
BusState *qbus = req->dev->qdev.parent_bus;
1457
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1459
if (bus->info->free_request && req->hba_private) {
1460
bus->info->free_request(bus, req->hba_private);
1462
if (req->ops->free_req) {
1463
req->ops->free_req(req);
1465
object_unref(OBJECT(req->dev));
1466
object_unref(OBJECT(qbus->parent));
1473
void scsi_req_continue(SCSIRequest *req)
1475
if (req->io_canceled) {
1476
trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1479
trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1480
if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1481
req->ops->write_data(req);
1483
req->ops->read_data(req);
1490
void scsi_req_data(SCSIRequest *req, int len)
1493
if (req->io_canceled) {
1494
trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1497
trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1498
assert(req->cmd.mode != SCSI_XFER_NONE);
1500
req->residual -= len;
1501
req->bus->info->transfer_data(req, len);
1508
assert(!req->dma_started);
1509
req->dma_started = true;
1511
buf = scsi_req_get_buf(req);
1512
if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1513
dma_buf_read(buf, len, &req->residual, req->sg,
1514
MEMTXATTRS_UNSPECIFIED);
1516
dma_buf_write(buf, len, &req->residual, req->sg,
1517
MEMTXATTRS_UNSPECIFIED);
1519
scsi_req_continue(req);
1522
void scsi_req_print(SCSIRequest *req)
1527
fprintf(fp, "[%s id=%d] %s",
1528
req->dev->qdev.parent_bus->name,
1530
scsi_command_name(req->cmd.buf[0]));
1531
for (i = 1; i < req->cmd.len; i++) {
1532
fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1534
switch (req->cmd.mode) {
1535
case SCSI_XFER_NONE:
1536
fprintf(fp, " - none\n");
1538
case SCSI_XFER_FROM_DEV:
1539
fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1541
case SCSI_XFER_TO_DEV:
1542
fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1545
fprintf(fp, " - Oops\n");
1550
void scsi_req_complete_failed(SCSIRequest *req, int host_status)
1555
assert(req->status == -1 && req->host_status == -1);
1556
assert(req->ops != &reqops_unit_attention);
1558
if (!req->bus->info->fail) {
1559
status = scsi_sense_from_host_status(req->host_status, &sense);
1560
if (status == CHECK_CONDITION) {
1561
scsi_req_build_sense(req, sense);
1563
scsi_req_complete(req, status);
1567
req->host_status = host_status;
1569
scsi_req_dequeue(req);
1570
req->bus->info->fail(req);
1573
notifier_list_notify(&req->cancel_notifiers, req);
1574
scsi_req_unref(req);
1577
void scsi_req_complete(SCSIRequest *req, int status)
1579
assert(req->status == -1 && req->host_status == -1);
1580
req->status = status;
1581
req->host_status = SCSI_HOST_OK;
1583
assert(req->sense_len <= sizeof(req->sense));
1584
if (status == GOOD) {
1588
if (req->sense_len) {
1589
memcpy(req->dev->sense, req->sense, req->sense_len);
1590
req->dev->sense_len = req->sense_len;
1591
req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1593
req->dev->sense_len = 0;
1594
req->dev->sense_is_ua = false;
1598
scsi_req_dequeue(req);
1599
req->bus->info->complete(req, req->residual);
1602
notifier_list_notify(&req->cancel_notifiers, req);
1603
scsi_req_unref(req);
1607
void scsi_req_cancel_complete(SCSIRequest *req)
1609
assert(req->io_canceled);
1610
if (req->bus->info->cancel) {
1611
req->bus->info->cancel(req);
1613
notifier_list_notify(&req->cancel_notifiers, req);
1614
scsi_req_unref(req);
1621
void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier)
1623
trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1625
notifier_list_add(&req->cancel_notifiers, notifier);
1627
if (req->io_canceled) {
1637
scsi_req_dequeue(req);
1638
req->io_canceled = true;
1640
blk_aio_cancel_async(req->aiocb);
1642
scsi_req_cancel_complete(req);
1646
void scsi_req_cancel(SCSIRequest *req)
1648
trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1649
if (!req->enqueued) {
1652
assert(!req->io_canceled);
1655
scsi_req_dequeue(req);
1656
req->io_canceled = true;
1658
blk_aio_cancel(req->aiocb);
1660
scsi_req_cancel_complete(req);
1664
static int scsi_ua_precedence(SCSISense sense)
1666
if (sense.key != UNIT_ATTENTION) {
1669
if (sense.asc == 0x29 && sense.ascq == 0x04) {
1672
} else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1675
} else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1678
} else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1686
} else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1690
return (sense.asc << 8) | sense.ascq;
1693
void scsi_bus_set_ua(SCSIBus *bus, SCSISense sense)
1696
if (sense.key != UNIT_ATTENTION) {
1704
prec1 = scsi_ua_precedence(bus->unit_attention);
1705
prec2 = scsi_ua_precedence(sense);
1706
if (prec2 < prec1) {
1707
bus->unit_attention = sense;
1711
void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1714
if (sense.key != UNIT_ATTENTION) {
1717
trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1718
sense.asc, sense.ascq);
1724
prec1 = scsi_ua_precedence(sdev->unit_attention);
1725
prec2 = scsi_ua_precedence(sense);
1726
if (prec2 < prec1) {
1727
sdev->unit_attention = sense;
1731
static void scsi_device_purge_one_req(SCSIRequest *req, void *opaque)
1733
scsi_req_cancel_async(req, NULL);
1739
void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1741
scsi_device_for_each_req_async(sdev, scsi_device_purge_one_req, NULL);
1748
blk_drain(sdev->conf.blk);
1750
scsi_device_set_ua(sdev, sense);
1753
void scsi_device_drained_begin(SCSIDevice *sdev)
1755
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, sdev->qdev.parent_bus);
1760
assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1761
assert(bus->drain_count < INT_MAX);
1767
if (bus->drain_count++ == 0) {
1768
trace_scsi_bus_drained_begin(bus, sdev);
1769
if (bus->info->drained_begin) {
1770
bus->info->drained_begin(bus);
1775
void scsi_device_drained_end(SCSIDevice *sdev)
1777
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, sdev->qdev.parent_bus);
1782
assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1783
assert(bus->drain_count > 0);
1785
if (bus->drain_count-- == 1) {
1786
trace_scsi_bus_drained_end(bus, sdev);
1787
if (bus->info->drained_end) {
1788
bus->info->drained_end(bus);
1793
static char *scsibus_get_dev_path(DeviceState *dev)
1795
SCSIDevice *d = SCSI_DEVICE(dev);
1796
DeviceState *hba = dev->parent_bus->parent;
1800
id = qdev_get_dev_path(hba);
1802
path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1804
path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1810
static char *scsibus_get_fw_dev_path(DeviceState *dev)
1812
SCSIDevice *d = SCSI_DEVICE(dev);
1813
return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1814
qdev_fw_name(dev), d->id, d->lun);
1819
static void put_scsi_req(SCSIRequest *req, void *opaque)
1821
QEMUFile *f = opaque;
1823
assert(!req->io_canceled);
1824
assert(req->status == -1 && req->host_status == -1);
1825
assert(req->enqueued);
1827
qemu_put_sbyte(f, req->retry ? 1 : 2);
1828
qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1829
qemu_put_be32s(f, &req->tag);
1830
qemu_put_be32s(f, &req->lun);
1831
if (req->bus->info->save_request) {
1832
req->bus->info->save_request(f, req);
1834
if (req->ops->save_request) {
1835
req->ops->save_request(f, req);
1839
static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
1840
const VMStateField *field, JSONWriter *vmdesc)
1844
scsi_device_for_each_req_sync(s, put_scsi_req, f);
1845
qemu_put_sbyte(f, 0);
1849
static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
1850
const VMStateField *field)
1853
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1856
while ((sbyte = qemu_get_sbyte(f)) > 0) {
1857
uint8_t buf[SCSI_CMD_BUF_SIZE];
1862
qemu_get_buffer(f, buf, sizeof(buf));
1863
qemu_get_be32s(f, &tag);
1864
qemu_get_be32s(f, &lun);
1869
req = scsi_req_new(s, tag, lun, buf, sizeof(buf), NULL);
1870
req->retry = (sbyte == 1);
1871
if (bus->info->load_request) {
1872
req->hba_private = bus->info->load_request(f, req);
1874
if (req->ops->load_request) {
1875
req->ops->load_request(f, req);
1879
scsi_req_enqueue_internal(req);
1886
scsi_req_unref(req);
1892
static const VMStateInfo vmstate_info_scsi_requests = {
1893
.name = "scsi-requests",
1894
.get = get_scsi_requests,
1895
.put = put_scsi_requests,
1898
static bool scsi_sense_state_needed(void *opaque)
1900
SCSIDevice *s = opaque;
1902
return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD;
1905
static const VMStateDescription vmstate_scsi_sense_state = {
1906
.name = "SCSIDevice/sense",
1908
.minimum_version_id = 1,
1909
.needed = scsi_sense_state_needed,
1910
.fields = (const VMStateField[]) {
1911
VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice,
1912
SCSI_SENSE_BUF_SIZE_OLD,
1913
SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD),
1914
VMSTATE_END_OF_LIST()
1918
const VMStateDescription vmstate_scsi_device = {
1919
.name = "SCSIDevice",
1921
.minimum_version_id = 1,
1922
.fields = (const VMStateField[]) {
1923
VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1924
VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1925
VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1926
VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1927
VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD),
1928
VMSTATE_UINT32(sense_len, SCSIDevice),
1932
.field_exists = NULL,
1934
.info = &vmstate_info_scsi_requests,
1935
.flags = VMS_SINGLE,
1938
VMSTATE_END_OF_LIST()
1940
.subsections = (const VMStateDescription * const []) {
1941
&vmstate_scsi_sense_state,
1946
static Property scsi_props[] = {
1947
DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
1948
DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
1949
DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
1950
DEFINE_PROP_END_OF_LIST(),
1953
static void scsi_device_class_init(ObjectClass *klass, void *data)
1955
DeviceClass *k = DEVICE_CLASS(klass);
1956
set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
1957
k->bus_type = TYPE_SCSI_BUS;
1958
k->realize = scsi_qdev_realize;
1959
k->unrealize = scsi_qdev_unrealize;
1960
device_class_set_props(k, scsi_props);
1963
static void scsi_dev_instance_init(Object *obj)
1965
DeviceState *dev = DEVICE(obj);
1966
SCSIDevice *s = SCSI_DEVICE(dev);
1968
device_add_bootindex_property(obj, &s->conf.bootindex,
1973
static const TypeInfo scsi_device_type_info = {
1974
.name = TYPE_SCSI_DEVICE,
1975
.parent = TYPE_DEVICE,
1976
.instance_size = sizeof(SCSIDevice),
1978
.class_size = sizeof(SCSIDeviceClass),
1979
.class_init = scsi_device_class_init,
1980
.instance_init = scsi_dev_instance_init,
1983
static void scsi_bus_class_init(ObjectClass *klass, void *data)
1985
BusClass *k = BUS_CLASS(klass);
1986
HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1988
k->get_dev_path = scsibus_get_dev_path;
1989
k->get_fw_dev_path = scsibus_get_fw_dev_path;
1990
k->check_address = scsi_bus_check_address;
1991
hc->unplug = qdev_simple_device_unplug_cb;
1994
static const TypeInfo scsi_bus_info = {
1995
.name = TYPE_SCSI_BUS,
1997
.instance_size = sizeof(SCSIBus),
1998
.class_init = scsi_bus_class_init,
1999
.interfaces = (InterfaceInfo[]) {
2000
{ TYPE_HOTPLUG_HANDLER },
2005
static void scsi_register_types(void)
2007
type_register_static(&scsi_bus_info);
2008
type_register_static(&scsi_device_type_info);
2011
type_init(scsi_register_types)