virtio_net.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include <common.h>
  7. #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. /* Amount of buffers to keep in the RX virtqueue */
  14. #define VIRTIO_NET_NUM_RX_BUFS 32
  15. /*
  16. * This value comes from the VirtIO spec: 1500 for maximum packet size,
  17. * 14 for the Ethernet header, 12 for virtio_net_hdr. In total 1526 bytes.
  18. */
  19. #define VIRTIO_NET_RX_BUF_SIZE 1526
  20. struct virtio_net_priv {
  21. union {
  22. struct virtqueue *vqs[2];
  23. struct {
  24. struct virtqueue *rx_vq;
  25. struct virtqueue *tx_vq;
  26. };
  27. };
  28. char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE];
  29. bool rx_running;
  30. int net_hdr_len;
  31. };
  32. /*
  33. * For simplicity, the driver only negotiates the VIRTIO_NET_F_MAC feature.
  34. * For the VIRTIO_NET_F_STATUS feature, we don't negotiate it, hence per spec
  35. * we should assume the link is always active.
  36. */
  37. static const u32 feature[] = {
  38. VIRTIO_NET_F_MAC
  39. };
  40. static const u32 feature_legacy[] = {
  41. VIRTIO_NET_F_MAC
  42. };
  43. static int virtio_net_start(struct udevice *dev)
  44. {
  45. struct virtio_net_priv *priv = dev_get_priv(dev);
  46. struct virtio_sg sg;
  47. struct virtio_sg *sgs[] = { &sg };
  48. int i;
  49. if (!priv->rx_running) {
  50. /* receive buffer length is always 1526 */
  51. sg.length = VIRTIO_NET_RX_BUF_SIZE;
  52. /* setup the receive buffer address */
  53. for (i = 0; i < VIRTIO_NET_NUM_RX_BUFS; i++) {
  54. sg.addr = priv->rx_buff[i];
  55. virtqueue_add(priv->rx_vq, sgs, 0, 1);
  56. }
  57. virtqueue_kick(priv->rx_vq);
  58. /* setup the receive queue only once */
  59. priv->rx_running = true;
  60. }
  61. return 0;
  62. }
  63. static int virtio_net_send(struct udevice *dev, void *packet, int length)
  64. {
  65. struct virtio_net_priv *priv = dev_get_priv(dev);
  66. struct virtio_net_hdr hdr;
  67. struct virtio_net_hdr_v1 hdr_v1;
  68. struct virtio_sg hdr_sg;
  69. struct virtio_sg data_sg = { packet, length };
  70. struct virtio_sg *sgs[] = { &hdr_sg, &data_sg };
  71. int ret;
  72. if (priv->net_hdr_len == sizeof(struct virtio_net_hdr))
  73. hdr_sg.addr = &hdr;
  74. else
  75. hdr_sg.addr = &hdr_v1;
  76. hdr_sg.length = priv->net_hdr_len;
  77. memset(hdr_sg.addr, 0, priv->net_hdr_len);
  78. ret = virtqueue_add(priv->tx_vq, sgs, 2, 0);
  79. if (ret)
  80. return ret;
  81. virtqueue_kick(priv->tx_vq);
  82. while (1) {
  83. if (virtqueue_get_buf(priv->tx_vq, NULL))
  84. break;
  85. }
  86. return 0;
  87. }
  88. static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp)
  89. {
  90. struct virtio_net_priv *priv = dev_get_priv(dev);
  91. unsigned int len;
  92. void *buf;
  93. buf = virtqueue_get_buf(priv->rx_vq, &len);
  94. if (!buf)
  95. return -EAGAIN;
  96. *packetp = buf + priv->net_hdr_len;
  97. return len - priv->net_hdr_len;
  98. }
  99. static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length)
  100. {
  101. struct virtio_net_priv *priv = dev_get_priv(dev);
  102. void *buf = packet - priv->net_hdr_len;
  103. struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE };
  104. struct virtio_sg *sgs[] = { &sg };
  105. /* Put the buffer back to the rx ring */
  106. virtqueue_add(priv->rx_vq, sgs, 0, 1);
  107. return 0;
  108. }
  109. static void virtio_net_stop(struct udevice *dev)
  110. {
  111. /*
  112. * There is no way to stop the queue from running, unless we issue
  113. * a reset to the virtio device, and re-do the queue initialization
  114. * from the beginning.
  115. */
  116. }
  117. static int virtio_net_write_hwaddr(struct udevice *dev)
  118. {
  119. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
  120. struct eth_pdata *pdata = dev_get_platdata(dev);
  121. int i;
  122. /*
  123. * v1.0 compliant device's MAC address is set through control channel,
  124. * which we don't support for now.
  125. */
  126. if (!uc_priv->legacy)
  127. return -ENOSYS;
  128. for (i = 0; i < sizeof(pdata->enetaddr); i++) {
  129. virtio_cwrite8(dev,
  130. offsetof(struct virtio_net_config, mac) + i,
  131. pdata->enetaddr[i]);
  132. }
  133. return 0;
  134. }
  135. static int virtio_net_read_rom_hwaddr(struct udevice *dev)
  136. {
  137. struct eth_pdata *pdata = dev_get_platdata(dev);
  138. if (!pdata)
  139. return -ENOSYS;
  140. if (virtio_has_feature(dev, VIRTIO_NET_F_MAC)) {
  141. virtio_cread_bytes(dev,
  142. offsetof(struct virtio_net_config, mac),
  143. pdata->enetaddr, sizeof(pdata->enetaddr));
  144. }
  145. return 0;
  146. }
  147. static int virtio_net_bind(struct udevice *dev)
  148. {
  149. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
  150. /* Indicate what driver features we support */
  151. virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature),
  152. feature_legacy, ARRAY_SIZE(feature_legacy));
  153. return 0;
  154. }
  155. static int virtio_net_probe(struct udevice *dev)
  156. {
  157. struct virtio_net_priv *priv = dev_get_priv(dev);
  158. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
  159. int ret;
  160. ret = virtio_find_vqs(dev, 2, priv->vqs);
  161. if (ret < 0)
  162. return ret;
  163. /*
  164. * For v1.0 compliant device, it always assumes the member
  165. * 'num_buffers' exists in the struct virtio_net_hdr while
  166. * the legacy driver only presented 'num_buffers' when
  167. * VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature
  168. * the structure was 2 bytes shorter.
  169. */
  170. if (uc_priv->legacy)
  171. priv->net_hdr_len = sizeof(struct virtio_net_hdr);
  172. else
  173. priv->net_hdr_len = sizeof(struct virtio_net_hdr_v1);
  174. return 0;
  175. }
  176. static const struct eth_ops virtio_net_ops = {
  177. .start = virtio_net_start,
  178. .send = virtio_net_send,
  179. .recv = virtio_net_recv,
  180. .free_pkt = virtio_net_free_pkt,
  181. .stop = virtio_net_stop,
  182. .write_hwaddr = virtio_net_write_hwaddr,
  183. .read_rom_hwaddr = virtio_net_read_rom_hwaddr,
  184. };
  185. U_BOOT_DRIVER(virtio_net) = {
  186. .name = VIRTIO_NET_DRV_NAME,
  187. .id = UCLASS_ETH,
  188. .bind = virtio_net_bind,
  189. .probe = virtio_net_probe,
  190. .remove = virtio_reset,
  191. .ops = &virtio_net_ops,
  192. .priv_auto_alloc_size = sizeof(struct virtio_net_priv),
  193. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  194. .flags = DM_FLAG_ACTIVE_DMA,
  195. };