1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Virtio PCI driver - modern (virtio 1.0) device support
4  *
5  * This module allows virtio devices to be used over a virtual PCI device.
6  * This can be used with QEMU based VMMs like KVM or Xen.
7  *
8  * Copyright IBM Corp. 2007
9  * Copyright Red Hat, Inc. 2014
10  *
11  * Authors:
12  *  Anthony Liguori  <aliguori@us.ibm.com>
13  *  Rusty Russell <rusty@rustcorp.com.au>
14  *  Michael S. Tsirkin <mst@redhat.com>
15  */
16 
17 #include <linux/delay.h>
18 #define VIRTIO_PCI_NO_LEGACY
19 #define VIRTIO_RING_NO_LEGACY
20 #include "virtio_pci_common.h"
21 
22 static u64 vp_get_features(struct virtio_device *vdev)
23 {
24 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
25 
26 	return vp_modern_get_features(&vp_dev->mdev);
27 }
28 
29 static void vp_transport_features(struct virtio_device *vdev, u64 features)
30 {
31 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
32 	struct pci_dev *pci_dev = vp_dev->pci_dev;
33 
34 	if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
35 			pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
36 		__virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
37 }
38 
39 /* virtio config->finalize_features() implementation */
40 static int vp_finalize_features(struct virtio_device *vdev)
41 {
42 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
43 	u64 features = vdev->features;
44 
45 	/* Give virtio_ring a chance to accept features. */
46 	vring_transport_features(vdev);
47 
48 	/* Give virtio_pci a chance to accept features. */
49 	vp_transport_features(vdev, features);
50 
51 	if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
52 		dev_err(&vdev->dev, "virtio: device uses modern interface "
53 			"but does not have VIRTIO_F_VERSION_1\n");
54 		return -EINVAL;
55 	}
56 
57 	vp_modern_set_features(&vp_dev->mdev, vdev->features);
58 
59 	return 0;
60 }
61 
62 /* virtio config->get() implementation */
63 static void vp_get(struct virtio_device *vdev, unsigned offset,
64 		   void *buf, unsigned len)
65 {
66 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
67 	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
68 	void __iomem *device = mdev->device;
69 	u8 b;
70 	__le16 w;
71 	__le32 l;
72 
73 	BUG_ON(offset + len > mdev->device_len);
74 
75 	switch (len) {
76 	case 1:
77 		b = ioread8(device + offset);
78 		memcpy(buf, &b, sizeof b);
79 		break;
80 	case 2:
81 		w = cpu_to_le16(ioread16(device + offset));
82 		memcpy(buf, &w, sizeof w);
83 		break;
84 	case 4:
85 		l = cpu_to_le32(ioread32(device + offset));
86 		memcpy(buf, &l, sizeof l);
87 		break;
88 	case 8:
89 		l = cpu_to_le32(ioread32(device + offset));
90 		memcpy(buf, &l, sizeof l);
91 		l = cpu_to_le32(ioread32(device + offset + sizeof l));
92 		memcpy(buf + sizeof l, &l, sizeof l);
93 		break;
94 	default:
95 		BUG();
96 	}
97 }
98 
99 /* the config->set() implementation.  it's symmetric to the config->get()
100  * implementation */
101 static void vp_set(struct virtio_device *vdev, unsigned offset,
102 		   const void *buf, unsigned len)
103 {
104 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
105 	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
106 	void __iomem *device = mdev->device;
107 	u8 b;
108 	__le16 w;
109 	__le32 l;
110 
111 	BUG_ON(offset + len > mdev->device_len);
112 
113 	switch (len) {
114 	case 1:
115 		memcpy(&b, buf, sizeof b);
116 		iowrite8(b, device + offset);
117 		break;
118 	case 2:
119 		memcpy(&w, buf, sizeof w);
120 		iowrite16(le16_to_cpu(w), device + offset);
121 		break;
122 	case 4:
123 		memcpy(&l, buf, sizeof l);
124 		iowrite32(le32_to_cpu(l), device + offset);
125 		break;
126 	case 8:
127 		memcpy(&l, buf, sizeof l);
128 		iowrite32(le32_to_cpu(l), device + offset);
129 		memcpy(&l, buf + sizeof l, sizeof l);
130 		iowrite32(le32_to_cpu(l), device + offset + sizeof l);
131 		break;
132 	default:
133 		BUG();
134 	}
135 }
136 
137 static u32 vp_generation(struct virtio_device *vdev)
138 {
139 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
140 
141 	return vp_modern_generation(&vp_dev->mdev);
142 }
143 
144 /* config->{get,set}_status() implementations */
145 static u8 vp_get_status(struct virtio_device *vdev)
146 {
147 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
148 
149 	return vp_modern_get_status(&vp_dev->mdev);
150 }
151 
152 static void vp_set_status(struct virtio_device *vdev, u8 status)
153 {
154 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
155 
156 	/* We should never be setting status to 0. */
157 	BUG_ON(status == 0);
158 	vp_modern_set_status(&vp_dev->mdev, status);
159 }
160 
161 static void vp_reset(struct virtio_device *vdev)
162 {
163 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
164 	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
165 
166 	/* 0 status means a reset. */
167 	vp_modern_set_status(mdev, 0);
168 	/* After writing 0 to device_status, the driver MUST wait for a read of
169 	 * device_status to return 0 before reinitializing the device.
170 	 * This will flush out the status write, and flush in device writes,
171 	 * including MSI-X interrupts, if any.
172 	 */
173 	while (vp_modern_get_status(mdev))
174 		msleep(1);
175 	/* Flush pending VQ/configuration callbacks. */
176 	vp_synchronize_vectors(vdev);
177 }
178 
179 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
180 {
181 	return vp_modern_config_vector(&vp_dev->mdev, vector);
182 }
183 
184 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
185 				  struct virtio_pci_vq_info *info,
186 				  unsigned index,
187 				  void (*callback)(struct virtqueue *vq),
188 				  const char *name,
189 				  bool ctx,
190 				  u16 msix_vec)
191 {
192 
193 	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
194 	struct virtqueue *vq;
195 	u16 num, off;
196 	int err;
197 
198 	if (index >= vp_modern_get_num_queues(mdev))
199 		return ERR_PTR(-ENOENT);
200 
201 	/* Check if queue is either not available or already active. */
202 	num = vp_modern_get_queue_size(mdev, index);
203 	if (!num || vp_modern_get_queue_enable(mdev, index))
204 		return ERR_PTR(-ENOENT);
205 
206 	if (num & (num - 1)) {
207 		dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
208 		return ERR_PTR(-EINVAL);
209 	}
210 
211 	/* get offset of notification word for this vq */
212 	off = vp_modern_get_queue_notify_off(mdev, index);
213 
214 	info->msix_vector = msix_vec;
215 
216 	/* create the vring */
217 	vq = vring_create_virtqueue(index, num,
218 				    SMP_CACHE_BYTES, &vp_dev->vdev,
219 				    true, true, ctx,
220 				    vp_notify, callback, name);
221 	if (!vq)
222 		return ERR_PTR(-ENOMEM);
223 
224 	/* activate the queue */
225 	vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
226 	vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
227 				virtqueue_get_avail_addr(vq),
228 				virtqueue_get_used_addr(vq));
229 
230 	if (mdev->notify_base) {
231 		/* offset should not wrap */
232 		if ((u64)off * mdev->notify_offset_multiplier + 2
233 		    > mdev->notify_len) {
234 			dev_warn(&mdev->pci_dev->dev,
235 				 "bad notification offset %u (x %u) "
236 				 "for queue %u > %zd",
237 				 off, mdev->notify_offset_multiplier,
238 				 index, mdev->notify_len);
239 			err = -EINVAL;
240 			goto err_map_notify;
241 		}
242 		vq->priv = (void __force *)mdev->notify_base +
243 			off * mdev->notify_offset_multiplier;
244 	} else {
245 		vq->priv = (void __force *)vp_modern_map_capability(mdev,
246 							  mdev->notify_map_cap, 2, 2,
247 							  off * mdev->notify_offset_multiplier, 2,
248 							  NULL);
249 	}
250 
251 	if (!vq->priv) {
252 		err = -ENOMEM;
253 		goto err_map_notify;
254 	}
255 
256 	if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
257 		msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
258 		if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
259 			err = -EBUSY;
260 			goto err_assign_vector;
261 		}
262 	}
263 
264 	return vq;
265 
266 err_assign_vector:
267 	if (!mdev->notify_base)
268 		pci_iounmap(mdev->pci_dev, (void __iomem __force *)vq->priv);
269 err_map_notify:
270 	vring_del_virtqueue(vq);
271 	return ERR_PTR(err);
272 }
273 
274 static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
275 			      struct virtqueue *vqs[],
276 			      vq_callback_t *callbacks[],
277 			      const char * const names[], const bool *ctx,
278 			      struct irq_affinity *desc)
279 {
280 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
281 	struct virtqueue *vq;
282 	int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
283 
284 	if (rc)
285 		return rc;
286 
287 	/* Select and activate all queues. Has to be done last: once we do
288 	 * this, there's no way to go back except reset.
289 	 */
290 	list_for_each_entry(vq, &vdev->vqs, list)
291 		vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
292 
293 	return 0;
294 }
295 
296 static void del_vq(struct virtio_pci_vq_info *info)
297 {
298 	struct virtqueue *vq = info->vq;
299 	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
300 	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
301 
302 	if (vp_dev->msix_enabled)
303 		vp_modern_queue_vector(mdev, vq->index,
304 				       VIRTIO_MSI_NO_VECTOR);
305 
306 	if (!mdev->notify_base)
307 		pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
308 
309 	vring_del_virtqueue(vq);
310 }
311 
312 static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
313 				   u8 *bar, u64 *offset, u64 *len)
314 {
315 	int pos;
316 
317 	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
318 	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
319 		u8 type, cap_len, id;
320 		u32 tmp32;
321 		u64 res_offset, res_length;
322 
323 		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
324 							 cfg_type), &type);
325 		if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
326 			continue;
327 
328 		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
329 							 cap_len), &cap_len);
330 		if (cap_len != sizeof(struct virtio_pci_cap64)) {
331 			dev_err(&dev->dev, "%s: shm cap with bad size offset:"
332 				" %d size: %d\n", __func__, pos, cap_len);
333 			continue;
334 		}
335 
336 		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
337 							 id), &id);
338 		if (id != required_id)
339 			continue;
340 
341 		/* Type, and ID match, looks good */
342 		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
343 							 bar), bar);
344 
345 		/* Read the lower 32bit of length and offset */
346 		pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
347 							  offset), &tmp32);
348 		res_offset = tmp32;
349 		pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
350 							  length), &tmp32);
351 		res_length = tmp32;
352 
353 		/* and now the top half */
354 		pci_read_config_dword(dev,
355 				      pos + offsetof(struct virtio_pci_cap64,
356 						     offset_hi), &tmp32);
357 		res_offset |= ((u64)tmp32) << 32;
358 		pci_read_config_dword(dev,
359 				      pos + offsetof(struct virtio_pci_cap64,
360 						     length_hi), &tmp32);
361 		res_length |= ((u64)tmp32) << 32;
362 
363 		*offset = res_offset;
364 		*len = res_length;
365 
366 		return pos;
367 	}
368 	return 0;
369 }
370 
371 static bool vp_get_shm_region(struct virtio_device *vdev,
372 			      struct virtio_shm_region *region, u8 id)
373 {
374 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
375 	struct pci_dev *pci_dev = vp_dev->pci_dev;
376 	u8 bar;
377 	u64 offset, len;
378 	phys_addr_t phys_addr;
379 	size_t bar_len;
380 
381 	if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
382 		return false;
383 
384 	phys_addr = pci_resource_start(pci_dev, bar);
385 	bar_len = pci_resource_len(pci_dev, bar);
386 
387 	if ((offset + len) < offset) {
388 		dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
389 			__func__);
390 		return false;
391 	}
392 
393 	if (offset + len > bar_len) {
394 		dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
395 			__func__);
396 		return false;
397 	}
398 
399 	region->len = len;
400 	region->addr = (u64) phys_addr + offset;
401 
402 	return true;
403 }
404 
405 static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
406 	.get		= NULL,
407 	.set		= NULL,
408 	.generation	= vp_generation,
409 	.get_status	= vp_get_status,
410 	.set_status	= vp_set_status,
411 	.reset		= vp_reset,
412 	.find_vqs	= vp_modern_find_vqs,
413 	.del_vqs	= vp_del_vqs,
414 	.get_features	= vp_get_features,
415 	.finalize_features = vp_finalize_features,
416 	.bus_name	= vp_bus_name,
417 	.set_vq_affinity = vp_set_vq_affinity,
418 	.get_vq_affinity = vp_get_vq_affinity,
419 	.get_shm_region  = vp_get_shm_region,
420 };
421 
422 static const struct virtio_config_ops virtio_pci_config_ops = {
423 	.get		= vp_get,
424 	.set		= vp_set,
425 	.generation	= vp_generation,
426 	.get_status	= vp_get_status,
427 	.set_status	= vp_set_status,
428 	.reset		= vp_reset,
429 	.find_vqs	= vp_modern_find_vqs,
430 	.del_vqs	= vp_del_vqs,
431 	.get_features	= vp_get_features,
432 	.finalize_features = vp_finalize_features,
433 	.bus_name	= vp_bus_name,
434 	.set_vq_affinity = vp_set_vq_affinity,
435 	.get_vq_affinity = vp_get_vq_affinity,
436 	.get_shm_region  = vp_get_shm_region,
437 };
438 
439 /* the PCI probing function */
440 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
441 {
442 	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
443 	struct pci_dev *pci_dev = vp_dev->pci_dev;
444 	int err;
445 
446 	mdev->pci_dev = pci_dev;
447 
448 	err = vp_modern_probe(mdev);
449 	if (err)
450 		return err;
451 
452 	if (mdev->device)
453 		vp_dev->vdev.config = &virtio_pci_config_ops;
454 	else
455 		vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
456 
457 	vp_dev->config_vector = vp_config_vector;
458 	vp_dev->setup_vq = setup_vq;
459 	vp_dev->del_vq = del_vq;
460 	vp_dev->isr = mdev->isr;
461 	vp_dev->vdev.id = mdev->id;
462 
463 	return 0;
464 }
465 
466 void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
467 {
468 	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
469 
470 	vp_modern_remove(mdev);
471 }
472