summaryrefslogtreecommitdiffstats
path: root/drivers/virtio/virtio_vdpa.c
blob: 9bc4d110b80003ce912ba5888ffebad177b3ddff (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// SPDX-License-Identifier: GPL-2.0-only
/*
 * VIRTIO based driver for vDPA device
 *
 * Copyright (c) 2020, Red Hat. All rights reserved.
 *     Author: Jason Wang <jasowang@redhat.com>
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/uuid.h>
#include <linux/virtio.h>
#include <linux/vdpa.h>
#include <linux/virtio_config.h>
#include <linux/virtio_ring.h>

#define MOD_VERSION  "0.1"
#define MOD_AUTHOR   "Jason Wang <jasowang@redhat.com>"
#define MOD_DESC     "vDPA bus driver for virtio devices"
#define MOD_LICENSE  "GPL v2"

struct virtio_vdpa_device {
	struct virtio_device vdev;
	struct vdpa_device *vdpa;
	u64 features;

	/* The lock to protect virtqueue list */
	spinlock_t lock;
	/* List of virtio_vdpa_vq_info */
	struct list_head virtqueues;
};

struct virtio_vdpa_vq_info {
	/* the actual virtqueue */
	struct virtqueue *vq;

	/* the list node for the virtqueues list */
	struct list_head node;
};

static inline struct virtio_vdpa_device *
to_virtio_vdpa_device(struct virtio_device *dev)
{
	return container_of(dev, struct virtio_vdpa_device, vdev);
}

static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev)
{
	return to_virtio_vdpa_device(vdev)->vdpa;
}

static void virtio_vdpa_get(struct virtio_device *vdev, unsigned int offset,
			    void *buf, unsigned int len)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);

	vdpa_get_config(vdpa, offset, buf, len);
}

static void virtio_vdpa_set(struct virtio_device *vdev, unsigned int offset,
			    const void *buf, unsigned int len)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);

	vdpa_set_config(vdpa, offset, buf, len);
}

static u32 virtio_vdpa_generation(struct virtio_device *vdev)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);
	const struct vdpa_config_ops *ops = vdpa->config;

	if (ops->get_generation)
		return ops->get_generation(vdpa);

	return 0;
}

static u8 virtio_vdpa_get_status(struct virtio_device *vdev)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);
	const struct vdpa_config_ops *ops = vdpa->config;

	return ops->get_status(vdpa);
}

static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);

	return vdpa_set_status(vdpa, status);
}

static void virtio_vdpa_reset(struct virtio_device *vdev)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);

	vdpa_reset(vdpa);
}

static bool virtio_vdpa_notify(struct virtqueue *vq)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vq->vdev);
	const struct vdpa_config_ops *ops = vdpa->config;

	ops->kick_vq(vdpa, vq->index);

	return true;
}

static irqreturn_t virtio_vdpa_config_cb(void *private)
{
	struct virtio_vdpa_device *vd_dev = private;

	virtio_config_changed(&vd_dev->vdev);

	return IRQ_HANDLED;
}

static irqreturn_t virtio_vdpa_virtqueue_cb(void *private)
{
	struct virtio_vdpa_vq_info *info = private;

	return vring_interrupt(0, info->vq);
}

static struct virtqueue *
virtio_vdpa_setup_vq(struct virtio_device *vdev, unsigned int index,
		     void (*callback)(struct virtqueue *vq),
		     const char *name, u32 size, bool ctx)
{
	struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);
	const struct vdpa_config_ops *ops = vdpa->config;
	struct virtio_vdpa_vq_info *info;
	struct vdpa_callback cb;
	struct virtqueue *vq;
	u64 desc_addr, driver_addr, device_addr;
	/* Assume split virtqueue, switch to packed if necessary */
	struct vdpa_vq_state state = {0};
	unsigned long flags;
	u32 align, max_num, min_num = 1;
	bool may_reduce_num = true;
	int err;

	if (!name)
		return NULL;

	if (index >= vdpa->nvqs)
		return ERR_PTR(-ENOENT);

	/* Queue shouldn't already be set up. */
	if (ops->get_vq_ready(vdpa, index))
		return ERR_PTR(-ENOENT);

	/* Allocate and fill out our active queue description */
	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (!info)
		return ERR_PTR(-ENOMEM);

	max_num = ops->get_vq_num_max(vdpa);
	if (max_num == 0) {
		err = -ENOENT;
		goto error_new_virtqueue;
	}

	if (!size || size > max_num)
		size = max_num;

	if (ops->get_vq_num_min)
		min_num = ops->get_vq_num_min(vdpa);

	may_reduce_num = (size == min_num) ? false : true;

	/* Create the vring */
	align = ops->get_vq_align(vdpa);
	vq = vring_create_virtqueue(index, size, align, vdev,
				    true, may_reduce_num, ctx,
				    virtio_vdpa_notify, callback, name);
	if (!vq) {
		err = -ENOMEM;
		goto error_new_virtqueue;
	}

	vq->num_max = max_num;

	/* Setup virtqueue callback */
	cb.callback = callback ? virtio_vdpa_virtqueue_cb : NULL;
	cb.private = info;
	ops->set_vq_cb(vdpa, index, &cb);
	ops->set_vq_num(vdpa, index, virtqueue_get_vring_size(vq));

	desc_addr = virtqueue_get_desc_addr(vq);
	driver_addr = virtqueue_get_avail_addr(vq);
	device_addr = virtqueue_get_used_addr(vq);

	if (ops->set_vq_address(vdpa, index,
				desc_addr, driver_addr,
				device_addr)) {
		err = -EINVAL;
		goto err_vq;
	}

	/* reset virtqueue state index */
	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
		struct vdpa_vq_state_packed *s = &state.packed;

		s->last_avail_counter = 1;
		s->last_avail_idx = 0;
		s->last_used_counter = 1;
		s->last_used_idx = 0;
	}
	err = ops->set_vq_state(vdpa, index, &state);
	if (err)
		goto err_vq;

	ops->set_vq_ready(vdpa, index, 1);

	vq->priv = info;
	info->vq = vq;

	spin_lock_irqsave(&vd_dev->lock, flags);
	list_add(&info->node, &vd_dev->virtqueues);
	spin_unlock_irqrestore(&vd_dev->lock, flags);

	return vq;

err_vq:
	vring_del_virtqueue(vq);
error_new_virtqueue:
	ops->set_vq_ready(vdpa, index, 0);
	/* VDPA driver should make sure vq is stopeed here */
	WARN_ON(ops->get_vq_ready(vdpa, index));
	kfree(info);
	return ERR_PTR(err);
}

static void virtio_vdpa_del_vq(struct virtqueue *vq)
{
	struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vq->vdev);
	struct vdpa_device *vdpa = vd_dev->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	struct virtio_vdpa_vq_info *info = vq->priv;
	unsigned int index = vq->index;
	unsigned long flags;

	spin_lock_irqsave(&vd_dev->lock, flags);
	list_del(&info->node);
	spin_unlock_irqrestore(&vd_dev->lock, flags);

	/* Select and deactivate the queue (best effort) */
	ops->set_vq_ready(vdpa, index, 0);

	vring_del_virtqueue(vq);

	kfree(info);
}

static void virtio_vdpa_del_vqs(struct virtio_device *vdev)
{
	struct virtqueue *vq, *n;

	list_for_each_entry_safe(vq, n, &vdev->vqs, list)
		virtio_vdpa_del_vq(vq);
}

static int virtio_vdpa_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
				struct virtqueue *vqs[],
				vq_callback_t *callbacks[],
				const char * const names[],
				u32 sizes[],
				const bool *ctx,
				struct irq_affinity *desc)
{
	struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);
	const struct vdpa_config_ops *ops = vdpa->config;
	struct vdpa_callback cb;
	int i, err, queue_idx = 0;

	for (i = 0; i < nvqs; ++i) {
		if (!names[i]) {
			vqs[i] = NULL;
			continue;
		}

		vqs[i] = virtio_vdpa_setup_vq(vdev, queue_idx++, callbacks[i],
						  names[i], sizes ? sizes[i] : 0,
						  ctx ? ctx[i] : false);
		if (IS_ERR(vqs[i])) {
			err = PTR_ERR(vqs[i]);
			goto err_setup_vq;
		}
	}

	cb.callback = virtio_vdpa_config_cb;
	cb.private = vd_dev;
	ops->set_config_cb(vdpa, &cb);

	return 0;

err_setup_vq:
	virtio_vdpa_del_vqs(vdev);
	return err;
}

static u64 virtio_vdpa_get_features(struct virtio_device *vdev)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);
	const struct vdpa_config_ops *ops = vdpa->config;

	return ops->get_device_features(vdpa);
}

static int virtio_vdpa_finalize_features(struct virtio_device *vdev)
{
	struct vdpa_device *vdpa = vd_get_vdpa(vdev);

	/* Give virtio_ring a chance to accept features. */
	vring_transport_features(vdev);

	return vdpa_set_features(vdpa, vdev->features);
}

static const char *virtio_vdpa_bus_name(struct virtio_device *vdev)
{
	struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
	struct vdpa_device *vdpa = vd_dev->vdpa;

	return dev_name(&vdpa->dev);
}

static const struct virtio_config_ops virtio_vdpa_config_ops = {
	.get		= virtio_vdpa_get,
	.set		= virtio_vdpa_set,
	.generation	= virtio_vdpa_generation,
	.get_status	= virtio_vdpa_get_status,
	.set_status	= virtio_vdpa_set_status,
	.reset		= virtio_vdpa_reset,
	.find_vqs	= virtio_vdpa_find_vqs,
	.del_vqs	= virtio_vdpa_del_vqs,
	.get_features	= virtio_vdpa_get_features,
	.finalize_features = virtio_vdpa_finalize_features,
	.bus_name	= virtio_vdpa_bus_name,
};

static void virtio_vdpa_release_dev(struct device *_d)
{
	struct virtio_device *vdev =
	       container_of(_d, struct virtio_device, dev);
	struct virtio_vdpa_device *vd_dev =
	       container_of(vdev, struct virtio_vdpa_device, vdev);

	kfree(vd_dev);
}

static int virtio_vdpa_probe(struct vdpa_device *vdpa)
{
	const struct vdpa_config_ops *ops = vdpa->config;
	struct virtio_vdpa_device *vd_dev, *reg_dev = NULL;
	int ret = -EINVAL;

	vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL);
	if (!vd_dev)
		return -ENOMEM;

	vd_dev->vdev.dev.parent = vdpa_get_dma_dev(vdpa);
	vd_dev->vdev.dev.release = virtio_vdpa_release_dev;
	vd_dev->vdev.config = &virtio_vdpa_config_ops;
	vd_dev->vdpa = vdpa;
	INIT_LIST_HEAD(&vd_dev->virtqueues);
	spin_lock_init(&vd_dev->lock);

	vd_dev->vdev.id.device = ops->get_device_id(vdpa);
	if (vd_dev->vdev.id.device == 0)
		goto err;

	vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa);
	ret = register_virtio_device(&vd_dev->vdev);
	reg_dev = vd_dev;
	if (ret)
		goto err;

	vdpa_set_drvdata(vdpa, vd_dev);

	return 0;

err:
	if (reg_dev)
		put_device(&vd_dev->vdev.dev);
	else
		kfree(vd_dev);
	return ret;
}

static void virtio_vdpa_remove(struct vdpa_device *vdpa)
{
	struct virtio_vdpa_device *vd_dev = vdpa_get_drvdata(vdpa);

	unregister_virtio_device(&vd_dev->vdev);
}

static struct vdpa_driver virtio_vdpa_driver = {
	.driver = {
		.name	= "virtio_vdpa",
	},
	.probe	= virtio_vdpa_probe,
	.remove = virtio_vdpa_remove,
};

module_vdpa_driver(virtio_vdpa_driver);

MODULE_VERSION(MOD_VERSION);
MODULE_LICENSE(MOD_LICENSE);
MODULE_AUTHOR(MOD_AUTHOR);
MODULE_DESCRIPTION(MOD_DESC);