Linux 4.1.16
[linux/fpc-iii.git] / drivers / virtio / virtio_pci_modern.c
blobe88e0997a8897e2ceaa25891f3ae080048d6e259
1 /*
2 * Virtio PCI driver - modern (virtio 1.0) device support
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.
7 * Copyright IBM Corp. 2007
8 * Copyright Red Hat, Inc. 2014
10 * Authors:
11 * Anthony Liguori <aliguori@us.ibm.com>
12 * Rusty Russell <rusty@rustcorp.com.au>
13 * Michael S. Tsirkin <mst@redhat.com>
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.
20 #define VIRTIO_PCI_NO_LEGACY
21 #include "virtio_pci_common.h"
24 * Type-safe wrappers for io accesses.
25 * Use these to enforce at compile time the following spec requirement:
27 * The driver MUST access each field using the “natural” access
28 * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses
29 * for 16-bit fields and 8-bit accesses for 8-bit fields.
31 static inline u8 vp_ioread8(u8 __iomem *addr)
33 return ioread8(addr);
35 static inline u16 vp_ioread16 (u16 __iomem *addr)
37 return ioread16(addr);
40 static inline u32 vp_ioread32(u32 __iomem *addr)
42 return ioread32(addr);
45 static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
47 iowrite8(value, addr);
50 static inline void vp_iowrite16(u16 value, u16 __iomem *addr)
52 iowrite16(value, addr);
55 static inline void vp_iowrite32(u32 value, u32 __iomem *addr)
57 iowrite32(value, addr);
60 static void vp_iowrite64_twopart(u64 val,
61 __le32 __iomem *lo, __le32 __iomem *hi)
63 vp_iowrite32((u32)val, lo);
64 vp_iowrite32(val >> 32, hi);
67 static void __iomem *map_capability(struct pci_dev *dev, int off,
68 size_t minlen,
69 u32 align,
70 u32 start, u32 size,
71 size_t *len)
73 u8 bar;
74 u32 offset, length;
75 void __iomem *p;
77 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
78 bar),
79 &bar);
80 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
81 &offset);
82 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
83 &length);
85 if (length <= start) {
86 dev_err(&dev->dev,
87 "virtio_pci: bad capability len %u (>%u expected)\n",
88 length, start);
89 return NULL;
92 if (length - start < minlen) {
93 dev_err(&dev->dev,
94 "virtio_pci: bad capability len %u (>=%zu expected)\n",
95 length, minlen);
96 return NULL;
99 length -= start;
101 if (start + offset < offset) {
102 dev_err(&dev->dev,
103 "virtio_pci: map wrap-around %u+%u\n",
104 start, offset);
105 return NULL;
108 offset += start;
110 if (offset & (align - 1)) {
111 dev_err(&dev->dev,
112 "virtio_pci: offset %u not aligned to %u\n",
113 offset, align);
114 return NULL;
117 if (length > size)
118 length = size;
120 if (len)
121 *len = length;
123 if (minlen + offset < minlen ||
124 minlen + offset > pci_resource_len(dev, bar)) {
125 dev_err(&dev->dev,
126 "virtio_pci: map virtio %zu@%u "
127 "out of range on bar %i length %lu\n",
128 minlen, offset,
129 bar, (unsigned long)pci_resource_len(dev, bar));
130 return NULL;
133 p = pci_iomap_range(dev, bar, offset, length);
134 if (!p)
135 dev_err(&dev->dev,
136 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
137 length, offset, bar);
138 return p;
141 /* virtio config->get_features() implementation */
142 static u64 vp_get_features(struct virtio_device *vdev)
144 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
145 u64 features;
147 vp_iowrite32(0, &vp_dev->common->device_feature_select);
148 features = vp_ioread32(&vp_dev->common->device_feature);
149 vp_iowrite32(1, &vp_dev->common->device_feature_select);
150 features |= ((u64)vp_ioread32(&vp_dev->common->device_feature) << 32);
152 return features;
155 /* virtio config->finalize_features() implementation */
156 static int vp_finalize_features(struct virtio_device *vdev)
158 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
160 /* Give virtio_ring a chance to accept features. */
161 vring_transport_features(vdev);
163 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
164 dev_err(&vdev->dev, "virtio: device uses modern interface "
165 "but does not have VIRTIO_F_VERSION_1\n");
166 return -EINVAL;
169 vp_iowrite32(0, &vp_dev->common->guest_feature_select);
170 vp_iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
171 vp_iowrite32(1, &vp_dev->common->guest_feature_select);
172 vp_iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
174 return 0;
177 /* virtio config->get() implementation */
178 static void vp_get(struct virtio_device *vdev, unsigned offset,
179 void *buf, unsigned len)
181 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
182 u8 b;
183 __le16 w;
184 __le32 l;
186 BUG_ON(offset + len > vp_dev->device_len);
188 switch (len) {
189 case 1:
190 b = ioread8(vp_dev->device + offset);
191 memcpy(buf, &b, sizeof b);
192 break;
193 case 2:
194 w = cpu_to_le16(ioread16(vp_dev->device + offset));
195 memcpy(buf, &w, sizeof w);
196 break;
197 case 4:
198 l = cpu_to_le32(ioread32(vp_dev->device + offset));
199 memcpy(buf, &l, sizeof l);
200 break;
201 case 8:
202 l = cpu_to_le32(ioread32(vp_dev->device + offset));
203 memcpy(buf, &l, sizeof l);
204 l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
205 memcpy(buf + sizeof l, &l, sizeof l);
206 break;
207 default:
208 BUG();
212 /* the config->set() implementation. it's symmetric to the config->get()
213 * implementation */
214 static void vp_set(struct virtio_device *vdev, unsigned offset,
215 const void *buf, unsigned len)
217 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
218 u8 b;
219 __le16 w;
220 __le32 l;
222 BUG_ON(offset + len > vp_dev->device_len);
224 switch (len) {
225 case 1:
226 memcpy(&b, buf, sizeof b);
227 iowrite8(b, vp_dev->device + offset);
228 break;
229 case 2:
230 memcpy(&w, buf, sizeof w);
231 iowrite16(le16_to_cpu(w), vp_dev->device + offset);
232 break;
233 case 4:
234 memcpy(&l, buf, sizeof l);
235 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
236 break;
237 case 8:
238 memcpy(&l, buf, sizeof l);
239 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
240 memcpy(&l, buf + sizeof l, sizeof l);
241 iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
242 break;
243 default:
244 BUG();
248 static u32 vp_generation(struct virtio_device *vdev)
250 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
251 return vp_ioread8(&vp_dev->common->config_generation);
254 /* config->{get,set}_status() implementations */
255 static u8 vp_get_status(struct virtio_device *vdev)
257 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
258 return vp_ioread8(&vp_dev->common->device_status);
261 static void vp_set_status(struct virtio_device *vdev, u8 status)
263 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
264 /* We should never be setting status to 0. */
265 BUG_ON(status == 0);
266 vp_iowrite8(status, &vp_dev->common->device_status);
269 static void vp_reset(struct virtio_device *vdev)
271 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
272 /* 0 status means a reset. */
273 vp_iowrite8(0, &vp_dev->common->device_status);
274 /* Flush out the status write, and flush in device writes,
275 * including MSI-X interrupts, if any. */
276 vp_ioread8(&vp_dev->common->device_status);
277 /* Flush pending VQ/configuration callbacks. */
278 vp_synchronize_vectors(vdev);
281 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
283 /* Setup the vector used for configuration events */
284 vp_iowrite16(vector, &vp_dev->common->msix_config);
285 /* Verify we had enough resources to assign the vector */
286 /* Will also flush the write out to device */
287 return vp_ioread16(&vp_dev->common->msix_config);
290 static size_t vring_pci_size(u16 num)
292 /* We only need a cacheline separation. */
293 return PAGE_ALIGN(vring_size(num, SMP_CACHE_BYTES));
296 static void *alloc_virtqueue_pages(int *num)
298 void *pages;
300 /* TODO: allocate each queue chunk individually */
301 for (; *num && vring_pci_size(*num) > PAGE_SIZE; *num /= 2) {
302 pages = alloc_pages_exact(vring_pci_size(*num),
303 GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
304 if (pages)
305 return pages;
308 if (!*num)
309 return NULL;
311 /* Try to get a single page. You are my only hope! */
312 return alloc_pages_exact(vring_pci_size(*num), GFP_KERNEL|__GFP_ZERO);
315 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
316 struct virtio_pci_vq_info *info,
317 unsigned index,
318 void (*callback)(struct virtqueue *vq),
319 const char *name,
320 u16 msix_vec)
322 struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
323 struct virtqueue *vq;
324 u16 num, off;
325 int err;
327 if (index >= vp_ioread16(&cfg->num_queues))
328 return ERR_PTR(-ENOENT);
330 /* Select the queue we're interested in */
331 vp_iowrite16(index, &cfg->queue_select);
333 /* Check if queue is either not available or already active. */
334 num = vp_ioread16(&cfg->queue_size);
335 if (!num || vp_ioread16(&cfg->queue_enable))
336 return ERR_PTR(-ENOENT);
338 if (num & (num - 1)) {
339 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
340 return ERR_PTR(-EINVAL);
343 /* get offset of notification word for this vq */
344 off = vp_ioread16(&cfg->queue_notify_off);
346 info->num = num;
347 info->msix_vector = msix_vec;
349 info->queue = alloc_virtqueue_pages(&info->num);
350 if (info->queue == NULL)
351 return ERR_PTR(-ENOMEM);
353 /* create the vring */
354 vq = vring_new_virtqueue(index, info->num,
355 SMP_CACHE_BYTES, &vp_dev->vdev,
356 true, info->queue, vp_notify, callback, name);
357 if (!vq) {
358 err = -ENOMEM;
359 goto err_new_queue;
362 /* activate the queue */
363 vp_iowrite16(num, &cfg->queue_size);
364 vp_iowrite64_twopart(virt_to_phys(info->queue),
365 &cfg->queue_desc_lo, &cfg->queue_desc_hi);
366 vp_iowrite64_twopart(virt_to_phys(virtqueue_get_avail(vq)),
367 &cfg->queue_avail_lo, &cfg->queue_avail_hi);
368 vp_iowrite64_twopart(virt_to_phys(virtqueue_get_used(vq)),
369 &cfg->queue_used_lo, &cfg->queue_used_hi);
371 if (vp_dev->notify_base) {
372 /* offset should not wrap */
373 if ((u64)off * vp_dev->notify_offset_multiplier + 2
374 > vp_dev->notify_len) {
375 dev_warn(&vp_dev->pci_dev->dev,
376 "bad notification offset %u (x %u) "
377 "for queue %u > %zd",
378 off, vp_dev->notify_offset_multiplier,
379 index, vp_dev->notify_len);
380 err = -EINVAL;
381 goto err_map_notify;
383 vq->priv = (void __force *)vp_dev->notify_base +
384 off * vp_dev->notify_offset_multiplier;
385 } else {
386 vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
387 vp_dev->notify_map_cap, 2, 2,
388 off * vp_dev->notify_offset_multiplier, 2,
389 NULL);
392 if (!vq->priv) {
393 err = -ENOMEM;
394 goto err_map_notify;
397 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
398 vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
399 msix_vec = vp_ioread16(&cfg->queue_msix_vector);
400 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
401 err = -EBUSY;
402 goto err_assign_vector;
406 return vq;
408 err_assign_vector:
409 if (!vp_dev->notify_base)
410 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
411 err_map_notify:
412 vring_del_virtqueue(vq);
413 err_new_queue:
414 free_pages_exact(info->queue, vring_pci_size(info->num));
415 return ERR_PTR(err);
418 static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
419 struct virtqueue *vqs[],
420 vq_callback_t *callbacks[],
421 const char *names[])
423 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
424 struct virtqueue *vq;
425 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names);
427 if (rc)
428 return rc;
430 /* Select and activate all queues. Has to be done last: once we do
431 * this, there's no way to go back except reset.
433 list_for_each_entry(vq, &vdev->vqs, list) {
434 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
435 vp_iowrite16(1, &vp_dev->common->queue_enable);
438 return 0;
441 static void del_vq(struct virtio_pci_vq_info *info)
443 struct virtqueue *vq = info->vq;
444 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
446 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
448 if (vp_dev->msix_enabled) {
449 vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
450 &vp_dev->common->queue_msix_vector);
451 /* Flush the write out to device */
452 vp_ioread16(&vp_dev->common->queue_msix_vector);
455 if (!vp_dev->notify_base)
456 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
458 vring_del_virtqueue(vq);
460 free_pages_exact(info->queue, vring_pci_size(info->num));
463 static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
464 .get = NULL,
465 .set = NULL,
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,
478 static const struct virtio_config_ops virtio_pci_config_ops = {
479 .get = vp_get,
480 .set = vp_set,
481 .generation = vp_generation,
482 .get_status = vp_get_status,
483 .set_status = vp_set_status,
484 .reset = vp_reset,
485 .find_vqs = vp_modern_find_vqs,
486 .del_vqs = vp_del_vqs,
487 .get_features = vp_get_features,
488 .finalize_features = vp_finalize_features,
489 .bus_name = vp_bus_name,
490 .set_vq_affinity = vp_set_vq_affinity,
494 * virtio_pci_find_capability - walk capabilities to find device info.
495 * @dev: the pci device
496 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
497 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
499 * Returns offset of the capability, or 0.
501 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
502 u32 ioresource_types)
504 int pos;
506 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
507 pos > 0;
508 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
509 u8 type, bar;
510 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
511 cfg_type),
512 &type);
513 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
514 bar),
515 &bar);
517 /* Ignore structures with reserved BAR values */
518 if (bar > 0x5)
519 continue;
521 if (type == cfg_type) {
522 if (pci_resource_len(dev, bar) &&
523 pci_resource_flags(dev, bar) & ioresource_types)
524 return pos;
527 return 0;
530 /* This is part of the ABI. Don't screw with it. */
531 static inline void check_offsets(void)
533 /* Note: disk space was harmed in compilation of this function. */
534 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
535 offsetof(struct virtio_pci_cap, cap_vndr));
536 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
537 offsetof(struct virtio_pci_cap, cap_next));
538 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
539 offsetof(struct virtio_pci_cap, cap_len));
540 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
541 offsetof(struct virtio_pci_cap, cfg_type));
542 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
543 offsetof(struct virtio_pci_cap, bar));
544 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
545 offsetof(struct virtio_pci_cap, offset));
546 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
547 offsetof(struct virtio_pci_cap, length));
548 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
549 offsetof(struct virtio_pci_notify_cap,
550 notify_off_multiplier));
551 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
552 offsetof(struct virtio_pci_common_cfg,
553 device_feature_select));
554 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
555 offsetof(struct virtio_pci_common_cfg, device_feature));
556 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
557 offsetof(struct virtio_pci_common_cfg,
558 guest_feature_select));
559 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
560 offsetof(struct virtio_pci_common_cfg, guest_feature));
561 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
562 offsetof(struct virtio_pci_common_cfg, msix_config));
563 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
564 offsetof(struct virtio_pci_common_cfg, num_queues));
565 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
566 offsetof(struct virtio_pci_common_cfg, device_status));
567 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
568 offsetof(struct virtio_pci_common_cfg, config_generation));
569 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
570 offsetof(struct virtio_pci_common_cfg, queue_select));
571 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
572 offsetof(struct virtio_pci_common_cfg, queue_size));
573 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
574 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
575 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
576 offsetof(struct virtio_pci_common_cfg, queue_enable));
577 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
578 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
579 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
580 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
581 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
582 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
583 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
584 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
585 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
586 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
587 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
588 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
589 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
590 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
593 /* the PCI probing function */
594 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
596 struct pci_dev *pci_dev = vp_dev->pci_dev;
597 int err, common, isr, notify, device;
598 u32 notify_length;
599 u32 notify_offset;
601 check_offsets();
603 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
604 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
605 return -ENODEV;
607 if (pci_dev->device < 0x1040) {
608 /* Transitional devices: use the PCI subsystem device id as
609 * virtio device id, same as legacy driver always did.
611 vp_dev->vdev.id.device = pci_dev->subsystem_device;
612 } else {
613 /* Modern devices: simply use PCI device id, but start from 0x1040. */
614 vp_dev->vdev.id.device = pci_dev->device - 0x1040;
616 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
618 /* check for a common config: if not, use legacy mode (bar 0). */
619 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
620 IORESOURCE_IO | IORESOURCE_MEM);
621 if (!common) {
622 dev_info(&pci_dev->dev,
623 "virtio_pci: leaving for legacy driver\n");
624 return -ENODEV;
627 /* If common is there, these should be too... */
628 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
629 IORESOURCE_IO | IORESOURCE_MEM);
630 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
631 IORESOURCE_IO | IORESOURCE_MEM);
632 if (!isr || !notify) {
633 dev_err(&pci_dev->dev,
634 "virtio_pci: missing capabilities %i/%i/%i\n",
635 common, isr, notify);
636 return -EINVAL;
639 /* Device capability is only mandatory for devices that have
640 * device-specific configuration.
642 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
643 IORESOURCE_IO | IORESOURCE_MEM);
645 err = -EINVAL;
646 vp_dev->common = map_capability(pci_dev, common,
647 sizeof(struct virtio_pci_common_cfg), 4,
648 0, sizeof(struct virtio_pci_common_cfg),
649 NULL);
650 if (!vp_dev->common)
651 goto err_map_common;
652 vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
653 0, 1,
654 NULL);
655 if (!vp_dev->isr)
656 goto err_map_isr;
658 /* Read notify_off_multiplier from config space. */
659 pci_read_config_dword(pci_dev,
660 notify + offsetof(struct virtio_pci_notify_cap,
661 notify_off_multiplier),
662 &vp_dev->notify_offset_multiplier);
663 /* Read notify length and offset from config space. */
664 pci_read_config_dword(pci_dev,
665 notify + offsetof(struct virtio_pci_notify_cap,
666 cap.length),
667 &notify_length);
669 pci_read_config_dword(pci_dev,
670 notify + offsetof(struct virtio_pci_notify_cap,
671 cap.length),
672 &notify_offset);
674 /* We don't know how many VQs we'll map, ahead of the time.
675 * If notify length is small, map it all now.
676 * Otherwise, map each VQ individually later.
678 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
679 vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
680 0, notify_length,
681 &vp_dev->notify_len);
682 if (!vp_dev->notify_base)
683 goto err_map_notify;
684 } else {
685 vp_dev->notify_map_cap = notify;
688 /* Again, we don't know how much we should map, but PAGE_SIZE
689 * is more than enough for all existing devices.
691 if (device) {
692 vp_dev->device = map_capability(pci_dev, device, 0, 4,
693 0, PAGE_SIZE,
694 &vp_dev->device_len);
695 if (!vp_dev->device)
696 goto err_map_device;
698 vp_dev->vdev.config = &virtio_pci_config_ops;
699 } else {
700 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
703 vp_dev->config_vector = vp_config_vector;
704 vp_dev->setup_vq = setup_vq;
705 vp_dev->del_vq = del_vq;
707 return 0;
709 err_map_device:
710 if (vp_dev->notify_base)
711 pci_iounmap(pci_dev, vp_dev->notify_base);
712 err_map_notify:
713 pci_iounmap(pci_dev, vp_dev->isr);
714 err_map_isr:
715 pci_iounmap(pci_dev, vp_dev->common);
716 err_map_common:
717 return err;
720 void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
722 struct pci_dev *pci_dev = vp_dev->pci_dev;
724 if (vp_dev->device)
725 pci_iounmap(pci_dev, vp_dev->device);
726 if (vp_dev->notify_base)
727 pci_iounmap(pci_dev, vp_dev->notify_base);
728 pci_iounmap(pci_dev, vp_dev->isr);
729 pci_iounmap(pci_dev, vp_dev->common);