Linux Audio

Check our new training course

Loading...
v5.9
  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/*
 23 * Type-safe wrappers for io accesses.
 24 * Use these to enforce at compile time the following spec requirement:
 25 *
 26 * The driver MUST access each field using the “natural” access
 27 * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses
 28 * for 16-bit fields and 8-bit accesses for 8-bit fields.
 29 */
 30static inline u8 vp_ioread8(const u8 __iomem *addr)
 31{
 32	return ioread8(addr);
 33}
 34static inline u16 vp_ioread16 (const __le16 __iomem *addr)
 35{
 36	return ioread16(addr);
 37}
 38
 39static inline u32 vp_ioread32(const __le32 __iomem *addr)
 40{
 41	return ioread32(addr);
 42}
 43
 44static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
 45{
 46	iowrite8(value, addr);
 47}
 48
 49static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
 50{
 51	iowrite16(value, addr);
 52}
 53
 54static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
 55{
 56	iowrite32(value, addr);
 
 57}
 58
 59static void vp_iowrite64_twopart(u64 val,
 60				 __le32 __iomem *lo, __le32 __iomem *hi)
 
 
 
 
 61{
 62	vp_iowrite32((u32)val, lo);
 63	vp_iowrite32(val >> 32, hi);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 64}
 65
 66static void __iomem *map_capability(struct pci_dev *dev, int off,
 67				    size_t minlen,
 68				    u32 align,
 69				    u32 start, u32 size,
 70				    size_t *len)
 71{
 72	u8 bar;
 73	u32 offset, length;
 74	void __iomem *p;
 
 
 
 
 75
 76	pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
 77						 bar),
 78			     &bar);
 79	pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
 80			     &offset);
 81	pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
 82			      &length);
 83
 84	if (length <= start) {
 85		dev_err(&dev->dev,
 86			"virtio_pci: bad capability len %u (>%u expected)\n",
 87			length, start);
 88		return NULL;
 89	}
 90
 91	if (length - start < minlen) {
 92		dev_err(&dev->dev,
 93			"virtio_pci: bad capability len %u (>=%zu expected)\n",
 94			length, minlen);
 95		return NULL;
 96	}
 97
 98	length -= start;
 
 
 
 
 
 
 
 99
100	if (start + offset < offset) {
101		dev_err(&dev->dev,
102			"virtio_pci: map wrap-around %u+%u\n",
103			start, offset);
104		return NULL;
105	}
106
107	offset += start;
 
 
 
108
109	if (offset & (align - 1)) {
110		dev_err(&dev->dev,
111			"virtio_pci: offset %u not aligned to %u\n",
112			offset, align);
113		return NULL;
114	}
115
116	if (length > size)
117		length = size;
 
 
 
118
119	if (len)
120		*len = length;
 
 
 
121
122	if (minlen + offset < minlen ||
123	    minlen + offset > pci_resource_len(dev, bar)) {
124		dev_err(&dev->dev,
125			"virtio_pci: map virtio %zu@%u "
126			"out of range on bar %i length %lu\n",
127			minlen, offset,
128			bar, (unsigned long)pci_resource_len(dev, bar));
129		return NULL;
130	}
131
132	p = pci_iomap_range(dev, bar, offset, length);
133	if (!p)
134		dev_err(&dev->dev,
135			"virtio_pci: unable to map virtio %u@%u on bar %i\n",
136			length, offset, bar);
137	return p;
138}
139
140/* virtio config->get_features() implementation */
141static u64 vp_get_features(struct virtio_device *vdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142{
143	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
144	u64 features;
145
146	vp_iowrite32(0, &vp_dev->common->device_feature_select);
147	features = vp_ioread32(&vp_dev->common->device_feature);
148	vp_iowrite32(1, &vp_dev->common->device_feature_select);
149	features |= ((u64)vp_ioread32(&vp_dev->common->device_feature) << 32);
150
151	return features;
 
 
 
 
 
 
 
 
 
 
 
 
152}
153
154static void vp_transport_features(struct virtio_device *vdev, u64 features)
155{
156	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
157	struct pci_dev *pci_dev = vp_dev->pci_dev;
158
159	if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
160			pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
161		__virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162}
163
164/* virtio config->finalize_features() implementation */
165static int vp_finalize_features(struct virtio_device *vdev)
166{
167	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
168	u64 features = vdev->features;
169
170	/* Give virtio_ring a chance to accept features. */
171	vring_transport_features(vdev);
172
173	/* Give virtio_pci a chance to accept features. */
174	vp_transport_features(vdev, features);
175
176	if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
177		dev_err(&vdev->dev, "virtio: device uses modern interface "
178			"but does not have VIRTIO_F_VERSION_1\n");
179		return -EINVAL;
180	}
181
182	vp_iowrite32(0, &vp_dev->common->guest_feature_select);
183	vp_iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
184	vp_iowrite32(1, &vp_dev->common->guest_feature_select);
185	vp_iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
186
187	return 0;
188}
189
190/* virtio config->get() implementation */
191static void vp_get(struct virtio_device *vdev, unsigned offset,
192		   void *buf, unsigned len)
193{
194	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 
 
195	u8 b;
196	__le16 w;
197	__le32 l;
198
199	BUG_ON(offset + len > vp_dev->device_len);
200
201	switch (len) {
202	case 1:
203		b = ioread8(vp_dev->device + offset);
204		memcpy(buf, &b, sizeof b);
205		break;
206	case 2:
207		w = cpu_to_le16(ioread16(vp_dev->device + offset));
208		memcpy(buf, &w, sizeof w);
209		break;
210	case 4:
211		l = cpu_to_le32(ioread32(vp_dev->device + offset));
212		memcpy(buf, &l, sizeof l);
213		break;
214	case 8:
215		l = cpu_to_le32(ioread32(vp_dev->device + offset));
216		memcpy(buf, &l, sizeof l);
217		l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
218		memcpy(buf + sizeof l, &l, sizeof l);
219		break;
220	default:
221		BUG();
222	}
223}
224
225/* the config->set() implementation.  it's symmetric to the config->get()
226 * implementation */
227static void vp_set(struct virtio_device *vdev, unsigned offset,
228		   const void *buf, unsigned len)
229{
230	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 
 
231	u8 b;
232	__le16 w;
233	__le32 l;
234
235	BUG_ON(offset + len > vp_dev->device_len);
236
237	switch (len) {
238	case 1:
239		memcpy(&b, buf, sizeof b);
240		iowrite8(b, vp_dev->device + offset);
241		break;
242	case 2:
243		memcpy(&w, buf, sizeof w);
244		iowrite16(le16_to_cpu(w), vp_dev->device + offset);
245		break;
246	case 4:
247		memcpy(&l, buf, sizeof l);
248		iowrite32(le32_to_cpu(l), vp_dev->device + offset);
249		break;
250	case 8:
251		memcpy(&l, buf, sizeof l);
252		iowrite32(le32_to_cpu(l), vp_dev->device + offset);
253		memcpy(&l, buf + sizeof l, sizeof l);
254		iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
255		break;
256	default:
257		BUG();
258	}
259}
260
261static u32 vp_generation(struct virtio_device *vdev)
262{
263	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
264	return vp_ioread8(&vp_dev->common->config_generation);
 
265}
266
267/* config->{get,set}_status() implementations */
268static u8 vp_get_status(struct virtio_device *vdev)
269{
270	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
271	return vp_ioread8(&vp_dev->common->device_status);
 
272}
273
274static void vp_set_status(struct virtio_device *vdev, u8 status)
275{
276	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 
277	/* We should never be setting status to 0. */
278	BUG_ON(status == 0);
279	vp_iowrite8(status, &vp_dev->common->device_status);
 
 
280}
281
282static void vp_reset(struct virtio_device *vdev)
283{
284	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 
 
285	/* 0 status means a reset. */
286	vp_iowrite8(0, &vp_dev->common->device_status);
287	/* After writing 0 to device_status, the driver MUST wait for a read of
288	 * device_status to return 0 before reinitializing the device.
289	 * This will flush out the status write, and flush in device writes,
290	 * including MSI-X interrupts, if any.
291	 */
292	while (vp_ioread8(&vp_dev->common->device_status))
293		msleep(1);
 
 
 
294	/* Flush pending VQ/configuration callbacks. */
295	vp_synchronize_vectors(vdev);
296}
297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
299{
300	/* Setup the vector used for configuration events */
301	vp_iowrite16(vector, &vp_dev->common->msix_config);
302	/* Verify we had enough resources to assign the vector */
303	/* Will also flush the write out to device */
304	return vp_ioread16(&vp_dev->common->msix_config);
 
 
 
 
 
305}
306
307static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
308				  struct virtio_pci_vq_info *info,
309				  unsigned index,
310				  void (*callback)(struct virtqueue *vq),
311				  const char *name,
312				  bool ctx,
313				  u16 msix_vec)
314{
315	struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
 
 
316	struct virtqueue *vq;
317	u16 num, off;
 
318	int err;
319
320	if (index >= vp_ioread16(&cfg->num_queues))
321		return ERR_PTR(-ENOENT);
 
 
322
323	/* Select the queue we're interested in */
324	vp_iowrite16(index, &cfg->queue_select);
 
325
 
 
326	/* Check if queue is either not available or already active. */
327	num = vp_ioread16(&cfg->queue_size);
328	if (!num || vp_ioread16(&cfg->queue_enable))
329		return ERR_PTR(-ENOENT);
330
331	if (num & (num - 1)) {
332		dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
333		return ERR_PTR(-EINVAL);
334	}
335
336	/* get offset of notification word for this vq */
337	off = vp_ioread16(&cfg->queue_notify_off);
338
339	info->msix_vector = msix_vec;
340
341	/* create the vring */
342	vq = vring_create_virtqueue(index, num,
343				    SMP_CACHE_BYTES, &vp_dev->vdev,
344				    true, true, ctx,
345				    vp_notify, callback, name);
346	if (!vq)
347		return ERR_PTR(-ENOMEM);
348
349	/* activate the queue */
350	vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
351	vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
352			     &cfg->queue_desc_lo, &cfg->queue_desc_hi);
353	vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
354			     &cfg->queue_avail_lo, &cfg->queue_avail_hi);
355	vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
356			     &cfg->queue_used_lo, &cfg->queue_used_hi);
357
358	if (vp_dev->notify_base) {
359		/* offset should not wrap */
360		if ((u64)off * vp_dev->notify_offset_multiplier + 2
361		    > vp_dev->notify_len) {
362			dev_warn(&vp_dev->pci_dev->dev,
363				 "bad notification offset %u (x %u) "
364				 "for queue %u > %zd",
365				 off, vp_dev->notify_offset_multiplier,
366				 index, vp_dev->notify_len);
367			err = -EINVAL;
368			goto err_map_notify;
369		}
370		vq->priv = (void __force *)vp_dev->notify_base +
371			off * vp_dev->notify_offset_multiplier;
372	} else {
373		vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
374					  vp_dev->notify_map_cap, 2, 2,
375					  off * vp_dev->notify_offset_multiplier, 2,
376					  NULL);
377	}
378
 
 
 
 
 
379	if (!vq->priv) {
380		err = -ENOMEM;
381		goto err_map_notify;
382	}
383
384	if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
385		vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
386		msix_vec = vp_ioread16(&cfg->queue_msix_vector);
387		if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
388			err = -EBUSY;
389			goto err_assign_vector;
390		}
391	}
392
393	return vq;
394
395err_assign_vector:
396	if (!vp_dev->notify_base)
397		pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
398err_map_notify:
399	vring_del_virtqueue(vq);
400	return ERR_PTR(err);
401}
402
403static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
404			      struct virtqueue *vqs[],
405			      vq_callback_t *callbacks[],
406			      const char * const names[], const bool *ctx,
407			      struct irq_affinity *desc)
408{
409	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
410	struct virtqueue *vq;
411	int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
412
413	if (rc)
414		return rc;
415
416	/* Select and activate all queues. Has to be done last: once we do
417	 * this, there's no way to go back except reset.
418	 */
419	list_for_each_entry(vq, &vdev->vqs, list) {
420		vp_iowrite16(vq->index, &vp_dev->common->queue_select);
421		vp_iowrite16(1, &vp_dev->common->queue_enable);
422	}
423
424	return 0;
425}
426
427static void del_vq(struct virtio_pci_vq_info *info)
428{
429	struct virtqueue *vq = info->vq;
430	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
 
431
432	vp_iowrite16(vq->index, &vp_dev->common->queue_select);
433
434	if (vp_dev->msix_enabled) {
435		vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
436			     &vp_dev->common->queue_msix_vector);
437		/* Flush the write out to device */
438		vp_ioread16(&vp_dev->common->queue_msix_vector);
439	}
440
441	if (!vp_dev->notify_base)
442		pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
 
 
 
 
443
444	vring_del_virtqueue(vq);
445}
446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
448	.get		= NULL,
449	.set		= NULL,
450	.generation	= vp_generation,
451	.get_status	= vp_get_status,
452	.set_status	= vp_set_status,
453	.reset		= vp_reset,
454	.find_vqs	= vp_modern_find_vqs,
455	.del_vqs	= vp_del_vqs,
 
456	.get_features	= vp_get_features,
457	.finalize_features = vp_finalize_features,
458	.bus_name	= vp_bus_name,
459	.set_vq_affinity = vp_set_vq_affinity,
460	.get_vq_affinity = vp_get_vq_affinity,
 
 
 
 
 
461};
462
463static const struct virtio_config_ops virtio_pci_config_ops = {
464	.get		= vp_get,
465	.set		= vp_set,
466	.generation	= vp_generation,
467	.get_status	= vp_get_status,
468	.set_status	= vp_set_status,
469	.reset		= vp_reset,
470	.find_vqs	= vp_modern_find_vqs,
471	.del_vqs	= vp_del_vqs,
 
472	.get_features	= vp_get_features,
473	.finalize_features = vp_finalize_features,
474	.bus_name	= vp_bus_name,
475	.set_vq_affinity = vp_set_vq_affinity,
476	.get_vq_affinity = vp_get_vq_affinity,
 
 
 
 
 
477};
478
479/**
480 * virtio_pci_find_capability - walk capabilities to find device info.
481 * @dev: the pci device
482 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
483 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
484 * @bars: the bitmask of BARs
485 *
486 * Returns offset of the capability, or 0.
487 */
488static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
489					     u32 ioresource_types, int *bars)
490{
491	int pos;
492
493	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
494	     pos > 0;
495	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
496		u8 type, bar;
497		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
498							 cfg_type),
499				     &type);
500		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
501							 bar),
502				     &bar);
503
504		/* Ignore structures with reserved BAR values */
505		if (bar > 0x5)
506			continue;
507
508		if (type == cfg_type) {
509			if (pci_resource_len(dev, bar) &&
510			    pci_resource_flags(dev, bar) & ioresource_types) {
511				*bars |= (1 << bar);
512				return pos;
513			}
514		}
515	}
516	return 0;
517}
518
519/* This is part of the ABI.  Don't screw with it. */
520static inline void check_offsets(void)
521{
522	/* Note: disk space was harmed in compilation of this function. */
523	BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
524		     offsetof(struct virtio_pci_cap, cap_vndr));
525	BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
526		     offsetof(struct virtio_pci_cap, cap_next));
527	BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
528		     offsetof(struct virtio_pci_cap, cap_len));
529	BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
530		     offsetof(struct virtio_pci_cap, cfg_type));
531	BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
532		     offsetof(struct virtio_pci_cap, bar));
533	BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
534		     offsetof(struct virtio_pci_cap, offset));
535	BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
536		     offsetof(struct virtio_pci_cap, length));
537	BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
538		     offsetof(struct virtio_pci_notify_cap,
539			      notify_off_multiplier));
540	BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
541		     offsetof(struct virtio_pci_common_cfg,
542			      device_feature_select));
543	BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
544		     offsetof(struct virtio_pci_common_cfg, device_feature));
545	BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
546		     offsetof(struct virtio_pci_common_cfg,
547			      guest_feature_select));
548	BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
549		     offsetof(struct virtio_pci_common_cfg, guest_feature));
550	BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
551		     offsetof(struct virtio_pci_common_cfg, msix_config));
552	BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
553		     offsetof(struct virtio_pci_common_cfg, num_queues));
554	BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
555		     offsetof(struct virtio_pci_common_cfg, device_status));
556	BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
557		     offsetof(struct virtio_pci_common_cfg, config_generation));
558	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
559		     offsetof(struct virtio_pci_common_cfg, queue_select));
560	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
561		     offsetof(struct virtio_pci_common_cfg, queue_size));
562	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
563		     offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
564	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
565		     offsetof(struct virtio_pci_common_cfg, queue_enable));
566	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
567		     offsetof(struct virtio_pci_common_cfg, queue_notify_off));
568	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
569		     offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
570	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
571		     offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
572	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
573		     offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
574	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
575		     offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
576	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
577		     offsetof(struct virtio_pci_common_cfg, queue_used_lo));
578	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
579		     offsetof(struct virtio_pci_common_cfg, queue_used_hi));
580}
581
582/* the PCI probing function */
583int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
584{
 
585	struct pci_dev *pci_dev = vp_dev->pci_dev;
586	int err, common, isr, notify, device;
587	u32 notify_length;
588	u32 notify_offset;
589
590	check_offsets();
591
592	/* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
593	if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
594		return -ENODEV;
595
596	if (pci_dev->device < 0x1040) {
597		/* Transitional devices: use the PCI subsystem device id as
598		 * virtio device id, same as legacy driver always did.
599		 */
600		vp_dev->vdev.id.device = pci_dev->subsystem_device;
601	} else {
602		/* Modern devices: simply use PCI device id, but start from 0x1040. */
603		vp_dev->vdev.id.device = pci_dev->device - 0x1040;
604	}
605	vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
606
607	/* check for a common config: if not, use legacy mode (bar 0). */
608	common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
609					    IORESOURCE_IO | IORESOURCE_MEM,
610					    &vp_dev->modern_bars);
611	if (!common) {
612		dev_info(&pci_dev->dev,
613			 "virtio_pci: leaving for legacy driver\n");
614		return -ENODEV;
615	}
616
617	/* If common is there, these should be too... */
618	isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
619					 IORESOURCE_IO | IORESOURCE_MEM,
620					 &vp_dev->modern_bars);
621	notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
622					    IORESOURCE_IO | IORESOURCE_MEM,
623					    &vp_dev->modern_bars);
624	if (!isr || !notify) {
625		dev_err(&pci_dev->dev,
626			"virtio_pci: missing capabilities %i/%i/%i\n",
627			common, isr, notify);
628		return -EINVAL;
629	}
630
631	err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
632	if (err)
633		err = dma_set_mask_and_coherent(&pci_dev->dev,
634						DMA_BIT_MASK(32));
635	if (err)
636		dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
637
638	/* Device capability is only mandatory for devices that have
639	 * device-specific configuration.
640	 */
641	device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
642					    IORESOURCE_IO | IORESOURCE_MEM,
643					    &vp_dev->modern_bars);
644
645	err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
646					   "virtio-pci-modern");
647	if (err)
648		return err;
649
650	err = -EINVAL;
651	vp_dev->common = map_capability(pci_dev, common,
652					sizeof(struct virtio_pci_common_cfg), 4,
653					0, sizeof(struct virtio_pci_common_cfg),
654					NULL);
655	if (!vp_dev->common)
656		goto err_map_common;
657	vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
658				     0, 1,
659				     NULL);
660	if (!vp_dev->isr)
661		goto err_map_isr;
662
663	/* Read notify_off_multiplier from config space. */
664	pci_read_config_dword(pci_dev,
665			      notify + offsetof(struct virtio_pci_notify_cap,
666						notify_off_multiplier),
667			      &vp_dev->notify_offset_multiplier);
668	/* Read notify length and offset from config space. */
669	pci_read_config_dword(pci_dev,
670			      notify + offsetof(struct virtio_pci_notify_cap,
671						cap.length),
672			      &notify_length);
673
674	pci_read_config_dword(pci_dev,
675			      notify + offsetof(struct virtio_pci_notify_cap,
676						cap.offset),
677			      &notify_offset);
678
679	/* We don't know how many VQs we'll map, ahead of the time.
680	 * If notify length is small, map it all now.
681	 * Otherwise, map each VQ individually later.
682	 */
683	if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
684		vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
685						     0, notify_length,
686						     &vp_dev->notify_len);
687		if (!vp_dev->notify_base)
688			goto err_map_notify;
689	} else {
690		vp_dev->notify_map_cap = notify;
691	}
692
693	/* Again, we don't know how much we should map, but PAGE_SIZE
694	 * is more than enough for all existing devices.
695	 */
696	if (device) {
697		vp_dev->device = map_capability(pci_dev, device, 0, 4,
698						0, PAGE_SIZE,
699						&vp_dev->device_len);
700		if (!vp_dev->device)
701			goto err_map_device;
702
703		vp_dev->vdev.config = &virtio_pci_config_ops;
704	} else {
705		vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
706	}
707
708	vp_dev->config_vector = vp_config_vector;
709	vp_dev->setup_vq = setup_vq;
710	vp_dev->del_vq = del_vq;
 
 
 
711
 
712	return 0;
713
714err_map_device:
715	if (vp_dev->notify_base)
716		pci_iounmap(pci_dev, vp_dev->notify_base);
717err_map_notify:
718	pci_iounmap(pci_dev, vp_dev->isr);
719err_map_isr:
720	pci_iounmap(pci_dev, vp_dev->common);
721err_map_common:
722	return err;
723}
724
725void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
726{
727	struct pci_dev *pci_dev = vp_dev->pci_dev;
728
729	if (vp_dev->device)
730		pci_iounmap(pci_dev, vp_dev->device);
731	if (vp_dev->notify_base)
732		pci_iounmap(pci_dev, vp_dev->notify_base);
733	pci_iounmap(pci_dev, vp_dev->isr);
734	pci_iounmap(pci_dev, vp_dev->common);
735	pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
736}
v6.9.4
  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#define VIRTIO_AVQ_SGS_MAX	4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 23
 24static u64 vp_get_features(struct virtio_device *vdev)
 25{
 26	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 
 27
 28	return vp_modern_get_features(&vp_dev->mdev);
 
 
 29}
 30
 31static bool vp_is_avq(struct virtio_device *vdev, unsigned int index)
 32{
 33	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 
 34
 35	if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
 36		return false;
 37
 38	return index == vp_dev->admin_vq.vq_index;
 39}
 40
 41static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
 42				    u16 opcode,
 43				    struct scatterlist **sgs,
 44				    unsigned int out_num,
 45				    unsigned int in_num,
 46				    void *data)
 47{
 48	struct virtqueue *vq;
 49	int ret, len;
 50
 51	vq = admin_vq->info.vq;
 52	if (!vq)
 53		return -EIO;
 54
 55	if (opcode != VIRTIO_ADMIN_CMD_LIST_QUERY &&
 56	    opcode != VIRTIO_ADMIN_CMD_LIST_USE &&
 57	    !((1ULL << opcode) & admin_vq->supported_cmds))
 58		return -EOPNOTSUPP;
 59
 60	ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, data, GFP_KERNEL);
 61	if (ret < 0)
 62		return -EIO;
 63
 64	if (unlikely(!virtqueue_kick(vq)))
 65		return -EIO;
 66
 67	while (!virtqueue_get_buf(vq, &len) &&
 68	       !virtqueue_is_broken(vq))
 69		cpu_relax();
 70
 71	if (virtqueue_is_broken(vq))
 72		return -EIO;
 73
 74	return 0;
 75}
 76
 77int vp_modern_admin_cmd_exec(struct virtio_device *vdev,
 78			     struct virtio_admin_cmd *cmd)
 
 
 
 79{
 80	struct scatterlist *sgs[VIRTIO_AVQ_SGS_MAX], hdr, stat;
 81	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 82	struct virtio_admin_cmd_status *va_status;
 83	unsigned int out_num = 0, in_num = 0;
 84	struct virtio_admin_cmd_hdr *va_hdr;
 85	u16 status;
 86	int ret;
 87
 88	if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
 89		return -EOPNOTSUPP;
 
 
 
 
 
 90
 91	va_status = kzalloc(sizeof(*va_status), GFP_KERNEL);
 92	if (!va_status)
 93		return -ENOMEM;
 
 
 
 94
 95	va_hdr = kzalloc(sizeof(*va_hdr), GFP_KERNEL);
 96	if (!va_hdr) {
 97		ret = -ENOMEM;
 98		goto err_alloc;
 
 99	}
100
101	va_hdr->opcode = cmd->opcode;
102	va_hdr->group_type = cmd->group_type;
103	va_hdr->group_member_id = cmd->group_member_id;
104
105	/* Add header */
106	sg_init_one(&hdr, va_hdr, sizeof(*va_hdr));
107	sgs[out_num] = &hdr;
108	out_num++;
109
110	if (cmd->data_sg) {
111		sgs[out_num] = cmd->data_sg;
112		out_num++;
 
 
113	}
114
115	/* Add return status */
116	sg_init_one(&stat, va_status, sizeof(*va_status));
117	sgs[out_num + in_num] = &stat;
118	in_num++;
119
120	if (cmd->result_sg) {
121		sgs[out_num + in_num] = cmd->result_sg;
122		in_num++;
 
 
123	}
124
125	mutex_lock(&vp_dev->admin_vq.cmd_lock);
126	ret = virtqueue_exec_admin_cmd(&vp_dev->admin_vq,
127				       le16_to_cpu(cmd->opcode),
128				       sgs, out_num, in_num, sgs);
129	mutex_unlock(&vp_dev->admin_vq.cmd_lock);
130
131	if (ret) {
132		dev_err(&vdev->dev,
133			"Failed to execute command on admin vq: %d\n.", ret);
134		goto err_cmd_exec;
135	}
136
137	status = le16_to_cpu(va_status->status);
138	if (status != VIRTIO_ADMIN_STATUS_OK) {
139		dev_err(&vdev->dev,
140			"admin command error: status(%#x) qualifier(%#x)\n",
141			status, le16_to_cpu(va_status->status_qualifier));
142		ret = -status;
 
 
143	}
144
145err_cmd_exec:
146	kfree(va_hdr);
147err_alloc:
148	kfree(va_status);
149	return ret;
 
150}
151
152static void virtio_pci_admin_cmd_list_init(struct virtio_device *virtio_dev)
153{
154	struct virtio_pci_device *vp_dev = to_vp_device(virtio_dev);
155	struct virtio_admin_cmd cmd = {};
156	struct scatterlist result_sg;
157	struct scatterlist data_sg;
158	__le64 *data;
159	int ret;
160
161	data = kzalloc(sizeof(*data), GFP_KERNEL);
162	if (!data)
163		return;
164
165	sg_init_one(&result_sg, data, sizeof(*data));
166	cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_QUERY);
167	cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
168	cmd.result_sg = &result_sg;
169
170	ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
171	if (ret)
172		goto end;
173
174	*data &= cpu_to_le64(VIRTIO_ADMIN_CMD_BITMAP);
175	sg_init_one(&data_sg, data, sizeof(*data));
176	cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_USE);
177	cmd.data_sg = &data_sg;
178	cmd.result_sg = NULL;
179
180	ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
181	if (ret)
182		goto end;
183
184	vp_dev->admin_vq.supported_cmds = le64_to_cpu(*data);
185end:
186	kfree(data);
187}
188
189static void vp_modern_avq_activate(struct virtio_device *vdev)
190{
191	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
192	struct virtio_pci_admin_vq *admin_vq = &vp_dev->admin_vq;
193
194	if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
195		return;
 
 
196
197	__virtqueue_unbreak(admin_vq->info.vq);
198	virtio_pci_admin_cmd_list_init(vdev);
199}
200
201static void vp_modern_avq_deactivate(struct virtio_device *vdev)
202{
203	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
204	struct virtio_pci_admin_vq *admin_vq = &vp_dev->admin_vq;
205
206	if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
207		return;
208
209	__virtqueue_break(admin_vq->info.vq);
210}
211
212static void vp_transport_features(struct virtio_device *vdev, u64 features)
213{
214	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
215	struct pci_dev *pci_dev = vp_dev->pci_dev;
216
217	if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
218			pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
219		__virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
220
221	if (features & BIT_ULL(VIRTIO_F_RING_RESET))
222		__virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
223
224	if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ))
225		__virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ);
226}
227
228static int __vp_check_common_size_one_feature(struct virtio_device *vdev, u32 fbit,
229					    u32 offset, const char *fname)
230{
231	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
232
233	if (!__virtio_test_bit(vdev, fbit))
234		return 0;
235
236	if (likely(vp_dev->mdev.common_len >= offset))
237		return 0;
238
239	dev_err(&vdev->dev,
240		"virtio: common cfg size(%zu) does not match the feature %s\n",
241		vp_dev->mdev.common_len, fname);
242
243	return -EINVAL;
244}
245
246#define vp_check_common_size_one_feature(vdev, fbit, field) \
247	__vp_check_common_size_one_feature(vdev, fbit, \
248		offsetofend(struct virtio_pci_modern_common_cfg, field), #fbit)
249
250static int vp_check_common_size(struct virtio_device *vdev)
251{
252	if (vp_check_common_size_one_feature(vdev, VIRTIO_F_NOTIF_CONFIG_DATA, queue_notify_data))
253		return -EINVAL;
254
255	if (vp_check_common_size_one_feature(vdev, VIRTIO_F_RING_RESET, queue_reset))
256		return -EINVAL;
257
258	if (vp_check_common_size_one_feature(vdev, VIRTIO_F_ADMIN_VQ, admin_queue_num))
259		return -EINVAL;
260
261	return 0;
262}
263
264/* virtio config->finalize_features() implementation */
265static int vp_finalize_features(struct virtio_device *vdev)
266{
267	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
268	u64 features = vdev->features;
269
270	/* Give virtio_ring a chance to accept features. */
271	vring_transport_features(vdev);
272
273	/* Give virtio_pci a chance to accept features. */
274	vp_transport_features(vdev, features);
275
276	if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
277		dev_err(&vdev->dev, "virtio: device uses modern interface "
278			"but does not have VIRTIO_F_VERSION_1\n");
279		return -EINVAL;
280	}
281
282	if (vp_check_common_size(vdev))
283		return -EINVAL;
284
285	vp_modern_set_features(&vp_dev->mdev, vdev->features);
286
287	return 0;
288}
289
290/* virtio config->get() implementation */
291static void vp_get(struct virtio_device *vdev, unsigned int offset,
292		   void *buf, unsigned int len)
293{
294	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
295	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
296	void __iomem *device = mdev->device;
297	u8 b;
298	__le16 w;
299	__le32 l;
300
301	BUG_ON(offset + len > mdev->device_len);
302
303	switch (len) {
304	case 1:
305		b = ioread8(device + offset);
306		memcpy(buf, &b, sizeof b);
307		break;
308	case 2:
309		w = cpu_to_le16(ioread16(device + offset));
310		memcpy(buf, &w, sizeof w);
311		break;
312	case 4:
313		l = cpu_to_le32(ioread32(device + offset));
314		memcpy(buf, &l, sizeof l);
315		break;
316	case 8:
317		l = cpu_to_le32(ioread32(device + offset));
318		memcpy(buf, &l, sizeof l);
319		l = cpu_to_le32(ioread32(device + offset + sizeof l));
320		memcpy(buf + sizeof l, &l, sizeof l);
321		break;
322	default:
323		BUG();
324	}
325}
326
327/* the config->set() implementation.  it's symmetric to the config->get()
328 * implementation */
329static void vp_set(struct virtio_device *vdev, unsigned int offset,
330		   const void *buf, unsigned int len)
331{
332	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
333	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
334	void __iomem *device = mdev->device;
335	u8 b;
336	__le16 w;
337	__le32 l;
338
339	BUG_ON(offset + len > mdev->device_len);
340
341	switch (len) {
342	case 1:
343		memcpy(&b, buf, sizeof b);
344		iowrite8(b, device + offset);
345		break;
346	case 2:
347		memcpy(&w, buf, sizeof w);
348		iowrite16(le16_to_cpu(w), device + offset);
349		break;
350	case 4:
351		memcpy(&l, buf, sizeof l);
352		iowrite32(le32_to_cpu(l), device + offset);
353		break;
354	case 8:
355		memcpy(&l, buf, sizeof l);
356		iowrite32(le32_to_cpu(l), device + offset);
357		memcpy(&l, buf + sizeof l, sizeof l);
358		iowrite32(le32_to_cpu(l), device + offset + sizeof l);
359		break;
360	default:
361		BUG();
362	}
363}
364
365static u32 vp_generation(struct virtio_device *vdev)
366{
367	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
368
369	return vp_modern_generation(&vp_dev->mdev);
370}
371
372/* config->{get,set}_status() implementations */
373static u8 vp_get_status(struct virtio_device *vdev)
374{
375	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
376
377	return vp_modern_get_status(&vp_dev->mdev);
378}
379
380static void vp_set_status(struct virtio_device *vdev, u8 status)
381{
382	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
383
384	/* We should never be setting status to 0. */
385	BUG_ON(status == 0);
386	vp_modern_set_status(&vp_dev->mdev, status);
387	if (status & VIRTIO_CONFIG_S_DRIVER_OK)
388		vp_modern_avq_activate(vdev);
389}
390
391static void vp_reset(struct virtio_device *vdev)
392{
393	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
394	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
395
396	/* 0 status means a reset. */
397	vp_modern_set_status(mdev, 0);
398	/* After writing 0 to device_status, the driver MUST wait for a read of
399	 * device_status to return 0 before reinitializing the device.
400	 * This will flush out the status write, and flush in device writes,
401	 * including MSI-X interrupts, if any.
402	 */
403	while (vp_modern_get_status(mdev))
404		msleep(1);
405
406	vp_modern_avq_deactivate(vdev);
407
408	/* Flush pending VQ/configuration callbacks. */
409	vp_synchronize_vectors(vdev);
410}
411
412static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
413{
414	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
415	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
416	unsigned long index;
417
418	index = vq->index;
419
420	/* activate the queue */
421	vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
422	vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
423				virtqueue_get_avail_addr(vq),
424				virtqueue_get_used_addr(vq));
425
426	if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
427		msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
428		if (msix_vec == VIRTIO_MSI_NO_VECTOR)
429			return -EBUSY;
430	}
431
432	return 0;
433}
434
435static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
436{
437	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
438	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
439	struct virtio_pci_vq_info *info;
440	unsigned long flags;
441
442	if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
443		return -ENOENT;
444
445	vp_modern_set_queue_reset(mdev, vq->index);
446
447	info = vp_dev->vqs[vq->index];
448
449	/* delete vq from irq handler */
450	spin_lock_irqsave(&vp_dev->lock, flags);
451	list_del(&info->node);
452	spin_unlock_irqrestore(&vp_dev->lock, flags);
453
454	INIT_LIST_HEAD(&info->node);
455
456#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
457	__virtqueue_break(vq);
458#endif
459
460	/* For the case where vq has an exclusive irq, call synchronize_irq() to
461	 * wait for completion.
462	 *
463	 * note: We can't use disable_irq() since it conflicts with the affinity
464	 * managed IRQ that is used by some drivers.
465	 */
466	if (vp_dev->per_vq_vectors && info->msix_vector != VIRTIO_MSI_NO_VECTOR)
467		synchronize_irq(pci_irq_vector(vp_dev->pci_dev, info->msix_vector));
468
469	vq->reset = true;
470
471	return 0;
472}
473
474static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
475{
476	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
477	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
478	struct virtio_pci_vq_info *info;
479	unsigned long flags, index;
480	int err;
481
482	if (!vq->reset)
483		return -EBUSY;
484
485	index = vq->index;
486	info = vp_dev->vqs[index];
487
488	if (vp_modern_get_queue_reset(mdev, index))
489		return -EBUSY;
490
491	if (vp_modern_get_queue_enable(mdev, index))
492		return -EBUSY;
493
494	err = vp_active_vq(vq, info->msix_vector);
495	if (err)
496		return err;
497
498	if (vq->callback) {
499		spin_lock_irqsave(&vp_dev->lock, flags);
500		list_add(&info->node, &vp_dev->virtqueues);
501		spin_unlock_irqrestore(&vp_dev->lock, flags);
502	} else {
503		INIT_LIST_HEAD(&info->node);
504	}
505
506#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
507	__virtqueue_unbreak(vq);
508#endif
509
510	vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
511	vq->reset = false;
512
513	return 0;
514}
515
516static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
517{
518	return vp_modern_config_vector(&vp_dev->mdev, vector);
519}
520
521static bool vp_notify_with_data(struct virtqueue *vq)
522{
523	u32 data = vring_notification_data(vq);
524
525	iowrite32(data, (void __iomem *)vq->priv);
526
527	return true;
528}
529
530static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
531				  struct virtio_pci_vq_info *info,
532				  unsigned int index,
533				  void (*callback)(struct virtqueue *vq),
534				  const char *name,
535				  bool ctx,
536				  u16 msix_vec)
537{
538
539	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
540	bool (*notify)(struct virtqueue *vq);
541	struct virtqueue *vq;
542	bool is_avq;
543	u16 num;
544	int err;
545
546	if (__virtio_test_bit(&vp_dev->vdev, VIRTIO_F_NOTIFICATION_DATA))
547		notify = vp_notify_with_data;
548	else
549		notify = vp_notify;
550
551	is_avq = vp_is_avq(&vp_dev->vdev, index);
552	if (index >= vp_modern_get_num_queues(mdev) && !is_avq)
553		return ERR_PTR(-EINVAL);
554
555	num = is_avq ?
556		VIRTIO_AVQ_SGS_MAX : vp_modern_get_queue_size(mdev, index);
557	/* Check if queue is either not available or already active. */
558	if (!num || vp_modern_get_queue_enable(mdev, index))
 
559		return ERR_PTR(-ENOENT);
560
 
 
 
 
 
 
 
 
561	info->msix_vector = msix_vec;
562
563	/* create the vring */
564	vq = vring_create_virtqueue(index, num,
565				    SMP_CACHE_BYTES, &vp_dev->vdev,
566				    true, true, ctx,
567				    notify, callback, name);
568	if (!vq)
569		return ERR_PTR(-ENOMEM);
570
571	vq->num_max = num;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572
573	err = vp_active_vq(vq, msix_vec);
574	if (err)
575		goto err;
576
577	vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
578	if (!vq->priv) {
579		err = -ENOMEM;
580		goto err;
581	}
582
583	if (is_avq) {
584		mutex_lock(&vp_dev->admin_vq.cmd_lock);
585		vp_dev->admin_vq.info.vq = vq;
586		mutex_unlock(&vp_dev->admin_vq.cmd_lock);
 
 
 
587	}
588
589	return vq;
590
591err:
 
 
 
592	vring_del_virtqueue(vq);
593	return ERR_PTR(err);
594}
595
596static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
597			      struct virtqueue *vqs[],
598			      vq_callback_t *callbacks[],
599			      const char * const names[], const bool *ctx,
600			      struct irq_affinity *desc)
601{
602	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
603	struct virtqueue *vq;
604	int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
605
606	if (rc)
607		return rc;
608
609	/* Select and activate all queues. Has to be done last: once we do
610	 * this, there's no way to go back except reset.
611	 */
612	list_for_each_entry(vq, &vdev->vqs, list)
613		vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
 
 
614
615	return 0;
616}
617
618static void del_vq(struct virtio_pci_vq_info *info)
619{
620	struct virtqueue *vq = info->vq;
621	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
622	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
623
624	if (vp_is_avq(&vp_dev->vdev, vq->index)) {
625		mutex_lock(&vp_dev->admin_vq.cmd_lock);
626		vp_dev->admin_vq.info.vq = NULL;
627		mutex_unlock(&vp_dev->admin_vq.cmd_lock);
 
 
 
628	}
629
630	if (vp_dev->msix_enabled)
631		vp_modern_queue_vector(mdev, vq->index,
632				       VIRTIO_MSI_NO_VECTOR);
633
634	if (!mdev->notify_base)
635		pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
636
637	vring_del_virtqueue(vq);
638}
639
640static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
641				   u8 *bar, u64 *offset, u64 *len)
642{
643	int pos;
644
645	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
646	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
647		u8 type, cap_len, id, res_bar;
648		u32 tmp32;
649		u64 res_offset, res_length;
650
651		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
652							 cfg_type), &type);
653		if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
654			continue;
655
656		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
657							 cap_len), &cap_len);
658		if (cap_len != sizeof(struct virtio_pci_cap64)) {
659			dev_err(&dev->dev, "%s: shm cap with bad size offset:"
660				" %d size: %d\n", __func__, pos, cap_len);
661			continue;
662		}
663
664		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
665							 id), &id);
666		if (id != required_id)
667			continue;
668
669		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
670							 bar), &res_bar);
671		if (res_bar >= PCI_STD_NUM_BARS)
672			continue;
673
674		/* Type and ID match, and the BAR value isn't reserved.
675		 * Looks good.
676		 */
677
678		/* Read the lower 32bit of length and offset */
679		pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
680							  offset), &tmp32);
681		res_offset = tmp32;
682		pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
683							  length), &tmp32);
684		res_length = tmp32;
685
686		/* and now the top half */
687		pci_read_config_dword(dev,
688				      pos + offsetof(struct virtio_pci_cap64,
689						     offset_hi), &tmp32);
690		res_offset |= ((u64)tmp32) << 32;
691		pci_read_config_dword(dev,
692				      pos + offsetof(struct virtio_pci_cap64,
693						     length_hi), &tmp32);
694		res_length |= ((u64)tmp32) << 32;
695
696		*bar = res_bar;
697		*offset = res_offset;
698		*len = res_length;
699
700		return pos;
701	}
702	return 0;
703}
704
705static bool vp_get_shm_region(struct virtio_device *vdev,
706			      struct virtio_shm_region *region, u8 id)
707{
708	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
709	struct pci_dev *pci_dev = vp_dev->pci_dev;
710	u8 bar;
711	u64 offset, len;
712	phys_addr_t phys_addr;
713	size_t bar_len;
714
715	if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
716		return false;
717
718	phys_addr = pci_resource_start(pci_dev, bar);
719	bar_len = pci_resource_len(pci_dev, bar);
720
721	if ((offset + len) < offset) {
722		dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
723			__func__);
724		return false;
725	}
726
727	if (offset + len > bar_len) {
728		dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
729			__func__);
730		return false;
731	}
732
733	region->len = len;
734	region->addr = (u64) phys_addr + offset;
735
736	return true;
737}
738
739static int vp_modern_create_avq(struct virtio_device *vdev)
740{
741	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
742	struct virtio_pci_admin_vq *avq;
743	struct virtqueue *vq;
744	u16 admin_q_num;
745
746	if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
747		return 0;
748
749	admin_q_num = vp_modern_avq_num(&vp_dev->mdev);
750	if (!admin_q_num)
751		return -EINVAL;
752
753	avq = &vp_dev->admin_vq;
754	avq->vq_index = vp_modern_avq_index(&vp_dev->mdev);
755	sprintf(avq->name, "avq.%u", avq->vq_index);
756	vq = vp_dev->setup_vq(vp_dev, &vp_dev->admin_vq.info, avq->vq_index, NULL,
757			      avq->name, NULL, VIRTIO_MSI_NO_VECTOR);
758	if (IS_ERR(vq)) {
759		dev_err(&vdev->dev, "failed to setup admin virtqueue, err=%ld",
760			PTR_ERR(vq));
761		return PTR_ERR(vq);
762	}
763
764	vp_modern_set_queue_enable(&vp_dev->mdev, avq->info.vq->index, true);
765	return 0;
766}
767
768static void vp_modern_destroy_avq(struct virtio_device *vdev)
769{
770	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
771
772	if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
773		return;
774
775	vp_dev->del_vq(&vp_dev->admin_vq.info);
776}
777
778static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
779	.get		= NULL,
780	.set		= NULL,
781	.generation	= vp_generation,
782	.get_status	= vp_get_status,
783	.set_status	= vp_set_status,
784	.reset		= vp_reset,
785	.find_vqs	= vp_modern_find_vqs,
786	.del_vqs	= vp_del_vqs,
787	.synchronize_cbs = vp_synchronize_vectors,
788	.get_features	= vp_get_features,
789	.finalize_features = vp_finalize_features,
790	.bus_name	= vp_bus_name,
791	.set_vq_affinity = vp_set_vq_affinity,
792	.get_vq_affinity = vp_get_vq_affinity,
793	.get_shm_region  = vp_get_shm_region,
794	.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
795	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
796	.create_avq = vp_modern_create_avq,
797	.destroy_avq = vp_modern_destroy_avq,
798};
799
800static const struct virtio_config_ops virtio_pci_config_ops = {
801	.get		= vp_get,
802	.set		= vp_set,
803	.generation	= vp_generation,
804	.get_status	= vp_get_status,
805	.set_status	= vp_set_status,
806	.reset		= vp_reset,
807	.find_vqs	= vp_modern_find_vqs,
808	.del_vqs	= vp_del_vqs,
809	.synchronize_cbs = vp_synchronize_vectors,
810	.get_features	= vp_get_features,
811	.finalize_features = vp_finalize_features,
812	.bus_name	= vp_bus_name,
813	.set_vq_affinity = vp_set_vq_affinity,
814	.get_vq_affinity = vp_get_vq_affinity,
815	.get_shm_region  = vp_get_shm_region,
816	.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
817	.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
818	.create_avq = vp_modern_create_avq,
819	.destroy_avq = vp_modern_destroy_avq,
820};
821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
822/* the PCI probing function */
823int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
824{
825	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
826	struct pci_dev *pci_dev = vp_dev->pci_dev;
827	int err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
828
829	mdev->pci_dev = pci_dev;
 
 
 
 
 
830
831	err = vp_modern_probe(mdev);
 
832	if (err)
833		return err;
834
835	if (mdev->device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
836		vp_dev->vdev.config = &virtio_pci_config_ops;
837	else
838		vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
 
839
840	vp_dev->config_vector = vp_config_vector;
841	vp_dev->setup_vq = setup_vq;
842	vp_dev->del_vq = del_vq;
843	vp_dev->is_avq = vp_is_avq;
844	vp_dev->isr = mdev->isr;
845	vp_dev->vdev.id = mdev->id;
846
847	mutex_init(&vp_dev->admin_vq.cmd_lock);
848	return 0;
 
 
 
 
 
 
 
 
 
 
849}
850
851void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
852{
853	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
854
855	mutex_destroy(&vp_dev->admin_vq.cmd_lock);
856	vp_modern_remove(mdev);
 
 
 
 
 
857}