scsi: add free_request callback

Most device models have a simple lifecycle for the hba_private field
and they can free it when a request is completed or cancelled.
However, in some cases it may be simpler to tie the lifetime
of hba_private to that of the included SCSIRequest.  This patch
adds a free_request callback to SCSIBusInfo that lets an HBA
device model do exactly that.

Normally, device models use req->hba_private == NULL to flag requests
that have been completed already.  Device models that use free_request
will still need to track this using a flag.  This is the reason why
"converting" existing HBAs to use free_request adds complexity and
makes little sense.  It is simply an additional convenience that is
provided by the SCSI layer.  USB-attached storage will be the first
user.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
usb: add usb attached scsi emulation

$subject says all.  First cut.

It's a pure UAS (usb attached scsi) emulation, without BOT (bulk-only
transport) compatibility.  If your guest can't handle it use usb-storage
instead.

The emulation works like any other scsi hba emulation (eps, lsi, virtio,
megasas, ...).  It provides just the HBA where you can attach scsi
devices as you like using '-device'.  A single scsi target with up to
256 luns is supported.

For now only usb 2.0 transport is supported.  This will change in the
future though as I plan to use this as playground when codeing up &
testing usb 3.0 transport and streams support in the qemu usb core and
the xhci emulation.

No migration support yet.  I'm planning to add usb 3.0 support first as
this probably requires saving additional state.

Special thanks go to Paolo for bringing the qemu scsi emulation into
shape, so this can be added nicely without having to touch a single line
of scsi code.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
uhci: initialize expire_time when loading v1 vmstate

$subject says all: when loading old (v1) vmstate which doesn't contain
expire_time initialize it with a reasonable default (current time).

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
ehci: raise irq in the frame timer

With the async schedule being kicked from other places than the frame
timer (commit 0f588df8b3688b00e77aabaa32e26ece5f19bd39) it may happen
that we call ehci_commit_interrupt() more than once per frame.

Move the call from the async schedule handler to the frame timer to
restore old irq behavior, which is more correct.  Fixes regressions
with some linux kernel versions.

TODO: implement full Interrupt Threshold Control support.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
ehci: implement Interrupt Threshold Control support

Also reorganize and comment the irq functions while being at it.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
ehci: improve expire time calculation

Move down the expire time calculation down in the frame timer, to the
point where the timer is actually reloaded.  This way we'll notice any
async_stepdown changes (especially resetting to 0 due to usb activity).

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
scsi: introduce hotplug() and hot_unplug() interfaces for SCSI bus

Add two interfaces hotplug() and hot_unplug() to scsi bus info.
The scsi bus can implement these two interfaces to signal the HBA driver
of guest kernel to add/remove the scsi device in question.

Signed-off-by: Sen Wang <senwang@linux.vnet.ibm.com>
Signed-off-by: Cong Meng <mc@linux.vnet.ibm.com>
[ Fixed braces and indentation - Paolo ]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virtio-scsi: Implement hotplug support for virtio-scsi

Implement the hotplug() and hot_unplug() interfaces in virtio-scsi, by signal
the virtio_scsi.ko in guest kernel via event virtual queue.

The counterpart patch of virtio_scsi.ko will be sent soon in another thread.

Signed-off-by: Sen Wang <senwang@linux.vnet.ibm.com>
Signed-off-by: Cong Meng <mc@linux.vnet.ibm.com>
[ Add memset, fix LUN field, placate checkpatch - Paolo ]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virtio-scsi: Report missed events

When an event is reported but no buffers are present in the event vq,
we can set a flag and report a dummy event as soon as one is added.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-block: remove properties that are not relevant for passthrough

scsi-block is a passthrough device and does not allow customization
of vendor, product, removable, DPOFUA, block size or any other piece of
information.  Thus, drop DEFINE_SCSI_DISK_PROPERTIES() from the
list of qdev properties.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
cutils: add strpadcpy()

Signed-off-by: Yan Vugenfirer <yan@ravellosystems.com>
Signed-off-by: Dmitry Fleytman <dmitry.fleytman@ravellosystems.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: let the user customize vendor and product name

This patch adds two new properties vendor and product to SCSI disks.
These options let the user customize the inquiry data returned by the
disk.

Signed-off-by: Yan Vugenfirer <yan@ravellosystems.com>
Signed-off-by: Dmitry Fleytman <dmitry.fleytman@ravellosystems.com>
[ Use vendor and product property names, avoid "if" statements. - PB ]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: make discard asynchronous

By making discard asynchronous, we can reuse all the error handling
code that is used for other commands.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: move all non-DMA commands to scsi_disk_emulate_command

We want to use separate SCSIReqOps for emulated commands needing an
allocated buffer vs. those that are zerocopy when the HBA supports
S/G lists.  Ensure that all of the former are in scsi_disk_emulate_command.

Commands that do not have any parameters are more similar to emulated
commands, so also move them, even if they do I/O.

Finally, MODE SELECT and MODE SELECT(10) are broken because we do not
yet support passing parameter data _to_ emulated commands, so disable
them.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: split scsi-disk reqops

Only checks for present medium were still done in scsi_send_command
for emulated commands.  So move those to scsi_disk_emulate_command
and return different SCSIReqOps depending on the kind of command.

Checks for present medium can be done unconditionally for the
scsi_disk_dma_reqops case.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: separate read_data/write_data implementation for emulate_reqops

The previous patch only separated the send_command callback.
Use different implementations also for read_data and write_data.
The latter is still unreachable, so it aborts for now.

read_data passes the data buffer that was prepared and completes
the command.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: support emulated TO_DEV requests

This adds the implementation of write_data for the emulated
command case.  The first time through it asks for more data,
the second time it finishes the processing of the command.

MODE SELECT and MODE SELECT(10) can now be re-enabled, but they
will not do much.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: adjust offsets in MODE SENSE by 2

This will make offsets the same when implementing MODE SELECT and patching
of MODE SENSE pages.  This is because these other two cases have to deal
with both 2-byte and 4-byte headers.  Unfortunately, this means that the
offsets are now off by two compared to the descriptions in the SCSI specs,
which include the header.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: fix changeable values for MODE_PAGE_R_W_ERROR

The changeable values were not all-zeros for this mode page, fix it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi-disk: parse MODE SELECT commands and parameters

This adds the bulk of the parsing code for MODE SELECT, including
breaking out changes to different mode pages, and checking that only
changeable values are modified.

In order to report errors correctly two passes are made through the
parameters; the first only looks for errors, the second actually
applies the changes to the mode page.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>