Re: [pciutils patch] add virtio vendor capability support

2015-01-21 Thread Martin Mares
Hello!

 virtio uses vendor-specific capabilities to specify the location of
 the virtio register ranges.  The specification can be found here:
 
 http://docs.oasis-open.org/virtio/virtio/v1.0/cs01/virtio-v1.0-cs01.html#x1-690004
 
 This patch adds support for decoding these capabilities to lspci.

I like the patch, except for a couple of details:

(1) Please follow the coding style of the rest of pciutils.

(2) You assume that PCI_CAP_ID_VNDR of all Redhat devices contains virtio,
but the comment nearby refers to a range of device IDs only.

(3) Moving code related to vendor-defined caps to a separate file sounds
good, but I think we should push the boundary a bit further: let the
main switch in ls-caps.c call a function from ls-caps-vendor.c as soon
as it finds PCI_CAP_ID_VENDOR, leaving all decisions based on
vendor/device ID to this function.

Could you please fix these and resubmit?

Have a nice fortnight
-- 
Martin `MJ' Mares  m...@ucw.cz   http://mj.ucw.cz/
Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth
VI has two modes: the one in which it beeps and the one in which it doesn't.
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [pciutils patch] add virtio vendor capability support

2015-01-21 Thread Martin Mares
Hi!

  (1) Please follow the coding style of the rest of pciutils.
 
 I've tried.  What specifically doesn't match?

Positions of braces (e.g., after switch). But if you fix the other things,
I can re-indent the source myself.

Martin
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[pciutils patch v2] add virtio vendor capability support

2015-01-21 Thread Gerd Hoffmann
virtio uses vendor-specific capabilities to specify the location of
the virtio register ranges.  The specification can be found here:

http://docs.oasis-open.org/virtio/virtio/v1.0/cs01/virtio-v1.0-cs01.html#x1-690004

This patch adds support for decoding these capabilities to lspci.

Signed-off-by: Gerd Hoffmann kra...@redhat.com
---
 Makefile |  2 +-
 ls-caps-vendor.c | 76 
 ls-caps.c|  2 +-
 lspci.h  |  4 +++
 4 files changed, 82 insertions(+), 2 deletions(-)
 create mode 100644 ls-caps-vendor.c

diff --git a/Makefile b/Makefile
index 8d49afa..39a07d1 100644
--- a/Makefile
+++ b/Makefile
@@ -69,7 +69,7 @@ force:
 lib/config.h lib/config.mk:
cd lib  ./configure
 
-lspci: lspci.o ls-vpd.o ls-caps.o ls-ecaps.o ls-kernel.o ls-tree.o ls-map.o 
common.o lib/$(PCILIB)
+lspci: lspci.o ls-vpd.o ls-caps.o ls-caps-vendor.o ls-ecaps.o ls-kernel.o 
ls-tree.o ls-map.o common.o lib/$(PCILIB)
 setpci: setpci.o common.o lib/$(PCILIB)
 
 LSPCIINC=lspci.h pciutils.h $(PCIINC)
diff --git a/ls-caps-vendor.c b/ls-caps-vendor.c
new file mode 100644
index 000..4cdfe22
--- /dev/null
+++ b/ls-caps-vendor.c
@@ -0,0 +1,76 @@
+/*
+ * The PCI Utilities -- Show Vendor-specific Capabilities
+ *
+ * Copyright (c) 2014 Gerd Hoffmann kra...@redhat.com
+ *
+ * Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#include stdio.h
+#include string.h
+
+#include lspci.h
+
+static void
+show_vendor_caps_virtio(struct device *d, int where, int cap)
+{
+  int length = BITS(cap, 0, 8);
+  int type = BITS(cap, 8, 8);
+  char *tname;
+
+  if (length  16)
+return;
+  if (!config_fetch(d, where, length))
+return;
+
+  switch (type)
+{
+case 1:
+  tname = CommonCfg;
+  break;
+case 2:
+  tname = Notify;
+  break;
+case 3:
+  tname = ISR;
+  break;
+case 4:
+  tname = DeviceCfg;
+  break;
+default:
+  tname = unknown;
+  break;
+}
+
+  printf(VirtIO: %s\n, tname);
+
+  if (verbose  2)
+return;
+
+  printf(\t\tBAR=%d offset=%08x size=%08x\n,
+get_conf_byte(d, where +  4),
+get_conf_long(d, where +  8),
+get_conf_long(d, where + 12));
+
+  if (type != 2 || length  20)
+return;
+
+  printf(\t\tmultiplier=%08x\n,
+get_conf_long(d, where+16));
+}
+
+void
+show_vendor_caps(struct device *d, int where, int cap)
+{
+  switch (get_conf_word(d, PCI_VENDOR_ID))
+{
+case 0x1af4: /* Red Hat */
+  if (get_conf_word(d, PCI_DEVICE_ID) = 0x1000 
+ get_conf_word(d, PCI_DEVICE_ID) = 0x107f)
+   show_vendor_caps_virtio(d, where, cap);
+  break;
+default:
+  printf(Vendor Specific Information: Len=%02x ?\n, BITS(cap, 0, 8));
+  break;
+}
+}
diff --git a/ls-caps.c b/ls-caps.c
index 7de55ef..c145ed6 100644
--- a/ls-caps.c
+++ b/ls-caps.c
@@ -1315,7 +1315,7 @@ show_caps(struct device *d, int where)
  cap_ht(d, where, cap);
  break;
case PCI_CAP_ID_VNDR:
- printf(Vendor Specific Information: Len=%02x ?\n, BITS(cap, 
0, 8));
+ show_vendor_caps(d, where, cap);
  break;
case PCI_CAP_ID_DBG:
  cap_debug_port(cap);
diff --git a/lspci.h b/lspci.h
index 86429b2..a3fc9d0 100644
--- a/lspci.h
+++ b/lspci.h
@@ -70,6 +70,10 @@ void show_caps(struct device *d, int where);
 
 void show_ext_caps(struct device *d);
 
+/* ls-caps-vendor.c */
+
+void show_vendor_caps(struct device *d, int where, int cap);
+
 /* ls-kernel.c */
 
 void show_kernel_machine(struct device *d UNUSED);
-- 
1.8.3.1

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: virtio for 3.20

2015-01-21 Thread Michael S. Tsirkin
On Mon, Jan 19, 2015 at 12:56:28PM +1030, Rusty Russell wrote:
 Michael S. Tsirkin m...@redhat.com writes:
  Hi Rusty, all
 
  I parked outstanding virtio patches here:
  git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git vhost-next
  this way they are merged in linux-next as well.
 
  Rusty if you'll be doing 3.20 window, would you mind
  checking that tree to verify nothing's lost?
 
 Hi Michael,
 
 Thanks, now I've returned from linux.conf.au, I'm starting to
 organize the 3.20 queue.  I've merged this branch into my virtio-next
 branch.
 
 Cheers,
 Rusty.

Thanks, good to know.
I tweaked some patches since due to test failure reports by Gerd.
Updated patches are tagged post-squash, if you want to see what
changed, see fixup patches in the series tagged pre-squash.

Or you can look at my tree:
git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git virtio-next
is pre-squash.
git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git vhost-next
is post-squash.

-- 
MST
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH RFC v6 16/20] virtio-net: support longer header

2015-01-21 Thread Cornelia Huck
On Tue, 20 Jan 2015 13:18:14 +
Stefan Hajnoczi stefa...@gmail.com wrote:

 On Thu, Dec 11, 2014 at 02:25:18PM +0100, Cornelia Huck wrote:
  diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
  index ebbea60..7ee2bd6 100644
  --- a/hw/net/virtio-net.c
  +++ b/hw/net/virtio-net.c
  @@ -373,15 +373,21 @@ static int peer_has_ufo(VirtIONet *n)
   return n-has_ufo;
   }
   
  -static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
  +static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
  +   int version_1)
 
 Please use bool, it makes it 100% clear what the meaning of version_1
 is.
 
 s/int/bool/

OK, will change.

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH] virtio-mmio: Update the device to OASIS spec version

2015-01-21 Thread Rusty Russell
Pawel Moll pawel.m...@arm.com writes:
 This patch add a support for second version of the virtio-mmio device,
 which follows OASIS Virtual I/O Device (VIRTIO) Version 1.0
 specification.

Nice job, that turned out quite neat!

Applied,
Rusty.
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH v2] virtio-mmio: Update the device to OASIS spec version

2015-01-21 Thread Rusty Russell
Michael S. Tsirkin m...@redhat.com writes:
 On Tue, Jan 20, 2015 at 06:12:11PM +, Pawel Moll wrote:
 This patch add a support for second version of the virtio-mmio device,
 which follows OASIS Virtual I/O Device (VIRTIO) Version 1.0
 specification.

OK, applied this instead :)

I'll leave it in my pending queue for a few days in case there's a v3.

Thanks,
Rusty.
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: virtio for 3.20

2015-01-21 Thread Rusty Russell
Michael S. Tsirkin m...@redhat.com writes:
 On Mon, Jan 19, 2015 at 12:56:28PM +1030, Rusty Russell wrote:
 Michael S. Tsirkin m...@redhat.com writes:
  Hi Rusty, all
 
  I parked outstanding virtio patches here:
  git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git vhost-next
  this way they are merged in linux-next as well.
 
  Rusty if you'll be doing 3.20 window, would you mind
  checking that tree to verify nothing's lost?
 
 Hi Michael,
 
 Thanks, now I've returned from linux.conf.au, I'm starting to
 organize the 3.20 queue.  I've merged this branch into my virtio-next
 branch.
 
 Cheers,
 Rusty.

 Thanks, good to know.
 I tweaked some patches since due to test failure reports by Gerd.
 Updated patches are tagged post-squash, if you want to see what
 changed, see fixup patches in the series tagged pre-squash.

 Or you can look at my tree:
 git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git 
 virtio-next
 is pre-squash.
 git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git vhost-next
 is post-squash.

OK, I merged your vhost-next with my virtio-next, and the merge was
empty.

So we're up-to-date.  Please send patches against my virtio-next from
now on.

Thanks,
Rusty.
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [Qemu-devel] [PATCH RFC v6 12/20] virtio: disallow late feature changes for virtio-1

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:14PM +0100, Cornelia Huck wrote:
 For virtio-1 devices, the driver must not attempt to set feature bits
 after it set FEATURES_OK in the device status. Simply reject it in
 that case.
 
 Signed-off-by: Cornelia Huck cornelia.h...@de.ibm.com

Reviewed-by: David Gibson da...@gibson.dropbear.id.au

-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgpKFozvrOUHe.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Re: [Qemu-devel] [PATCH RFC v6 08/20] dataplane: allow virtio-1 devices

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:10PM +0100, Cornelia Huck wrote:
 Handle endianness conversion for virtio-1 virtqueues correctly.
 
 Note that dataplane now needs to be built per-target.
 
 Signed-off-by: Cornelia Huck cornelia.h...@de.ibm.com

Reviewed-by: David Gibson da...@gibson.dropbear.id.au
-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgpqGpIy5jPt3.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Re: [Qemu-devel] [PATCH RFC v6 05/20] virtio: support more feature bits

2015-01-21 Thread David Gibson
On Fri, Dec 12, 2014 at 11:17:47AM +0100, Cornelia Huck wrote:
 On Fri, 12 Dec 2014 11:06:40 +0100
 Thomas Huth th...@linux.vnet.ibm.com wrote:
 
  On Thu, 11 Dec 2014 14:25:07 +0100
  Cornelia Huck cornelia.h...@de.ibm.com wrote:
[snip]
vdev-queue_sel = 0;
vdev-status = 0;
vdev-isr = 0;
   @@ -924,7 +925,8 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f)
qemu_put_8s(f, vdev-status);
qemu_put_8s(f, vdev-isr);
qemu_put_be16s(f, vdev-queue_sel);
   -qemu_put_be32s(f, vdev-guest_features);
   +/* XXX features = 32 */
   +qemu_put_be32s(f, (uint32_t *)vdev-guest_features);
  
  Casting a uint64_t* to a uint32_t* here sounds very wrong - this likely
  only works on little endian sytems, but certainly not on big-endian
  systems.
  If you do not want to extend this for 64-bit right from the beginning,
  I think you've got to use a temporary 32-bit variable to get this right.
 
 Hm... always store the old 32 bits here, then store the new 32 bits
 later? Should be able to get that compatible.

I think Thomas' point is that since vdev-guest_features has changed
to a uint64_t, the old bits are now in the second 32-bit half on a
BE system.

Or maybe I'm misunderstanding your reply.

-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgpsZfQICJyy1.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Re: [Qemu-devel] [PATCH RFC v6 06/20] virtio: endianness checks for virtio 1.0 devices

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:08PM +0100, Cornelia Huck wrote:
 Add code that checks for the VERSION_1 feature bit in order to make
 decisions about the device's endianness. This allows us to support
 transitional devices.
 
 Signed-off-by: Cornelia Huck cornelia.h...@de.ibm.com
 ---
  hw/virtio/virtio.c|6 +-
  include/hw/virtio/virtio-access.h |4 
  include/hw/virtio/virtio.h|8 ++--
  3 files changed, 15 insertions(+), 3 deletions(-)
 
 diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
 index 7f74ae5..8f69ffa 100644
 --- a/hw/virtio/virtio.c
 +++ b/hw/virtio/virtio.c
 @@ -881,7 +881,11 @@ static bool virtio_device_endian_needed(void *opaque)
  VirtIODevice *vdev = opaque;
  
  assert(vdev-device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
 -return vdev-device_endian != virtio_default_endian();
 +if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
 +return vdev-device_endian != virtio_default_endian();
 +}
 +/* Devices conforming to VIRTIO 1.0 or later are always LE. */
 +return vdev-device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;

This doesn't seem quite right.  Since virtio 1.0 is always LE, this
should just assert that device_endian == LE and return false,
right?

  }
  
  static const VMStateDescription vmstate_virtio_device_endian = {
 diff --git a/include/hw/virtio/virtio-access.h 
 b/include/hw/virtio/virtio-access.h
 index 46456fd..ee28c21 100644
 --- a/include/hw/virtio/virtio-access.h
 +++ b/include/hw/virtio/virtio-access.h
 @@ -19,6 +19,10 @@
  
  static inline bool virtio_access_is_big_endian(VirtIODevice *vdev)
  {
 +if (virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
 +/* Devices conforming to VIRTIO 1.0 or later are always LE. */
 +return false;
 +}
  #if defined(TARGET_IS_BIENDIAN)
  return virtio_is_big_endian(vdev);
  #elif defined(TARGET_WORDS_BIGENDIAN)
 diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
 index 08141c7..68c40db 100644
 --- a/include/hw/virtio/virtio.h
 +++ b/include/hw/virtio/virtio.h
 @@ -297,7 +297,11 @@ static inline bool virtio_has_feature(VirtIODevice 
 *vdev, unsigned int fbit)
  
  static inline bool virtio_is_big_endian(VirtIODevice *vdev)
  {
 -assert(vdev-device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
 -return vdev-device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
 +if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
 +assert(vdev-device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
 +return vdev-device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
 +}
 +/* Devices conforming to VIRTIO 1.0 or later are always LE. */
 +return false;
  }
  #endif

AFAICT, the only real difference between virtio_is_big_endian() and
virtio_access_is_big_endian() is that the latter will become
compile-time constant on targets that don't do bi-endian.

With virtio 1.0 support, that's no longer true, so those two macros
should just be merged, I think.

-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgpj_tNTjUrSz.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Re: [Qemu-devel] [PATCH RFC v6 07/20] virtio: allow virtio-1 queue layout

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:09PM +0100, Cornelia Huck wrote:
 For virtio-1 devices, we allow a more complex queue layout that doesn't
 require descriptor table and rings on a physically-contigous memory area:
 add virtio_queue_set_rings() to allow transports to set this up.
 
 Signed-off-by: Cornelia Huck cornelia.h...@de.ibm.com
 ---
  hw/virtio/virtio-mmio.c|3 +++
  hw/virtio/virtio.c |   53 
 
  include/hw/virtio/virtio.h |3 +++
  3 files changed, 40 insertions(+), 19 deletions(-)
 
 diff --git a/hw/virtio/virtio-mmio.c b/hw/virtio/virtio-mmio.c
 index 43b7e02..0c9b63b 100644
 --- a/hw/virtio/virtio-mmio.c
 +++ b/hw/virtio/virtio-mmio.c
 @@ -244,8 +244,11 @@ static void virtio_mmio_write(void *opaque, hwaddr 
 offset, uint64_t value,
  case VIRTIO_MMIO_QUEUENUM:
  DPRINTF(mmio_queue write %d max %d\n, (int)value, 
 VIRTQUEUE_MAX_SIZE);
  virtio_queue_set_num(vdev, vdev-queue_sel, value);
 +/* Note: only call this function for legacy devices */

It's not clear to me if this is an assertion that this *does* only
call the function for legacy devices or a fixme, that it *should* only
call the function for legacy devices.

 +virtio_queue_update_rings(vdev, vdev-queue_sel);
  break;
  case VIRTIO_MMIO_QUEUEALIGN:
 +/* Note: this is only valid for legacy devices */
  virtio_queue_set_align(vdev, vdev-queue_sel, value);
  break;
  case VIRTIO_MMIO_QUEUEPFN:
 diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
 index 8f69ffa..57190ba 100644
 --- a/hw/virtio/virtio.c
 +++ b/hw/virtio/virtio.c
 @@ -69,7 +69,6 @@ typedef struct VRing
  struct VirtQueue
  {
  VRing vring;
 -hwaddr pa;
  uint16_t last_avail_idx;
  /* Last used index value we have signalled on */
  uint16_t signalled_used;
 @@ -92,15 +91,18 @@ struct VirtQueue
  };
  
  /* virt queue functions */
 -static void virtqueue_init(VirtQueue *vq)
 +void virtio_queue_update_rings(VirtIODevice *vdev, int n)

Perhaps something in the name to emphasise that this is only for v1.0
devices?

  {
 -hwaddr pa = vq-pa;
 +VRing *vring = vdev-vq[n].vring;
  
 -vq-vring.desc = pa;
 -vq-vring.avail = pa + vq-vring.num * sizeof(VRingDesc);
 -vq-vring.used = vring_align(vq-vring.avail +
 - offsetof(VRingAvail, ring[vq-vring.num]),
 - vq-vring.align);
 +if (!vring-desc) {
 +/* not yet setup - nothing to do */
 +return;
 +}
 +vring-avail = vring-desc + vring-num * sizeof(VRingDesc);
 +vring-used = vring_align(vring-avail +
 +  offsetof(VRingAvail, ring[vring-num]),
 +  vring-align);

Would it make sense to implement this in terms of
virtio_queue_set_rings()?

  }
  
  static inline uint64_t vring_desc_addr(VirtIODevice *vdev, hwaddr desc_pa,
 @@ -605,7 +607,6 @@ void virtio_reset(void *opaque)
  vdev-vq[i].vring.avail = 0;
  vdev-vq[i].vring.used = 0;
  vdev-vq[i].last_avail_idx = 0;
 -vdev-vq[i].pa = 0;
  vdev-vq[i].vector = VIRTIO_NO_VECTOR;
  vdev-vq[i].signalled_used = 0;
  vdev-vq[i].signalled_used_valid = false;
 @@ -708,13 +709,21 @@ void virtio_config_writel(VirtIODevice *vdev, uint32_t 
 addr, uint32_t data)
  
  void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
  {
 -vdev-vq[n].pa = addr;
 -virtqueue_init(vdev-vq[n]);
 +vdev-vq[n].vring.desc = addr;
 +virtio_queue_update_rings(vdev, n);
  }
  
  hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
  {
 -return vdev-vq[n].pa;
 +return vdev-vq[n].vring.desc;
 +}
 +
 +void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
 +hwaddr avail, hwaddr used)
 +{
 +vdev-vq[n].vring.desc = desc;
 +vdev-vq[n].vring.avail = avail;
 +vdev-vq[n].vring.used = used;
  }
  
  void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 @@ -728,7 +737,6 @@ void virtio_queue_set_num(VirtIODevice *vdev, int n, int 
 num)
  return;
  }
  vdev-vq[n].vring.num = num;
 -virtqueue_init(vdev-vq[n]);
  }
  
  int virtio_queue_get_num(VirtIODevice *vdev, int n)
 @@ -748,6 +756,11 @@ void virtio_queue_set_align(VirtIODevice *vdev, int n, 
 int align)
  BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  
 +/* virtio-1 compliant devices cannot change the aligment */
 +if (virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
 +error_report(tried to modify queue alignment for virtio-1 device);
 +return;
 +}
  /* Check that the transport told us it was going to do this
   * (so a buggy transport will immediately assert rather than
   * silently failing to migrate this state)
 @@ -755,7 +768,7 @@ void virtio_queue_set_align(VirtIODevice *vdev, int n, 
 int 

Re: [Qemu-devel] [PATCH RFC v6 02/20] virtio: cull virtio_bus_set_vdev_features

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:04PM +0100, Cornelia Huck wrote:
 The only user of this function was virtio-ccw, and it should use
 virtio_set_features() like everybody else: We need to make sure
 that bad features are masked out properly, which this function did
 not do.
 
 Reviewed-by: Thomas Huth th...@linux.vnet.ibm.com
 Signed-off-by: Cornelia Huck cornelia.h...@de.ibm.com

Reviewed-by: David Gibson da...@gibson.dropbear.id.au

-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgpNij5Pd6FBD.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Re: [Qemu-devel] [PATCH RFC v6 05/20] virtio: support more feature bits

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:07PM +0100, Cornelia Huck wrote:
 With virtio-1, we support more than 32 feature bits. Let's extend both
 host and guest features to 64, which should suffice for a while.
 
 vhost and migration have been ignored for now.

[snip]

 diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
 index f6c0379..08141c7 100644
 --- a/include/hw/virtio/virtio.h
 +++ b/include/hw/virtio/virtio.h
 @@ -55,6 +55,12 @@
  /* A guest should never accept this.  It implies negotiation is broken. */
  #define VIRTIO_F_BAD_FEATURE 30
  
 +/* v1.0 compliant. */
 +#define VIRTIO_F_VERSION_1  32

This is already in the kernel header, isn't it?

-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgpLtI7pmdjsG.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Re: [Qemu-devel] [PATCH RFC v6 03/20] virtio: feature bit manipulation helpers

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:05PM +0100, Cornelia Huck wrote:
 Add virtio_{add,clear}_feature helper functions for manipulating a
 feature bits variable. This has some benefits over open coding:
 - add check that the bit is in a sane range
 - make it obvious at a glance what is going on
 - have a central point to change when we want to extend feature bits
 
 Convert existing code manipulating features to use the new helpers.
 
 Signed-off-by: Cornelia Huck cornelia.h...@de.ibm.com

Reviewed-by: David Gibson da...@gibson.dropbear.id.au

-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgpvtqOvIfXaE.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Re: [Qemu-devel] [PATCH RFC v6 04/20] virtio: add feature checking helpers

2015-01-21 Thread David Gibson
On Thu, Dec 11, 2014 at 02:25:06PM +0100, Cornelia Huck wrote:
 Add a helper function for checking whether a bit is set in the guest
 features for a vdev as well as one that works on a feature bit set.
 
 Convert code that open-coded this: It cleans up the code and makes it
 easier to extend the guest feature bits.
 
 Signed-off-by: Cornelia Huck cornelia.h...@de.ibm.com

Reviewed-by: David Gibson da...@gibson.dropbear.id.au

-- 
David Gibson| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


pgponHlg_GEdG.pgp
Description: PGP signature
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

[pciutils patch] add virtio vendor capability support

2015-01-21 Thread Gerd Hoffmann
virtio uses vendor-specific capabilities to specify the location of
the virtio register ranges.  The specification can be found here:

http://docs.oasis-open.org/virtio/virtio/v1.0/cs01/virtio-v1.0-cs01.html#x1-690004

This patch adds support for decoding these capabilities to lspci.

Signed-off-by: Gerd Hoffmann kra...@redhat.com
---
 Makefile |  2 +-
 ls-caps-vendor.c | 59 
 ls-caps.c|  9 -
 lspci.h  |  4 
 4 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 ls-caps-vendor.c

diff --git a/Makefile b/Makefile
index 8d49afa..39a07d1 100644
--- a/Makefile
+++ b/Makefile
@@ -69,7 +69,7 @@ force:
 lib/config.h lib/config.mk:
cd lib  ./configure
 
-lspci: lspci.o ls-vpd.o ls-caps.o ls-ecaps.o ls-kernel.o ls-tree.o ls-map.o 
common.o lib/$(PCILIB)
+lspci: lspci.o ls-vpd.o ls-caps.o ls-caps-vendor.o ls-ecaps.o ls-kernel.o 
ls-tree.o ls-map.o common.o lib/$(PCILIB)
 setpci: setpci.o common.o lib/$(PCILIB)
 
 LSPCIINC=lspci.h pciutils.h $(PCIINC)
diff --git a/ls-caps-vendor.c b/ls-caps-vendor.c
new file mode 100644
index 000..edcc44a
--- /dev/null
+++ b/ls-caps-vendor.c
@@ -0,0 +1,59 @@
+/*
+ * The PCI Utilities -- Show Vendor-specific Capabilities
+ *
+ * Copyright (c) 2014 Gerd Hoffmann kra...@redhat.com
+ *
+ * Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#include stdio.h
+#include string.h
+
+#include lspci.h
+
+void
+show_vendor_caps_virtio(struct device *d, int where, int cap)
+{
+  int length = BITS(cap, 0, 8);
+  int type = BITS(cap, 8, 8);
+  char *tname;
+
+  if (length  16)
+return;
+  if (!config_fetch(d, where, length))
+return;
+
+  switch (type) {
+  case 1:
+tname = CommonCfg;
+break;
+  case 2:
+tname = Notify;
+break;
+  case 3:
+tname = ISR;
+break;
+  case 4:
+tname = DeviceCfg;
+break;
+  default:
+tname = unknown;
+break;
+  }
+
+  printf(VirtIO: %s\n, tname);
+
+  if (verbose  2)
+return;
+
+  printf(\t\tBAR=%d offset=%08x size=%08x\n,
+get_conf_byte(d, where +  4),
+get_conf_long(d, where +  8),
+get_conf_long(d, where + 12));
+
+  if (type != 2 || length  20)
+return;
+
+  printf(\t\tmultiplier=%08x\n,
+get_conf_long(d, where+16));
+}
diff --git a/ls-caps.c b/ls-caps.c
index 7de55ef..54a64a7 100644
--- a/ls-caps.c
+++ b/ls-caps.c
@@ -1315,7 +1315,14 @@ show_caps(struct device *d, int where)
  cap_ht(d, where, cap);
  break;
case PCI_CAP_ID_VNDR:
- printf(Vendor Specific Information: Len=%02x ?\n, BITS(cap, 
0, 8));
+ switch (get_conf_word(d, PCI_VENDOR_ID)) {
+ case 0x1af4: /* Red Hat, devices 0x1000 - 0x107f are virtio */
+   show_vendor_caps_virtio(d, where, cap);
+   break;
+ default:
+   printf(Vendor Specific Information: Len=%02x ?\n, BITS(cap, 
0, 8));
+   break;
+ }
  break;
case PCI_CAP_ID_DBG:
  cap_debug_port(cap);
diff --git a/lspci.h b/lspci.h
index 86429b2..5ca9899 100644
--- a/lspci.h
+++ b/lspci.h
@@ -70,6 +70,10 @@ void show_caps(struct device *d, int where);
 
 void show_ext_caps(struct device *d);
 
+/* ls-caps-vendor.c */
+
+void show_vendor_caps_virtio(struct device *d, int where, int cap);
+
 /* ls-kernel.c */
 
 void show_kernel_machine(struct device *d UNUSED);
-- 
1.8.3.1

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH v3 00/16] virtio-pci: towards virtio 1.0 guest support

2015-01-21 Thread Gerd Hoffmann
  Hi,

  While hacking it up I've noticed spec doesn't match reality.  The
  Virtio Structure PCI Capabilities section here ...
  
  http://docs.oasis-open.org/virtio/virtio/v1.0/cs01/virtio-v1.0-cs01.html#x1-690004
  
  ... doesn't match what qemu is doing.  Huh?
 
 Thanks a lot for the report, and the tool!

pciutils patch needs an update too (because I've made it decode the
broken qemu capabilities).  Will come as separate mail with proper
commit message in a moment.

 I sent patches to fix this all up.
 You can try my qemu and linux virtio-net branches now,
 they should be spec compliant.

Yep, much better.  caps match spec, the system boots fine from
virtio-scsi, and the nfs-mounts (using virtio-net) are there too.

cheers,
  Gerd


___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH v3 00/16] virtio-pci: towards virtio 1.0 guest support

2015-01-21 Thread Michael S. Tsirkin
On Wed, Jan 21, 2015 at 12:31:08PM +0100, Gerd Hoffmann wrote:
   Hi,
 
   While hacking it up I've noticed spec doesn't match reality.  The
   Virtio Structure PCI Capabilities section here ...
   
   http://docs.oasis-open.org/virtio/virtio/v1.0/cs01/virtio-v1.0-cs01.html#x1-690004
   
   ... doesn't match what qemu is doing.  Huh?
  
  Thanks a lot for the report, and the tool!
 
 pciutils patch needs an update too (because I've made it decode the
 broken qemu capabilities).  Will come as separate mail with proper
 commit message in a moment.
 
  I sent patches to fix this all up.
  You can try my qemu and linux virtio-net branches now,
  they should be spec compliant.
 
 Yep, much better.  caps match spec, the system boots fine from
 virtio-scsi, and the nfs-mounts (using virtio-net) are there too.
 
 cheers,
   Gerd


Thanks!
Want to respond to the patches with a tested-by tag?

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [Xen-devel] [PATCH v14 11/11] pvqspinlock, x86: Enable PV qspinlock for XEN

2015-01-21 Thread David Vrabel
On 20/01/15 20:12, Waiman Long wrote:
 This patch adds the necessary XEN specific code to allow XEN to
 support the CPU halting and kicking operations needed by the queue
 spinlock PV code.

Xen is a word, please don't capitalize it.

 +void xen_lock_stats(int stat_types)
 +{
 + if (stat_types  PV_LOCKSTAT_WAKE_KICKED)
 + add_smp(wake_kick_stats, 1);
 + if (stat_types  PV_LOCKSTAT_WAKE_SPURIOUS)
 + add_smp(wake_spur_stats, 1);
 + if (stat_types  PV_LOCKSTAT_KICK_NOHALT)
 + add_smp(kick_nohlt_stats, 1);
 + if (stat_types  PV_LOCKSTAT_HALT_QHEAD)
 + add_smp(halt_qhead_stats, 1);
 + if (stat_types  PV_LOCKSTAT_HALT_QNODE)
 + add_smp(halt_qnode_stats, 1);
 + if (stat_types  PV_LOCKSTAT_HALT_ABORT)
 + add_smp(halt_abort_stats, 1);
 +}
 +PV_CALLEE_SAVE_REGS_THUNK(xen_lock_stats);

This is not inlined and the 6 test-and-branch cannot be optimized away.

 +/*
 + * Halt the current CPU  release it back to the host
 + * Return 0 if halted, -1 otherwise.
 + */
 +int xen_halt_cpu(u8 *byte, u8 val)
 +{
 + int irq = __this_cpu_read(lock_kicker_irq);
 + unsigned long flags;
 + u64 start;
 +
 + /* If kicker interrupts not initialized yet, just spin */
 + if (irq == -1)
 + return -1;
 +
 + /*
 +  * Make sure an interrupt handler can't upset things in a
 +  * partially setup state.
 +  */
 + local_irq_save(flags);
 + start = spin_time_start();
 +
 + /* clear pending */
 + xen_clear_irq_pending(irq);
 +
 + /* Allow interrupts while blocked */
 + local_irq_restore(flags);

It's not clear what partially setup state is being protected here.
xen_clear_irq_pending() is an atomic bit clear.

I think you can drop the irq save/restore here.

 + /*
 +  * Don't halt if the content of the given byte address differs from
 +  * the expected value. A read memory barrier is added to make sure that
 +  * the latest value of the byte address is fetched.
 +  */
 + smp_rmb();

The atomic bit clear in xen_clear_irq_pending() acts as a full memory
barrier.  I don't think you need an additional memory barrier here, only
a compiler one.  I suggest using READ_ONCE().

 + if (*byte != val) {
 + xen_lock_stats(PV_LOCKSTAT_HALT_ABORT);
 + return -1;
 + }
 + /*
 +  * If an interrupt happens here, it will leave the wakeup irq
 +  * pending, which will cause xen_poll_irq() to return
 +  * immediately.
 +  */
 +
 + /* Block until irq becomes pending (or perhaps a spurious wakeup) */
 + xen_poll_irq(irq);
 + spin_time_accum_blocked(start);
 + return 0;
 +}
 +PV_CALLEE_SAVE_REGS_THUNK(xen_halt_cpu);
 +
 +#endif /* CONFIG_QUEUE_SPINLOCK */
 +
  static irqreturn_t dummy_handler(int irq, void *dev_id)
  {
   BUG();

David

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [Qemu-devel] [PATCH RFC v6 10/20] s390x/virtio-ccw: add virtio set-revision call

2015-01-21 Thread Thomas Huth
On Wed, 21 Jan 2015 12:23:18 +0100
Cornelia Huck cornelia.h...@de.ibm.com wrote:

 On Tue, 20 Jan 2015 11:08:24 +
 Stefan Hajnoczi stefa...@gmail.com wrote:
 
  On Thu, Dec 11, 2014 at 02:25:12PM +0100, Cornelia Huck wrote:
   @@ -608,6 +631,25 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
}
}
break;
   +case CCW_CMD_SET_VIRTIO_REV:
   +len = sizeof(revinfo);
   +if (ccw.count  len || (check_len  ccw.count  len)) {
   +ret = -EINVAL;
   +break;
   +}
   +if (!ccw.cda) {
   +ret = -EFAULT;
   +break;
   +}
   +cpu_physical_memory_read(ccw.cda, revinfo, len);
   +if (dev-revision = 0 ||
   +revinfo.revision  virtio_ccw_rev_max(dev)) {
  
  In the next patch virtio_ccw_handle_set_vq() uses big-endian memory
  access functions to load a struct from guest memory.
  
  Here you just copy the struct in without byteswaps.
  
  Are the byteswaps missing here?  (I guess this normally runs big-endian
  guests on big-endian hosts so it's not noticable.)
 
 Indeed, these are supposed to be big-endian. I'll double check the
 other payloads.

Right. Cornelia, can you take care of this or shall I rework the patch?

NB: Actually, there are a couple of XXX config space endianness
comments in that virtio_ccw_cb() function, so there are likely a bunch
of problems when this code should be run on little endian hosts one day.
So far, this code only runs with big-endian guests on big-endian hosts
since the virtio-ccw machine is currently KVM-only as far as I know,
that's likely why nobody complained about this yet.

 Thomas

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH RFC v6 10/20] s390x/virtio-ccw: add virtio set-revision call

2015-01-21 Thread Cornelia Huck
On Tue, 20 Jan 2015 11:08:24 +
Stefan Hajnoczi stefa...@gmail.com wrote:

 On Thu, Dec 11, 2014 at 02:25:12PM +0100, Cornelia Huck wrote:
  @@ -608,6 +631,25 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
   }
   }
   break;
  +case CCW_CMD_SET_VIRTIO_REV:
  +len = sizeof(revinfo);
  +if (ccw.count  len || (check_len  ccw.count  len)) {
  +ret = -EINVAL;
  +break;
  +}
  +if (!ccw.cda) {
  +ret = -EFAULT;
  +break;
  +}
  +cpu_physical_memory_read(ccw.cda, revinfo, len);
  +if (dev-revision = 0 ||
  +revinfo.revision  virtio_ccw_rev_max(dev)) {
 
 In the next patch virtio_ccw_handle_set_vq() uses big-endian memory
 access functions to load a struct from guest memory.
 
 Here you just copy the struct in without byteswaps.
 
 Are the byteswaps missing here?  (I guess this normally runs big-endian
 guests on big-endian hosts so it's not noticable.)

Indeed, these are supposed to be big-endian. I'll double check the
other payloads.

Thanks for spotting this!

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH v14 08/11] qspinlock, x86: Rename paravirt_ticketlocks_enabled

2015-01-21 Thread Raghavendra K T

On 01/21/2015 01:42 AM, Waiman Long wrote:

This patch renames the paravirt_ticketlocks_enabled static key to a
more generic paravirt_spinlocks_enabled name.

Signed-off-by: Waiman Long waiman.l...@hp.com
Signed-off-by: Peter Zijlstra pet...@infradead.org
---

Reviewed-by: Raghavendra K T raghavendra...@linux.vnet.ibm.com

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 00/14] virtio 1.0: virtio-pci fixup

2015-01-21 Thread Michael S. Tsirkin
This is just the full patchset reposted with fixups in correct order,
before squashing them.
I also tweaked commit log for patch
virtio_pci: modern driver
I also included Gerd's tag:
Tested-by: Gerd Hoffmann kra...@redhat.com

You can find it all before the rebase -i --autosquash in my tree:
git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git virtio-next
or same thing after rebase -i --autosquash:
git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git vhost-next

Hope it helps!

Michael S. Tsirkin (12):
  virtio_pci: move probe/remove code to common
  fixup! virtio-pci: define layout for virtio 1.0
  virtio_pci: modern driver
  fixup! virtio_pci: modern driver
  fixup! virtio_pci: modern driver
  fixup! virtio_pci: macros for PCI layout offsets
  virtio_pci_modern: reduce number of mappings
  virtio_pci_modern: support devices with no config
  virtio_pci: add an option to disable legacy driver
  virtio_pci: add module param to force legacy mode
  fixup! virtio_pci: add module param to force legacy mode
  virtio_pci_modern: drop an unused function

Rusty Russell (2):
  virtio-pci: define layout for virtio 1.0
  virtio_pci: macros for PCI layout offsets

 drivers/virtio/virtio_pci_common.h |  43 ++-
 include/uapi/linux/virtio_pci.h|  89 +
 drivers/virtio/virtio_pci_common.c |  94 -
 drivers/virtio/virtio_pci_legacy.c |  75 +---
 drivers/virtio/virtio_pci_modern.c | 695 +
 drivers/virtio/Kconfig |  19 +
 drivers/virtio/Makefile|   3 +-
 7 files changed, 942 insertions(+), 76 deletions(-)
 create mode 100644 drivers/virtio/virtio_pci_modern.c

-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 01/14] virtio_pci: move probe/remove code to common

2015-01-21 Thread Michael S. Tsirkin
Most of initialization is device-independent.
Let's move it to common.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  5 +--
 drivers/virtio/virtio_pci_common.c | 69 ++-
 drivers/virtio/virtio_pci_legacy.c | 75 --
 3 files changed, 77 insertions(+), 72 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index 5a49728..2b1e70d 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -127,8 +127,7 @@ const char *vp_bus_name(struct virtio_device *vdev);
  */
 int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
 
-int virtio_pci_legacy_probe(struct pci_dev *pci_dev,
-   const struct pci_device_id *id);
-void virtio_pci_legacy_remove(struct pci_dev *pci_dev);
+int virtio_pci_legacy_probe(struct virtio_pci_device *);
+void virtio_pci_legacy_remove(struct virtio_pci_device *);
 
 #endif
diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 9756f21..457cbe2 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -464,15 +464,80 @@ static const struct pci_device_id virtio_pci_id_table[] = 
{
 
 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
 
+static void virtio_pci_release_dev(struct device *_d)
+{
+   struct virtio_device *vdev = dev_to_virtio(_d);
+   struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+
+   /* As struct device is a kobject, it's not safe to
+* free the memory (including the reference counter itself)
+* until it's release callback. */
+   kfree(vp_dev);
+}
+
 static int virtio_pci_probe(struct pci_dev *pci_dev,
const struct pci_device_id *id)
 {
-   return virtio_pci_legacy_probe(pci_dev, id);
+   struct virtio_pci_device *vp_dev;
+   int rc;
+
+   /* allocate our structure and fill it out */
+   vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
+   if (!vp_dev)
+   return -ENOMEM;
+
+   pci_set_drvdata(pci_dev, vp_dev);
+   vp_dev-vdev.dev.parent = pci_dev-dev;
+   vp_dev-vdev.dev.release = virtio_pci_release_dev;
+   vp_dev-pci_dev = pci_dev;
+   INIT_LIST_HEAD(vp_dev-virtqueues);
+   spin_lock_init(vp_dev-lock);
+
+   /* Disable MSI/MSIX to bring device to a known good state. */
+   pci_msi_off(pci_dev);
+
+   /* enable the device */
+   rc = pci_enable_device(pci_dev);
+   if (rc)
+   goto err_enable_device;
+
+   rc = pci_request_regions(pci_dev, virtio-pci);
+   if (rc)
+   goto err_request_regions;
+
+   rc = virtio_pci_legacy_probe(vp_dev);
+   if (rc)
+   goto err_probe;
+
+   pci_set_master(pci_dev);
+
+   rc = register_virtio_device(vp_dev-vdev);
+   if (rc)
+   goto err_register;
+
+   return 0;
+
+err_register:
+   virtio_pci_legacy_remove(vp_dev);
+err_probe:
+   pci_release_regions(pci_dev);
+err_request_regions:
+   pci_disable_device(pci_dev);
+err_enable_device:
+   kfree(vp_dev);
+   return rc;
 }
 
 static void virtio_pci_remove(struct pci_dev *pci_dev)
 {
- virtio_pci_legacy_remove(pci_dev);
+   struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
+
+   unregister_virtio_device(vp_dev-vdev);
+
+   virtio_pci_legacy_remove(pci_dev);
+
+   pci_release_regions(pci_dev);
+   pci_disable_device(pci_dev);
 }
 
 static struct pci_driver virtio_pci_driver = {
diff --git a/drivers/virtio/virtio_pci_legacy.c 
b/drivers/virtio/virtio_pci_legacy.c
index 19f9309..256a527 100644
--- a/drivers/virtio/virtio_pci_legacy.c
+++ b/drivers/virtio/virtio_pci_legacy.c
@@ -211,23 +211,10 @@ static const struct virtio_config_ops 
virtio_pci_config_ops = {
.set_vq_affinity = vp_set_vq_affinity,
 };
 
-static void virtio_pci_release_dev(struct device *_d)
-{
-   struct virtio_device *vdev = dev_to_virtio(_d);
-   struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-
-   /* As struct device is a kobject, it's not safe to
-* free the memory (including the reference counter itself)
-* until it's release callback. */
-   kfree(vp_dev);
-}
-
 /* the PCI probing function */
-int virtio_pci_legacy_probe(struct pci_dev *pci_dev,
-   const struct pci_device_id *id)
+int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
 {
-   struct virtio_pci_device *vp_dev;
-   int err;
+   struct pci_dev *pci_dev = vp_dev-pci_dev;
 
/* We only own devices = 0x1000 and = 0x103f: leave the rest. */
if (pci_dev-device  0x1000 || pci_dev-device  0x103f)
@@ -239,41 +226,12 @@ int virtio_pci_legacy_probe(struct pci_dev *pci_dev,
return -ENODEV;

[PATCH pre-squash 02/14] virtio-pci: define layout for virtio 1.0

2015-01-21 Thread Michael S. Tsirkin
From: Rusty Russell ru...@rustcorp.com.au

Based on patches by Michael S. Tsirkin m...@redhat.com, but I found it
hard to follow so changed to use structures which are more
self-documenting.

Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 include/uapi/linux/virtio_pci.h | 62 +
 1 file changed, 62 insertions(+)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index 509d630..4e05423 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -99,4 +99,66 @@
 /* Vector value used to disable MSI for queue */
 #define VIRTIO_MSI_NO_VECTOR0x
 
+#ifndef VIRTIO_PCI_NO_MODERN
+
+/* IDs for different capabilities.  Must all exist. */
+
+/* Common configuration */
+#define VIRTIO_PCI_CAP_COMMON_CFG  1
+/* Notifications */
+#define VIRTIO_PCI_CAP_NOTIFY_CFG  2
+/* ISR access */
+#define VIRTIO_PCI_CAP_ISR_CFG 3
+/* Device specific confiuration */
+#define VIRTIO_PCI_CAP_DEVICE_CFG  4
+
+/* This is the PCI capability header: */
+struct virtio_pci_cap {
+   __u8 cap_vndr;  /* Generic PCI field: PCI_CAP_ID_VNDR */
+   __u8 cap_next;  /* Generic PCI field: next ptr. */
+   __u8 cap_len;   /* Generic PCI field: capability length */
+   __u8 type_and_bar;  /* Upper 3 bits: bar.
+* Lower 3 is VIRTIO_PCI_CAP_*_CFG. */
+   __le32 offset;  /* Offset within bar. */
+   __le32 length;  /* Length. */
+};
+
+#define VIRTIO_PCI_CAP_BAR_SHIFT   5
+#define VIRTIO_PCI_CAP_BAR_MASK0x7
+#define VIRTIO_PCI_CAP_TYPE_SHIFT  0
+#define VIRTIO_PCI_CAP_TYPE_MASK   0x7
+
+struct virtio_pci_notify_cap {
+   struct virtio_pci_cap cap;
+   __le32 notify_off_multiplier;   /* Multiplier for queue_notify_off. */
+};
+
+/* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
+struct virtio_pci_common_cfg {
+   /* About the whole device. */
+   __le32 device_feature_select;   /* read-write */
+   __le32 device_feature;  /* read-only */
+   __le32 guest_feature_select;/* read-write */
+   __le32 guest_feature;   /* read-write */
+   __le16 msix_config; /* read-write */
+   __le16 num_queues;  /* read-only */
+   __u8 device_status; /* read-write */
+   __u8 config_generation; /* read-only */
+
+   /* About a specific virtqueue. */
+   __le16 queue_select;/* read-write */
+   __le16 queue_size;  /* read-write, power of 2. */
+   __le16 queue_msix_vector;   /* read-write */
+   __le16 queue_enable;/* read-write */
+   __le16 queue_notify_off;/* read-only */
+   __le32 queue_desc_lo;   /* read-write */
+   __le32 queue_desc_hi;   /* read-write */
+   __le32 queue_avail_lo;  /* read-write */
+   __le32 queue_avail_hi;  /* read-write */
+   __le32 queue_used_lo;   /* read-write */
+   __le32 queue_used_hi;   /* read-write */
+};
+
+#endif /* VIRTIO_PCI_NO_MODERN */
+
 #endif
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 03/14] fixup! virtio-pci: define layout for virtio 1.0

2015-01-21 Thread Michael S. Tsirkin
virtio-pci: fix up virtio 1.0 vendor capability

Gerd Hoffmann noticed that we implemented
capability layout from an old draft.
Unfortunately the code was copied to host as well,
so we didn't notice.

Luckily we caught this in time.

This fixes commit virtio-pci: define layout for virtio 1.0
and should be smashed with it.

Reported-by: Gerd Hoffmann kra...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 include/uapi/linux/virtio_pci.h | 7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index 4e05423..a2b2e13 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -117,10 +117,11 @@ struct virtio_pci_cap {
__u8 cap_vndr;  /* Generic PCI field: PCI_CAP_ID_VNDR */
__u8 cap_next;  /* Generic PCI field: next ptr. */
__u8 cap_len;   /* Generic PCI field: capability length */
-   __u8 type_and_bar;  /* Upper 3 bits: bar.
-* Lower 3 is VIRTIO_PCI_CAP_*_CFG. */
+   __u8 cfg_type;  /* Identifies the structure. */
+   __u8 bar;   /* Where to find it. */
+   __u8 padding[3];/* Pad to full dword. */
__le32 offset;  /* Offset within bar. */
-   __le32 length;  /* Length. */
+   __le32 length;  /* Length of the structure, in bytes. */
 };
 
 #define VIRTIO_PCI_CAP_BAR_SHIFT   5
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 04/14] virtio_pci: modern driver

2015-01-21 Thread Michael S. Tsirkin
Lightly tested against qemu.

One thing *not* implemented here is separate mappings
for descriptor/avail/used rings. That's nice to have,
will be done later after we have core support.

Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  25 +-
 drivers/virtio/virtio_pci_common.c |  14 +-
 drivers/virtio/virtio_pci_modern.c | 592 +
 drivers/virtio/Makefile|   2 +-
 4 files changed, 627 insertions(+), 6 deletions(-)
 create mode 100644 drivers/virtio/virtio_pci_modern.c

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index 2b1e70d..610c43f 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -53,12 +53,29 @@ struct virtio_pci_device {
struct virtio_device vdev;
struct pci_dev *pci_dev;
 
+   /* In legacy mode, these two point to within -legacy. */
+   /* Where to read and clear interrupt */
+   u8 __iomem *isr;
+
+   /* Modern only fields */
+   /* The IO mapping for the PCI config space (non-legacy mode) */
+   struct virtio_pci_common_cfg __iomem *common;
+   /* Device-specific data (non-legacy mode)  */
+   void __iomem *device;
+
+   /* So we can sanity-check accesses. */
+   size_t device_len;
+
+   /* Capability for when we need to map notifications per-vq. */
+   int notify_map_cap;
+
+   /* Multiply queue_notify_off by this value. (non-legacy mode). */
+   u32 notify_offset_multiplier;
+
+   /* Legacy only field */
/* the IO mapping for the PCI config space */
void __iomem *ioaddr;
 
-   /* the IO mapping for ISR operation */
-   void __iomem *isr;
-
/* a list of queues so we can dispatch IRQs */
spinlock_t lock;
struct list_head virtqueues;
@@ -129,5 +146,7 @@ int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
 
 int virtio_pci_legacy_probe(struct virtio_pci_device *);
 void virtio_pci_legacy_remove(struct virtio_pci_device *);
+int virtio_pci_modern_probe(struct virtio_pci_device *);
+void virtio_pci_modern_remove(struct virtio_pci_device *);
 
 #endif
diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 457cbe2..20c7638 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -505,6 +505,10 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
if (rc)
goto err_request_regions;
 
+   rc = virtio_pci_modern_probe(vp_dev);
+   if (rc != -ENODEV)
+   return rc;
+
rc = virtio_pci_legacy_probe(vp_dev);
if (rc)
goto err_probe;
@@ -518,7 +522,10 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
return 0;
 
 err_register:
-   virtio_pci_legacy_remove(vp_dev);
+   if (vp_dev-ioaddr)
+virtio_pci_legacy_remove(vp_dev);
+   else
+virtio_pci_modern_remove(vp_dev);
 err_probe:
pci_release_regions(pci_dev);
 err_request_regions:
@@ -534,7 +541,10 @@ static void virtio_pci_remove(struct pci_dev *pci_dev)
 
unregister_virtio_device(vp_dev-vdev);
 
-   virtio_pci_legacy_remove(pci_dev);
+   if (vp_dev-ioaddr)
+   virtio_pci_legacy_remove(vp_dev);
+   else
+   virtio_pci_modern_remove(vp_dev);
 
pci_release_regions(pci_dev);
pci_disable_device(pci_dev);
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
new file mode 100644
index 000..e2f41c9
--- /dev/null
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -0,0 +1,592 @@
+/*
+ * Virtio PCI driver - modern (virtio 1.0) device support
+ *
+ * This module allows virtio devices to be used over a virtual PCI device.
+ * This can be used with QEMU based VMMs like KVM or Xen.
+ *
+ * Copyright IBM Corp. 2007
+ * Copyright Red Hat, Inc. 2014
+ *
+ * Authors:
+ *  Anthony Liguori  aligu...@us.ibm.com
+ *  Rusty Russell ru...@rustcorp.com.au
+ *  Michael S. Tsirkin m...@redhat.com
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#define VIRTIO_PCI_NO_LEGACY
+#include virtio_pci_common.h
+
+static void __iomem *map_capability(struct pci_dev *dev, int off,
+   size_t minlen,
+   u32 align,
+   u32 start, u32 size,
+   size_t *len)
+{
+   u8 type_and_bar, bar;
+   u32 offset, length;
+   void __iomem *p;
+
+   pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
+type_and_bar),
+type_and_bar);
+   pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, 
offset),
+   

[PATCH pre-squash 10/14] virtio_pci_modern: support devices with no config

2015-01-21 Thread Michael S. Tsirkin
Virtio 1.0 spec lists device config as optional.
Set get/set callbacks to NULL. Drivers can check that
and fail gracefully.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_modern.c | 21 +++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index 0e54cc8..68ebc20 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -422,6 +422,21 @@ static void del_vq(struct virtio_pci_vq_info *info)
free_pages_exact(info-queue, vring_pci_size(info-num));
 }
 
+static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
+   .get= NULL,
+   .set= NULL,
+   .generation = vp_generation,
+   .get_status = vp_get_status,
+   .set_status = vp_set_status,
+   .reset  = vp_reset,
+   .find_vqs   = vp_modern_find_vqs,
+   .del_vqs= vp_del_vqs,
+   .get_features   = vp_get_features,
+   .finalize_features = vp_finalize_features,
+   .bus_name   = vp_bus_name,
+   .set_vq_affinity = vp_set_vq_affinity,
+};
+
 static const struct virtio_config_ops virtio_pci_config_ops = {
.get= vp_get,
.set= vp_set,
@@ -652,9 +667,11 @@ int virtio_pci_modern_probe(struct virtio_pci_device 
*vp_dev)
vp_dev-device_len);
if (!vp_dev-device)
goto err_map_device;
-   }
 
-   vp_dev-vdev.config = virtio_pci_config_ops;
+   vp_dev-vdev.config = virtio_pci_config_ops;
+   } else {
+   vp_dev-vdev.config = virtio_pci_config_nodev_ops;
+   }
 
vp_dev-config_vector = vp_config_vector;
vp_dev-setup_vq = setup_vq;
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 09/14] virtio_pci_modern: reduce number of mappings

2015-01-21 Thread Michael S. Tsirkin
We don't know the # of VQs that drivers are going to use so it's hard to
predict how much memory we'll need to map. However, the relevant
capability does give us an upper limit.
If that's below a page, we can reduce the number of required
mappings by mapping it all once ahead of the time.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  3 ++
 drivers/virtio/virtio_pci_modern.c | 57 --
 2 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index 610c43f..d391805 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -62,8 +62,11 @@ struct virtio_pci_device {
struct virtio_pci_common_cfg __iomem *common;
/* Device-specific data (non-legacy mode)  */
void __iomem *device;
+   /* Base of vq notifications (non-legacy mode). */
+   void __iomem *notify_base;
 
/* So we can sanity-check accesses. */
+   size_t notify_len;
size_t device_len;
 
/* Capability for when we need to map notifications per-vq. */
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index b2e707ad..0e54cc8 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -330,10 +330,26 @@ static struct virtqueue *setup_vq(struct 
virtio_pci_device *vp_dev,
iowrite64_twopart(virt_to_phys(virtqueue_get_used(vq)),
  cfg-queue_used_lo, cfg-queue_used_hi);
 
-   vq-priv = (void __force *)map_capability(vp_dev-pci_dev,
- vp_dev-notify_map_cap, 2, 2,
- off * vp_dev-notify_offset_multiplier, 2,
- NULL);
+   if (vp_dev-notify_base) {
+   /* offset should not wrap */
+   if ((u64)off * vp_dev-notify_offset_multiplier + 2
+vp_dev-notify_len) {
+   dev_warn(vp_dev-pci_dev-dev,
+bad notification offset %u (x %u) 
+for queue %u  %zd,
+off, vp_dev-notify_offset_multiplier,
+index, vp_dev-notify_len);
+   err = -EINVAL;
+   goto err_map_notify;
+   }
+   vq-priv = (void __force *)vp_dev-notify_base +
+   off * vp_dev-notify_offset_multiplier;
+   } else {
+   vq-priv = (void __force *)map_capability(vp_dev-pci_dev,
+ vp_dev-notify_map_cap, 2, 2,
+ off * 
vp_dev-notify_offset_multiplier, 2,
+ NULL);
+   }
 
if (!vq-priv) {
err = -ENOMEM;
@@ -352,7 +368,8 @@ static struct virtqueue *setup_vq(struct virtio_pci_device 
*vp_dev,
return vq;
 
 err_assign_vector:
-   pci_iounmap(vp_dev-pci_dev, (void __iomem __force *)vq-priv);
+   if (!vp_dev-notify_base)
+   pci_iounmap(vp_dev-pci_dev, (void __iomem __force *)vq-priv);
 err_map_notify:
vring_del_virtqueue(vq);
 err_new_queue:
@@ -397,7 +414,8 @@ static void del_vq(struct virtio_pci_vq_info *info)
ioread16(vp_dev-common-queue_msix_vector);
}
 
-   pci_iounmap(vp_dev-pci_dev, (void __force __iomem *)vq-priv);
+   if (!vp_dev-notify_base)
+   pci_iounmap(vp_dev-pci_dev, (void __force __iomem *)vq-priv);
 
vring_del_virtqueue(vq);
 
@@ -533,6 +551,7 @@ int virtio_pci_modern_probe(struct virtio_pci_device 
*vp_dev)
struct pci_dev *pci_dev = vp_dev-pci_dev;
int err, common, isr, notify, device;
u32 notify_length;
+   u32 notify_offset;
 
check_offsets();
 
@@ -599,13 +618,30 @@ int virtio_pci_modern_probe(struct virtio_pci_device 
*vp_dev)
  notify + offsetof(struct virtio_pci_notify_cap,
notify_off_multiplier),
  vp_dev-notify_offset_multiplier);
-   /* Read notify length from config space. */
+   /* Read notify length and offset from config space. */
pci_read_config_dword(pci_dev,
  notify + offsetof(struct virtio_pci_notify_cap,
cap.length),
  notify_length);
 
-   vp_dev-notify_map_cap = notify;
+   pci_read_config_dword(pci_dev,
+ notify + offsetof(struct virtio_pci_notify_cap,
+   cap.length),
+ notify_offset);
+
+   /* We don't know how many VQs we'll map, ahead of the time.
+* 

[PATCH pre-squash 11/14] virtio_pci: add an option to disable legacy driver

2015-01-21 Thread Michael S. Tsirkin
Useful for testing device virtio 1 compatibility.
Based on patch by Rusty - couldn't resist putting
that flying car joke in there!

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h | 10 ++
 drivers/virtio/Kconfig | 19 +++
 drivers/virtio/Makefile|  3 ++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index d391805..28ee4e5 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -147,8 +147,18 @@ const char *vp_bus_name(struct virtio_device *vdev);
  */
 int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
 
+#if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
 int virtio_pci_legacy_probe(struct virtio_pci_device *);
 void virtio_pci_legacy_remove(struct virtio_pci_device *);
+#else
+static inline int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
+{
+   return -ENODEV;
+}
+static inline void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev)
+{
+}
+#endif
 int virtio_pci_modern_probe(struct virtio_pci_device *);
 void virtio_pci_modern_remove(struct virtio_pci_device *);
 
diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 083fb45..b546da5 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -19,6 +19,25 @@ config VIRTIO_PCI
 
  If unsure, say M.
 
+config VIRTIO_PCI_LEGACY
+   bool Support for legacy virtio draft 0.9.X and older devices
+   default y
+   depends on VIRTIO_PCI
+   ---help---
+  Virtio PCI Card 0.9.X Draft (circa 2014) and older device support.
+
+ This option enables building a transitional driver, supporting
+ both devices conforming to Virtio 1 specification, and legacy devices.
+ If disabled, you get a slightly smaller, non-transitional driver,
+ with no legacy compatibility.
+
+  So look out into your driveway.  Do you have a flying car?  If
+  so, you can happily disable this option and virtio will not
+  break.  Otherwise, leave it set.  Unless you're testing what
+  life will be like in The Future.
+
+ If unsure, say Y.
+
 config VIRTIO_BALLOON
tristate Virtio balloon driver
depends on VIRTIO
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index bd230d1..d85565b 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -1,5 +1,6 @@
 obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
-virtio_pci-y := virtio_pci_modern.o virtio_pci_legacy.o virtio_pci_common.o
+virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o
+virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 13/14] fixup! virtio_pci: add module param to force legacy mode

2015-01-21 Thread Michael S. Tsirkin
virtio modern: fix up fallback logic with force_legacy

This bails out if legacy driver succeeds - not what we wanted.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.c | 12 
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 0f87b99..e894eb2 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -516,18 +516,14 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
if (force_legacy) {
rc = virtio_pci_legacy_probe(vp_dev);
/* Also try modern mode if we can't map BAR0 (no IO space). */
-   if (rc != -ENODEV  rc != -ENOMEM)
-   return rc;
-
-   rc = virtio_pci_modern_probe(vp_dev);
+   if (rc == -ENODEV || rc == -ENOMEM)
+   rc = virtio_pci_modern_probe(vp_dev);
if (rc)
goto err_probe;
} else {
rc = virtio_pci_modern_probe(vp_dev);
-   if (rc != -ENODEV)
-   return rc;
-
-   rc = virtio_pci_legacy_probe(vp_dev);
+   if (rc == -ENODEV)
+   rc = virtio_pci_legacy_probe(vp_dev);
if (rc)
goto err_probe;
}
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 07/14] virtio_pci: macros for PCI layout offsets

2015-01-21 Thread Michael S. Tsirkin
From: Rusty Russell ru...@rustcorp.com.au

QEMU wants it, so why not?  Trust, but verify.

Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 include/uapi/linux/virtio_pci.h| 30 
 drivers/virtio/virtio_pci_modern.c | 58 +-
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index a2b2e13..0911c62 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -160,6 +160,36 @@ struct virtio_pci_common_cfg {
__le32 queue_used_hi;   /* read-write */
 };
 
+/* Macro versions of offsets for the Old Timers! */
+#define VIRTIO_PCI_CAP_VNDR0
+#define VIRTIO_PCI_CAP_NEXT1
+#define VIRTIO_PCI_CAP_LEN 2
+#define VIRTIO_PCI_CAP_TYPE_AND_BAR3
+#define VIRTIO_PCI_CAP_OFFSET  4
+#define VIRTIO_PCI_CAP_LENGTH  8
+
+#define VIRTIO_PCI_NOTIFY_CAP_MULT 12
+
+#define VIRTIO_PCI_COMMON_DFSELECT 0
+#define VIRTIO_PCI_COMMON_DF   4
+#define VIRTIO_PCI_COMMON_GFSELECT 8
+#define VIRTIO_PCI_COMMON_GF   12
+#define VIRTIO_PCI_COMMON_MSIX 16
+#define VIRTIO_PCI_COMMON_NUMQ 18
+#define VIRTIO_PCI_COMMON_STATUS   20
+#define VIRTIO_PCI_COMMON_CFGGENERATION21
+#define VIRTIO_PCI_COMMON_Q_SELECT 22
+#define VIRTIO_PCI_COMMON_Q_SIZE   24
+#define VIRTIO_PCI_COMMON_Q_MSIX   26
+#define VIRTIO_PCI_COMMON_Q_ENABLE 28
+#define VIRTIO_PCI_COMMON_Q_NOFF   30
+#define VIRTIO_PCI_COMMON_Q_DESCLO 32
+#define VIRTIO_PCI_COMMON_Q_DESCHI 36
+#define VIRTIO_PCI_COMMON_Q_AVAILLO40
+#define VIRTIO_PCI_COMMON_Q_AVAILHI44
+#define VIRTIO_PCI_COMMON_Q_USEDLO 48
+#define VIRTIO_PCI_COMMON_Q_USEDHI 52
+
 #endif /* VIRTIO_PCI_NO_MODERN */
 
 #endif
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index a3d8101..c86594e 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -464,9 +464,65 @@ static void virtio_pci_release_dev(struct device *_d)
kfree(vp_dev);
 }
 
-/* TODO: validate the ABI statically. */
+/* This is part of the ABI.  Don't screw with it. */
 static inline void check_offsets(void)
 {
+   /* Note: disk space was harmed in compilation of this function. */
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
+offsetof(struct virtio_pci_cap, cap_vndr));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
+offsetof(struct virtio_pci_cap, cap_next));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
+offsetof(struct virtio_pci_cap, cap_len));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_TYPE_AND_BAR !=
+offsetof(struct virtio_pci_cap, type_and_bar));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
+offsetof(struct virtio_pci_cap, offset));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
+offsetof(struct virtio_pci_cap, length));
+   BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
+offsetof(struct virtio_pci_notify_cap,
+ notify_off_multiplier));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
+offsetof(struct virtio_pci_common_cfg,
+ device_feature_select));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
+offsetof(struct virtio_pci_common_cfg, device_feature));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
+offsetof(struct virtio_pci_common_cfg,
+ guest_feature_select));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
+offsetof(struct virtio_pci_common_cfg, guest_feature));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
+offsetof(struct virtio_pci_common_cfg, msix_config));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
+offsetof(struct virtio_pci_common_cfg, num_queues));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
+offsetof(struct virtio_pci_common_cfg, device_status));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
+offsetof(struct virtio_pci_common_cfg, config_generation));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
+offsetof(struct virtio_pci_common_cfg, queue_select));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
+offsetof(struct virtio_pci_common_cfg, queue_size));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
+offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
+offsetof(struct virtio_pci_common_cfg, queue_enable));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
+offsetof(struct virtio_pci_common_cfg, 

[PATCH pre-squash 08/14] fixup! virtio_pci: macros for PCI layout offsets

2015-01-21 Thread Michael S. Tsirkin
virtio_pci_modern: fix up vendor capability macros

Gerd Hoffmann noticed that we implemented
capability layout from an old draft.
Unfortunately the code was copied to host as well,
so we didn't notice.

Luckily we caught this in time.

This fixes commit virtio_pci: macros for PCI layout offsets
and should be smashed with it.

Reported-by: Gerd Hoffmann kra...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 include/uapi/linux/virtio_pci.h| 14 +-
 drivers/virtio/virtio_pci_modern.c |  6 --
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index 0911c62..3b7e4d2 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -124,11 +124,6 @@ struct virtio_pci_cap {
__le32 length;  /* Length of the structure, in bytes. */
 };
 
-#define VIRTIO_PCI_CAP_BAR_SHIFT   5
-#define VIRTIO_PCI_CAP_BAR_MASK0x7
-#define VIRTIO_PCI_CAP_TYPE_SHIFT  0
-#define VIRTIO_PCI_CAP_TYPE_MASK   0x7
-
 struct virtio_pci_notify_cap {
struct virtio_pci_cap cap;
__le32 notify_off_multiplier;   /* Multiplier for queue_notify_off. */
@@ -164,11 +159,12 @@ struct virtio_pci_common_cfg {
 #define VIRTIO_PCI_CAP_VNDR0
 #define VIRTIO_PCI_CAP_NEXT1
 #define VIRTIO_PCI_CAP_LEN 2
-#define VIRTIO_PCI_CAP_TYPE_AND_BAR3
-#define VIRTIO_PCI_CAP_OFFSET  4
-#define VIRTIO_PCI_CAP_LENGTH  8
+#define VIRTIO_PCI_CAP_CFG_TYPE3
+#define VIRTIO_PCI_CAP_BAR 4
+#define VIRTIO_PCI_CAP_OFFSET  8
+#define VIRTIO_PCI_CAP_LENGTH  12
 
-#define VIRTIO_PCI_NOTIFY_CAP_MULT 12
+#define VIRTIO_PCI_NOTIFY_CAP_MULT 16
 
 #define VIRTIO_PCI_COMMON_DFSELECT 0
 #define VIRTIO_PCI_COMMON_DF   4
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index c86594e..b2e707ad 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -474,8 +474,10 @@ static inline void check_offsets(void)
 offsetof(struct virtio_pci_cap, cap_next));
BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
 offsetof(struct virtio_pci_cap, cap_len));
-   BUILD_BUG_ON(VIRTIO_PCI_CAP_TYPE_AND_BAR !=
-offsetof(struct virtio_pci_cap, type_and_bar));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
+offsetof(struct virtio_pci_cap, cfg_type));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
+offsetof(struct virtio_pci_cap, bar));
BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
 offsetof(struct virtio_pci_cap, offset));
BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 06/14] fixup! virtio_pci: modern driver

2015-01-21 Thread Michael S. Tsirkin
virtio modern: fix up fallback logic

This bails out if modern driver succeeds - not what we wanted.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 20c7638..8ae34a3 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -506,10 +506,8 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
goto err_request_regions;
 
rc = virtio_pci_modern_probe(vp_dev);
-   if (rc != -ENODEV)
-   return rc;
-
-   rc = virtio_pci_legacy_probe(vp_dev);
+   if (rc == -ENODEV)
+   rc = virtio_pci_legacy_probe(vp_dev);
if (rc)
goto err_probe;
 
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 14/14] virtio_pci_modern: drop an unused function

2015-01-21 Thread Michael S. Tsirkin
release function in modern driver is unused:
it's a left-over from when each driver had
to have its own release.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_modern.c | 8 
 1 file changed, 8 deletions(-)

diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index 68ebc20..f16e462 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -489,14 +489,6 @@ static inline int virtio_pci_find_capability(struct 
pci_dev *dev, u8 cfg_type,
return 0;
 }
 
-static void virtio_pci_release_dev(struct device *_d)
-{
-   struct virtio_device *vdev = dev_to_virtio(_d);
-   struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-
-   kfree(vp_dev);
-}
-
 /* This is part of the ABI.  Don't screw with it. */
 static inline void check_offsets(void)
 {
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH pre-squash 12/14] virtio_pci: add module param to force legacy mode

2015-01-21 Thread Michael S. Tsirkin
If set, try legacy interface first, modern one if that fails.  Useful to
work around device/driver bugs, and for compatibility testing.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.c | 29 +
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 8ae34a3..0f87b99 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -19,6 +19,14 @@
 
 #include virtio_pci_common.h
 
+static bool force_legacy = false;
+
+#if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
+module_param(force_legacy, bool, 0444);
+MODULE_PARM_DESC(force_legacy,
+Force legacy mode for transitional virtio 1 devices);
+#endif
+
 /* wait for pending irq handlers */
 void vp_synchronize_vectors(struct virtio_device *vdev)
 {
@@ -505,11 +513,24 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
if (rc)
goto err_request_regions;
 
-   rc = virtio_pci_modern_probe(vp_dev);
-   if (rc == -ENODEV)
+   if (force_legacy) {
rc = virtio_pci_legacy_probe(vp_dev);
-   if (rc)
-   goto err_probe;
+   /* Also try modern mode if we can't map BAR0 (no IO space). */
+   if (rc != -ENODEV  rc != -ENOMEM)
+   return rc;
+
+   rc = virtio_pci_modern_probe(vp_dev);
+   if (rc)
+   goto err_probe;
+   } else {
+   rc = virtio_pci_modern_probe(vp_dev);
+   if (rc != -ENODEV)
+   return rc;
+
+   rc = virtio_pci_legacy_probe(vp_dev);
+   if (rc)
+   goto err_probe;
+   }
 
pci_set_master(pci_dev);
 
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH post-squash 1/9] virtio_pci: move probe/remove code to common

2015-01-21 Thread Michael S. Tsirkin
Most of initialization is device-independent.
Let's move it to common.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  5 +--
 drivers/virtio/virtio_pci_common.c | 69 ++-
 drivers/virtio/virtio_pci_legacy.c | 75 --
 3 files changed, 77 insertions(+), 72 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index 5a49728..2b1e70d 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -127,8 +127,7 @@ const char *vp_bus_name(struct virtio_device *vdev);
  */
 int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
 
-int virtio_pci_legacy_probe(struct pci_dev *pci_dev,
-   const struct pci_device_id *id);
-void virtio_pci_legacy_remove(struct pci_dev *pci_dev);
+int virtio_pci_legacy_probe(struct virtio_pci_device *);
+void virtio_pci_legacy_remove(struct virtio_pci_device *);
 
 #endif
diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 9756f21..457cbe2 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -464,15 +464,80 @@ static const struct pci_device_id virtio_pci_id_table[] = 
{
 
 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
 
+static void virtio_pci_release_dev(struct device *_d)
+{
+   struct virtio_device *vdev = dev_to_virtio(_d);
+   struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+
+   /* As struct device is a kobject, it's not safe to
+* free the memory (including the reference counter itself)
+* until it's release callback. */
+   kfree(vp_dev);
+}
+
 static int virtio_pci_probe(struct pci_dev *pci_dev,
const struct pci_device_id *id)
 {
-   return virtio_pci_legacy_probe(pci_dev, id);
+   struct virtio_pci_device *vp_dev;
+   int rc;
+
+   /* allocate our structure and fill it out */
+   vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
+   if (!vp_dev)
+   return -ENOMEM;
+
+   pci_set_drvdata(pci_dev, vp_dev);
+   vp_dev-vdev.dev.parent = pci_dev-dev;
+   vp_dev-vdev.dev.release = virtio_pci_release_dev;
+   vp_dev-pci_dev = pci_dev;
+   INIT_LIST_HEAD(vp_dev-virtqueues);
+   spin_lock_init(vp_dev-lock);
+
+   /* Disable MSI/MSIX to bring device to a known good state. */
+   pci_msi_off(pci_dev);
+
+   /* enable the device */
+   rc = pci_enable_device(pci_dev);
+   if (rc)
+   goto err_enable_device;
+
+   rc = pci_request_regions(pci_dev, virtio-pci);
+   if (rc)
+   goto err_request_regions;
+
+   rc = virtio_pci_legacy_probe(vp_dev);
+   if (rc)
+   goto err_probe;
+
+   pci_set_master(pci_dev);
+
+   rc = register_virtio_device(vp_dev-vdev);
+   if (rc)
+   goto err_register;
+
+   return 0;
+
+err_register:
+   virtio_pci_legacy_remove(vp_dev);
+err_probe:
+   pci_release_regions(pci_dev);
+err_request_regions:
+   pci_disable_device(pci_dev);
+err_enable_device:
+   kfree(vp_dev);
+   return rc;
 }
 
 static void virtio_pci_remove(struct pci_dev *pci_dev)
 {
- virtio_pci_legacy_remove(pci_dev);
+   struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
+
+   unregister_virtio_device(vp_dev-vdev);
+
+   virtio_pci_legacy_remove(pci_dev);
+
+   pci_release_regions(pci_dev);
+   pci_disable_device(pci_dev);
 }
 
 static struct pci_driver virtio_pci_driver = {
diff --git a/drivers/virtio/virtio_pci_legacy.c 
b/drivers/virtio/virtio_pci_legacy.c
index 19f9309..256a527 100644
--- a/drivers/virtio/virtio_pci_legacy.c
+++ b/drivers/virtio/virtio_pci_legacy.c
@@ -211,23 +211,10 @@ static const struct virtio_config_ops 
virtio_pci_config_ops = {
.set_vq_affinity = vp_set_vq_affinity,
 };
 
-static void virtio_pci_release_dev(struct device *_d)
-{
-   struct virtio_device *vdev = dev_to_virtio(_d);
-   struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-
-   /* As struct device is a kobject, it's not safe to
-* free the memory (including the reference counter itself)
-* until it's release callback. */
-   kfree(vp_dev);
-}
-
 /* the PCI probing function */
-int virtio_pci_legacy_probe(struct pci_dev *pci_dev,
-   const struct pci_device_id *id)
+int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
 {
-   struct virtio_pci_device *vp_dev;
-   int err;
+   struct pci_dev *pci_dev = vp_dev-pci_dev;
 
/* We only own devices = 0x1000 and = 0x103f: leave the rest. */
if (pci_dev-device  0x1000 || pci_dev-device  0x103f)
@@ -239,41 +226,12 @@ int virtio_pci_legacy_probe(struct pci_dev *pci_dev,
return -ENODEV;

[PATCH post-squash 4/9] virtio_pci: macros for PCI layout offsets

2015-01-21 Thread Michael S. Tsirkin
From: Rusty Russell ru...@rustcorp.com.au

QEMU wants it, so why not?  Trust, but verify.

Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 include/uapi/linux/virtio_pci.h| 36 +++
 drivers/virtio/virtio_pci_modern.c | 60 +-
 2 files changed, 90 insertions(+), 6 deletions(-)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index a2b2e13..3b7e4d2 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -124,11 +124,6 @@ struct virtio_pci_cap {
__le32 length;  /* Length of the structure, in bytes. */
 };
 
-#define VIRTIO_PCI_CAP_BAR_SHIFT   5
-#define VIRTIO_PCI_CAP_BAR_MASK0x7
-#define VIRTIO_PCI_CAP_TYPE_SHIFT  0
-#define VIRTIO_PCI_CAP_TYPE_MASK   0x7
-
 struct virtio_pci_notify_cap {
struct virtio_pci_cap cap;
__le32 notify_off_multiplier;   /* Multiplier for queue_notify_off. */
@@ -160,6 +155,37 @@ struct virtio_pci_common_cfg {
__le32 queue_used_hi;   /* read-write */
 };
 
+/* Macro versions of offsets for the Old Timers! */
+#define VIRTIO_PCI_CAP_VNDR0
+#define VIRTIO_PCI_CAP_NEXT1
+#define VIRTIO_PCI_CAP_LEN 2
+#define VIRTIO_PCI_CAP_CFG_TYPE3
+#define VIRTIO_PCI_CAP_BAR 4
+#define VIRTIO_PCI_CAP_OFFSET  8
+#define VIRTIO_PCI_CAP_LENGTH  12
+
+#define VIRTIO_PCI_NOTIFY_CAP_MULT 16
+
+#define VIRTIO_PCI_COMMON_DFSELECT 0
+#define VIRTIO_PCI_COMMON_DF   4
+#define VIRTIO_PCI_COMMON_GFSELECT 8
+#define VIRTIO_PCI_COMMON_GF   12
+#define VIRTIO_PCI_COMMON_MSIX 16
+#define VIRTIO_PCI_COMMON_NUMQ 18
+#define VIRTIO_PCI_COMMON_STATUS   20
+#define VIRTIO_PCI_COMMON_CFGGENERATION21
+#define VIRTIO_PCI_COMMON_Q_SELECT 22
+#define VIRTIO_PCI_COMMON_Q_SIZE   24
+#define VIRTIO_PCI_COMMON_Q_MSIX   26
+#define VIRTIO_PCI_COMMON_Q_ENABLE 28
+#define VIRTIO_PCI_COMMON_Q_NOFF   30
+#define VIRTIO_PCI_COMMON_Q_DESCLO 32
+#define VIRTIO_PCI_COMMON_Q_DESCHI 36
+#define VIRTIO_PCI_COMMON_Q_AVAILLO40
+#define VIRTIO_PCI_COMMON_Q_AVAILHI44
+#define VIRTIO_PCI_COMMON_Q_USEDLO 48
+#define VIRTIO_PCI_COMMON_Q_USEDHI 52
+
 #endif /* VIRTIO_PCI_NO_MODERN */
 
 #endif
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index a3d8101..b2e707ad 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -464,9 +464,67 @@ static void virtio_pci_release_dev(struct device *_d)
kfree(vp_dev);
 }
 
-/* TODO: validate the ABI statically. */
+/* This is part of the ABI.  Don't screw with it. */
 static inline void check_offsets(void)
 {
+   /* Note: disk space was harmed in compilation of this function. */
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
+offsetof(struct virtio_pci_cap, cap_vndr));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
+offsetof(struct virtio_pci_cap, cap_next));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
+offsetof(struct virtio_pci_cap, cap_len));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
+offsetof(struct virtio_pci_cap, cfg_type));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
+offsetof(struct virtio_pci_cap, bar));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
+offsetof(struct virtio_pci_cap, offset));
+   BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
+offsetof(struct virtio_pci_cap, length));
+   BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
+offsetof(struct virtio_pci_notify_cap,
+ notify_off_multiplier));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
+offsetof(struct virtio_pci_common_cfg,
+ device_feature_select));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
+offsetof(struct virtio_pci_common_cfg, device_feature));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
+offsetof(struct virtio_pci_common_cfg,
+ guest_feature_select));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
+offsetof(struct virtio_pci_common_cfg, guest_feature));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
+offsetof(struct virtio_pci_common_cfg, msix_config));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
+offsetof(struct virtio_pci_common_cfg, num_queues));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
+offsetof(struct virtio_pci_common_cfg, device_status));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
+offsetof(struct virtio_pci_common_cfg, config_generation));
+   

[PATCH post-squash 6/9] virtio_pci_modern: support devices with no config

2015-01-21 Thread Michael S. Tsirkin
Virtio 1.0 spec lists device config as optional.
Set get/set callbacks to NULL. Drivers can check that
and fail gracefully.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_modern.c | 21 +++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index 0e54cc8..68ebc20 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -422,6 +422,21 @@ static void del_vq(struct virtio_pci_vq_info *info)
free_pages_exact(info-queue, vring_pci_size(info-num));
 }
 
+static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
+   .get= NULL,
+   .set= NULL,
+   .generation = vp_generation,
+   .get_status = vp_get_status,
+   .set_status = vp_set_status,
+   .reset  = vp_reset,
+   .find_vqs   = vp_modern_find_vqs,
+   .del_vqs= vp_del_vqs,
+   .get_features   = vp_get_features,
+   .finalize_features = vp_finalize_features,
+   .bus_name   = vp_bus_name,
+   .set_vq_affinity = vp_set_vq_affinity,
+};
+
 static const struct virtio_config_ops virtio_pci_config_ops = {
.get= vp_get,
.set= vp_set,
@@ -652,9 +667,11 @@ int virtio_pci_modern_probe(struct virtio_pci_device 
*vp_dev)
vp_dev-device_len);
if (!vp_dev-device)
goto err_map_device;
-   }
 
-   vp_dev-vdev.config = virtio_pci_config_ops;
+   vp_dev-vdev.config = virtio_pci_config_ops;
+   } else {
+   vp_dev-vdev.config = virtio_pci_config_nodev_ops;
+   }
 
vp_dev-config_vector = vp_config_vector;
vp_dev-setup_vq = setup_vq;
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH post-squash 7/9] virtio_pci: add an option to disable legacy driver

2015-01-21 Thread Michael S. Tsirkin
Useful for testing device virtio 1 compatibility.
Based on patch by Rusty - couldn't resist putting
that flying car joke in there!

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h | 10 ++
 drivers/virtio/Kconfig | 19 +++
 drivers/virtio/Makefile|  3 ++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index d391805..28ee4e5 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -147,8 +147,18 @@ const char *vp_bus_name(struct virtio_device *vdev);
  */
 int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
 
+#if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
 int virtio_pci_legacy_probe(struct virtio_pci_device *);
 void virtio_pci_legacy_remove(struct virtio_pci_device *);
+#else
+static inline int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
+{
+   return -ENODEV;
+}
+static inline void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev)
+{
+}
+#endif
 int virtio_pci_modern_probe(struct virtio_pci_device *);
 void virtio_pci_modern_remove(struct virtio_pci_device *);
 
diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 083fb45..b546da5 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -19,6 +19,25 @@ config VIRTIO_PCI
 
  If unsure, say M.
 
+config VIRTIO_PCI_LEGACY
+   bool Support for legacy virtio draft 0.9.X and older devices
+   default y
+   depends on VIRTIO_PCI
+   ---help---
+  Virtio PCI Card 0.9.X Draft (circa 2014) and older device support.
+
+ This option enables building a transitional driver, supporting
+ both devices conforming to Virtio 1 specification, and legacy devices.
+ If disabled, you get a slightly smaller, non-transitional driver,
+ with no legacy compatibility.
+
+  So look out into your driveway.  Do you have a flying car?  If
+  so, you can happily disable this option and virtio will not
+  break.  Otherwise, leave it set.  Unless you're testing what
+  life will be like in The Future.
+
+ If unsure, say Y.
+
 config VIRTIO_BALLOON
tristate Virtio balloon driver
depends on VIRTIO
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index bd230d1..d85565b 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -1,5 +1,6 @@
 obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
-virtio_pci-y := virtio_pci_modern.o virtio_pci_legacy.o virtio_pci_common.o
+virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o
+virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH post-squash 3/9] virtio_pci: modern driver

2015-01-21 Thread Michael S. Tsirkin
Lightly tested against qemu.

One thing *not* implemented here is separate mappings
for descriptor/avail/used rings. That's nice to have,
will be done later after we have core support.

Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  25 +-
 drivers/virtio/virtio_pci_common.c |  14 +-
 drivers/virtio/virtio_pci_modern.c | 587 +
 drivers/virtio/Makefile|   2 +-
 4 files changed, 621 insertions(+), 7 deletions(-)
 create mode 100644 drivers/virtio/virtio_pci_modern.c

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index 2b1e70d..610c43f 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -53,12 +53,29 @@ struct virtio_pci_device {
struct virtio_device vdev;
struct pci_dev *pci_dev;
 
+   /* In legacy mode, these two point to within -legacy. */
+   /* Where to read and clear interrupt */
+   u8 __iomem *isr;
+
+   /* Modern only fields */
+   /* The IO mapping for the PCI config space (non-legacy mode) */
+   struct virtio_pci_common_cfg __iomem *common;
+   /* Device-specific data (non-legacy mode)  */
+   void __iomem *device;
+
+   /* So we can sanity-check accesses. */
+   size_t device_len;
+
+   /* Capability for when we need to map notifications per-vq. */
+   int notify_map_cap;
+
+   /* Multiply queue_notify_off by this value. (non-legacy mode). */
+   u32 notify_offset_multiplier;
+
+   /* Legacy only field */
/* the IO mapping for the PCI config space */
void __iomem *ioaddr;
 
-   /* the IO mapping for ISR operation */
-   void __iomem *isr;
-
/* a list of queues so we can dispatch IRQs */
spinlock_t lock;
struct list_head virtqueues;
@@ -129,5 +146,7 @@ int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
 
 int virtio_pci_legacy_probe(struct virtio_pci_device *);
 void virtio_pci_legacy_remove(struct virtio_pci_device *);
+int virtio_pci_modern_probe(struct virtio_pci_device *);
+void virtio_pci_modern_remove(struct virtio_pci_device *);
 
 #endif
diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 457cbe2..8ae34a3 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -505,7 +505,9 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
if (rc)
goto err_request_regions;
 
-   rc = virtio_pci_legacy_probe(vp_dev);
+   rc = virtio_pci_modern_probe(vp_dev);
+   if (rc == -ENODEV)
+   rc = virtio_pci_legacy_probe(vp_dev);
if (rc)
goto err_probe;
 
@@ -518,7 +520,10 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
return 0;
 
 err_register:
-   virtio_pci_legacy_remove(vp_dev);
+   if (vp_dev-ioaddr)
+virtio_pci_legacy_remove(vp_dev);
+   else
+virtio_pci_modern_remove(vp_dev);
 err_probe:
pci_release_regions(pci_dev);
 err_request_regions:
@@ -534,7 +539,10 @@ static void virtio_pci_remove(struct pci_dev *pci_dev)
 
unregister_virtio_device(vp_dev-vdev);
 
-   virtio_pci_legacy_remove(pci_dev);
+   if (vp_dev-ioaddr)
+   virtio_pci_legacy_remove(vp_dev);
+   else
+   virtio_pci_modern_remove(vp_dev);
 
pci_release_regions(pci_dev);
pci_disable_device(pci_dev);
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
new file mode 100644
index 000..a3d8101
--- /dev/null
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -0,0 +1,587 @@
+/*
+ * Virtio PCI driver - modern (virtio 1.0) device support
+ *
+ * This module allows virtio devices to be used over a virtual PCI device.
+ * This can be used with QEMU based VMMs like KVM or Xen.
+ *
+ * Copyright IBM Corp. 2007
+ * Copyright Red Hat, Inc. 2014
+ *
+ * Authors:
+ *  Anthony Liguori  aligu...@us.ibm.com
+ *  Rusty Russell ru...@rustcorp.com.au
+ *  Michael S. Tsirkin m...@redhat.com
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#define VIRTIO_PCI_NO_LEGACY
+#include virtio_pci_common.h
+
+static void __iomem *map_capability(struct pci_dev *dev, int off,
+   size_t minlen,
+   u32 align,
+   u32 start, u32 size,
+   size_t *len)
+{
+   u8 bar;
+   u32 offset, length;
+   void __iomem *p;
+
+   pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
+bar),
+bar);
+   pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, 
offset),
+ 

[PATCH post-squash 2/9] virtio-pci: define layout for virtio 1.0

2015-01-21 Thread Michael S. Tsirkin
From: Rusty Russell ru...@rustcorp.com.au

Based on patches by Michael S. Tsirkin m...@redhat.com, but I found it
hard to follow so changed to use structures which are more
self-documenting.

Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 include/uapi/linux/virtio_pci.h | 63 +
 1 file changed, 63 insertions(+)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index 509d630..a2b2e13 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -99,4 +99,67 @@
 /* Vector value used to disable MSI for queue */
 #define VIRTIO_MSI_NO_VECTOR0x
 
+#ifndef VIRTIO_PCI_NO_MODERN
+
+/* IDs for different capabilities.  Must all exist. */
+
+/* Common configuration */
+#define VIRTIO_PCI_CAP_COMMON_CFG  1
+/* Notifications */
+#define VIRTIO_PCI_CAP_NOTIFY_CFG  2
+/* ISR access */
+#define VIRTIO_PCI_CAP_ISR_CFG 3
+/* Device specific confiuration */
+#define VIRTIO_PCI_CAP_DEVICE_CFG  4
+
+/* This is the PCI capability header: */
+struct virtio_pci_cap {
+   __u8 cap_vndr;  /* Generic PCI field: PCI_CAP_ID_VNDR */
+   __u8 cap_next;  /* Generic PCI field: next ptr. */
+   __u8 cap_len;   /* Generic PCI field: capability length */
+   __u8 cfg_type;  /* Identifies the structure. */
+   __u8 bar;   /* Where to find it. */
+   __u8 padding[3];/* Pad to full dword. */
+   __le32 offset;  /* Offset within bar. */
+   __le32 length;  /* Length of the structure, in bytes. */
+};
+
+#define VIRTIO_PCI_CAP_BAR_SHIFT   5
+#define VIRTIO_PCI_CAP_BAR_MASK0x7
+#define VIRTIO_PCI_CAP_TYPE_SHIFT  0
+#define VIRTIO_PCI_CAP_TYPE_MASK   0x7
+
+struct virtio_pci_notify_cap {
+   struct virtio_pci_cap cap;
+   __le32 notify_off_multiplier;   /* Multiplier for queue_notify_off. */
+};
+
+/* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
+struct virtio_pci_common_cfg {
+   /* About the whole device. */
+   __le32 device_feature_select;   /* read-write */
+   __le32 device_feature;  /* read-only */
+   __le32 guest_feature_select;/* read-write */
+   __le32 guest_feature;   /* read-write */
+   __le16 msix_config; /* read-write */
+   __le16 num_queues;  /* read-only */
+   __u8 device_status; /* read-write */
+   __u8 config_generation; /* read-only */
+
+   /* About a specific virtqueue. */
+   __le16 queue_select;/* read-write */
+   __le16 queue_size;  /* read-write, power of 2. */
+   __le16 queue_msix_vector;   /* read-write */
+   __le16 queue_enable;/* read-write */
+   __le16 queue_notify_off;/* read-only */
+   __le32 queue_desc_lo;   /* read-write */
+   __le32 queue_desc_hi;   /* read-write */
+   __le32 queue_avail_lo;  /* read-write */
+   __le32 queue_avail_hi;  /* read-write */
+   __le32 queue_used_lo;   /* read-write */
+   __le32 queue_used_hi;   /* read-write */
+};
+
+#endif /* VIRTIO_PCI_NO_MODERN */
+
 #endif
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH post-squash 5/9] virtio_pci_modern: reduce number of mappings

2015-01-21 Thread Michael S. Tsirkin
We don't know the # of VQs that drivers are going to use so it's hard to
predict how much memory we'll need to map. However, the relevant
capability does give us an upper limit.
If that's below a page, we can reduce the number of required
mappings by mapping it all once ahead of the time.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  3 ++
 drivers/virtio/virtio_pci_modern.c | 57 --
 2 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index 610c43f..d391805 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -62,8 +62,11 @@ struct virtio_pci_device {
struct virtio_pci_common_cfg __iomem *common;
/* Device-specific data (non-legacy mode)  */
void __iomem *device;
+   /* Base of vq notifications (non-legacy mode). */
+   void __iomem *notify_base;
 
/* So we can sanity-check accesses. */
+   size_t notify_len;
size_t device_len;
 
/* Capability for when we need to map notifications per-vq. */
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index b2e707ad..0e54cc8 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -330,10 +330,26 @@ static struct virtqueue *setup_vq(struct 
virtio_pci_device *vp_dev,
iowrite64_twopart(virt_to_phys(virtqueue_get_used(vq)),
  cfg-queue_used_lo, cfg-queue_used_hi);
 
-   vq-priv = (void __force *)map_capability(vp_dev-pci_dev,
- vp_dev-notify_map_cap, 2, 2,
- off * vp_dev-notify_offset_multiplier, 2,
- NULL);
+   if (vp_dev-notify_base) {
+   /* offset should not wrap */
+   if ((u64)off * vp_dev-notify_offset_multiplier + 2
+vp_dev-notify_len) {
+   dev_warn(vp_dev-pci_dev-dev,
+bad notification offset %u (x %u) 
+for queue %u  %zd,
+off, vp_dev-notify_offset_multiplier,
+index, vp_dev-notify_len);
+   err = -EINVAL;
+   goto err_map_notify;
+   }
+   vq-priv = (void __force *)vp_dev-notify_base +
+   off * vp_dev-notify_offset_multiplier;
+   } else {
+   vq-priv = (void __force *)map_capability(vp_dev-pci_dev,
+ vp_dev-notify_map_cap, 2, 2,
+ off * 
vp_dev-notify_offset_multiplier, 2,
+ NULL);
+   }
 
if (!vq-priv) {
err = -ENOMEM;
@@ -352,7 +368,8 @@ static struct virtqueue *setup_vq(struct virtio_pci_device 
*vp_dev,
return vq;
 
 err_assign_vector:
-   pci_iounmap(vp_dev-pci_dev, (void __iomem __force *)vq-priv);
+   if (!vp_dev-notify_base)
+   pci_iounmap(vp_dev-pci_dev, (void __iomem __force *)vq-priv);
 err_map_notify:
vring_del_virtqueue(vq);
 err_new_queue:
@@ -397,7 +414,8 @@ static void del_vq(struct virtio_pci_vq_info *info)
ioread16(vp_dev-common-queue_msix_vector);
}
 
-   pci_iounmap(vp_dev-pci_dev, (void __force __iomem *)vq-priv);
+   if (!vp_dev-notify_base)
+   pci_iounmap(vp_dev-pci_dev, (void __force __iomem *)vq-priv);
 
vring_del_virtqueue(vq);
 
@@ -533,6 +551,7 @@ int virtio_pci_modern_probe(struct virtio_pci_device 
*vp_dev)
struct pci_dev *pci_dev = vp_dev-pci_dev;
int err, common, isr, notify, device;
u32 notify_length;
+   u32 notify_offset;
 
check_offsets();
 
@@ -599,13 +618,30 @@ int virtio_pci_modern_probe(struct virtio_pci_device 
*vp_dev)
  notify + offsetof(struct virtio_pci_notify_cap,
notify_off_multiplier),
  vp_dev-notify_offset_multiplier);
-   /* Read notify length from config space. */
+   /* Read notify length and offset from config space. */
pci_read_config_dword(pci_dev,
  notify + offsetof(struct virtio_pci_notify_cap,
cap.length),
  notify_length);
 
-   vp_dev-notify_map_cap = notify;
+   pci_read_config_dword(pci_dev,
+ notify + offsetof(struct virtio_pci_notify_cap,
+   cap.length),
+ notify_offset);
+
+   /* We don't know how many VQs we'll map, ahead of the time.
+* 

[PATCH post-squash 9/9] virtio_pci_modern: drop an unused function

2015-01-21 Thread Michael S. Tsirkin
release function in modern driver is unused:
it's a left-over from when each driver had
to have its own release.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_modern.c | 8 
 1 file changed, 8 deletions(-)

diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index 68ebc20..f16e462 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -489,14 +489,6 @@ static inline int virtio_pci_find_capability(struct 
pci_dev *dev, u8 cfg_type,
return 0;
 }
 
-static void virtio_pci_release_dev(struct device *_d)
-{
-   struct virtio_device *vdev = dev_to_virtio(_d);
-   struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-
-   kfree(vp_dev);
-}
-
 /* This is part of the ABI.  Don't screw with it. */
 static inline void check_offsets(void)
 {
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH post-squash 8/9] virtio_pci: add module param to force legacy mode

2015-01-21 Thread Michael S. Tsirkin
If set, try legacy interface first, modern one if that fails.  Useful to
work around device/driver bugs, and for compatibility testing.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
Tested-by: Gerd Hoffmann kra...@redhat.com
---
 drivers/virtio/virtio_pci_common.c | 25 +
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 8ae34a3..e894eb2 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -19,6 +19,14 @@
 
 #include virtio_pci_common.h
 
+static bool force_legacy = false;
+
+#if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
+module_param(force_legacy, bool, 0444);
+MODULE_PARM_DESC(force_legacy,
+Force legacy mode for transitional virtio 1 devices);
+#endif
+
 /* wait for pending irq handlers */
 void vp_synchronize_vectors(struct virtio_device *vdev)
 {
@@ -505,11 +513,20 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
if (rc)
goto err_request_regions;
 
-   rc = virtio_pci_modern_probe(vp_dev);
-   if (rc == -ENODEV)
+   if (force_legacy) {
rc = virtio_pci_legacy_probe(vp_dev);
-   if (rc)
-   goto err_probe;
+   /* Also try modern mode if we can't map BAR0 (no IO space). */
+   if (rc == -ENODEV || rc == -ENOMEM)
+   rc = virtio_pci_modern_probe(vp_dev);
+   if (rc)
+   goto err_probe;
+   } else {
+   rc = virtio_pci_modern_probe(vp_dev);
+   if (rc == -ENODEV)
+   rc = virtio_pci_legacy_probe(vp_dev);
+   if (rc)
+   goto err_probe;
+   }
 
pci_set_master(pci_dev);
 
-- 
MST

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [Qemu-devel] [PATCH RFC v6 10/20] s390x/virtio-ccw: add virtio set-revision call

2015-01-21 Thread Cornelia Huck
On Wed, 21 Jan 2015 12:51:41 +0100
Thomas Huth th...@linux.vnet.ibm.com wrote:

 On Wed, 21 Jan 2015 12:23:18 +0100
 Cornelia Huck cornelia.h...@de.ibm.com wrote:
 
  On Tue, 20 Jan 2015 11:08:24 +
  Stefan Hajnoczi stefa...@gmail.com wrote:
  
   On Thu, Dec 11, 2014 at 02:25:12PM +0100, Cornelia Huck wrote:
@@ -608,6 +631,25 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
 }
 }
 break;
+case CCW_CMD_SET_VIRTIO_REV:
+len = sizeof(revinfo);
+if (ccw.count  len || (check_len  ccw.count  len)) {
+ret = -EINVAL;
+break;
+}
+if (!ccw.cda) {
+ret = -EFAULT;
+break;
+}
+cpu_physical_memory_read(ccw.cda, revinfo, len);
+if (dev-revision = 0 ||
+revinfo.revision  virtio_ccw_rev_max(dev)) {
   
   In the next patch virtio_ccw_handle_set_vq() uses big-endian memory
   access functions to load a struct from guest memory.
   
   Here you just copy the struct in without byteswaps.
   
   Are the byteswaps missing here?  (I guess this normally runs big-endian
   guests on big-endian hosts so it's not noticable.)
  
  Indeed, these are supposed to be big-endian. I'll double check the
  other payloads.
 
 Right. Cornelia, can you take care of this or shall I rework the patch?

Currently already working on a patch :)

 NB: Actually, there are a couple of XXX config space endianness
 comments in that virtio_ccw_cb() function, so there are likely a bunch
 of problems when this code should be run on little endian hosts one day.
 So far, this code only runs with big-endian guests on big-endian hosts
 since the virtio-ccw machine is currently KVM-only as far as I know,
 that's likely why nobody complained about this yet.

The transport can't take care of the config space endianness, this
needs to be done by the individual devices. Probably best to simply
ditch the comments.

___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH v3 00/16] virtio-pci: towards virtio 1.0 guest support

2015-01-21 Thread Michael S. Tsirkin
On Wed, Jan 21, 2015 at 02:43:11PM +0100, Gerd Hoffmann wrote:
 On Mi, 2015-01-21 at 13:36 +0200, Michael S. Tsirkin wrote:
  Want to respond to the patches with a tested-by tag?
 
 Done.
 
 Making multiplier smaller isn't in there yet, right?
 
 cheers,
   Gerd

No - but I'll make sure to address this before I
post a new version.

-- 
MST
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH v3 00/16] virtio-pci: towards virtio 1.0 guest support

2015-01-21 Thread Gerd Hoffmann
On Mi, 2015-01-21 at 13:36 +0200, Michael S. Tsirkin wrote:
 Want to respond to the patches with a tested-by tag?

Done.

Making multiplier smaller isn't in there yet, right?

cheers,
  Gerd


___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [pciutils patch] add virtio vendor capability support

2015-01-21 Thread Gerd Hoffmann
On Mi, 2015-01-21 at 15:57 +0100, Martin Mares wrote:
 Hello!
 
  virtio uses vendor-specific capabilities to specify the location of
  the virtio register ranges.  The specification can be found here:
  
  http://docs.oasis-open.org/virtio/virtio/v1.0/cs01/virtio-v1.0-cs01.html#x1-690004
  
  This patch adds support for decoding these capabilities to lspci.
 
 I like the patch, except for a couple of details:
 
 (1) Please follow the coding style of the rest of pciutils.

I've tried.  What specifically doesn't match?

 (2) You assume that PCI_CAP_ID_VNDR of all Redhat devices contains virtio,
 but the comment nearby refers to a range of device IDs only.

I'll add a check.  Doesn't matter today, there are no other redhat
devices with vendor caps, so I was a bit lazy.  But who knows what
happens in the future ...

 (3) Moving code related to vendor-defined caps to a separate file sounds
 good, but I think we should push the boundary a bit further: let the
 main switch in ls-caps.c call a function from ls-caps-vendor.c as soon
 as it finds PCI_CAP_ID_VENDOR, leaving all decisions based on
 vendor/device ID to this function.

Makes sense indeed, will do.

cheers,
  Gerd



___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization