1 // SPDX-License-Identifier: GPL-2.0-only
3 * VDPA simulator for networking device.
5 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/etherdevice.h>
16 #include <linux/vringh.h>
17 #include <linux/vdpa.h>
18 #include <uapi/linux/virtio_net.h>
19 #include <uapi/linux/vdpa.h>
23 #define DRV_VERSION "0.1"
25 #define DRV_DESC "vDPA Device Simulator for networking device"
26 #define DRV_LICENSE "GPL v2"
28 #define VDPASIM_NET_FEATURES (VDPASIM_FEATURES | \
29 (1ULL << VIRTIO_NET_F_MAC))
31 #define VDPASIM_NET_VQ_NUM 2
33 static void vdpasim_net_work(struct work_struct *work)
35 struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
36 struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
37 struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
43 spin_lock(&vdpasim->lock);
45 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
48 if (!txq->ready || !rxq->ready)
53 err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
54 &txq->head, GFP_ATOMIC);
58 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
59 &rxq->head, GFP_ATOMIC);
61 vringh_complete_iotlb(&txq->vring, txq->head, 0);
66 read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
72 write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
73 vdpasim->buffer, read);
80 /* Make sure data is wrote before advancing index */
83 vringh_complete_iotlb(&txq->vring, txq->head, 0);
84 vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
86 /* Make sure used is visible before rasing the interrupt. */
90 if (vringh_need_notify_iotlb(&txq->vring) > 0)
91 vringh_notify(&txq->vring);
92 if (vringh_need_notify_iotlb(&rxq->vring) > 0)
93 vringh_notify(&rxq->vring);
97 schedule_work(&vdpasim->work);
103 spin_unlock(&vdpasim->lock);
106 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
108 struct virtio_net_config *net_config = config;
110 net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
113 static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
114 const struct vdpa_dev_set_config *config)
116 struct virtio_net_config *vio_config = vdpasim->config;
118 if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR))
119 memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
120 if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MTU))
121 vio_config->mtu = cpu_to_vdpasim16(vdpasim, config->net.mtu);
123 /* Setup default MTU to be 1500 */
124 vio_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
127 static void vdpasim_net_mgmtdev_release(struct device *dev)
131 static struct device vdpasim_net_mgmtdev = {
132 .init_name = "vdpasim_net",
133 .release = vdpasim_net_mgmtdev_release,
136 static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
137 const struct vdpa_dev_set_config *config)
139 struct vdpasim_dev_attr dev_attr = {};
140 struct vdpasim *simdev;
143 dev_attr.mgmt_dev = mdev;
144 dev_attr.name = name;
145 dev_attr.id = VIRTIO_ID_NET;
146 dev_attr.supported_features = VDPASIM_NET_FEATURES;
147 dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
148 dev_attr.config_size = sizeof(struct virtio_net_config);
149 dev_attr.get_config = vdpasim_net_get_config;
150 dev_attr.work_fn = vdpasim_net_work;
151 dev_attr.buffer_size = PAGE_SIZE;
153 simdev = vdpasim_create(&dev_attr);
155 return PTR_ERR(simdev);
157 vdpasim_net_setup_config(simdev, config);
159 ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_NET_VQ_NUM);
166 put_device(&simdev->vdpa.dev);
170 static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
171 struct vdpa_device *dev)
173 struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa);
175 _vdpa_unregister_device(&simdev->vdpa);
178 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
179 .dev_add = vdpasim_net_dev_add,
180 .dev_del = vdpasim_net_dev_del
183 static struct virtio_device_id id_table[] = {
184 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
188 static struct vdpa_mgmt_dev mgmt_dev = {
189 .device = &vdpasim_net_mgmtdev,
190 .id_table = id_table,
191 .ops = &vdpasim_net_mgmtdev_ops,
192 .config_attr_mask = (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR |
193 1 << VDPA_ATTR_DEV_NET_CFG_MTU),
194 .max_supported_vqs = VDPASIM_NET_VQ_NUM,
195 .supported_features = VDPASIM_NET_FEATURES,
198 static int __init vdpasim_net_init(void)
202 ret = device_register(&vdpasim_net_mgmtdev);
206 ret = vdpa_mgmtdev_register(&mgmt_dev);
212 device_unregister(&vdpasim_net_mgmtdev);
216 static void __exit vdpasim_net_exit(void)
218 vdpa_mgmtdev_unregister(&mgmt_dev);
219 device_unregister(&vdpasim_net_mgmtdev);
222 module_init(vdpasim_net_init);
223 module_exit(vdpasim_net_exit);
225 MODULE_VERSION(DRV_VERSION);
226 MODULE_LICENSE(DRV_LICENSE);
227 MODULE_AUTHOR(DRV_AUTHOR);
228 MODULE_DESCRIPTION(DRV_DESC);