usb-msc: support commands without payload

This lets the usb-msc driver send TEST UNIT READY commands.

Modified to avoid divide by zero by Kevin O'Connor <kevin@koconnor.net>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
cdrom: use TEST UNIT READY to detect ready medium

The READ CAPACITY output is not used except for some debugging messages.
In the future, we will use this code for USB sticks too, but those
already send READ CAPACITY.  To avoid code duplication, switch to TEST
UNIT READY for this task.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-uhci: reorganize wait_qh into wait_pipe

Four changes:

1) Add explicit GET_FLATPTR/SET_FLATPTR.

2) Pass the whole pipe to wait_qh so that we can get the iobase from there.

3) Clean up the pipe upon timeout, since that is the only sensible
thing to do: tds are on the stack, and leaving pointers to them in
the pipe is not a good idea.

4) Add a variable timeout argument, since bulk transfers might take more
than 500 ms.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-uhci: fix race against host controller

While processing a frame, the host controller will write to the queue
head's element link field.  The following sequence could then happen
when two consecutive sends occur to the same pipe.

    controller                      SeaBIOS
    ---------------------------------------------------------------------
                                    td->link = UHCI_PTR_TERM;
                                    td->ctrl |= TD_CTRL_ACTIVE;
    read TD from memory
                                    wait_td(td);
    td->ctrl &= ~TD_CTRL_ACTIVE;
    write back td->ctrl
                                    exit usb_send_bulk
                                    restart usb_send_bulk
                                    pipe->qh.element = &tds;
    pipe->qh.element = td->link;    ... go on and set up the first td ...
    write back pipe->qh.element
                                    td->ctrl |= TD_CTRL_ACTIVE;

Once the host controller has written UHCI_PTR_TERM to the element link,
subsequent tds would never be processed.  This is surprisingly frequent
when the two consecutive sends are in the OUT direction (and just as
surprisingly, it seems like it never happens in the IN direction).

To fix this, at the end of the processing do not wait for each single
TD to become inactive, but for the host controller to invalidate the
element link (which implies it's done with all TDs).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: add usb_msc_send

This makes it a bit nicer to later introduce writes.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: support WRITE commands

Writes only require building the CDB and some care with the direction
in the USB packet.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: move READ CAPACITY to usb_msc_init, fix off-by-one

Only leave the bootprio code in setup_drive_hd, like in setup_drive_cdrom.
This is a preparatory step; later, the SCSI code in usb_msc_init will
become entirely generic.

Also, the returned number of sectors is off by one.  This will become
more important when CHS translation is added later.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: pass drive to setup_drive_*

The two functions do not need anymore a disk_op_s.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: rename INQUIRY types

They are generic SCSI constants, rename them as such.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: go through TEST UNIT READY for hard disks.

Add the wait loop that CDs are already using to usb-msc in the HD
case, to cope with a NOT READY or UNIT ATTENTION condition.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: move common scsi code to blockcmd.c

Finally move the INQUIRY/TEST UNIT READY/READ CAPACITY sequence to
generic code, so that virtio-scsi will be able to use it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-msc: move cdb dispatch to block.c

virtio-scsi's low-level dispatch code is exactly the same as USB's,
since in the end both are actually SCSI HBAs.  Move it to common code.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scsi: get physical chs geometry from mode page 0x04

The mode page is marked as obsolete, but QEMU can provide the information.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
always specify virtio-blk rather than virtio

Avoid ambiguity when virtio-scsi will be introduced.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virtio-pci: introduce vp_init_simple

Put together the common parts of all virtio device initialization.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virtio-pci: allocate vq in vp_find_vq

Another common bit that can be made generic.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
usb-uhci: Be sure to wrap pipe->iobase in GET_FLATPTR().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
usb-ehci: Fix races with controller on updates to QH.

The EHCI controller writes to the TD after writing to the QH, so the
driver must wait for all the TDs to be complete before considering the
transfer completed.  (The previous implementation was particularly bad
as it only checked that the last TD was in progress before considering
the transfer complete.)

Also, avoid writing to the qh.token field when starting a transfer to
eliminate a potential race.  Place the qh.token in an available state
by default - that way only the qtd_next field needs to be updated to
start the transfer.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
usb: fix boot paths

The fw paths for USB devices that SeaBIOS computes are off-by-one,
because QEMU builds those paths with a numbering that starts from one
(see usb_fill_port and usb_hub_initfn in QEMU).  Fix that so that
the numbering agrees.
virtio-pci: include pci.h and pci_regs.h

Include pci.h and pci_regs.h.

Signed-off-by: Sebastian Herbszt <herbszt@gmx.de>