1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtio PCI driver - modern (virtio 1.0) device support
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.
8 * Copyright IBM Corp. 2007
9 * Copyright Red Hat, Inc. 2014
17 #include <linux/delay.h>
18 #define VIRTIO_PCI_NO_LEGACY
19 #define VIRTIO_RING_NO_LEGACY
20 #include "virtio_pci_common.h"
23 * Type-safe wrappers for io accesses.
24 * Use these to enforce at compile time the following spec requirement:
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.
30 static inline u8 vp_ioread8(const u8 __iomem *addr)
34 static inline u16 vp_ioread16 (const __le16 __iomem *addr)
36 return ioread16(addr);
39 static inline u32 vp_ioread32(const __le32 __iomem *addr)
41 return ioread32(addr);
44 static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
46 iowrite8(value, addr);
49 static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
51 iowrite16(value, addr);
54 static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
56 iowrite32(value, addr);
59 static void vp_iowrite64_twopart(u64 val,
60 __le32 __iomem *lo, __le32 __iomem *hi)
62 vp_iowrite32((u32)val, lo);
63 vp_iowrite32(val >> 32, hi);
66 static void __iomem *map_capability(struct pci_dev *dev, int off,
76 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
79 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
81 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
84 if (length <= start) {
86 "virtio_pci: bad capability len %u (>%u expected)\n",
91 if (length - start < minlen) {
93 "virtio_pci: bad capability len %u (>=%zu expected)\n",
100 if (start + offset < offset) {
102 "virtio_pci: map wrap-around %u+%u\n",
109 if (offset & (align - 1)) {
111 "virtio_pci: offset %u not aligned to %u\n",
122 if (minlen + offset < minlen ||
123 minlen + offset > pci_resource_len(dev, bar)) {
125 "virtio_pci: map virtio %zu@%u "
126 "out of range on bar %i length %lu\n",
128 bar, (unsigned long)pci_resource_len(dev, bar));
132 p = pci_iomap_range(dev, bar, offset, length);
135 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
136 length, offset, bar);
140 /* virtio config->get_features() implementation */
141 static u64 vp_get_features(struct virtio_device *vdev)
143 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
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);
154 static void vp_transport_features(struct virtio_device *vdev, u64 features)
156 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
157 struct pci_dev *pci_dev = vp_dev->pci_dev;
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);
164 /* virtio config->finalize_features() implementation */
165 static int vp_finalize_features(struct virtio_device *vdev)
167 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
168 u64 features = vdev->features;
170 /* Give virtio_ring a chance to accept features. */
171 vring_transport_features(vdev);
173 /* Give virtio_pci a chance to accept features. */
174 vp_transport_features(vdev, features);
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");
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);
190 /* virtio config->get() implementation */
191 static void vp_get(struct virtio_device *vdev, unsigned offset,
192 void *buf, unsigned len)
194 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
199 BUG_ON(offset + len > vp_dev->device_len);
203 b = ioread8(vp_dev->device + offset);
204 memcpy(buf, &b, sizeof b);
207 w = cpu_to_le16(ioread16(vp_dev->device + offset));
208 memcpy(buf, &w, sizeof w);
211 l = cpu_to_le32(ioread32(vp_dev->device + offset));
212 memcpy(buf, &l, sizeof l);
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);
225 /* the config->set() implementation. it's symmetric to the config->get()
227 static void vp_set(struct virtio_device *vdev, unsigned offset,
228 const void *buf, unsigned len)
230 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
235 BUG_ON(offset + len > vp_dev->device_len);
239 memcpy(&b, buf, sizeof b);
240 iowrite8(b, vp_dev->device + offset);
243 memcpy(&w, buf, sizeof w);
244 iowrite16(le16_to_cpu(w), vp_dev->device + offset);
247 memcpy(&l, buf, sizeof l);
248 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
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);
261 static u32 vp_generation(struct virtio_device *vdev)
263 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
264 return vp_ioread8(&vp_dev->common->config_generation);
267 /* config->{get,set}_status() implementations */
268 static u8 vp_get_status(struct virtio_device *vdev)
270 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
271 return vp_ioread8(&vp_dev->common->device_status);
274 static void vp_set_status(struct virtio_device *vdev, u8 status)
276 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
277 /* We should never be setting status to 0. */
279 vp_iowrite8(status, &vp_dev->common->device_status);
282 static void vp_reset(struct virtio_device *vdev)
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.
292 while (vp_ioread8(&vp_dev->common->device_status))
294 /* Flush pending VQ/configuration callbacks. */
295 vp_synchronize_vectors(vdev);
298 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
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);
307 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
308 struct virtio_pci_vq_info *info,
310 void (*callback)(struct virtqueue *vq),
315 struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
316 struct virtqueue *vq;
320 if (index >= vp_ioread16(&cfg->num_queues))
321 return ERR_PTR(-ENOENT);
323 /* Select the queue we're interested in */
324 vp_iowrite16(index, &cfg->queue_select);
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);
331 if (num & (num - 1)) {
332 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
333 return ERR_PTR(-EINVAL);
336 /* get offset of notification word for this vq */
337 off = vp_ioread16(&cfg->queue_notify_off);
339 info->msix_vector = msix_vec;
341 /* create the vring */
342 vq = vring_create_virtqueue(index, num,
343 SMP_CACHE_BYTES, &vp_dev->vdev,
345 vp_notify, callback, name);
347 return ERR_PTR(-ENOMEM);
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);
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);
370 vq->priv = (void __force *)vp_dev->notify_base +
371 off * vp_dev->notify_offset_multiplier;
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,
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) {
389 goto err_assign_vector;
396 if (!vp_dev->notify_base)
397 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
399 vring_del_virtqueue(vq);
403 static 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)
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);
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.
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);
427 static void del_vq(struct virtio_pci_vq_info *info)
429 struct virtqueue *vq = info->vq;
430 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
432 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
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);
441 if (!vp_dev->notify_base)
442 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
444 vring_del_virtqueue(vq);
447 static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
448 u8 *bar, u64 *offset, u64 *len)
452 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
453 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
454 u8 type, cap_len, id;
456 u64 res_offset, res_length;
458 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
460 if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
463 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
465 if (cap_len != sizeof(struct virtio_pci_cap64)) {
466 dev_err(&dev->dev, "%s: shm cap with bad size offset:"
467 " %d size: %d\n", __func__, pos, cap_len);
471 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
473 if (id != required_id)
476 /* Type, and ID match, looks good */
477 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
480 /* Read the lower 32bit of length and offset */
481 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
484 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
488 /* and now the top half */
489 pci_read_config_dword(dev,
490 pos + offsetof(struct virtio_pci_cap64,
492 res_offset |= ((u64)tmp32) << 32;
493 pci_read_config_dword(dev,
494 pos + offsetof(struct virtio_pci_cap64,
496 res_length |= ((u64)tmp32) << 32;
498 *offset = res_offset;
506 static bool vp_get_shm_region(struct virtio_device *vdev,
507 struct virtio_shm_region *region, u8 id)
509 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
510 struct pci_dev *pci_dev = vp_dev->pci_dev;
513 phys_addr_t phys_addr;
516 if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
519 phys_addr = pci_resource_start(pci_dev, bar);
520 bar_len = pci_resource_len(pci_dev, bar);
522 if ((offset + len) < offset) {
523 dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
528 if (offset + len > bar_len) {
529 dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
535 region->addr = (u64) phys_addr + offset;
540 static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
543 .generation = vp_generation,
544 .get_status = vp_get_status,
545 .set_status = vp_set_status,
547 .find_vqs = vp_modern_find_vqs,
548 .del_vqs = vp_del_vqs,
549 .get_features = vp_get_features,
550 .finalize_features = vp_finalize_features,
551 .bus_name = vp_bus_name,
552 .set_vq_affinity = vp_set_vq_affinity,
553 .get_vq_affinity = vp_get_vq_affinity,
554 .get_shm_region = vp_get_shm_region,
557 static const struct virtio_config_ops virtio_pci_config_ops = {
560 .generation = vp_generation,
561 .get_status = vp_get_status,
562 .set_status = vp_set_status,
564 .find_vqs = vp_modern_find_vqs,
565 .del_vqs = vp_del_vqs,
566 .get_features = vp_get_features,
567 .finalize_features = vp_finalize_features,
568 .bus_name = vp_bus_name,
569 .set_vq_affinity = vp_set_vq_affinity,
570 .get_vq_affinity = vp_get_vq_affinity,
571 .get_shm_region = vp_get_shm_region,
575 * virtio_pci_find_capability - walk capabilities to find device info.
576 * @dev: the pci device
577 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
578 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
579 * @bars: the bitmask of BARs
581 * Returns offset of the capability, or 0.
583 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
584 u32 ioresource_types, int *bars)
588 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
590 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
592 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
595 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
599 /* Ignore structures with reserved BAR values */
603 if (type == cfg_type) {
604 if (pci_resource_len(dev, bar) &&
605 pci_resource_flags(dev, bar) & ioresource_types) {
614 /* This is part of the ABI. Don't screw with it. */
615 static inline void check_offsets(void)
617 /* Note: disk space was harmed in compilation of this function. */
618 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
619 offsetof(struct virtio_pci_cap, cap_vndr));
620 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
621 offsetof(struct virtio_pci_cap, cap_next));
622 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
623 offsetof(struct virtio_pci_cap, cap_len));
624 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
625 offsetof(struct virtio_pci_cap, cfg_type));
626 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
627 offsetof(struct virtio_pci_cap, bar));
628 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
629 offsetof(struct virtio_pci_cap, offset));
630 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
631 offsetof(struct virtio_pci_cap, length));
632 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
633 offsetof(struct virtio_pci_notify_cap,
634 notify_off_multiplier));
635 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
636 offsetof(struct virtio_pci_common_cfg,
637 device_feature_select));
638 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
639 offsetof(struct virtio_pci_common_cfg, device_feature));
640 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
641 offsetof(struct virtio_pci_common_cfg,
642 guest_feature_select));
643 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
644 offsetof(struct virtio_pci_common_cfg, guest_feature));
645 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
646 offsetof(struct virtio_pci_common_cfg, msix_config));
647 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
648 offsetof(struct virtio_pci_common_cfg, num_queues));
649 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
650 offsetof(struct virtio_pci_common_cfg, device_status));
651 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
652 offsetof(struct virtio_pci_common_cfg, config_generation));
653 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
654 offsetof(struct virtio_pci_common_cfg, queue_select));
655 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
656 offsetof(struct virtio_pci_common_cfg, queue_size));
657 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
658 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
659 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
660 offsetof(struct virtio_pci_common_cfg, queue_enable));
661 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
662 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
663 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
664 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
665 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
666 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
667 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
668 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
669 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
670 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
671 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
672 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
673 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
674 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
677 /* the PCI probing function */
678 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
680 struct pci_dev *pci_dev = vp_dev->pci_dev;
681 int err, common, isr, notify, device;
687 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
688 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
691 if (pci_dev->device < 0x1040) {
692 /* Transitional devices: use the PCI subsystem device id as
693 * virtio device id, same as legacy driver always did.
695 vp_dev->vdev.id.device = pci_dev->subsystem_device;
697 /* Modern devices: simply use PCI device id, but start from 0x1040. */
698 vp_dev->vdev.id.device = pci_dev->device - 0x1040;
700 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
702 /* check for a common config: if not, use legacy mode (bar 0). */
703 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
704 IORESOURCE_IO | IORESOURCE_MEM,
705 &vp_dev->modern_bars);
707 dev_info(&pci_dev->dev,
708 "virtio_pci: leaving for legacy driver\n");
712 /* If common is there, these should be too... */
713 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
714 IORESOURCE_IO | IORESOURCE_MEM,
715 &vp_dev->modern_bars);
716 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
717 IORESOURCE_IO | IORESOURCE_MEM,
718 &vp_dev->modern_bars);
719 if (!isr || !notify) {
720 dev_err(&pci_dev->dev,
721 "virtio_pci: missing capabilities %i/%i/%i\n",
722 common, isr, notify);
726 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
728 err = dma_set_mask_and_coherent(&pci_dev->dev,
731 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
733 /* Device capability is only mandatory for devices that have
734 * device-specific configuration.
736 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
737 IORESOURCE_IO | IORESOURCE_MEM,
738 &vp_dev->modern_bars);
740 err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
741 "virtio-pci-modern");
746 vp_dev->common = map_capability(pci_dev, common,
747 sizeof(struct virtio_pci_common_cfg), 4,
748 0, sizeof(struct virtio_pci_common_cfg),
752 vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
758 /* Read notify_off_multiplier from config space. */
759 pci_read_config_dword(pci_dev,
760 notify + offsetof(struct virtio_pci_notify_cap,
761 notify_off_multiplier),
762 &vp_dev->notify_offset_multiplier);
763 /* Read notify length and offset from config space. */
764 pci_read_config_dword(pci_dev,
765 notify + offsetof(struct virtio_pci_notify_cap,
769 pci_read_config_dword(pci_dev,
770 notify + offsetof(struct virtio_pci_notify_cap,
774 /* We don't know how many VQs we'll map, ahead of the time.
775 * If notify length is small, map it all now.
776 * Otherwise, map each VQ individually later.
778 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
779 vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
781 &vp_dev->notify_len);
782 if (!vp_dev->notify_base)
785 vp_dev->notify_map_cap = notify;
788 /* Again, we don't know how much we should map, but PAGE_SIZE
789 * is more than enough for all existing devices.
792 vp_dev->device = map_capability(pci_dev, device, 0, 4,
794 &vp_dev->device_len);
798 vp_dev->vdev.config = &virtio_pci_config_ops;
800 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
803 vp_dev->config_vector = vp_config_vector;
804 vp_dev->setup_vq = setup_vq;
805 vp_dev->del_vq = del_vq;
810 if (vp_dev->notify_base)
811 pci_iounmap(pci_dev, vp_dev->notify_base);
813 pci_iounmap(pci_dev, vp_dev->isr);
815 pci_iounmap(pci_dev, vp_dev->common);
820 void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
822 struct pci_dev *pci_dev = vp_dev->pci_dev;
825 pci_iounmap(pci_dev, vp_dev->device);
826 if (vp_dev->notify_base)
827 pci_iounmap(pci_dev, vp_dev->notify_base);
828 pci_iounmap(pci_dev, vp_dev->isr);
829 pci_iounmap(pci_dev, vp_dev->common);
830 pci_release_selected_regions(pci_dev, vp_dev->modern_bars);