From 63f001dc7191618fd596567a530726ac90ce2ba7 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 22 Feb 2012 14:11:39 +0100 Subject: [PATCH 023/109] scsi: allow arbitrary LUNs RH-Author: Paolo Bonzini Message-id: <1329919979-20948-23-git-send-email-pbonzini@redhat.com> Patchwork-id: 37507 O-Subject: [RHEL 6.3 qemu-kvm PATCH v2 022/102] scsi: allow arbitrary LUNs Bugzilla: 782029 RH-Acked-by: Laszlo Ersek RH-Acked-by: Orit Wasserman RH-Acked-by: Gerd Hoffmann This only requires changes in two places: in SCSIBus, we need to look for a free LUN if somebody creates a device with a pre-existing scsi-id but the default LUN (-1, meaning "search for a free spot"); in vSCSI, we need to actually parse the LUN according to the SCSI spec. For vSCSI, max_target/max_lun are set according to the logical unit addressing format in SAM. [ vSCSI is not in RHEL, so those parts of the commit message make little sense here. ] Signed-off-by: Paolo Bonzini Signed-off-by: Kevin Wolf (cherry picked from 7e0380b9bbf7c40361e06e6e0d8675a55bd0dea0) Conflicts: hw/esp.c (not merged because it's not on x86) hw/lsi53c895a.c (not merged because we disable it) hw/spapr_vscsi.c (absent in RHEL6) --- hw/scsi-bus.c | 48 ++++++++++++++++++++++++++++++++---------------- hw/scsi.h | 3 ++- hw/usb-msd.c | 3 ++- 3 files changed, 36 insertions(+), 18 deletions(-) Signed-off-by: Michal Novotny --- hw/scsi-bus.c | 48 ++++++++++++++++++++++++++++++++---------------- hw/scsi.h | 3 ++- hw/usb-msd.c | 3 ++- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/hw/scsi-bus.c b/hw/scsi-bus.c index ff5c8f3..76248ce 100644 --- a/hw/scsi-bus.c +++ b/hw/scsi-bus.c @@ -17,7 +17,7 @@ static struct BusInfo scsi_bus_info = { .get_fw_dev_path = scsibus_get_fw_dev_path, .props = (Property[]) { DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), - DEFINE_PROP_UINT32("lun", SCSIDevice, lun, 0), + DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), DEFINE_PROP_END_OF_LIST(), }, }; @@ -37,26 +37,42 @@ 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); - SCSIDevice *olddev; + SCSIDevice *d; int rc = -1; - if (dev->id == -1) { - int id; - for (id = 0; id < bus->info->ndev; id++) { - if (!scsi_device_find(bus, id, 0)) { - dev->id = id; - break; - } - } - } - if (dev->id >= bus->info->ndev) { + if (dev->id != -1 && dev->id > bus->info->max_target) { error_report("bad scsi device id: %d", dev->id); goto err; } - olddev = scsi_device_find(bus, dev->id, dev->lun); - if (olddev && dev->lun == olddev->lun) { - qdev_free(&olddev->qdev); + if (dev->id == -1) { + int id = -1; + if (dev->lun == -1) { + dev->lun = 0; + } + do { + d = scsi_device_find(bus, ++id, dev->lun); + } while (d && d->lun == dev->lun && id <= bus->info->max_target); + if (id > bus->info->max_target) { + error_report("no free target"); + goto err; + } + dev->id = id; + } else if (dev->lun == -1) { + int lun = -1; + do { + d = scsi_device_find(bus, dev->id, ++lun); + } while (d && d->lun == lun && lun < bus->info->max_lun); + if (lun > bus->info->max_lun) { + error_report("no free lun"); + goto err; + } + dev->lun = lun; + } else { + d = scsi_device_find(bus, dev->id, dev->lun); + if (dev->lun == d->lun && dev != d) { + qdev_free(&d->qdev); + } } dev->info = info; @@ -115,7 +131,7 @@ int scsi_bus_legacy_handle_cmdline(SCSIBus *bus) int res = 0, unit; loc_push_none(&loc); - for (unit = 0; unit < bus->info->ndev; unit++) { + for (unit = 0; unit < bus->info->max_target; unit++) { dinfo = drive_get(IF_SCSI, bus->busnr, unit); if (dinfo == NULL) { continue; diff --git a/hw/scsi.h b/hw/scsi.h index 39da0d1..9b550b8 100644 --- a/hw/scsi.h +++ b/hw/scsi.h @@ -97,7 +97,8 @@ struct SCSIDeviceInfo { }; struct SCSIBusInfo { - int tcq, ndev; + int tcq; + int max_target, max_lun; void (*transfer_data)(SCSIRequest *req, uint32_t arg); void (*complete)(SCSIRequest *req, uint32_t arg); void (*cancel)(SCSIRequest *req); diff --git a/hw/usb-msd.c b/hw/usb-msd.c index f098d5e..cd8ba60 100644 --- a/hw/usb-msd.c +++ b/hw/usb-msd.c @@ -514,7 +514,8 @@ static void usb_msd_password_cb(void *opaque, int err) static const struct SCSIBusInfo usb_msd_scsi_info = { .tcq = false, - .ndev = 1, + .max_target = 0, + .max_lun = 0, .transfer_data = usb_msd_transfer_data, .complete = usb_msd_command_complete, -- 1.7.7.6