aboutsummaryrefslogtreecommitdiffstats
path: root/virtio_loopback_driver.h
blob: 3e02222ddbb34a6f9d1bd33c31dcd7317dcc095c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Virtio loopback device driver
 *
 * Copyright 2022-2024 Virtual Open Systems SAS.
 *
 * Authors:
 *  Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
 *  Anna Panagopoulou <anna@virtualopensystems.com>
 *  Alvise Rigo <a.rigo@virtualopensystems.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef __LOOPBACK_H__
#define __LOOPBACK_H__

#define DRIVER  "LOOPBACK"

#include <linux/cdev.h>
#include <linux/eventfd.h>
#include <linux/fdtable.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/of_address.h>
#include <linux/cpumask.h>
#include <linux/smp.h>
#include <linux/version.h>
#include <linux/completion.h>

/* MMIO includes */
#include <linux/acpi.h>
#include <linux/dma-mapping.h>
#include <linux/highmem.h>
#include <linux/io.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/virtio.h>
#include <linux/virtio_config.h>
#include <linux/virtio_mmio.h>
#include <linux/virtio_ring.h>

#include <linux/kernel.h>
#include <linux/pid.h>
#include <linux/sched.h>
#include <linux/rcupdate.h>
#include <linux/kthread.h>

/* mmap includes */
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/mutex.h>

#include <linux/pagemap.h>
#include <linux/delay.h>

/* max Minor devices */
#define MAX_DEV	1
#define MAX_PDEV 100
#define PDEV_TYPES 2

/* Define mmap elements limit */
#define MMAP_LIMIT	200

/*
 * The alignment to use between consumer and producer parts of vring.
 * Currently hardcoded to the page size.
 */
#define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE

#define to_virtio_loopback_device(ptr) \
	container_of(ptr, struct virtio_loopback_device, vdev)

/* mmap functionality */
#ifndef VM_RESERVED
#define VM_RESERVED (VM_DONTEXPAND | VM_DONTDUMP)
#endif

/* IOCTL defines */
#define EFD_INIT _IOC(_IOC_WRITE, 'k', 1, sizeof(efd_data))
#define WAKEUP _IOC(_IOC_WRITE, 'k', 2, 0)
#define START_LOOPBACK _IOC(_IOC_WRITE, 'k', 3, sizeof(struct virtio_device_info_struct))
#define IRQ _IOC(_IOC_WRITE, 'k', 4, sizeof(int))
#define SHARE_VQS _IOC(_IOC_WRITE, 'k', 5, sizeof(uint32_t))
#define SHARE_COM_STRUCT _IOC(_IOC_WRITE, 'k', 6, 0)
#define SHARE_VQS_NOTIF _IOC(_IOC_WRITE, 'k', 7, sizeof(struct vq_notifier))

/* Data structures */
struct virtio_device_info_struct {
	unsigned long magic;
	unsigned long version;
	unsigned long device_id;
	unsigned long vendor;
	bool priority_enabled;
	unsigned long init_notif_credits;
	unsigned long priority_group;
};

struct virtio_neg {
	uint64_t notification;
	uint64_t data;
	uint64_t size;
	bool read;
	atomic_t done;
};

struct share_mmap {
	uint64_t pfn;
	uint64_t vm_start;
	uint32_t size;
	uint32_t uid;
	struct page *page;
};

struct mmap_data {
	int mmap_index;
	bool share_communication_struct;
	bool share_vqs;
	struct share_mmap share_mmap_list[MMAP_LIMIT];
	int cur_ram_idx;
	uint64_t sum_pgfaults;
};

struct vq_notifier {
	uint32_t vq_index;
	int notifier_fd;
	int pid;
};

/* vq related data */
struct vq_data {
	uint32_t vq_index;
	uint64_t vq_pfns[16];
	uint64_t vq_pfn;
	struct eventfd_ctx *vq_notifiers[16];
	bool vq_notifiers_enabled;
};

/* Data describing each device private status */
struct device_data {
	/* Info needed for adapter ops */
	struct mmap_info *info;
	/* Waitqueue for the adapter */
	wait_queue_head_t wq;
	struct mutex read_write_lock;
	struct eventfd_ctx *efd_ctx;
	/*
	 * If this variable is true then read/write should wait
	 * the adapter to unlock this operation by sending an
	 * eventfd. If it's equal to "false" then the operation
	 * does not wait for adapter's confirmation.
	 */
	bool valid_eventfd;
	/* vq data */
	struct vq_data vq_data;

	/* virtio device data */
	struct virtio_device_info_struct *vdev_data;
	bool priority_enabled;
	uint32_t priority_group;
	atomic_t notif_credits;
	atomic_t avail_notifs;
	atomic_t avail_inters;
	uint64_t served_timestamp;
};

/* Data describing each entry of the driver */
struct loopback_devices_array {
	/* Array of probed devices */
	struct virtio_loopback_device *devices[MAX_PDEV];
	/* list of the devices */
	struct list_head virtio_devices_list;
	/* Number of available devices */
	atomic_t device_num;
	/* Registration completion */
	struct completion reg_vl_dev_completion[MAX_PDEV];
	/* Counter for all devices pending notifications */
	atomic_t highest_active_prior_notifs;
	atomic_t pending_notifs;
	atomic_t pending_inters;
	wait_queue_head_t wq_notifs_inters;

	/* Spin lock for removing the device */
	spinlock_t running_lock;
};

/* Data concealed in the file private pointer */
struct file_priv_data {
	/* Device needed data */
	struct device_data *dev_data;
	/* mmap needed data */
	struct mmap_data *mm_data;
	/* Device info! */
	struct virtio_device_info_struct device_info;
	/* The vl_dev pointer for the irq */
	struct virtio_loopback_device *vl_dev_irq;
};

struct virtio_loopback_device {
	struct virtio_device vdev;
	struct platform_device *pdev;
	/* Corresponding data pointer */
	struct device_data *data;

	/* Status: -1 not initialized, 0 running, 1 paused */
	int status;

	void __iomem *base;
	unsigned long version;

	/* A list of queues so we can dispatch IRQs */
	spinlock_t lock;
	struct list_head virtqueues;

	/* Define workqueue for notifications */
	struct workqueue_struct *notify_workqueue;

	/* Notify list and work struct */
	spinlock_t notify_q_lock;
	struct list_head notify_list;
	struct work_struct notify_work;

	/* Notification waitqueue */
	wait_queue_head_t wq_notifs_inters;
};

struct virtio_loopback_vq_info {
	/* the actual virtqueue */
	struct virtqueue *vq;
	/* the list node for the virtqueues list */
	struct list_head node;
};

struct virtio_loopback_device_node {
	/* the actual virtqueue */
	uint32_t vq_index;
	atomic_t is_deleted;
	/* the list node for the virtqueues list */
	struct list_head node;
	struct rcu_head rcu;
};

/* Notify data*/
struct notify_data {
	uint32_t index;
	struct list_head list;
};

/* Interrupt data*/
struct interrupt_data {
	uint32_t index;
	struct list_head list;
};

/* Shared data structure between driver and user-space application */
struct mmap_info {
	void *data;
	int reference;
};

/*
 * This structure holds the eventfds shared between the driver
 * and the user-space application.
 */
struct efd_data {
	int efd[2];
	int pid;
};

/* device data holder, this structure may be extended to hold additional data */
struct loopback_device_data {
	/*device Major number */
	int dev_major;
	/* sysfs class structure */
	struct class *class;
	struct cdev cdev;
	struct task_struct *notif_thread;
};

/* Global variables */
extern struct platform_driver virtio_loopback_driver;

/* Global functions */
int insert_entry_data(struct virtio_loopback_device *vl_dev, int id);
int loopback_register_virtio_dev(struct virtio_loopback_device *vl_dev);
bool register_interrupt(struct virtio_loopback_device *vl_dev, int irq);
int notif_sched_func(void *data);
bool add_dev_to_list(uint32_t array_dev_pos);
void note_dev_deletion(struct virtio_loopback_device *vl_dev);
extern struct loopback_devices_array loopback_devices;
extern struct loopback_device_data loopback_data;

#endif /* __LOOPBACK_H__ */