summaryrefslogtreecommitdiffstats
path: root/drivers/virtio
diff options
context:
space:
mode:
authorMichael S. Tsirkin <mst@redhat.com>2014-12-11 06:59:51 -0500
committerRusty Russell <rusty@rustcorp.com.au>2015-01-21 00:58:53 -0500
commit1fcf0512c9c870e78e1c9898ecb9458593403466 (patch)
tree002fa2a03688453e491f2224c31a96c1b3a47be6 /drivers/virtio
parent71d70c266c84c4e708bb36b20d0c0a29af42821c (diff)
virtio_pci: modern driver
Lightly tested against qemu. One thing *not* implemented here is separate mappings for descriptor/avail/used rings. That's nice to have, will be done later after we have core support. This also exposes the PCI layout to userspace, and adds macros for PCI layout offsets: QEMU wants it, so why not? Trust, but verify. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Diffstat (limited to 'drivers/virtio')
-rw-r--r--drivers/virtio/Makefile2
-rw-r--r--drivers/virtio/virtio_pci_common.c14
-rw-r--r--drivers/virtio/virtio_pci_common.h25
-rw-r--r--drivers/virtio/virtio_pci_modern.c587
4 files changed, 621 insertions, 7 deletions
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index bf5104b56894..bd230d1c0533 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -1,5 +1,5 @@
1obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o 1obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o
2obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o 2obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
3obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o 3obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
4virtio_pci-y := virtio_pci_legacy.o virtio_pci_common.o 4virtio_pci-y := virtio_pci_modern.o virtio_pci_legacy.o virtio_pci_common.o
5obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o 5obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
index 457cbe29c8c4..8ae34a34f3af 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -505,7 +505,9 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
505 if (rc) 505 if (rc)
506 goto err_request_regions; 506 goto err_request_regions;
507 507
508 rc = virtio_pci_legacy_probe(vp_dev); 508 rc = virtio_pci_modern_probe(vp_dev);
509 if (rc == -ENODEV)
510 rc = virtio_pci_legacy_probe(vp_dev);
509 if (rc) 511 if (rc)
510 goto err_probe; 512 goto err_probe;
511 513
@@ -518,7 +520,10 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
518 return 0; 520 return 0;
519 521
520err_register: 522err_register:
521 virtio_pci_legacy_remove(vp_dev); 523 if (vp_dev->ioaddr)
524 virtio_pci_legacy_remove(vp_dev);
525 else
526 virtio_pci_modern_remove(vp_dev);
522err_probe: 527err_probe:
523 pci_release_regions(pci_dev); 528 pci_release_regions(pci_dev);
524err_request_regions: 529err_request_regions:
@@ -534,7 +539,10 @@ static void virtio_pci_remove(struct pci_dev *pci_dev)
534 539
535 unregister_virtio_device(&vp_dev->vdev); 540 unregister_virtio_device(&vp_dev->vdev);
536 541
537 virtio_pci_legacy_remove(pci_dev); 542 if (vp_dev->ioaddr)
543 virtio_pci_legacy_remove(vp_dev);
544 else
545 virtio_pci_modern_remove(vp_dev);
538 546
539 pci_release_regions(pci_dev); 547 pci_release_regions(pci_dev);
540 pci_disable_device(pci_dev); 548 pci_disable_device(pci_dev);
diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci_common.h
index 2b1e70db44a0..610c43f19230 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -53,12 +53,29 @@ struct virtio_pci_device {
53 struct virtio_device vdev; 53 struct virtio_device vdev;
54 struct pci_dev *pci_dev; 54 struct pci_dev *pci_dev;
55 55
56 /* In legacy mode, these two point to within ->legacy. */
57 /* Where to read and clear interrupt */
58 u8 __iomem *isr;
59
60 /* Modern only fields */
61 /* The IO mapping for the PCI config space (non-legacy mode) */
62 struct virtio_pci_common_cfg __iomem *common;
63 /* Device-specific data (non-legacy mode) */
64 void __iomem *device;
65
66 /* So we can sanity-check accesses. */
67 size_t device_len;
68
69 /* Capability for when we need to map notifications per-vq. */
70 int notify_map_cap;
71
72 /* Multiply queue_notify_off by this value. (non-legacy mode). */
73 u32 notify_offset_multiplier;
74
75 /* Legacy only field */
56 /* the IO mapping for the PCI config space */ 76 /* the IO mapping for the PCI config space */
57 void __iomem *ioaddr; 77 void __iomem *ioaddr;
58 78
59 /* the IO mapping for ISR operation */
60 void __iomem *isr;
61
62 /* a list of queues so we can dispatch IRQs */ 79 /* a list of queues so we can dispatch IRQs */
63 spinlock_t lock; 80 spinlock_t lock;
64 struct list_head virtqueues; 81 struct list_head virtqueues;
@@ -129,5 +146,7 @@ int vp_set_vq_affinity(struct virtqueue *vq, int cpu);
129 146
130int virtio_pci_legacy_probe(struct virtio_pci_device *); 147int virtio_pci_legacy_probe(struct virtio_pci_device *);
131void virtio_pci_legacy_remove(struct virtio_pci_device *); 148void virtio_pci_legacy_remove(struct virtio_pci_device *);
149int virtio_pci_modern_probe(struct virtio_pci_device *);
150void virtio_pci_modern_remove(struct virtio_pci_device *);
132 151
133#endif 152#endif
diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
new file mode 100644
index 000000000000..a3d81013e0c2
--- /dev/null
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -0,0 +1,587 @@
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#define VIRTIO_PCI_NO_LEGACY
21#include "virtio_pci_common.h"
22
23static void __iomem *map_capability(struct pci_dev *dev, int off,
24 size_t minlen,
25 u32 align,
26 u32 start, u32 size,
27 size_t *len)
28{
29 u8 bar;
30 u32 offset, length;
31 void __iomem *p;
32
33 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
34 bar),
35 &bar);
36 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
37 &offset);
38 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
39 &length);
40
41 if (length <= start) {
42 dev_err(&dev->dev,
43 "virtio_pci: bad capability len %u (>%u expected)\n",
44 length, start);
45 return NULL;
46 }
47
48 if (length - start < minlen) {
49 dev_err(&dev->dev,
50 "virtio_pci: bad capability len %u (>=%zu expected)\n",
51 length, minlen);
52 return NULL;
53 }
54
55 length -= start;
56
57 if (start + offset < offset) {
58 dev_err(&dev->dev,
59 "virtio_pci: map wrap-around %u+%u\n",
60 start, offset);
61 return NULL;
62 }
63
64 offset += start;
65
66 if (offset & (align - 1)) {
67 dev_err(&dev->dev,
68 "virtio_pci: offset %u not aligned to %u\n",
69 offset, align);
70 return NULL;
71 }
72
73 if (length > size)
74 length = size;
75
76 if (len)
77 *len = length;
78
79 if (minlen + offset < minlen ||
80 minlen + offset > pci_resource_len(dev, bar)) {
81 dev_err(&dev->dev,
82 "virtio_pci: map virtio %zu@%u "
83 "out of range on bar %i length %lu\n",
84 minlen, offset,
85 bar, (unsigned long)pci_resource_len(dev, bar));
86 return NULL;
87 }
88
89 p = pci_iomap_range(dev, bar, offset, length);
90 if (!p)
91 dev_err(&dev->dev,
92 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
93 length, offset, bar);
94 return p;
95}
96
97static void iowrite64_twopart(u64 val, __le32 __iomem *lo, __le32 __iomem *hi)
98{
99 iowrite32((u32)val, lo);
100 iowrite32(val >> 32, hi);
101}
102
103/* virtio config->get_features() implementation */
104static u64 vp_get_features(struct virtio_device *vdev)
105{
106 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
107 u64 features;
108
109 iowrite32(0, &vp_dev->common->device_feature_select);
110 features = ioread32(&vp_dev->common->device_feature);
111 iowrite32(1, &vp_dev->common->device_feature_select);
112 features |= ((u64)ioread32(&vp_dev->common->device_feature) << 32);
113
114 return features;
115}
116
117/* virtio config->finalize_features() implementation */
118static int vp_finalize_features(struct virtio_device *vdev)
119{
120 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
121
122 /* Give virtio_ring a chance to accept features. */
123 vring_transport_features(vdev);
124
125 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
126 dev_err(&vdev->dev, "virtio: device uses modern interface "
127 "but does not have VIRTIO_F_VERSION_1\n");
128 return -EINVAL;
129 }
130
131 iowrite32(0, &vp_dev->common->guest_feature_select);
132 iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
133 iowrite32(1, &vp_dev->common->guest_feature_select);
134 iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
135
136 return 0;
137}
138
139/* virtio config->get() implementation */
140static void vp_get(struct virtio_device *vdev, unsigned offset,
141 void *buf, unsigned len)
142{
143 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
144 u8 b;
145 __le16 w;
146 __le32 l;
147
148 BUG_ON(offset + len > vp_dev->device_len);
149
150 switch (len) {
151 case 1:
152 b = ioread8(vp_dev->device + offset);
153 memcpy(buf, &b, sizeof b);
154 break;
155 case 2:
156 w = cpu_to_le16(ioread16(vp_dev->device + offset));
157 memcpy(buf, &w, sizeof w);
158 break;
159 case 4:
160 l = cpu_to_le32(ioread32(vp_dev->device + offset));
161 memcpy(buf, &l, sizeof l);
162 break;
163 case 8:
164 l = cpu_to_le32(ioread32(vp_dev->device + offset));
165 memcpy(buf, &l, sizeof l);
166 l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
167 memcpy(buf + sizeof l, &l, sizeof l);
168 break;
169 default:
170 BUG();
171 }
172}
173
174/* the config->set() implementation. it's symmetric to the config->get()
175 * implementation */
176static void vp_set(struct virtio_device *vdev, unsigned offset,
177 const void *buf, unsigned len)
178{
179 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
180 u8 b;
181 __le16 w;
182 __le32 l;
183
184 BUG_ON(offset + len > vp_dev->device_len);
185
186 switch (len) {
187 case 1:
188 memcpy(&b, buf, sizeof b);
189 iowrite8(b, vp_dev->device + offset);
190 break;
191 case 2:
192 memcpy(&w, buf, sizeof w);
193 iowrite16(le16_to_cpu(w), vp_dev->device + offset);
194 break;
195 case 4:
196 memcpy(&l, buf, sizeof l);
197 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
198 break;
199 case 8:
200 memcpy(&l, buf, sizeof l);
201 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
202 memcpy(&l, buf + sizeof l, sizeof l);
203 iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
204 break;
205 default:
206 BUG();
207 }
208}
209
210static u32 vp_generation(struct virtio_device *vdev)
211{
212 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
213 return ioread8(&vp_dev->common->config_generation);
214}
215
216/* config->{get,set}_status() implementations */
217static u8 vp_get_status(struct virtio_device *vdev)
218{
219 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
220 return ioread8(&vp_dev->common->device_status);
221}
222
223static void vp_set_status(struct virtio_device *vdev, u8 status)
224{
225 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
226 /* We should never be setting status to 0. */
227 BUG_ON(status == 0);
228 iowrite8(status, &vp_dev->common->device_status);
229}
230
231static void vp_reset(struct virtio_device *vdev)
232{
233 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
234 /* 0 status means a reset. */
235 iowrite8(0, &vp_dev->common->device_status);
236 /* Flush out the status write, and flush in device writes,
237 * including MSI-X interrupts, if any. */
238 ioread8(&vp_dev->common->device_status);
239 /* Flush pending VQ/configuration callbacks. */
240 vp_synchronize_vectors(vdev);
241}
242
243static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
244{
245 /* Setup the vector used for configuration events */
246 iowrite16(vector, &vp_dev->common->msix_config);
247 /* Verify we had enough resources to assign the vector */
248 /* Will also flush the write out to device */
249 return ioread16(&vp_dev->common->msix_config);
250}
251
252static size_t vring_pci_size(u16 num)
253{
254 /* We only need a cacheline separation. */
255 return PAGE_ALIGN(vring_size(num, SMP_CACHE_BYTES));
256}
257
258static void *alloc_virtqueue_pages(int *num)
259{
260 void *pages;
261
262 /* TODO: allocate each queue chunk individually */
263 for (; *num && vring_pci_size(*num) > PAGE_SIZE; *num /= 2) {
264 pages = alloc_pages_exact(vring_pci_size(*num),
265 GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
266 if (pages)
267 return pages;
268 }
269
270 if (!*num)
271 return NULL;
272
273 /* Try to get a single page. You are my only hope! */
274 return alloc_pages_exact(vring_pci_size(*num), GFP_KERNEL|__GFP_ZERO);
275}
276
277static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
278 struct virtio_pci_vq_info *info,
279 unsigned index,
280 void (*callback)(struct virtqueue *vq),
281 const char *name,
282 u16 msix_vec)
283{
284 struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
285 struct virtqueue *vq;
286 u16 num, off;
287 int err;
288
289 if (index >= ioread16(&cfg->num_queues))
290 return ERR_PTR(-ENOENT);
291
292 /* Select the queue we're interested in */
293 iowrite16(index, &cfg->queue_select);
294
295 /* Check if queue is either not available or already active. */
296 num = ioread16(&cfg->queue_size);
297 if (!num || ioread8(&cfg->queue_enable))
298 return ERR_PTR(-ENOENT);
299
300 if (num & (num - 1)) {
301 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
302 return ERR_PTR(-EINVAL);
303 }
304
305 /* get offset of notification word for this vq */
306 off = ioread16(&cfg->queue_notify_off);
307
308 info->num = num;
309 info->msix_vector = msix_vec;
310
311 info->queue = alloc_virtqueue_pages(&info->num);
312 if (info->queue == NULL)
313 return ERR_PTR(-ENOMEM);
314
315 /* create the vring */
316 vq = vring_new_virtqueue(index, info->num,
317 SMP_CACHE_BYTES, &vp_dev->vdev,
318 true, info->queue, vp_notify, callback, name);
319 if (!vq) {
320 err = -ENOMEM;
321 goto err_new_queue;
322 }
323
324 /* activate the queue */
325 iowrite16(num, &cfg->queue_size);
326 iowrite64_twopart(virt_to_phys(info->queue),
327 &cfg->queue_desc_lo, &cfg->queue_desc_hi);
328 iowrite64_twopart(virt_to_phys(virtqueue_get_avail(vq)),
329 &cfg->queue_avail_lo, &cfg->queue_avail_hi);
330 iowrite64_twopart(virt_to_phys(virtqueue_get_used(vq)),
331 &cfg->queue_used_lo, &cfg->queue_used_hi);
332
333 vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
334 vp_dev->notify_map_cap, 2, 2,
335 off * vp_dev->notify_offset_multiplier, 2,
336 NULL);
337
338 if (!vq->priv) {
339 err = -ENOMEM;
340 goto err_map_notify;
341 }
342
343 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
344 iowrite16(msix_vec, &cfg->queue_msix_vector);
345 msix_vec = ioread16(&cfg->queue_msix_vector);
346 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
347 err = -EBUSY;
348 goto err_assign_vector;
349 }
350 }
351
352 return vq;
353
354err_assign_vector:
355 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
356err_map_notify:
357 vring_del_virtqueue(vq);
358err_new_queue:
359 free_pages_exact(info->queue, vring_pci_size(info->num));
360 return ERR_PTR(err);
361}
362
363static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
364 struct virtqueue *vqs[],
365 vq_callback_t *callbacks[],
366 const char *names[])
367{
368 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
369 struct virtqueue *vq;
370 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names);
371
372 if (rc)
373 return rc;
374
375 /* Select and activate all queues. Has to be done last: once we do
376 * this, there's no way to go back except reset.
377 */
378 list_for_each_entry(vq, &vdev->vqs, list) {
379 iowrite16(vq->index, &vp_dev->common->queue_select);
380 iowrite8(1, &vp_dev->common->queue_enable);
381 }
382
383 return 0;
384}
385
386static void del_vq(struct virtio_pci_vq_info *info)
387{
388 struct virtqueue *vq = info->vq;
389 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
390
391 iowrite16(vq->index, &vp_dev->common->queue_select);
392
393 if (vp_dev->msix_enabled) {
394 iowrite16(VIRTIO_MSI_NO_VECTOR,
395 &vp_dev->common->queue_msix_vector);
396 /* Flush the write out to device */
397 ioread16(&vp_dev->common->queue_msix_vector);
398 }
399
400 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
401
402 vring_del_virtqueue(vq);
403
404 free_pages_exact(info->queue, vring_pci_size(info->num));
405}
406
407static const struct virtio_config_ops virtio_pci_config_ops = {
408 .get = vp_get,
409 .set = vp_set,
410 .generation = vp_generation,
411 .get_status = vp_get_status,
412 .set_status = vp_set_status,
413 .reset = vp_reset,
414 .find_vqs = vp_modern_find_vqs,
415 .del_vqs = vp_del_vqs,
416 .get_features = vp_get_features,
417 .finalize_features = vp_finalize_features,
418 .bus_name = vp_bus_name,
419 .set_vq_affinity = vp_set_vq_affinity,
420};
421
422/**
423 * virtio_pci_find_capability - walk capabilities to find device info.
424 * @dev: the pci device
425 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
426 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
427 *
428 * Returns offset of the capability, or 0.
429 */
430static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
431 u32 ioresource_types)
432{
433 int pos;
434
435 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
436 pos > 0;
437 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
438 u8 type, bar;
439 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
440 cfg_type),
441 &type);
442 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
443 bar),
444 &bar);
445
446 /* Ignore structures with reserved BAR values */
447 if (bar > 0x5)
448 continue;
449
450 if (type == cfg_type) {
451 if (pci_resource_len(dev, bar) &&
452 pci_resource_flags(dev, bar) & ioresource_types)
453 return pos;
454 }
455 }
456 return 0;
457}
458
459static void virtio_pci_release_dev(struct device *_d)
460{
461 struct virtio_device *vdev = dev_to_virtio(_d);
462 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
463
464 kfree(vp_dev);
465}
466
467/* TODO: validate the ABI statically. */
468static inline void check_offsets(void)
469{
470}
471
472/* the PCI probing function */
473int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
474{
475 struct pci_dev *pci_dev = vp_dev->pci_dev;
476 int err, common, isr, notify, device;
477 u32 notify_length;
478
479 check_offsets();
480
481 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
482 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
483 return -ENODEV;
484
485 if (pci_dev->device < 0x1040) {
486 /* Transitional devices: use the PCI subsystem device id as
487 * virtio device id, same as legacy driver always did.
488 */
489 vp_dev->vdev.id.device = pci_dev->subsystem_device;
490 } else {
491 /* Modern devices: simply use PCI device id, but start from 0x1040. */
492 vp_dev->vdev.id.device = pci_dev->device - 0x1040;
493 }
494 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
495
496 if (virtio_device_is_legacy_only(vp_dev->vdev.id))
497 return -ENODEV;
498
499 /* check for a common config: if not, use legacy mode (bar 0). */
500 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
501 IORESOURCE_IO | IORESOURCE_MEM);
502 if (!common) {
503 dev_info(&pci_dev->dev,
504 "virtio_pci: leaving for legacy driver\n");
505 return -ENODEV;
506 }
507
508 /* If common is there, these should be too... */
509 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
510 IORESOURCE_IO | IORESOURCE_MEM);
511 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
512 IORESOURCE_IO | IORESOURCE_MEM);
513 if (!isr || !notify) {
514 dev_err(&pci_dev->dev,
515 "virtio_pci: missing capabilities %i/%i/%i\n",
516 common, isr, notify);
517 return -EINVAL;
518 }
519
520 /* Device capability is only mandatory for devices that have
521 * device-specific configuration.
522 */
523 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
524 IORESOURCE_IO | IORESOURCE_MEM);
525
526 err = -EINVAL;
527 vp_dev->common = map_capability(pci_dev, common,
528 sizeof(struct virtio_pci_common_cfg), 4,
529 0, sizeof(struct virtio_pci_common_cfg),
530 NULL);
531 if (!vp_dev->common)
532 goto err_map_common;
533 vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
534 0, 1,
535 NULL);
536 if (!vp_dev->isr)
537 goto err_map_isr;
538
539 /* Read notify_off_multiplier from config space. */
540 pci_read_config_dword(pci_dev,
541 notify + offsetof(struct virtio_pci_notify_cap,
542 notify_off_multiplier),
543 &vp_dev->notify_offset_multiplier);
544 /* Read notify length from config space. */
545 pci_read_config_dword(pci_dev,
546 notify + offsetof(struct virtio_pci_notify_cap,
547 cap.length),
548 &notify_length);
549
550 vp_dev->notify_map_cap = notify;
551
552 /* Again, we don't know how much we should map, but PAGE_SIZE
553 * is more than enough for all existing devices.
554 */
555 if (device) {
556 vp_dev->device = map_capability(pci_dev, device, 0, 4,
557 0, PAGE_SIZE,
558 &vp_dev->device_len);
559 if (!vp_dev->device)
560 goto err_map_device;
561 }
562
563 vp_dev->vdev.config = &virtio_pci_config_ops;
564
565 vp_dev->config_vector = vp_config_vector;
566 vp_dev->setup_vq = setup_vq;
567 vp_dev->del_vq = del_vq;
568
569 return 0;
570
571err_map_device:
572 pci_iounmap(pci_dev, vp_dev->isr);
573err_map_isr:
574 pci_iounmap(pci_dev, vp_dev->common);
575err_map_common:
576 return err;
577}
578
579void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
580{
581 struct pci_dev *pci_dev = vp_dev->pci_dev;
582
583 if (vp_dev->device)
584 pci_iounmap(pci_dev, vp_dev->device);
585 pci_iounmap(pci_dev, vp_dev->isr);
586 pci_iounmap(pci_dev, vp_dev->common);
587}