Linux Audio

Check our new training course

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