blob: 0e5367a085e4e75b659118930d88bf6696b15936 [file] [log] [blame]
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 */
6
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -07007#include <dm.h>
8#include <net.h>
9#include <virtio_types.h>
10#include <virtio.h>
11#include <virtio_ring.h>
12#include "virtio_net.h"
13
14/* Amount of buffers to keep in the RX virtqueue */
15#define VIRTIO_NET_NUM_RX_BUFS 32
16
17/*
18 * This value comes from the VirtIO spec: 1500 for maximum packet size,
19 * 14 for the Ethernet header, 12 for virtio_net_hdr. In total 1526 bytes.
20 */
21#define VIRTIO_NET_RX_BUF_SIZE 1526
22
23struct virtio_net_priv {
24 union {
25 struct virtqueue *vqs[2];
26 struct {
27 struct virtqueue *rx_vq;
28 struct virtqueue *tx_vq;
29 };
30 };
31
32 char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE];
33 bool rx_running;
Bin Meng699aae02018-10-15 02:21:24 -070034 int net_hdr_len;
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -070035};
36
37/*
38 * For simplicity, the driver only negotiates the VIRTIO_NET_F_MAC feature.
39 * For the VIRTIO_NET_F_STATUS feature, we don't negotiate it, hence per spec
40 * we should assume the link is always active.
41 */
42static const u32 feature[] = {
43 VIRTIO_NET_F_MAC
44};
45
46static const u32 feature_legacy[] = {
47 VIRTIO_NET_F_MAC
48};
49
50static int virtio_net_start(struct udevice *dev)
51{
52 struct virtio_net_priv *priv = dev_get_priv(dev);
53 struct virtio_sg sg;
54 struct virtio_sg *sgs[] = { &sg };
55 int i;
56
57 if (!priv->rx_running) {
58 /* receive buffer length is always 1526 */
59 sg.length = VIRTIO_NET_RX_BUF_SIZE;
60
61 /* setup the receive buffer address */
62 for (i = 0; i < VIRTIO_NET_NUM_RX_BUFS; i++) {
63 sg.addr = priv->rx_buff[i];
64 virtqueue_add(priv->rx_vq, sgs, 0, 1);
65 }
66
67 virtqueue_kick(priv->rx_vq);
68
69 /* setup the receive queue only once */
70 priv->rx_running = true;
71 }
72
73 return 0;
74}
75
76static int virtio_net_send(struct udevice *dev, void *packet, int length)
77{
78 struct virtio_net_priv *priv = dev_get_priv(dev);
79 struct virtio_net_hdr hdr;
Bin Meng699aae02018-10-15 02:21:24 -070080 struct virtio_net_hdr_v1 hdr_v1;
81 struct virtio_sg hdr_sg;
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -070082 struct virtio_sg data_sg = { packet, length };
83 struct virtio_sg *sgs[] = { &hdr_sg, &data_sg };
84 int ret;
85
Bin Meng699aae02018-10-15 02:21:24 -070086 if (priv->net_hdr_len == sizeof(struct virtio_net_hdr))
87 hdr_sg.addr = &hdr;
88 else
89 hdr_sg.addr = &hdr_v1;
90 hdr_sg.length = priv->net_hdr_len;
91
92 memset(hdr_sg.addr, 0, priv->net_hdr_len);
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -070093
94 ret = virtqueue_add(priv->tx_vq, sgs, 2, 0);
95 if (ret)
96 return ret;
97
98 virtqueue_kick(priv->tx_vq);
99
100 while (1) {
101 if (virtqueue_get_buf(priv->tx_vq, NULL))
102 break;
103 }
104
105 return 0;
106}
107
108static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp)
109{
110 struct virtio_net_priv *priv = dev_get_priv(dev);
111 unsigned int len;
112 void *buf;
113
114 buf = virtqueue_get_buf(priv->rx_vq, &len);
115 if (!buf)
116 return -EAGAIN;
117
Bin Meng699aae02018-10-15 02:21:24 -0700118 *packetp = buf + priv->net_hdr_len;
119 return len - priv->net_hdr_len;
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -0700120}
121
122static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length)
123{
124 struct virtio_net_priv *priv = dev_get_priv(dev);
Bin Meng699aae02018-10-15 02:21:24 -0700125 void *buf = packet - priv->net_hdr_len;
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -0700126 struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE };
127 struct virtio_sg *sgs[] = { &sg };
128
129 /* Put the buffer back to the rx ring */
130 virtqueue_add(priv->rx_vq, sgs, 0, 1);
131
132 return 0;
133}
134
135static void virtio_net_stop(struct udevice *dev)
136{
137 /*
138 * There is no way to stop the queue from running, unless we issue
139 * a reset to the virtio device, and re-do the queue initialization
140 * from the beginning.
141 */
142}
143
144static int virtio_net_write_hwaddr(struct udevice *dev)
145{
146 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
Simon Glassc69cda22020-12-03 16:55:20 -0700147 struct eth_pdata *pdata = dev_get_plat(dev);
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -0700148 int i;
149
150 /*
151 * v1.0 compliant device's MAC address is set through control channel,
152 * which we don't support for now.
153 */
154 if (!uc_priv->legacy)
155 return -ENOSYS;
156
157 for (i = 0; i < sizeof(pdata->enetaddr); i++) {
158 virtio_cwrite8(dev,
159 offsetof(struct virtio_net_config, mac) + i,
160 pdata->enetaddr[i]);
161 }
162
163 return 0;
164}
165
166static int virtio_net_read_rom_hwaddr(struct udevice *dev)
167{
Simon Glassc69cda22020-12-03 16:55:20 -0700168 struct eth_pdata *pdata = dev_get_plat(dev);
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -0700169
170 if (!pdata)
171 return -ENOSYS;
172
173 if (virtio_has_feature(dev, VIRTIO_NET_F_MAC)) {
174 virtio_cread_bytes(dev,
175 offsetof(struct virtio_net_config, mac),
176 pdata->enetaddr, sizeof(pdata->enetaddr));
177 }
178
179 return 0;
180}
181
182static int virtio_net_bind(struct udevice *dev)
183{
184 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
185
186 /* Indicate what driver features we support */
187 virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature),
188 feature_legacy, ARRAY_SIZE(feature_legacy));
189
190 return 0;
191}
192
193static int virtio_net_probe(struct udevice *dev)
194{
195 struct virtio_net_priv *priv = dev_get_priv(dev);
Bin Meng699aae02018-10-15 02:21:24 -0700196 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -0700197 int ret;
198
199 ret = virtio_find_vqs(dev, 2, priv->vqs);
200 if (ret < 0)
201 return ret;
202
Bin Meng699aae02018-10-15 02:21:24 -0700203 /*
204 * For v1.0 compliant device, it always assumes the member
205 * 'num_buffers' exists in the struct virtio_net_hdr while
206 * the legacy driver only presented 'num_buffers' when
207 * VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature
208 * the structure was 2 bytes shorter.
209 */
210 if (uc_priv->legacy)
211 priv->net_hdr_len = sizeof(struct virtio_net_hdr);
212 else
213 priv->net_hdr_len = sizeof(struct virtio_net_hdr_v1);
214
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -0700215 return 0;
216}
217
218static const struct eth_ops virtio_net_ops = {
219 .start = virtio_net_start,
220 .send = virtio_net_send,
221 .recv = virtio_net_recv,
222 .free_pkt = virtio_net_free_pkt,
223 .stop = virtio_net_stop,
224 .write_hwaddr = virtio_net_write_hwaddr,
225 .read_rom_hwaddr = virtio_net_read_rom_hwaddr,
226};
227
228U_BOOT_DRIVER(virtio_net) = {
229 .name = VIRTIO_NET_DRV_NAME,
230 .id = UCLASS_ETH,
231 .bind = virtio_net_bind,
232 .probe = virtio_net_probe,
233 .remove = virtio_reset,
234 .ops = &virtio_net_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700235 .priv_auto = sizeof(struct virtio_net_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700236 .plat_auto = sizeof(struct eth_pdata),
Tuomas Tynkkynenf371ad32018-10-15 02:21:03 -0700237 .flags = DM_FLAG_ACTIVE_DMA,
238};