Re: [PATCH 9/9] virtio_pci: update file descriptions and copyright

2014-12-15 Thread Rusty Russell
Michael S. Tsirkin m...@redhat.com writes:
 There's been a lot of changes since 2007.
 List main authors, add Red Hat copyright.

Acked-by: Rusty Russell ru...@rustcorp.com.au

Cheers,
Rusty.

 Signed-off-by: Michael S. Tsirkin m...@redhat.com
 ---
  drivers/virtio/virtio_pci.h| 5 -
  drivers/virtio/virtio_pci.c| 5 -
  drivers/virtio/virtio_pci_legacy.c | 5 -
  3 files changed, 12 insertions(+), 3 deletions(-)

 diff --git a/drivers/virtio/virtio_pci.h b/drivers/virtio/virtio_pci.h
 index a3b1259..fba383c 100644
 --- a/drivers/virtio/virtio_pci.h
 +++ b/drivers/virtio/virtio_pci.h
 @@ -1,15 +1,18 @@
  #ifndef _DRIVERS_VIRTIO_VIRTIO_PCI_H
  #define _DRIVERS_VIRTIO_VIRTIO_PCI_H
  /*
 - * Virtio PCI driver
 + * Virtio PCI driver - APIs for common functionality for all device versions
   *
   * 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.
 diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci.c
 index 6c7e186..c263f67 100644
 --- a/drivers/virtio/virtio_pci.c
 +++ b/drivers/virtio/virtio_pci.c
 @@ -1,13 +1,16 @@
  /*
 - * Virtio PCI driver
 + * Virtio PCI driver - common functionality for all device versions
   *
   * 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.
 diff --git a/drivers/virtio/virtio_pci_legacy.c 
 b/drivers/virtio/virtio_pci_legacy.c
 index 9fdec9a..1ca4422 100644
 --- a/drivers/virtio/virtio_pci_legacy.c
 +++ b/drivers/virtio/virtio_pci_legacy.c
 @@ -1,13 +1,16 @@
  /*
 - * Virtio PCI driver
 + * Virtio PCI driver - legacy 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.
 -- 
 MST
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


[PATCH 0/3] fix up vringh/mic sparse errors

2014-12-15 Thread Michael S. Tsirkin
This fixes remaining sparse warnings in vringh and mic by using
virtio 1.0 compliant wrappers.

This also needs by get_user patches to avoid getting warnings
from these calls.

Tested by running vringh_test.

Rusty, I prefer fixing all these warnings for 3.19, any objections?

Michael S. Tsirkin (3):
  vringh: 64 bit features
  vringh: initial virtio 1.0 support
  mic/host: initial virtio 1.0 support

 include/linux/vringh.h  |  37 ++-
 drivers/misc/mic/host/mic_debugfs.c |  18 --
 drivers/vhost/vringh.c  | 125 ++--
 3 files changed, 123 insertions(+), 57 deletions(-)

-- 
MST

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


[PATCH 3/3] mic/host: initial virtio 1.0 support

2014-12-15 Thread Michael S. Tsirkin
This just makes code sparse-clean.
The new feature bit is not yet negotiated.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 drivers/misc/mic/host/mic_debugfs.c | 18 --
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/misc/mic/host/mic_debugfs.c 
b/drivers/misc/mic/host/mic_debugfs.c
index 028ba5d6..687e9aa 100644
--- a/drivers/misc/mic/host/mic_debugfs.c
+++ b/drivers/misc/mic/host/mic_debugfs.c
@@ -326,21 +326,27 @@ static int mic_vdev_info_show(struct seq_file *s, void 
*unused)
}
avail = vrh-vring.avail;
seq_printf(s, avail flags 0x%x idx %d\n,
-  avail-flags, avail-idx  (num - 1));
+  vringh16_to_cpu(vrh, avail-flags),
+  vringh16_to_cpu(vrh, avail-idx)  (num - 
1));
seq_printf(s, avail flags 0x%x idx %d\n,
-  avail-flags, avail-idx);
+  vringh16_to_cpu(vrh, avail-flags),
+  vringh16_to_cpu(vrh, avail-idx));
for (j = 0; j  num; j++)
seq_printf(s, avail ring[%d] %d\n,
   j, avail-ring[j]);
used = vrh-vring.used;
seq_printf(s, used flags 0x%x idx %d\n,
-  used-flags, used-idx  (num - 1));
+  vringh16_to_cpu(vrh, used-flags),
+  vringh16_to_cpu(vrh, used-idx)  (num - 1));
seq_printf(s, used flags 0x%x idx %d\n,
-  used-flags, used-idx);
+  vringh16_to_cpu(vrh, used-flags),
+  vringh16_to_cpu(vrh, used-idx));
for (j = 0; j  num; j++)
seq_printf(s, used ring[%d] id %d len %d\n,
-  j, used-ring[j].id,
-  used-ring[j].len);
+  j, vringh32_to_cpu(vrh,
+ used-ring[j].id),
+  vringh32_to_cpu(vrh,
+  used-ring[j].len));
}
}
mutex_unlock(mdev-mic_mutex);
-- 
MST

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


[PATCH 2/3] vringh: initial virtio 1.0 support

2014-12-15 Thread Michael S. Tsirkin
Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 include/linux/vringh.h |  33 ++
 drivers/vhost/vringh.c | 121 ++---
 2 files changed, 107 insertions(+), 47 deletions(-)

diff --git a/include/linux/vringh.h b/include/linux/vringh.h
index f696dd0..a3fa537 100644
--- a/include/linux/vringh.h
+++ b/include/linux/vringh.h
@@ -24,12 +24,16 @@
 #ifndef _LINUX_VRINGH_H
 #define _LINUX_VRINGH_H
 #include uapi/linux/virtio_ring.h
+#include linux/virtio_byteorder.h
 #include linux/uio.h
 #include linux/slab.h
 #include asm/barrier.h
 
 /* virtio_ring with information needed for host access. */
 struct vringh {
+   /* Everything is little endian */
+   bool little_endian;
+
/* Guest publishes used event idx (note: we always do). */
bool event_indices;
 
@@ -222,4 +226,33 @@ static inline void vringh_notify(struct vringh *vrh)
vrh-notify(vrh);
 }
 
+static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
+{
+   return __virtio16_to_cpu(vrh-little_endian, val);
+}
+
+static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
+{
+   return __cpu_to_virtio16(vrh-little_endian, val);
+}
+
+static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
+{
+   return __virtio32_to_cpu(vrh-little_endian, val);
+}
+
+static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
+{
+   return __cpu_to_virtio32(vrh-little_endian, val);
+}
+
+static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
+{
+   return __virtio64_to_cpu(vrh-little_endian, val);
+}
+
+static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
+{
+   return __cpu_to_virtio64(vrh-little_endian, val);
+}
 #endif /* _LINUX_VRINGH_H */
diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
index ac3fe27..3bb02c6 100644
--- a/drivers/vhost/vringh.c
+++ b/drivers/vhost/vringh.c
@@ -11,6 +11,7 @@
 #include linux/uaccess.h
 #include linux/slab.h
 #include linux/export.h
+#include uapi/linux/virtio_config.h
 
 static __printf(1,2) __cold void vringh_bad(const char *fmt, ...)
 {
@@ -28,13 +29,14 @@ static __printf(1,2) __cold void vringh_bad(const char 
*fmt, ...)
 
 /* Returns vring-num if empty, -ve on error. */
 static inline int __vringh_get_head(const struct vringh *vrh,
-   int (*getu16)(u16 *val, const u16 *p),
+   int (*getu16)(const struct vringh *vrh,
+ u16 *val, const __virtio16 
*p),
u16 *last_avail_idx)
 {
u16 avail_idx, i, head;
int err;
 
-   err = getu16(avail_idx, vrh-vring.avail-idx);
+   err = getu16(vrh, avail_idx, vrh-vring.avail-idx);
if (err) {
vringh_bad(Failed to access avail idx at %p,
   vrh-vring.avail-idx);
@@ -49,7 +51,7 @@ static inline int __vringh_get_head(const struct vringh *vrh,
 
i = *last_avail_idx  (vrh-vring.num - 1);
 
-   err = getu16(head, vrh-vring.avail-ring[i]);
+   err = getu16(vrh, head, vrh-vring.avail-ring[i]);
if (err) {
vringh_bad(Failed to read head: idx %d address %p,
   *last_avail_idx, vrh-vring.avail-ring[i]);
@@ -144,28 +146,32 @@ static inline bool no_range_check(struct vringh *vrh, u64 
addr, size_t *len,
 }
 
 /* No reason for this code to be inline. */
-static int move_to_indirect(int *up_next, u16 *i, void *addr,
+static int move_to_indirect(const struct vringh *vrh,
+   int *up_next, u16 *i, void *addr,
const struct vring_desc *desc,
struct vring_desc **descs, int *desc_max)
 {
+   u32 len;
+
/* Indirect tables can't have indirect. */
if (*up_next != -1) {
vringh_bad(Multilevel indirect %u-%u, *up_next, *i);
return -EINVAL;
}
 
-   if (unlikely(desc-len % sizeof(struct vring_desc))) {
+   len = vringh32_to_cpu(vrh, desc-len);
+   if (unlikely(len % sizeof(struct vring_desc))) {
vringh_bad(Strange indirect len %u, desc-len);
return -EINVAL;
}
 
/* We will check this when we follow it! */
-   if (desc-flags  VRING_DESC_F_NEXT)
-   *up_next = desc-next;
+   if (desc-flags  cpu_to_vringh16(vrh, VRING_DESC_F_NEXT))
+   *up_next = vringh16_to_cpu(vrh, desc-next);
else
*up_next = -2;
*descs = addr;
-   *desc_max = desc-len / sizeof(struct vring_desc);
+   *desc_max = len / sizeof(struct vring_desc);
 
/* Now, start at the first indirect. */
*i = 0;
@@ -287,22 +293,25 @@ __vringh_iov(struct vringh *vrh, u16 i,
if (unlikely(err))
goto fail;
 
-   if (unlikely(desc.flags 

[PATCH 1/3] vringh: 64 bit features

2014-12-15 Thread Michael S. Tsirkin
Pass u64 everywhere.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 include/linux/vringh.h | 4 ++--
 drivers/vhost/vringh.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/linux/vringh.h b/include/linux/vringh.h
index 749cde2..f696dd0 100644
--- a/include/linux/vringh.h
+++ b/include/linux/vringh.h
@@ -105,7 +105,7 @@ struct vringh_kiov {
 #define VRINGH_IOV_ALLOCATED 0x800
 
 /* Helpers for userspace vrings. */
-int vringh_init_user(struct vringh *vrh, u32 features,
+int vringh_init_user(struct vringh *vrh, u64 features,
 unsigned int num, bool weak_barriers,
 struct vring_desc __user *desc,
 struct vring_avail __user *avail,
@@ -167,7 +167,7 @@ bool vringh_notify_enable_user(struct vringh *vrh);
 void vringh_notify_disable_user(struct vringh *vrh);
 
 /* Helpers for kernelspace vrings. */
-int vringh_init_kern(struct vringh *vrh, u32 features,
+int vringh_init_kern(struct vringh *vrh, u64 features,
 unsigned int num, bool weak_barriers,
 struct vring_desc *desc,
 struct vring_avail *avail,
diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c
index 5174eba..ac3fe27 100644
--- a/drivers/vhost/vringh.c
+++ b/drivers/vhost/vringh.c
@@ -577,7 +577,7 @@ static inline int xfer_to_user(void *dst, void *src, size_t 
len)
  * Returns an error if num is invalid: you should check pointers
  * yourself!
  */
-int vringh_init_user(struct vringh *vrh, u32 features,
+int vringh_init_user(struct vringh *vrh, u64 features,
 unsigned int num, bool weak_barriers,
 struct vring_desc __user *desc,
 struct vring_avail __user *avail,
@@ -836,7 +836,7 @@ static inline int xfer_kern(void *src, void *dst, size_t 
len)
  *
  * Returns an error if num is invalid.
  */
-int vringh_init_kern(struct vringh *vrh, u32 features,
+int vringh_init_kern(struct vringh *vrh, u64 features,
 unsigned int num, bool weak_barriers,
 struct vring_desc *desc,
 struct vring_avail *avail,
-- 
MST

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


[PATCH 0/6] virtio 1.0 fixups, tweaks

2014-12-15 Thread Michael S. Tsirkin
Fixes a couple of minor compliance issues in new virtio 1.0 code.
Plus, adds a couple of minor cleanups - not bugfixes,
but seem safe enough for 3.19.

Michael S. Tsirkin (6):
  virtio: set VIRTIO_CONFIG_S_FEATURES_OK on restore
  virtio_config: fix virtio_cread_bytes
  virtio_pci_common.h: drop VIRTIO_PCI_NO_LEGACY
  virtio_pci: move probe to common file
  virtio_pci: add VIRTIO_PCI_NO_LEGACY
  virtio: core support for config generation

 drivers/virtio/virtio_pci_common.h |  7 +++
 include/linux/virtio_config.h  | 29 -
 include/uapi/linux/virtio_pci.h| 15 ++-
 drivers/virtio/virtio.c| 37 +++--
 drivers/virtio/virtio_pci_common.c | 34 +-
 drivers/virtio/virtio_pci_legacy.c | 24 ++--
 6 files changed, 99 insertions(+), 47 deletions(-)

-- 
MST

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


[PATCH 2/6] virtio_config: fix virtio_cread_bytes

2014-12-15 Thread Michael S. Tsirkin
virtio_cread_bytes is implemented incorrectly in case length happens to
be 2,4 or 8 bytes: transports and devices will assume it's an integer
value that has to be converted to LE format.

Let's just do multiple 1-byte reads: this also makes life easier
for transports who only need to implement 1,2,4 and 8 byte reads.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 include/linux/virtio_config.h | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
index 7979f85..a61cd37 100644
--- a/include/linux/virtio_config.h
+++ b/include/linux/virtio_config.h
@@ -305,7 +305,10 @@ static inline void virtio_cread_bytes(struct virtio_device 
*vdev,
  unsigned int offset,
  void *buf, size_t len)
 {
-   vdev-config-get(vdev, offset, buf, len);
+   int i;
+
+   for (i = 0; i  len; i++)
+   vdev-config-get(vdev, offset + i, buf + i, 1);
 }
 
 static inline void virtio_cwrite8(struct virtio_device *vdev,
-- 
MST

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


[PATCH 1/6] virtio: set VIRTIO_CONFIG_S_FEATURES_OK on restore

2014-12-15 Thread Michael S. Tsirkin
virtio 1.0 devices require that drivers set VIRTIO_CONFIG_S_FEATURES_OK
after finalizing features.
virtio core missed doing this on restore, fix it up.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Reviewed-by: Cornelia Huck cornelia.h...@de.ibm.com
---
 drivers/virtio/virtio.c | 37 +++--
 1 file changed, 23 insertions(+), 14 deletions(-)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index f226658..b9f70df 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -162,6 +162,27 @@ static void virtio_config_enable(struct virtio_device *dev)
spin_unlock_irq(dev-config_lock);
 }
 
+static int virtio_finalize_features(struct virtio_device *dev)
+{
+   int ret = dev-config-finalize_features(dev);
+   unsigned status;
+
+   if (ret)
+   return ret;
+
+   if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
+   return 0;
+
+   add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
+   status = dev-config-get_status(dev);
+   if (!(status  VIRTIO_CONFIG_S_FEATURES_OK)) {
+   dev_err(dev-dev, virtio: device refuses features: %x\n,
+   status);
+   return -ENODEV;
+   }
+   return 0;
+}
+
 static int virtio_dev_probe(struct device *_d)
 {
int err, i;
@@ -170,7 +191,6 @@ static int virtio_dev_probe(struct device *_d)
u64 device_features;
u64 driver_features;
u64 driver_features_legacy;
-   unsigned status;
 
/* We have a driver! */
add_status(dev, VIRTIO_CONFIG_S_DRIVER);
@@ -208,21 +228,10 @@ static int virtio_dev_probe(struct device *_d)
if (device_features  (1ULL  i))
__virtio_set_bit(dev, i);
 
-   err = dev-config-finalize_features(dev);
+   err = virtio_finalize_features(dev);
if (err)
goto err;
 
-   if (virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
-   add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
-   status = dev-config-get_status(dev);
-   if (!(status  VIRTIO_CONFIG_S_FEATURES_OK)) {
-   dev_err(_d, virtio: device refuses features: %x\n,
-  status);
-   err = -ENODEV;
-   goto err;
-   }
-   }
-
err = drv-probe(dev);
if (err)
goto err;
@@ -372,7 +381,7 @@ int virtio_device_restore(struct virtio_device *dev)
/* We have a driver! */
add_status(dev, VIRTIO_CONFIG_S_DRIVER);
 
-   ret = dev-config-finalize_features(dev);
+   ret = virtio_finalize_features(dev);
if (ret)
goto err;
 
-- 
MST

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


[PATCH 3/6] virtio_pci_common.h: drop VIRTIO_PCI_NO_LEGACY

2014-12-15 Thread Michael S. Tsirkin
Legacy drivers use virtio_pci_common.h too, we should not
define VIRTIO_PCI_NO_LEGACY there.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 drivers/virtio/virtio_pci_common.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index d840dad..38d99ad 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -27,7 +27,6 @@
 #include linux/virtio.h
 #include linux/virtio_config.h
 #include linux/virtio_ring.h
-#define VIRTIO_PCI_NO_LEGACY
 #include linux/virtio_pci.h
 #include linux/highmem.h
 #include linux/spinlock.h
-- 
MST

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


[PATCH 4/6] virtio_pci: move probe to common file

2014-12-15 Thread Michael S. Tsirkin
It turns out this make everything easier.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  6 +++---
 drivers/virtio/virtio_pci_common.c | 34 +-
 drivers/virtio/virtio_pci_legacy.c | 24 ++--
 3 files changed, 38 insertions(+), 26 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.h 
b/drivers/virtio/virtio_pci_common.h
index 38d99ad..adddb64 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -128,8 +128,8 @@ const char *vp_bus_name(struct virtio_device *vdev);
 int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
 void virtio_pci_release_dev(struct device *);
 
-#ifdef CONFIG_PM_SLEEP
-extern const struct dev_pm_ops virtio_pci_pm_ops;
-#endif
+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);
 
 #endif
diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 953057d..59d3685 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -458,7 +458,39 @@ static int virtio_pci_restore(struct device *dev)
return virtio_device_restore(vp_dev-vdev);
 }
 
-const struct dev_pm_ops virtio_pci_pm_ops = {
+static const struct dev_pm_ops virtio_pci_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
 };
 #endif
+
+
+/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
+static const struct pci_device_id virtio_pci_id_table[] = {
+   { PCI_DEVICE(0x1af4, PCI_ANY_ID) },
+   { 0 }
+};
+
+MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
+
+static int virtio_pci_probe(struct pci_dev *pci_dev,
+   const struct pci_device_id *id)
+{
+   return virtio_pci_legacy_probe(pci_dev, id);
+}
+
+static void virtio_pci_remove(struct pci_dev *pci_dev)
+{
+ virtio_pci_legacy_remove(pci_dev);
+}
+
+static struct pci_driver virtio_pci_driver = {
+   .name   = virtio-pci,
+   .id_table   = virtio_pci_id_table,
+   .probe  = virtio_pci_probe,
+   .remove = virtio_pci_remove,
+#ifdef CONFIG_PM_SLEEP
+   .driver.pm  = virtio_pci_pm_ops,
+#endif
+};
+
+module_pci_driver(virtio_pci_driver);
diff --git a/drivers/virtio/virtio_pci_legacy.c 
b/drivers/virtio/virtio_pci_legacy.c
index 2588252..6c76f0f 100644
--- a/drivers/virtio/virtio_pci_legacy.c
+++ b/drivers/virtio/virtio_pci_legacy.c
@@ -19,14 +19,6 @@
 
 #include virtio_pci_common.h
 
-/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
-static const struct pci_device_id virtio_pci_id_table[] = {
-   { PCI_DEVICE(0x1af4, PCI_ANY_ID) },
-   { 0 }
-};
-
-MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
-
 /* virtio config-get_features() implementation */
 static u64 vp_get_features(struct virtio_device *vdev)
 {
@@ -220,7 +212,7 @@ static const struct virtio_config_ops virtio_pci_config_ops 
= {
 };
 
 /* the PCI probing function */
-static int virtio_pci_probe(struct pci_dev *pci_dev,
+int virtio_pci_legacy_probe(struct pci_dev *pci_dev,
const struct pci_device_id *id)
 {
struct virtio_pci_device *vp_dev;
@@ -300,7 +292,7 @@ out:
return err;
 }
 
-static void virtio_pci_remove(struct pci_dev *pci_dev)
+void virtio_pci_legacy_remove(struct pci_dev *pci_dev)
 {
struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
 
@@ -312,15 +304,3 @@ static void virtio_pci_remove(struct pci_dev *pci_dev)
pci_disable_device(pci_dev);
kfree(vp_dev);
 }
-
-static struct pci_driver virtio_pci_driver = {
-   .name   = virtio-pci,
-   .id_table   = virtio_pci_id_table,
-   .probe  = virtio_pci_probe,
-   .remove = virtio_pci_remove,
-#ifdef CONFIG_PM_SLEEP
-   .driver.pm  = virtio_pci_pm_ops,
-#endif
-};
-
-module_pci_driver(virtio_pci_driver);
-- 
MST

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


[PATCH 5/6] virtio_pci: add VIRTIO_PCI_NO_LEGACY

2014-12-15 Thread Michael S. Tsirkin
Add macro to disable all legacy register defines.
Helpful to make sure legacy macros don't leak
through into modern code.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 include/uapi/linux/virtio_pci.h | 15 ++-
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index e5ec1ca..35b552c 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -41,6 +41,8 @@
 
 #include linux/virtio_config.h
 
+#ifndef VIRTIO_PCI_NO_LEGACY
+
 /* A 32-bit r/o bitmask of the features supported by the host */
 #define VIRTIO_PCI_HOST_FEATURES   0
 
@@ -67,16 +69,11 @@
  * a read-and-acknowledge. */
 #define VIRTIO_PCI_ISR 19
 
-/* The bit of the ISR which indicates a device configuration change. */
-#define VIRTIO_PCI_ISR_CONFIG  0x2
-
 /* MSI-X registers: only enabled if MSI-X is enabled. */
 /* A 16-bit vector for configuration changes. */
 #define VIRTIO_MSI_CONFIG_VECTOR20
 /* A 16-bit vector for selected queue notifications. */
 #define VIRTIO_MSI_QUEUE_VECTOR 22
-/* Vector value used to disable MSI for queue */
-#define VIRTIO_MSI_NO_VECTOR0x
 
 /* The remaining space is defined by each driver as the per-driver
  * configuration space */
@@ -94,4 +91,12 @@
 /* The alignment to use between consumer and producer parts of vring.
  * x86 pagesize again. */
 #define VIRTIO_PCI_VRING_ALIGN 4096
+
+#endif /* VIRTIO_PCI_NO_LEGACY */
+
+/* The bit of the ISR which indicates a device configuration change. */
+#define VIRTIO_PCI_ISR_CONFIG  0x2
+/* Vector value used to disable MSI for queue */
+#define VIRTIO_MSI_NO_VECTOR0x
+
 #endif
-- 
MST

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


[PATCH 6/6] virtio: core support for config generation

2014-12-15 Thread Michael S. Tsirkin
virtio 1.0 spec says:

Drivers MUST NOT assume reads from fields greater than 32 bits wide are
atomic, nor are reads from multiple fields: drivers SHOULD read device
configuration space fields like so:
u32 before, after;
do {
before = get_config_generation(device);
// read config entry/entries.
after = get_config_generation(device);
} while (after != before);

Do exactly this, for transports that support it.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 include/linux/virtio_config.h | 32 
 1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
index a61cd37..ca3ed78 100644
--- a/include/linux/virtio_config.h
+++ b/include/linux/virtio_config.h
@@ -19,6 +19,9 @@
  * offset: the offset of the configuration field
  * buf: the buffer to read the field value from.
  * len: the length of the buffer
+ * @generation: config generation counter
+ * vdev: the virtio_device
+ * Returns the config generation counter
  * @get_status: read the status byte
  * vdev: the virtio_device
  * Returns the status byte
@@ -60,6 +63,7 @@ struct virtio_config_ops {
void *buf, unsigned len);
void (*set)(struct virtio_device *vdev, unsigned offset,
const void *buf, unsigned len);
+   u32 (*generation)(struct virtio_device *vdev);
u8 (*get_status)(struct virtio_device *vdev);
void (*set_status)(struct virtio_device *vdev, u8 status);
void (*reset)(struct virtio_device *vdev);
@@ -301,14 +305,33 @@ static inline u8 virtio_cread8(struct virtio_device 
*vdev, unsigned int offset)
return ret;
 }
 
+/* Read @count fields, @bytes each. */
+static inline void __virtio_cread_many(struct virtio_device *vdev,
+  unsigned int offset,
+  void *buf, size_t count, size_t bytes)
+{
+   u32 old, gen = vdev-config-generation ?
+   vdev-config-generation(vdev) : 0;
+   int i;
+
+   do {
+   old = gen;
+
+   for (i = 0; i  count; i++)
+   vdev-config-get(vdev, offset + bytes * i,
+ buf + i * bytes, bytes);
+
+   gen = vdev-config-generation ?
+   vdev-config-generation(vdev) : 0;
+   } while (gen != old);
+}
+
+
 static inline void virtio_cread_bytes(struct virtio_device *vdev,
  unsigned int offset,
  void *buf, size_t len)
 {
-   int i;
-
-   for (i = 0; i  len; i++)
-   vdev-config-get(vdev, offset + i, buf + i, 1);
+   __virtio_cread_many(vdev, offset, buf, len, 1);
 }
 
 static inline void virtio_cwrite8(struct virtio_device *vdev,
@@ -352,6 +375,7 @@ static inline u64 virtio_cread64(struct virtio_device *vdev,
 {
u64 ret;
vdev-config-get(vdev, offset, ret, sizeof(ret));
+   __virtio_cread_many(vdev, offset, ret, 1, sizeof(ret));
return virtio64_to_cpu(vdev, (__force __virtio64)ret);
 }
 
-- 
MST

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


[PATCH RFC 0/5] virtio pci: virtio 1.0 support

2014-12-15 Thread Michael S. Tsirkin
This is on top of 3.19 master + my bugfix patches, and adds virtio 1.0 support
to virtio pci.
This is 3.20 material I think.

Would like to get feedback on s390 change as it's untested.

Michael S Tsirkin (2):
  pci: add pci_iomap_range
  s390: add pci_iomap_range

Michael S. Tsirkin (2):
  virtio_pci: modern driver
  virtio_pci: macros for PCI layout offsets.

Rusty Russell (1):
  virtio-pci: define layout for virtio 1.0

 arch/s390/include/asm/pci_io.h |   1 +
 drivers/virtio/virtio_pci_common.h |  29 +-
 include/asm-generic/pci_iomap.h|  10 +
 include/uapi/linux/virtio_pci.h|  92 +
 tools/virtio/linux/virtio_config.h |  45 +++
 arch/s390/pci/pci.c|  34 +-
 drivers/virtio/virtio_pci_common.c |  13 +-
 drivers/virtio/virtio_pci_modern.c | 684 +
 lib/pci_iomap.c|  35 +-
 tools/virtio/virtio_test.c |   1 +
 drivers/virtio/Makefile|   2 +-
 11 files changed, 929 insertions(+), 17 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 RFC 2/5] s390: add pci_iomap_range

2014-12-15 Thread Michael S. Tsirkin
From: Michael S Tsirkin m...@redhat.com

Virtio drivers should map the part of the range they need, not
necessarily all of it.
To this end, support mapping ranges within BAR on s390.
Since multiple ranges can now be mapped within a BAR, we keep track of
the number of mappings created, and only clear out the mapping for a BAR
when this number reaches 0.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
---
 arch/s390/include/asm/pci_io.h |  1 +
 arch/s390/pci/pci.c| 34 +++---
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/arch/s390/include/asm/pci_io.h b/arch/s390/include/asm/pci_io.h
index d194d54..25228b3 100644
--- a/arch/s390/include/asm/pci_io.h
+++ b/arch/s390/include/asm/pci_io.h
@@ -16,6 +16,7 @@
 struct zpci_iomap_entry {
u32 fh;
u8 bar;
+   u16 count;
 };
 
 extern struct zpci_iomap_entry *zpci_iomap_start;
diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
index 2fa7b14..51cb653 100644
--- a/arch/s390/pci/pci.c
+++ b/arch/s390/pci/pci.c
@@ -259,7 +259,10 @@ void __iowrite64_copy(void __iomem *to, const void *from, 
size_t count)
 }
 
 /* Create a virtual mapping cookie for a PCI BAR */
-void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
+void __iomem *pci_iomap_range(struct pci_dev *pdev,
+ int bar,
+ unsigned long offset,
+ unsigned long max)
 {
struct zpci_dev *zdev = get_zdev(pdev);
u64 addr;
@@ -270,14 +273,27 @@ void __iomem *pci_iomap(struct pci_dev *pdev, int bar, 
unsigned long max)
 
idx = zdev-bars[bar].map_idx;
spin_lock(zpci_iomap_lock);
-   zpci_iomap_start[idx].fh = zdev-fh;
-   zpci_iomap_start[idx].bar = bar;
+   if (zpci_iomap_start[idx].count++) {
+   BUG_ON(zpci_iomap_start[idx].fh != zdev-fh ||
+  zpci_iomap_start[idx].bar != bar);
+   } else {
+   zpci_iomap_start[idx].fh = zdev-fh;
+   zpci_iomap_start[idx].bar = bar;
+   }
+   /* Detect overrun */
+   BUG_ON(!zpci_iomap_start[idx].count);
spin_unlock(zpci_iomap_lock);
 
addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx  48);
-   return (void __iomem *) addr;
+   return (void __iomem *) addr + offset;
 }
-EXPORT_SYMBOL_GPL(pci_iomap);
+EXPORT_SYMBOL_GPL(pci_iomap_range);
+
+void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
+{
+   return pci_iomap_range(dev, bar, 0, maxlen);
+}
+EXPORT_SYMBOL(pci_iomap);
 
 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
 {
@@ -285,8 +301,12 @@ void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
 
idx = (((__force u64) addr)  ~ZPCI_IOMAP_ADDR_BASE)  48;
spin_lock(zpci_iomap_lock);
-   zpci_iomap_start[idx].fh = 0;
-   zpci_iomap_start[idx].bar = 0;
+   /* Detect underrun */
+   BUG_ON(!zpci_iomap_start[idx].count);
+   if (!--zpci_iomap_start[idx].count) {
+   zpci_iomap_start[idx].fh = 0;
+   zpci_iomap_start[idx].bar = 0;
+   }
spin_unlock(zpci_iomap_lock);
 }
 EXPORT_SYMBOL_GPL(pci_iounmap);
-- 
MST

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


[PATCH RFC 1/5] pci: add pci_iomap_range

2014-12-15 Thread Michael S. Tsirkin
From: Michael S Tsirkin m...@redhat.com

Virtio drivers should map the part of the BAR they need, not necessarily
all of it.

Signed-off-by: Michael S. Tsirkin m...@redhat.com
Signed-off-by: Rusty Russell ru...@rustcorp.com.au
---
 include/asm-generic/pci_iomap.h | 10 ++
 lib/pci_iomap.c | 35 ++-
 2 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/include/asm-generic/pci_iomap.h b/include/asm-generic/pci_iomap.h
index ce37349..7389c87 100644
--- a/include/asm-generic/pci_iomap.h
+++ b/include/asm-generic/pci_iomap.h
@@ -15,6 +15,9 @@ struct pci_dev;
 #ifdef CONFIG_PCI
 /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
 extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long 
max);
+extern void __iomem *pci_iomap_range(struct pci_dev *dev, int bar,
+unsigned long offset,
+unsigned long maxlen);
 /* Create a virtual mapping cookie for a port on a given PCI device.
  * Do not call this directly, it exists to make it easier for architectures
  * to override */
@@ -30,6 +33,13 @@ static inline void __iomem *pci_iomap(struct pci_dev *dev, 
int bar, unsigned lon
 {
return NULL;
 }
+
+static inline void __iomem *pci_iomap_range(struct pci_dev *dev, int bar,
+   unsigned long offset,
+   unsigned long maxlen)
+{
+   return NULL;
+}
 #endif
 
 #endif /* __ASM_GENERIC_IO_H */
diff --git a/lib/pci_iomap.c b/lib/pci_iomap.c
index 0d83ea8..bcce5f1 100644
--- a/lib/pci_iomap.c
+++ b/lib/pci_iomap.c
@@ -10,10 +10,11 @@
 
 #ifdef CONFIG_PCI
 /**
- * pci_iomap - create a virtual mapping cookie for a PCI BAR
+ * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
  * @dev: PCI device that owns the BAR
  * @bar: BAR number
- * @maxlen: length of the memory to map
+ * @offset: map memory at the given offset in BAR
+ * @maxlen: max length of the memory to map
  *
  * Using this function you will get a __iomem address to your device BAR.
  * You can access it using ioread*() and iowrite*(). These functions hide
@@ -21,16 +22,21 @@
  * you expect from them in the correct way.
  *
  * @maxlen specifies the maximum length to map. If you want to get access to
- * the complete BAR without checking for its length first, pass %0 here.
+ * the complete BAR from offset to the end, pass %0 here.
  * */
-void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
+void __iomem *pci_iomap_range(struct pci_dev *dev,
+ int bar,
+ unsigned long offset,
+ unsigned long maxlen)
 {
resource_size_t start = pci_resource_start(dev, bar);
resource_size_t len = pci_resource_len(dev, bar);
unsigned long flags = pci_resource_flags(dev, bar);
 
-   if (!len || !start)
+   if (len = offset || !start)
return NULL;
+   len -= offset;
+   start += offset;
if (maxlen  len  maxlen)
len = maxlen;
if (flags  IORESOURCE_IO)
@@ -43,6 +49,25 @@ void __iomem *pci_iomap(struct pci_dev *dev, int bar, 
unsigned long maxlen)
/* What? */
return NULL;
 }
+EXPORT_SYMBOL(pci_iomap_range);
 
+/**
+ * pci_iomap - create a virtual mapping cookie for a PCI BAR
+ * @dev: PCI device that owns the BAR
+ * @bar: BAR number
+ * @maxlen: length of the memory to map
+ *
+ * Using this function you will get a __iomem address to your device BAR.
+ * You can access it using ioread*() and iowrite*(). These functions hide
+ * the details if this is a MMIO or PIO address space and will just do what
+ * you expect from them in the correct way.
+ *
+ * @maxlen specifies the maximum length to map. If you want to get access to
+ * the complete BAR without checking for its length first, pass %0 here.
+ * */
+void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
+{
+   return pci_iomap_range(dev, bar, 0, maxlen);
+}
 EXPORT_SYMBOL(pci_iomap);
 #endif /* CONFIG_PCI */
-- 
MST

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


[PATCH RFC 4/5] virtio_pci: modern driver

2014-12-15 Thread Michael S. Tsirkin
It compiles! Must be perfect.

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: Michael S. Tsirkin m...@redhat.com
---
 drivers/virtio/virtio_pci_common.h |  29 +-
 tools/virtio/linux/virtio_config.h |  45 +++
 drivers/virtio/virtio_pci_common.c |  13 +-
 drivers/virtio/virtio_pci_modern.c | 621 +
 tools/virtio/virtio_test.c |   1 +
 drivers/virtio/Makefile|   2 +-
 6 files changed, 706 insertions(+), 5 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 adddb64..1584833 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -53,12 +53,32 @@ 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;
+   /* 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. */
+   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;
@@ -131,5 +151,8 @@ void virtio_pci_release_dev(struct device *);
 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_modern_probe(struct pci_dev *pci_dev,
+   const struct pci_device_id *id);
+void virtio_pci_modern_remove(struct pci_dev *pci_dev);
 
 #endif
diff --git a/tools/virtio/linux/virtio_config.h 
b/tools/virtio/linux/virtio_config.h
index 83b27e8..dafe1c9 100644
--- a/tools/virtio/linux/virtio_config.h
+++ b/tools/virtio/linux/virtio_config.h
@@ -1,6 +1,51 @@
+#include linux/virtio_byteorder.h
+#include linux/virtio.h
+
 #define VIRTIO_TRANSPORT_F_START   28
 #define VIRTIO_TRANSPORT_F_END 32
+/*
+ * __virtio_test_bit - helper to test feature bits. For use by transports.
+ * Devices should normally use virtio_has_feature,
+ * which includes more checks.
+ * @vdev: the device
+ * @fbit: the feature bit
+ */
+static inline bool __virtio_test_bit(const struct virtio_device *vdev,
+unsigned int fbit)
+{
+   return vdev-features  (1ULL  fbit);
+}
 
 #define virtio_has_feature(dev, feature) \
(__virtio_test_bit((dev), feature))
 
+static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
+{
+   return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), 
val);
+}
+
+static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
+{
+   return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), 
val);
+}
+
+static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
+{
+   return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), 
val);
+}
+
+static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
+{
+   return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), 
val);
+}
+
+static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
+{
+   return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), 
val);
+}
+
+static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
+{
+   return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), 
val);
+}
+
diff --git a/drivers/virtio/virtio_pci_common.c 
b/drivers/virtio/virtio_pci_common.c
index 59d3685..3821703 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -475,12 +475,23 @@ MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
 static int virtio_pci_probe(struct pci_dev *pci_dev,
const struct pci_device_id *id)
 {
+   int rc;
+
+   rc = virtio_pci_modern_probe(pci_dev, id);
+   if (rc != -ENODEV)
+   return rc;
+
return virtio_pci_legacy_probe(pci_dev, id);
 }
 
 

[PATCH RFC 3/5] virtio-pci: define layout for virtio 1.0

2014-12-15 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
---
 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 35b552c..28c2ce0 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 RFC 5/5] virtio_pci: macros for PCI layout offsets.

2014-12-15 Thread Michael S. Tsirkin
QEMU wants it, so why not?  Trust, but verify.

Signed-off-by: Rusty Russell ru...@rustcorp.com.au
---
 include/uapi/linux/virtio_pci.h| 30 ++
 drivers/virtio/virtio_pci_modern.c | 63 ++
 2 files changed, 93 insertions(+)

diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index 28c2ce0..5d546c6 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -159,6 +159,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 63c..5b0c012 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -446,6 +446,67 @@ static inline int virtio_pci_find_capability(struct 
pci_dev *dev, u8 cfg_type,
return 0;
 }
 
+/* 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, queue_notify_off));
+   BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
+offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
+