1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VDPA simulator for networking device.
4  *
5  * Copyright (c) 2020, Red Hat Inc. All rights reserved.
6  *     Author: Jason Wang <jasowang@redhat.com>
7  *
8  */
9 
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 
20 #include "vdpa_sim.h"
21 
22 #define DRV_VERSION  "0.1"
23 #define DRV_AUTHOR   "Jason Wang <jasowang@redhat.com>"
24 #define DRV_DESC     "vDPA Device Simulator for networking device"
25 #define DRV_LICENSE  "GPL v2"
26 
27 #define VDPASIM_NET_FEATURES	(VDPASIM_FEATURES | \
28 				 (1ULL << VIRTIO_NET_F_MAC))
29 
30 #define VDPASIM_NET_VQ_NUM	2
31 
32 static char *macaddr;
33 module_param(macaddr, charp, 0);
34 MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
35 
36 static u8 macaddr_buf[ETH_ALEN];
37 
38 static void vdpasim_net_work(struct work_struct *work)
39 {
40 	struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
41 	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
42 	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
43 	ssize_t read, write;
44 	size_t total_write;
45 	int pkts = 0;
46 	int err;
47 
48 	spin_lock(&vdpasim->lock);
49 
50 	if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
51 		goto out;
52 
53 	if (!txq->ready || !rxq->ready)
54 		goto out;
55 
56 	while (true) {
57 		total_write = 0;
58 		err = vringh_getdesc_iotlb(&txq->vring, &txq->out_iov, NULL,
59 					   &txq->head, GFP_ATOMIC);
60 		if (err <= 0)
61 			break;
62 
63 		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->in_iov,
64 					   &rxq->head, GFP_ATOMIC);
65 		if (err <= 0) {
66 			vringh_complete_iotlb(&txq->vring, txq->head, 0);
67 			break;
68 		}
69 
70 		while (true) {
71 			read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
72 						     vdpasim->buffer,
73 						     PAGE_SIZE);
74 			if (read <= 0)
75 				break;
76 
77 			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
78 						      vdpasim->buffer, read);
79 			if (write <= 0)
80 				break;
81 
82 			total_write += write;
83 		}
84 
85 		/* Make sure data is wrote before advancing index */
86 		smp_wmb();
87 
88 		vringh_complete_iotlb(&txq->vring, txq->head, 0);
89 		vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
90 
91 		/* Make sure used is visible before rasing the interrupt. */
92 		smp_wmb();
93 
94 		local_bh_disable();
95 		if (vringh_need_notify_iotlb(&txq->vring) > 0)
96 			vringh_notify(&txq->vring);
97 		if (vringh_need_notify_iotlb(&rxq->vring) > 0)
98 			vringh_notify(&rxq->vring);
99 		local_bh_enable();
100 
101 		if (++pkts > 4) {
102 			schedule_work(&vdpasim->work);
103 			goto out;
104 		}
105 	}
106 
107 out:
108 	spin_unlock(&vdpasim->lock);
109 }
110 
111 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
112 {
113 	struct virtio_net_config *net_config =
114 		(struct virtio_net_config *)config;
115 
116 	net_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
117 	net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
118 	memcpy(net_config->mac, macaddr_buf, ETH_ALEN);
119 }
120 
121 static void vdpasim_net_mgmtdev_release(struct device *dev)
122 {
123 }
124 
125 static struct device vdpasim_net_mgmtdev = {
126 	.init_name = "vdpasim_net",
127 	.release = vdpasim_net_mgmtdev_release,
128 };
129 
130 static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name)
131 {
132 	struct vdpasim_dev_attr dev_attr = {};
133 	struct vdpasim *simdev;
134 	int ret;
135 
136 	dev_attr.mgmt_dev = mdev;
137 	dev_attr.name = name;
138 	dev_attr.id = VIRTIO_ID_NET;
139 	dev_attr.supported_features = VDPASIM_NET_FEATURES;
140 	dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
141 	dev_attr.config_size = sizeof(struct virtio_net_config);
142 	dev_attr.get_config = vdpasim_net_get_config;
143 	dev_attr.work_fn = vdpasim_net_work;
144 	dev_attr.buffer_size = PAGE_SIZE;
145 
146 	simdev = vdpasim_create(&dev_attr);
147 	if (IS_ERR(simdev))
148 		return PTR_ERR(simdev);
149 
150 	ret = _vdpa_register_device(&simdev->vdpa);
151 	if (ret)
152 		goto reg_err;
153 
154 	return 0;
155 
156 reg_err:
157 	put_device(&simdev->vdpa.dev);
158 	return ret;
159 }
160 
161 static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
162 				struct vdpa_device *dev)
163 {
164 	struct vdpasim *simdev = container_of(dev, struct vdpasim, vdpa);
165 
166 	_vdpa_unregister_device(&simdev->vdpa);
167 }
168 
169 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
170 	.dev_add = vdpasim_net_dev_add,
171 	.dev_del = vdpasim_net_dev_del
172 };
173 
174 static struct virtio_device_id id_table[] = {
175 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
176 	{ 0 },
177 };
178 
179 static struct vdpa_mgmt_dev mgmt_dev = {
180 	.device = &vdpasim_net_mgmtdev,
181 	.id_table = id_table,
182 	.ops = &vdpasim_net_mgmtdev_ops,
183 };
184 
185 static int __init vdpasim_net_init(void)
186 {
187 	int ret;
188 
189 	if (macaddr) {
190 		mac_pton(macaddr, macaddr_buf);
191 		if (!is_valid_ether_addr(macaddr_buf))
192 			return -EADDRNOTAVAIL;
193 	} else {
194 		eth_random_addr(macaddr_buf);
195 	}
196 
197 	ret = device_register(&vdpasim_net_mgmtdev);
198 	if (ret)
199 		return ret;
200 
201 	ret = vdpa_mgmtdev_register(&mgmt_dev);
202 	if (ret)
203 		goto parent_err;
204 	return 0;
205 
206 parent_err:
207 	device_unregister(&vdpasim_net_mgmtdev);
208 	return ret;
209 }
210 
211 static void __exit vdpasim_net_exit(void)
212 {
213 	vdpa_mgmtdev_unregister(&mgmt_dev);
214 	device_unregister(&vdpasim_net_mgmtdev);
215 }
216 
217 module_init(vdpasim_net_init);
218 module_exit(vdpasim_net_exit);
219 
220 MODULE_VERSION(DRV_VERSION);
221 MODULE_LICENSE(DRV_LICENSE);
222 MODULE_AUTHOR(DRV_AUTHOR);
223 MODULE_DESCRIPTION(DRV_DESC);
224