1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
10 #include <virtio_types.h>
12 #include <virtio_ring.h>
13 #include "virtio_net.h"
15 /* Amount of buffers to keep in the RX virtqueue */
16 #define VIRTIO_NET_NUM_RX_BUFS 32
19 * This value comes from the VirtIO spec: 1500 for maximum packet size,
20 * 14 for the Ethernet header, 12 for virtio_net_hdr. In total 1526 bytes.
22 #define VIRTIO_NET_RX_BUF_SIZE 1526
24 struct virtio_net_priv {
26 struct virtqueue *vqs[2];
28 struct virtqueue *rx_vq;
29 struct virtqueue *tx_vq;
33 char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE];
39 * For simplicity, the driver only negotiates the VIRTIO_NET_F_MAC feature.
40 * For the VIRTIO_NET_F_STATUS feature, we don't negotiate it, hence per spec
41 * we should assume the link is always active.
43 static const u32 feature[] = {
47 static const u32 feature_legacy[] = {
51 static int virtio_net_start(struct udevice *dev)
53 struct virtio_net_priv *priv = dev_get_priv(dev);
55 struct virtio_sg *sgs[] = { &sg };
58 if (!priv->rx_running) {
59 /* receive buffer length is always 1526 */
60 sg.length = VIRTIO_NET_RX_BUF_SIZE;
62 /* setup the receive buffer address */
63 for (i = 0; i < VIRTIO_NET_NUM_RX_BUFS; i++) {
64 sg.addr = priv->rx_buff[i];
65 virtqueue_add(priv->rx_vq, sgs, 0, 1);
68 virtqueue_kick(priv->rx_vq);
70 /* setup the receive queue only once */
71 priv->rx_running = true;
77 static int virtio_net_send(struct udevice *dev, void *packet, int length)
79 struct virtio_net_priv *priv = dev_get_priv(dev);
80 struct virtio_net_hdr hdr;
81 struct virtio_net_hdr_v1 hdr_v1;
82 struct virtio_sg hdr_sg;
83 struct virtio_sg data_sg = { packet, length };
84 struct virtio_sg *sgs[] = { &hdr_sg, &data_sg };
87 if (priv->net_hdr_len == sizeof(struct virtio_net_hdr))
90 hdr_sg.addr = &hdr_v1;
91 hdr_sg.length = priv->net_hdr_len;
93 memset(hdr_sg.addr, 0, priv->net_hdr_len);
95 ret = virtqueue_add(priv->tx_vq, sgs, 2, 0);
99 virtqueue_kick(priv->tx_vq);
102 if (virtqueue_get_buf(priv->tx_vq, NULL))
109 static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp)
111 struct virtio_net_priv *priv = dev_get_priv(dev);
115 buf = virtqueue_get_buf(priv->rx_vq, &len);
119 *packetp = buf + priv->net_hdr_len;
120 return len - priv->net_hdr_len;
123 static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length)
125 struct virtio_net_priv *priv = dev_get_priv(dev);
126 void *buf = packet - priv->net_hdr_len;
127 struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE };
128 struct virtio_sg *sgs[] = { &sg };
130 /* Put the buffer back to the rx ring */
131 virtqueue_add(priv->rx_vq, sgs, 0, 1);
136 static void virtio_net_stop(struct udevice *dev)
139 * There is no way to stop the queue from running, unless we issue
140 * a reset to the virtio device, and re-do the queue initialization
141 * from the beginning.
145 static int virtio_net_write_hwaddr(struct udevice *dev)
147 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
148 struct eth_pdata *pdata = dev_get_platdata(dev);
152 * v1.0 compliant device's MAC address is set through control channel,
153 * which we don't support for now.
155 if (!uc_priv->legacy)
158 for (i = 0; i < sizeof(pdata->enetaddr); i++) {
160 offsetof(struct virtio_net_config, mac) + i,
167 static int virtio_net_read_rom_hwaddr(struct udevice *dev)
169 struct eth_pdata *pdata = dev_get_platdata(dev);
174 if (virtio_has_feature(dev, VIRTIO_NET_F_MAC)) {
175 virtio_cread_bytes(dev,
176 offsetof(struct virtio_net_config, mac),
177 pdata->enetaddr, sizeof(pdata->enetaddr));
183 static int virtio_net_bind(struct udevice *dev)
185 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
187 /* Indicate what driver features we support */
188 virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature),
189 feature_legacy, ARRAY_SIZE(feature_legacy));
194 static int virtio_net_probe(struct udevice *dev)
196 struct virtio_net_priv *priv = dev_get_priv(dev);
197 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
200 ret = virtio_find_vqs(dev, 2, priv->vqs);
205 * For v1.0 compliant device, it always assumes the member
206 * 'num_buffers' exists in the struct virtio_net_hdr while
207 * the legacy driver only presented 'num_buffers' when
208 * VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature
209 * the structure was 2 bytes shorter.
212 priv->net_hdr_len = sizeof(struct virtio_net_hdr);
214 priv->net_hdr_len = sizeof(struct virtio_net_hdr_v1);
219 static const struct eth_ops virtio_net_ops = {
220 .start = virtio_net_start,
221 .send = virtio_net_send,
222 .recv = virtio_net_recv,
223 .free_pkt = virtio_net_free_pkt,
224 .stop = virtio_net_stop,
225 .write_hwaddr = virtio_net_write_hwaddr,
226 .read_rom_hwaddr = virtio_net_read_rom_hwaddr,
229 U_BOOT_DRIVER(virtio_net) = {
230 .name = VIRTIO_NET_DRV_NAME,
232 .bind = virtio_net_bind,
233 .probe = virtio_net_probe,
234 .remove = virtio_reset,
235 .ops = &virtio_net_ops,
236 .priv_auto_alloc_size = sizeof(struct virtio_net_priv),
237 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
238 .flags = DM_FLAG_ACTIVE_DMA,