diff mbox series

[U-Boot,26/27] virtio: net: Support non-legacy device

Message ID 1537710145-1888-27-git-send-email-bmeng.cn@gmail.com
State Changes Requested
Delegated to: Simon Glass
Headers show
Series virtio: Introduce VirtIO driver support | expand

Commit Message

Bin Meng Sept. 23, 2018, 1:42 p.m. UTC
For v1.0 compliant device, it always assumes the member 'num_buffers'
exists in the struct virtio_net_hdr while the legacy driver only
presented 'num_buffers' when VIRTIO_NET_F_MRG_RXBUF was negotiated.
Without that feature the structure was 2 bytes shorter.

Update the driver to support the non-legacy device.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
---

 drivers/virtio/virtio_net.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

Comments

Simon Glass Sept. 27, 2018, 1:43 p.m. UTC | #1
On 23 September 2018 at 06:42, Bin Meng <bmeng.cn@gmail.com> wrote:
> For v1.0 compliant device, it always assumes the member 'num_buffers'
> exists in the struct virtio_net_hdr while the legacy driver only
> presented 'num_buffers' when VIRTIO_NET_F_MRG_RXBUF was negotiated.
> Without that feature the structure was 2 bytes shorter.
>
> Update the driver to support the non-legacy device.
>
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
> ---
>
>  drivers/virtio/virtio_net.c | 31 ++++++++++++++++++++++++++-----
>  1 file changed, 26 insertions(+), 5 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>
Joe Hershberger Oct. 15, 2018, 9:59 p.m. UTC | #2
On Sun, Sep 23, 2018 at 8:58 AM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> For v1.0 compliant device, it always assumes the member 'num_buffers'
> exists in the struct virtio_net_hdr while the legacy driver only
> presented 'num_buffers' when VIRTIO_NET_F_MRG_RXBUF was negotiated.
> Without that feature the structure was 2 bytes shorter.
>
> Update the driver to support the non-legacy device.
>
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
diff mbox series

Patch

diff --git a/drivers/virtio/virtio_net.c b/drivers/virtio/virtio_net.c
index 1ab1513..080b0be 100644
--- a/drivers/virtio/virtio_net.c
+++ b/drivers/virtio/virtio_net.c
@@ -31,6 +31,7 @@  struct virtio_net_priv {
 
 	char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE];
 	bool rx_running;
+	int net_hdr_len;
 };
 
 /*
@@ -76,12 +77,19 @@  static int virtio_net_send(struct udevice *dev, void *packet, int length)
 {
 	struct virtio_net_priv *priv = dev_get_priv(dev);
 	struct virtio_net_hdr hdr;
-	struct virtio_sg hdr_sg = { &hdr, sizeof(hdr) };
+	struct virtio_net_hdr_v1 hdr_v1;
+	struct virtio_sg hdr_sg;
 	struct virtio_sg data_sg = { packet, length };
 	struct virtio_sg *sgs[] = { &hdr_sg, &data_sg };
 	int ret;
 
-	memset(&hdr, 0, sizeof(struct virtio_net_hdr));
+	if (priv->net_hdr_len == sizeof(struct virtio_net_hdr))
+		hdr_sg.addr = &hdr;
+	else
+		hdr_sg.addr = &hdr_v1;
+	hdr_sg.length = priv->net_hdr_len;
+
+	memset(hdr_sg.addr, 0, priv->net_hdr_len);
 
 	ret = virtqueue_add(priv->tx_vq, sgs, 2, 0);
 	if (ret)
@@ -107,14 +115,14 @@  static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp)
 	if (!buf)
 		return -EAGAIN;
 
-	*packetp = buf + sizeof(struct virtio_net_hdr);
-	return len - sizeof(struct virtio_net_hdr);
+	*packetp = buf + priv->net_hdr_len;
+	return len - priv->net_hdr_len;
 }
 
 static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length)
 {
 	struct virtio_net_priv *priv = dev_get_priv(dev);
-	void *buf = packet - sizeof(struct virtio_net_hdr);
+	void *buf = packet - priv->net_hdr_len;
 	struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE };
 	struct virtio_sg *sgs[] = { &sg };
 
@@ -185,12 +193,25 @@  static int virtio_net_bind(struct udevice *dev)
 static int virtio_net_probe(struct udevice *dev)
 {
 	struct virtio_net_priv *priv = dev_get_priv(dev);
+	struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
 	int ret;
 
 	ret = virtio_find_vqs(dev, 2, priv->vqs);
 	if (ret < 0)
 		return ret;
 
+	/*
+	 * For v1.0 compliant device, it always assumes the member
+	 * 'num_buffers' exists in the struct virtio_net_hdr while
+	 * the legacy driver only presented 'num_buffers' when
+	 * VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature
+	 * the structure was 2 bytes shorter.
+	 */
+	if (uc_priv->legacy)
+		priv->net_hdr_len = sizeof(struct virtio_net_hdr);
+	else
+		priv->net_hdr_len = sizeof(struct virtio_net_hdr_v1);
+
 	return 0;
 }