]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/virtio/virtio_pci_modern.c
Merge tag 'v4.11-next-fixes' of https://github.com/mbgg/linux-mediatek into fixes
[mirror_ubuntu-bionic-kernel.git] / drivers / virtio / virtio_pci_modern.c
CommitLineData
1fcf0512
MT
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
05dbcb43 20#include <linux/delay.h>
1fcf0512
MT
21#define VIRTIO_PCI_NO_LEGACY
22#include "virtio_pci_common.h"
23
c5d4c2c9
MT
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}
61bd405f 36static inline u16 vp_ioread16 (__le16 __iomem *addr)
c5d4c2c9
MT
37{
38 return ioread16(addr);
39}
40
61bd405f 41static inline u32 vp_ioread32(__le32 __iomem *addr)
c5d4c2c9
MT
42{
43 return ioread32(addr);
44}
45
46static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
47{
48 iowrite8(value, addr);
49}
50
61bd405f 51static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
c5d4c2c9
MT
52{
53 iowrite16(value, addr);
54}
55
61bd405f 56static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
c5d4c2c9
MT
57{
58 iowrite32(value, addr);
59}
60
a8557d32
MT
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
1fcf0512
MT
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
1fcf0512
MT
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
a8557d32
MT
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);
1fcf0512
MT
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
a8557d32
MT
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);
1fcf0512
MT
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);
a8557d32 252 return vp_ioread8(&vp_dev->common->config_generation);
1fcf0512
MT
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);
a8557d32 259 return vp_ioread8(&vp_dev->common->device_status);
1fcf0512
MT
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);
a8557d32 267 vp_iowrite8(status, &vp_dev->common->device_status);
1fcf0512
MT
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. */
a8557d32 274 vp_iowrite8(0, &vp_dev->common->device_status);
05dbcb43
MT
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);
1fcf0512
MT
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 */
a8557d32 289 vp_iowrite16(vector, &vp_dev->common->msix_config);
1fcf0512
MT
290 /* Verify we had enough resources to assign the vector */
291 /* Will also flush the write out to device */
a8557d32 292 return vp_ioread16(&vp_dev->common->msix_config);
1fcf0512
MT
293}
294
1fcf0512 295static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
0a9b3f47 296 struct virtio_pci_vq_info *info,
1fcf0512
MT
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
a8557d32 307 if (index >= vp_ioread16(&cfg->num_queues))
1fcf0512
MT
308 return ERR_PTR(-ENOENT);
309
310 /* Select the queue we're interested in */
a8557d32 311 vp_iowrite16(index, &cfg->queue_select);
1fcf0512
MT
312
313 /* Check if queue is either not available or already active. */
a8557d32
MT
314 num = vp_ioread16(&cfg->queue_size);
315 if (!num || vp_ioread16(&cfg->queue_enable))
1fcf0512
MT
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 */
a8557d32 324 off = vp_ioread16(&cfg->queue_notify_off);
1fcf0512 325
0a9b3f47
MT
326 info->msix_vector = msix_vec;
327
1fcf0512 328 /* create the vring */
7a5589b2
AL
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);
1fcf0512
MT
334
335 /* activate the queue */
7a5589b2
AL
336 vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
337 vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
a8557d32 338 &cfg->queue_desc_lo, &cfg->queue_desc_hi);
7a5589b2 339 vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
a8557d32 340 &cfg->queue_avail_lo, &cfg->queue_avail_hi);
7a5589b2 341 vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
a8557d32 342 &cfg->queue_used_lo, &cfg->queue_used_hi);
1fcf0512 343
3909213c
MT
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 }
1fcf0512
MT
364
365 if (!vq->priv) {
366 err = -ENOMEM;
367 goto err_map_notify;
368 }
369
370 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
a8557d32
MT
371 vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
372 msix_vec = vp_ioread16(&cfg->queue_msix_vector);
1fcf0512
MT
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:
3909213c
MT
382 if (!vp_dev->notify_base)
383 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
1fcf0512
MT
384err_map_notify:
385 vring_del_virtqueue(vq);
1fcf0512
MT
386 return ERR_PTR(err);
387}
388
389static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
fb5e31d9
CH
390 struct virtqueue *vqs[], vq_callback_t *callbacks[],
391 const char * const names[], struct irq_affinity *desc)
1fcf0512
MT
392{
393 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
394 struct virtqueue *vq;
fb5e31d9 395 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, desc);
1fcf0512
MT
396
397 if (rc)
398 return rc;
399
400 /* Select and activate all queues. Has to be done last: once we do
401 * this, there's no way to go back except reset.
402 */
403 list_for_each_entry(vq, &vdev->vqs, list) {
a8557d32
MT
404 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
405 vp_iowrite16(1, &vp_dev->common->queue_enable);
1fcf0512
MT
406 }
407
408 return 0;
409}
410
0a9b3f47 411static void del_vq(struct virtio_pci_vq_info *info)
1fcf0512 412{
0a9b3f47 413 struct virtqueue *vq = info->vq;
1fcf0512
MT
414 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
415
a8557d32 416 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
1fcf0512 417
2008c154 418 if (vp_dev->msix_enabled) {
a8557d32
MT
419 vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
420 &vp_dev->common->queue_msix_vector);
1fcf0512 421 /* Flush the write out to device */
a8557d32 422 vp_ioread16(&vp_dev->common->queue_msix_vector);
1fcf0512
MT
423 }
424
3909213c
MT
425 if (!vp_dev->notify_base)
426 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
1fcf0512
MT
427
428 vring_del_virtqueue(vq);
1fcf0512
MT
429}
430
d3f5f065
MT
431static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
432 .get = NULL,
433 .set = NULL,
434 .generation = vp_generation,
435 .get_status = vp_get_status,
436 .set_status = vp_set_status,
437 .reset = vp_reset,
438 .find_vqs = vp_modern_find_vqs,
439 .del_vqs = vp_del_vqs,
440 .get_features = vp_get_features,
441 .finalize_features = vp_finalize_features,
442 .bus_name = vp_bus_name,
443 .set_vq_affinity = vp_set_vq_affinity,
bbaba479 444 .get_vq_affinity = vp_get_vq_affinity,
d3f5f065
MT
445};
446
1fcf0512
MT
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,
bbaba479 460 .get_vq_affinity = vp_get_vq_affinity,
1fcf0512
MT
461};
462
463/**
464 * virtio_pci_find_capability - walk capabilities to find device info.
465 * @dev: the pci device
466 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
467 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
468 *
469 * Returns offset of the capability, or 0.
470 */
471static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
59a5b0f7 472 u32 ioresource_types, int *bars)
1fcf0512
MT
473{
474 int pos;
475
476 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
477 pos > 0;
478 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
479 u8 type, bar;
480 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
481 cfg_type),
482 &type);
483 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
484 bar),
485 &bar);
486
487 /* Ignore structures with reserved BAR values */
488 if (bar > 0x5)
489 continue;
490
491 if (type == cfg_type) {
492 if (pci_resource_len(dev, bar) &&
59a5b0f7
GH
493 pci_resource_flags(dev, bar) & ioresource_types) {
494 *bars |= (1 << bar);
1fcf0512 495 return pos;
59a5b0f7 496 }
1fcf0512
MT
497 }
498 }
499 return 0;
500}
501
89461c4a 502/* This is part of the ABI. Don't screw with it. */
1fcf0512
MT
503static inline void check_offsets(void)
504{
89461c4a
RR
505 /* Note: disk space was harmed in compilation of this function. */
506 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
507 offsetof(struct virtio_pci_cap, cap_vndr));
508 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
509 offsetof(struct virtio_pci_cap, cap_next));
510 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
511 offsetof(struct virtio_pci_cap, cap_len));
512 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
513 offsetof(struct virtio_pci_cap, cfg_type));
514 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
515 offsetof(struct virtio_pci_cap, bar));
516 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
517 offsetof(struct virtio_pci_cap, offset));
518 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
519 offsetof(struct virtio_pci_cap, length));
520 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
521 offsetof(struct virtio_pci_notify_cap,
522 notify_off_multiplier));
523 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
524 offsetof(struct virtio_pci_common_cfg,
525 device_feature_select));
526 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
527 offsetof(struct virtio_pci_common_cfg, device_feature));
528 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
529 offsetof(struct virtio_pci_common_cfg,
530 guest_feature_select));
531 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
532 offsetof(struct virtio_pci_common_cfg, guest_feature));
533 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
534 offsetof(struct virtio_pci_common_cfg, msix_config));
535 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
536 offsetof(struct virtio_pci_common_cfg, num_queues));
537 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
538 offsetof(struct virtio_pci_common_cfg, device_status));
539 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
540 offsetof(struct virtio_pci_common_cfg, config_generation));
541 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
542 offsetof(struct virtio_pci_common_cfg, queue_select));
543 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
544 offsetof(struct virtio_pci_common_cfg, queue_size));
545 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
546 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
547 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
548 offsetof(struct virtio_pci_common_cfg, queue_enable));
549 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
550 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
551 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
552 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
553 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
554 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
555 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
556 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
557 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
558 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
559 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
560 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
561 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
562 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
1fcf0512
MT
563}
564
565/* the PCI probing function */
566int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
567{
568 struct pci_dev *pci_dev = vp_dev->pci_dev;
569 int err, common, isr, notify, device;
570 u32 notify_length;
3909213c 571 u32 notify_offset;
1fcf0512
MT
572
573 check_offsets();
574
575 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
576 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
577 return -ENODEV;
578
579 if (pci_dev->device < 0x1040) {
580 /* Transitional devices: use the PCI subsystem device id as
581 * virtio device id, same as legacy driver always did.
582 */
583 vp_dev->vdev.id.device = pci_dev->subsystem_device;
584 } else {
585 /* Modern devices: simply use PCI device id, but start from 0x1040. */
586 vp_dev->vdev.id.device = pci_dev->device - 0x1040;
587 }
588 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
589
1fcf0512
MT
590 /* check for a common config: if not, use legacy mode (bar 0). */
591 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
59a5b0f7
GH
592 IORESOURCE_IO | IORESOURCE_MEM,
593 &vp_dev->modern_bars);
1fcf0512
MT
594 if (!common) {
595 dev_info(&pci_dev->dev,
596 "virtio_pci: leaving for legacy driver\n");
597 return -ENODEV;
598 }
599
600 /* If common is there, these should be too... */
601 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
59a5b0f7
GH
602 IORESOURCE_IO | IORESOURCE_MEM,
603 &vp_dev->modern_bars);
1fcf0512 604 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
59a5b0f7
GH
605 IORESOURCE_IO | IORESOURCE_MEM,
606 &vp_dev->modern_bars);
1fcf0512
MT
607 if (!isr || !notify) {
608 dev_err(&pci_dev->dev,
609 "virtio_pci: missing capabilities %i/%i/%i\n",
610 common, isr, notify);
611 return -EINVAL;
612 }
613
7a5589b2
AL
614 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
615 if (err)
616 err = dma_set_mask_and_coherent(&pci_dev->dev,
617 DMA_BIT_MASK(32));
618 if (err)
619 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
620
1fcf0512
MT
621 /* Device capability is only mandatory for devices that have
622 * device-specific configuration.
623 */
624 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
59a5b0f7
GH
625 IORESOURCE_IO | IORESOURCE_MEM,
626 &vp_dev->modern_bars);
627
628 err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
629 "virtio-pci-modern");
630 if (err)
631 return err;
1fcf0512
MT
632
633 err = -EINVAL;
634 vp_dev->common = map_capability(pci_dev, common,
635 sizeof(struct virtio_pci_common_cfg), 4,
636 0, sizeof(struct virtio_pci_common_cfg),
637 NULL);
638 if (!vp_dev->common)
639 goto err_map_common;
640 vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
641 0, 1,
642 NULL);
643 if (!vp_dev->isr)
644 goto err_map_isr;
645
646 /* Read notify_off_multiplier from config space. */
647 pci_read_config_dword(pci_dev,
648 notify + offsetof(struct virtio_pci_notify_cap,
649 notify_off_multiplier),
650 &vp_dev->notify_offset_multiplier);
3909213c 651 /* Read notify length and offset from config space. */
1fcf0512
MT
652 pci_read_config_dword(pci_dev,
653 notify + offsetof(struct virtio_pci_notify_cap,
654 cap.length),
655 &notify_length);
656
3909213c
MT
657 pci_read_config_dword(pci_dev,
658 notify + offsetof(struct virtio_pci_notify_cap,
4e94ebdd 659 cap.offset),
3909213c
MT
660 &notify_offset);
661
662 /* We don't know how many VQs we'll map, ahead of the time.
663 * If notify length is small, map it all now.
664 * Otherwise, map each VQ individually later.
665 */
666 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
667 vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
668 0, notify_length,
669 &vp_dev->notify_len);
670 if (!vp_dev->notify_base)
671 goto err_map_notify;
672 } else {
673 vp_dev->notify_map_cap = notify;
674 }
1fcf0512
MT
675
676 /* Again, we don't know how much we should map, but PAGE_SIZE
677 * is more than enough for all existing devices.
678 */
679 if (device) {
680 vp_dev->device = map_capability(pci_dev, device, 0, 4,
681 0, PAGE_SIZE,
682 &vp_dev->device_len);
683 if (!vp_dev->device)
684 goto err_map_device;
1fcf0512 685
d3f5f065
MT
686 vp_dev->vdev.config = &virtio_pci_config_ops;
687 } else {
688 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
689 }
1fcf0512
MT
690
691 vp_dev->config_vector = vp_config_vector;
692 vp_dev->setup_vq = setup_vq;
693 vp_dev->del_vq = del_vq;
694
695 return 0;
696
697err_map_device:
3909213c
MT
698 if (vp_dev->notify_base)
699 pci_iounmap(pci_dev, vp_dev->notify_base);
700err_map_notify:
1fcf0512
MT
701 pci_iounmap(pci_dev, vp_dev->isr);
702err_map_isr:
703 pci_iounmap(pci_dev, vp_dev->common);
704err_map_common:
705 return err;
706}
707
708void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
709{
710 struct pci_dev *pci_dev = vp_dev->pci_dev;
711
712 if (vp_dev->device)
713 pci_iounmap(pci_dev, vp_dev->device);
3909213c
MT
714 if (vp_dev->notify_base)
715 pci_iounmap(pci_dev, vp_dev->notify_base);
1fcf0512
MT
716 pci_iounmap(pci_dev, vp_dev->isr);
717 pci_iounmap(pci_dev, vp_dev->common);
59a5b0f7 718 pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
1fcf0512 719}