1 // 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 7 #include <common.h> 8 #include <dm.h> 9 #include <net.h> 10 #include <virtio_types.h> 11 #include <virtio.h> 12 #include <virtio_ring.h> 13 #include "virtio_net.h" 14 15 /* Amount of buffers to keep in the RX virtqueue */ 16 #define VIRTIO_NET_NUM_RX_BUFS 32 17 18 /* 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. 21 */ 22 #define VIRTIO_NET_RX_BUF_SIZE 1526 23 24 struct virtio_net_priv { 25 union { 26 struct virtqueue *vqs[2]; 27 struct { 28 struct virtqueue *rx_vq; 29 struct virtqueue *tx_vq; 30 }; 31 }; 32 33 char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE]; 34 bool rx_running; 35 }; 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 */ 42 static const u32 feature[] = { 43 VIRTIO_NET_F_MAC 44 }; 45 46 static const u32 feature_legacy[] = { 47 VIRTIO_NET_F_MAC 48 }; 49 50 static 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 76 static 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; 80 struct virtio_sg hdr_sg = { &hdr, sizeof(hdr) }; 81 struct virtio_sg data_sg = { packet, length }; 82 struct virtio_sg *sgs[] = { &hdr_sg, &data_sg }; 83 int ret; 84 85 memset(&hdr, 0, sizeof(struct virtio_net_hdr)); 86 87 ret = virtqueue_add(priv->tx_vq, sgs, 2, 0); 88 if (ret) 89 return ret; 90 91 virtqueue_kick(priv->tx_vq); 92 93 while (1) { 94 if (virtqueue_get_buf(priv->tx_vq, NULL)) 95 break; 96 } 97 98 return 0; 99 } 100 101 static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp) 102 { 103 struct virtio_net_priv *priv = dev_get_priv(dev); 104 unsigned int len; 105 void *buf; 106 107 buf = virtqueue_get_buf(priv->rx_vq, &len); 108 if (!buf) 109 return -EAGAIN; 110 111 *packetp = buf + sizeof(struct virtio_net_hdr); 112 return len - sizeof(struct virtio_net_hdr); 113 } 114 115 static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length) 116 { 117 struct virtio_net_priv *priv = dev_get_priv(dev); 118 void *buf = packet - sizeof(struct virtio_net_hdr); 119 struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE }; 120 struct virtio_sg *sgs[] = { &sg }; 121 122 /* Put the buffer back to the rx ring */ 123 virtqueue_add(priv->rx_vq, sgs, 0, 1); 124 125 return 0; 126 } 127 128 static void virtio_net_stop(struct udevice *dev) 129 { 130 /* 131 * There is no way to stop the queue from running, unless we issue 132 * a reset to the virtio device, and re-do the queue initialization 133 * from the beginning. 134 */ 135 } 136 137 static int virtio_net_write_hwaddr(struct udevice *dev) 138 { 139 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); 140 struct eth_pdata *pdata = dev_get_platdata(dev); 141 int i; 142 143 /* 144 * v1.0 compliant device's MAC address is set through control channel, 145 * which we don't support for now. 146 */ 147 if (!uc_priv->legacy) 148 return -ENOSYS; 149 150 for (i = 0; i < sizeof(pdata->enetaddr); i++) { 151 virtio_cwrite8(dev, 152 offsetof(struct virtio_net_config, mac) + i, 153 pdata->enetaddr[i]); 154 } 155 156 return 0; 157 } 158 159 static int virtio_net_read_rom_hwaddr(struct udevice *dev) 160 { 161 struct eth_pdata *pdata = dev_get_platdata(dev); 162 163 if (!pdata) 164 return -ENOSYS; 165 166 if (virtio_has_feature(dev, VIRTIO_NET_F_MAC)) { 167 virtio_cread_bytes(dev, 168 offsetof(struct virtio_net_config, mac), 169 pdata->enetaddr, sizeof(pdata->enetaddr)); 170 } 171 172 return 0; 173 } 174 175 static int virtio_net_bind(struct udevice *dev) 176 { 177 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); 178 179 /* Indicate what driver features we support */ 180 virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature), 181 feature_legacy, ARRAY_SIZE(feature_legacy)); 182 183 return 0; 184 } 185 186 static int virtio_net_probe(struct udevice *dev) 187 { 188 struct virtio_net_priv *priv = dev_get_priv(dev); 189 int ret; 190 191 ret = virtio_find_vqs(dev, 2, priv->vqs); 192 if (ret < 0) 193 return ret; 194 195 return 0; 196 } 197 198 static const struct eth_ops virtio_net_ops = { 199 .start = virtio_net_start, 200 .send = virtio_net_send, 201 .recv = virtio_net_recv, 202 .free_pkt = virtio_net_free_pkt, 203 .stop = virtio_net_stop, 204 .write_hwaddr = virtio_net_write_hwaddr, 205 .read_rom_hwaddr = virtio_net_read_rom_hwaddr, 206 }; 207 208 U_BOOT_DRIVER(virtio_net) = { 209 .name = VIRTIO_NET_DRV_NAME, 210 .id = UCLASS_ETH, 211 .bind = virtio_net_bind, 212 .probe = virtio_net_probe, 213 .remove = virtio_reset, 214 .ops = &virtio_net_ops, 215 .priv_auto_alloc_size = sizeof(struct virtio_net_priv), 216 .platdata_auto_alloc_size = sizeof(struct eth_pdata), 217 .flags = DM_FLAG_ACTIVE_DMA, 218 }; 219