diff mbox series

[RFC,3/4] vDPA: detect device endian in _net_config_fill() and _fill_stats_rec()

Message ID 20220901101601.61420-4-lingshan.zhu@intel.com (mailing list archive)
State RFC
Headers show
Series vDPA: support VHOST_GET/SET_VRING_ENDIAN | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Zhu, Lingshan Sept. 1, 2022, 10:16 a.m. UTC
This commit detect endian-ness of the device by
vdpa_config_ops.get_vq_endian(), so that functions
vdpa_dev_net_config_fill() and vdpa_fill_stats_rec()
can convert the endian-ness correctly by __virtio16_to_cpu().

Signed-off-by: Zhu Lingshan <lingshan.zhu@intel.com>
---
 drivers/vdpa/vdpa.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index c06c02704461..8a08caf573d1 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -12,6 +12,7 @@ 
 #include <linux/slab.h>
 #include <linux/vdpa.h>
 #include <uapi/linux/vdpa.h>
+#include <uapi/linux/vhost.h>
 #include <net/genetlink.h>
 #include <linux/mod_devicetable.h>
 #include <linux/virtio_ids.h>
@@ -814,21 +815,31 @@  static int vdpa_dev_net_mq_config_fill(struct vdpa_device *vdev,
 
 static int vdpa_dev_net_config_fill(struct vdpa_device *vdev, struct sk_buff *msg)
 {
+	const struct vdpa_config_ops *ops = vdev->config;
 	struct virtio_net_config config = {};
 	u64 features;
 	u16 val_u16;
+	u8 le;
 
-	vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
+	ops->get_config(vdev, 0, &config, sizeof(config));
+
+	/* endian-ness is a device wide attr, so reading the endian-ness of vq0
+	 * can get the endian-ness of the device config space.
+	 */
+	if (ops->get_vq_endian(vdev, 0) == VHOST_VRING_LITTLE_ENDIAN)
+		le = true;
+	else
+		le = false;
 
 	if (nla_put(msg, VDPA_ATTR_DEV_NET_CFG_MACADDR, sizeof(config.mac),
 		    config.mac))
 		return -EMSGSIZE;
 
-	val_u16 = __virtio16_to_cpu(true, config.status);
+	val_u16 = __virtio16_to_cpu(le, config.status);
 	if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_STATUS, val_u16))
 		return -EMSGSIZE;
 
-	val_u16 = __virtio16_to_cpu(true, config.mtu);
+	val_u16 = __virtio16_to_cpu(le, config.mtu);
 	if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MTU, val_u16))
 		return -EMSGSIZE;
 
@@ -897,6 +908,7 @@  static int vdpa_fill_stats_rec(struct vdpa_device *vdev, struct sk_buff *msg,
 	u16 max_vqp;
 	u8 status;
 	int err;
+	u8 le;
 
 	status = vdev->config->get_status(vdev);
 	if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
@@ -905,7 +917,15 @@  static int vdpa_fill_stats_rec(struct vdpa_device *vdev, struct sk_buff *msg,
 	}
 	vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
 
-	max_vqp = __virtio16_to_cpu(true, config.max_virtqueue_pairs);
+	/* endian-ness is a device wide attr, so reading the endian-ness of vq0
+	 * can get the endian-ness of the device config space.
+	 */
+	if (vdev->config->get_vq_endian(vdev, 0) == VHOST_VRING_LITTLE_ENDIAN)
+		le = true;
+	else
+		le = false;
+
+	max_vqp = __virtio16_to_cpu(le, config.max_virtqueue_pairs);
 	if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MAX_VQP, max_vqp))
 		return -EMSGSIZE;