blob: 07571daccfec13581a7e5ce59c242de4e2ef1bb6 [file] [log] [blame]
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +02001/*
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
Michael S. Tsirkin05dbcb42016-04-03 15:23:37 +030020#include <linux/delay.h>
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +020021#define VIRTIO_PCI_NO_LEGACY
22#include "virtio_pci_common.h"
23
Michael S. Tsirkinc5d4c2c2015-04-01 13:33:20 +103024/*
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}
Gonglei61bd4052016-11-22 13:51:49 +080036static inline u16 vp_ioread16 (__le16 __iomem *addr)
Michael S. Tsirkinc5d4c2c2015-04-01 13:33:20 +103037{
38 return ioread16(addr);
39}
40
Gonglei61bd4052016-11-22 13:51:49 +080041static inline u32 vp_ioread32(__le32 __iomem *addr)
Michael S. Tsirkinc5d4c2c2015-04-01 13:33:20 +103042{
43 return ioread32(addr);
44}
45
46static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
47{
48 iowrite8(value, addr);
49}
50
Gonglei61bd4052016-11-22 13:51:49 +080051static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
Michael S. Tsirkinc5d4c2c2015-04-01 13:33:20 +103052{
53 iowrite16(value, addr);
54}
55
Gonglei61bd4052016-11-22 13:51:49 +080056static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
Michael S. Tsirkinc5d4c2c2015-04-01 13:33:20 +103057{
58 iowrite32(value, addr);
59}
60
Michael S. Tsirkina8557d32015-04-01 14:43:15 +103061static 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
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +020068static 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
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200142/* 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
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030148 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);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200152
153 return features;
154}
155
Tiwei Biecfecc292018-06-01 12:02:39 +0800156static void vp_transport_features(struct virtio_device *vdev, u64 features)
157{
158 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
159 struct pci_dev *pci_dev = vp_dev->pci_dev;
160
161 if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
162 pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
163 __virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
164}
165
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200166/* virtio config->finalize_features() implementation */
167static int vp_finalize_features(struct virtio_device *vdev)
168{
169 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Tiwei Biecfecc292018-06-01 12:02:39 +0800170 u64 features = vdev->features;
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200171
172 /* Give virtio_ring a chance to accept features. */
173 vring_transport_features(vdev);
174
Tiwei Biecfecc292018-06-01 12:02:39 +0800175 /* Give virtio_pci a chance to accept features. */
176 vp_transport_features(vdev, features);
177
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200178 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
179 dev_err(&vdev->dev, "virtio: device uses modern interface "
180 "but does not have VIRTIO_F_VERSION_1\n");
181 return -EINVAL;
182 }
183
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030184 vp_iowrite32(0, &vp_dev->common->guest_feature_select);
185 vp_iowrite32((u32)vdev->features, &vp_dev->common->guest_feature);
186 vp_iowrite32(1, &vp_dev->common->guest_feature_select);
187 vp_iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200188
189 return 0;
190}
191
192/* virtio config->get() implementation */
193static void vp_get(struct virtio_device *vdev, unsigned offset,
194 void *buf, unsigned len)
195{
196 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
197 u8 b;
198 __le16 w;
199 __le32 l;
200
201 BUG_ON(offset + len > vp_dev->device_len);
202
203 switch (len) {
204 case 1:
205 b = ioread8(vp_dev->device + offset);
206 memcpy(buf, &b, sizeof b);
207 break;
208 case 2:
209 w = cpu_to_le16(ioread16(vp_dev->device + offset));
210 memcpy(buf, &w, sizeof w);
211 break;
212 case 4:
213 l = cpu_to_le32(ioread32(vp_dev->device + offset));
214 memcpy(buf, &l, sizeof l);
215 break;
216 case 8:
217 l = cpu_to_le32(ioread32(vp_dev->device + offset));
218 memcpy(buf, &l, sizeof l);
219 l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l));
220 memcpy(buf + sizeof l, &l, sizeof l);
221 break;
222 default:
223 BUG();
224 }
225}
226
227/* the config->set() implementation. it's symmetric to the config->get()
228 * implementation */
229static void vp_set(struct virtio_device *vdev, unsigned offset,
230 const void *buf, unsigned len)
231{
232 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
233 u8 b;
234 __le16 w;
235 __le32 l;
236
237 BUG_ON(offset + len > vp_dev->device_len);
238
239 switch (len) {
240 case 1:
241 memcpy(&b, buf, sizeof b);
242 iowrite8(b, vp_dev->device + offset);
243 break;
244 case 2:
245 memcpy(&w, buf, sizeof w);
246 iowrite16(le16_to_cpu(w), vp_dev->device + offset);
247 break;
248 case 4:
249 memcpy(&l, buf, sizeof l);
250 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
251 break;
252 case 8:
253 memcpy(&l, buf, sizeof l);
254 iowrite32(le32_to_cpu(l), vp_dev->device + offset);
255 memcpy(&l, buf + sizeof l, sizeof l);
256 iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l);
257 break;
258 default:
259 BUG();
260 }
261}
262
263static u32 vp_generation(struct virtio_device *vdev)
264{
265 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030266 return vp_ioread8(&vp_dev->common->config_generation);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200267}
268
269/* config->{get,set}_status() implementations */
270static u8 vp_get_status(struct virtio_device *vdev)
271{
272 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030273 return vp_ioread8(&vp_dev->common->device_status);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200274}
275
276static void vp_set_status(struct virtio_device *vdev, u8 status)
277{
278 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
279 /* We should never be setting status to 0. */
280 BUG_ON(status == 0);
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030281 vp_iowrite8(status, &vp_dev->common->device_status);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200282}
283
284static void vp_reset(struct virtio_device *vdev)
285{
286 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
287 /* 0 status means a reset. */
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030288 vp_iowrite8(0, &vp_dev->common->device_status);
Michael S. Tsirkin05dbcb42016-04-03 15:23:37 +0300289 /* After writing 0 to device_status, the driver MUST wait for a read of
290 * device_status to return 0 before reinitializing the device.
291 * This will flush out the status write, and flush in device writes,
292 * including MSI-X interrupts, if any.
293 */
294 while (vp_ioread8(&vp_dev->common->device_status))
295 msleep(1);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200296 /* Flush pending VQ/configuration callbacks. */
297 vp_synchronize_vectors(vdev);
298}
299
300static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
301{
302 /* Setup the vector used for configuration events */
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030303 vp_iowrite16(vector, &vp_dev->common->msix_config);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200304 /* Verify we had enough resources to assign the vector */
305 /* Will also flush the write out to device */
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030306 return vp_ioread16(&vp_dev->common->msix_config);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200307}
308
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200309static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
Michael S. Tsirkin0a9b3f42017-04-04 21:44:44 +0300310 struct virtio_pci_vq_info *info,
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200311 unsigned index,
312 void (*callback)(struct virtqueue *vq),
313 const char *name,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200314 bool ctx,
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200315 u16 msix_vec)
316{
317 struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common;
318 struct virtqueue *vq;
319 u16 num, off;
320 int err;
321
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030322 if (index >= vp_ioread16(&cfg->num_queues))
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200323 return ERR_PTR(-ENOENT);
324
325 /* Select the queue we're interested in */
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030326 vp_iowrite16(index, &cfg->queue_select);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200327
328 /* Check if queue is either not available or already active. */
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030329 num = vp_ioread16(&cfg->queue_size);
330 if (!num || vp_ioread16(&cfg->queue_enable))
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200331 return ERR_PTR(-ENOENT);
332
333 if (num & (num - 1)) {
334 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
335 return ERR_PTR(-EINVAL);
336 }
337
338 /* get offset of notification word for this vq */
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030339 off = vp_ioread16(&cfg->queue_notify_off);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200340
Michael S. Tsirkin0a9b3f42017-04-04 21:44:44 +0300341 info->msix_vector = msix_vec;
342
Andy Lutomirski7a5589b2016-02-02 21:46:39 -0800343 /* create the vring */
344 vq = vring_create_virtqueue(index, num,
345 SMP_CACHE_BYTES, &vp_dev->vdev,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200346 true, true, ctx,
347 vp_notify, callback, name);
Andy Lutomirski7a5589b2016-02-02 21:46:39 -0800348 if (!vq)
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200349 return ERR_PTR(-ENOMEM);
350
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200351 /* activate the queue */
Andy Lutomirski7a5589b2016-02-02 21:46:39 -0800352 vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
353 vp_iowrite64_twopart(virtqueue_get_desc_addr(vq),
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030354 &cfg->queue_desc_lo, &cfg->queue_desc_hi);
Andy Lutomirski7a5589b2016-02-02 21:46:39 -0800355 vp_iowrite64_twopart(virtqueue_get_avail_addr(vq),
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030356 &cfg->queue_avail_lo, &cfg->queue_avail_hi);
Andy Lutomirski7a5589b2016-02-02 21:46:39 -0800357 vp_iowrite64_twopart(virtqueue_get_used_addr(vq),
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030358 &cfg->queue_used_lo, &cfg->queue_used_hi);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200359
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200360 if (vp_dev->notify_base) {
361 /* offset should not wrap */
362 if ((u64)off * vp_dev->notify_offset_multiplier + 2
363 > vp_dev->notify_len) {
364 dev_warn(&vp_dev->pci_dev->dev,
365 "bad notification offset %u (x %u) "
366 "for queue %u > %zd",
367 off, vp_dev->notify_offset_multiplier,
368 index, vp_dev->notify_len);
369 err = -EINVAL;
370 goto err_map_notify;
371 }
372 vq->priv = (void __force *)vp_dev->notify_base +
373 off * vp_dev->notify_offset_multiplier;
374 } else {
375 vq->priv = (void __force *)map_capability(vp_dev->pci_dev,
376 vp_dev->notify_map_cap, 2, 2,
377 off * vp_dev->notify_offset_multiplier, 2,
378 NULL);
379 }
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200380
381 if (!vq->priv) {
382 err = -ENOMEM;
383 goto err_map_notify;
384 }
385
386 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030387 vp_iowrite16(msix_vec, &cfg->queue_msix_vector);
388 msix_vec = vp_ioread16(&cfg->queue_msix_vector);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200389 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
390 err = -EBUSY;
391 goto err_assign_vector;
392 }
393 }
394
395 return vq;
396
397err_assign_vector:
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200398 if (!vp_dev->notify_base)
399 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200400err_map_notify:
401 vring_del_virtqueue(vq);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200402 return ERR_PTR(err);
403}
404
405static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200406 struct virtqueue *vqs[],
407 vq_callback_t *callbacks[],
408 const char * const names[], const bool *ctx,
409 struct irq_affinity *desc)
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200410{
411 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
412 struct virtqueue *vq;
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200413 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200414
415 if (rc)
416 return rc;
417
418 /* Select and activate all queues. Has to be done last: once we do
419 * this, there's no way to go back except reset.
420 */
421 list_for_each_entry(vq, &vdev->vqs, list) {
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030422 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
423 vp_iowrite16(1, &vp_dev->common->queue_enable);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200424 }
425
426 return 0;
427}
428
Michael S. Tsirkin0a9b3f42017-04-04 21:44:44 +0300429static void del_vq(struct virtio_pci_vq_info *info)
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200430{
Michael S. Tsirkin0a9b3f42017-04-04 21:44:44 +0300431 struct virtqueue *vq = info->vq;
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200432 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
433
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030434 vp_iowrite16(vq->index, &vp_dev->common->queue_select);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200435
Michael S. Tsirkin2008c152017-04-04 21:09:20 +0300436 if (vp_dev->msix_enabled) {
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030437 vp_iowrite16(VIRTIO_MSI_NO_VECTOR,
438 &vp_dev->common->queue_msix_vector);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200439 /* Flush the write out to device */
Michael S. Tsirkina8557d32015-04-01 14:43:15 +1030440 vp_ioread16(&vp_dev->common->queue_msix_vector);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200441 }
442
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200443 if (!vp_dev->notify_base)
444 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200445
446 vring_del_virtqueue(vq);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200447}
448
Michael S. Tsirkind3f5f062015-01-13 16:34:58 +0200449static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
450 .get = NULL,
451 .set = NULL,
452 .generation = vp_generation,
453 .get_status = vp_get_status,
454 .set_status = vp_set_status,
455 .reset = vp_reset,
456 .find_vqs = vp_modern_find_vqs,
457 .del_vqs = vp_del_vqs,
458 .get_features = vp_get_features,
459 .finalize_features = vp_finalize_features,
460 .bus_name = vp_bus_name,
461 .set_vq_affinity = vp_set_vq_affinity,
Christoph Hellwigbbaba472017-02-05 18:15:23 +0100462 .get_vq_affinity = vp_get_vq_affinity,
Michael S. Tsirkind3f5f062015-01-13 16:34:58 +0200463};
464
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200465static const struct virtio_config_ops virtio_pci_config_ops = {
466 .get = vp_get,
467 .set = vp_set,
468 .generation = vp_generation,
469 .get_status = vp_get_status,
470 .set_status = vp_set_status,
471 .reset = vp_reset,
472 .find_vqs = vp_modern_find_vqs,
473 .del_vqs = vp_del_vqs,
474 .get_features = vp_get_features,
475 .finalize_features = vp_finalize_features,
476 .bus_name = vp_bus_name,
477 .set_vq_affinity = vp_set_vq_affinity,
Christoph Hellwigbbaba472017-02-05 18:15:23 +0100478 .get_vq_affinity = vp_get_vq_affinity,
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200479};
480
481/**
482 * virtio_pci_find_capability - walk capabilities to find device info.
483 * @dev: the pci device
484 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
485 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
486 *
487 * Returns offset of the capability, or 0.
488 */
489static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200490 u32 ioresource_types, int *bars)
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200491{
492 int pos;
493
494 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
495 pos > 0;
496 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
497 u8 type, bar;
498 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
499 cfg_type),
500 &type);
501 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
502 bar),
503 &bar);
504
505 /* Ignore structures with reserved BAR values */
506 if (bar > 0x5)
507 continue;
508
509 if (type == cfg_type) {
510 if (pci_resource_len(dev, bar) &&
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200511 pci_resource_flags(dev, bar) & ioresource_types) {
512 *bars |= (1 << bar);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200513 return pos;
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200514 }
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200515 }
516 }
517 return 0;
518}
519
Rusty Russell89461c42013-05-30 16:29:32 +0930520/* This is part of the ABI. Don't screw with it. */
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200521static inline void check_offsets(void)
522{
Rusty Russell89461c42013-05-30 16:29:32 +0930523 /* Note: disk space was harmed in compilation of this function. */
524 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
525 offsetof(struct virtio_pci_cap, cap_vndr));
526 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
527 offsetof(struct virtio_pci_cap, cap_next));
528 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
529 offsetof(struct virtio_pci_cap, cap_len));
530 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
531 offsetof(struct virtio_pci_cap, cfg_type));
532 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
533 offsetof(struct virtio_pci_cap, bar));
534 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
535 offsetof(struct virtio_pci_cap, offset));
536 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
537 offsetof(struct virtio_pci_cap, length));
538 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
539 offsetof(struct virtio_pci_notify_cap,
540 notify_off_multiplier));
541 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
542 offsetof(struct virtio_pci_common_cfg,
543 device_feature_select));
544 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
545 offsetof(struct virtio_pci_common_cfg, device_feature));
546 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
547 offsetof(struct virtio_pci_common_cfg,
548 guest_feature_select));
549 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
550 offsetof(struct virtio_pci_common_cfg, guest_feature));
551 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
552 offsetof(struct virtio_pci_common_cfg, msix_config));
553 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
554 offsetof(struct virtio_pci_common_cfg, num_queues));
555 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
556 offsetof(struct virtio_pci_common_cfg, device_status));
557 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
558 offsetof(struct virtio_pci_common_cfg, config_generation));
559 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
560 offsetof(struct virtio_pci_common_cfg, queue_select));
561 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
562 offsetof(struct virtio_pci_common_cfg, queue_size));
563 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
564 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
565 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
566 offsetof(struct virtio_pci_common_cfg, queue_enable));
567 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
568 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
569 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
570 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
571 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
572 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
573 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
574 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
575 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
576 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
577 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
578 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
579 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
580 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200581}
582
583/* the PCI probing function */
584int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
585{
586 struct pci_dev *pci_dev = vp_dev->pci_dev;
587 int err, common, isr, notify, device;
588 u32 notify_length;
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200589 u32 notify_offset;
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200590
591 check_offsets();
592
593 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
594 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
595 return -ENODEV;
596
597 if (pci_dev->device < 0x1040) {
598 /* Transitional devices: use the PCI subsystem device id as
599 * virtio device id, same as legacy driver always did.
600 */
601 vp_dev->vdev.id.device = pci_dev->subsystem_device;
602 } else {
603 /* Modern devices: simply use PCI device id, but start from 0x1040. */
604 vp_dev->vdev.id.device = pci_dev->device - 0x1040;
605 }
606 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
607
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200608 /* check for a common config: if not, use legacy mode (bar 0). */
609 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200610 IORESOURCE_IO | IORESOURCE_MEM,
611 &vp_dev->modern_bars);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200612 if (!common) {
613 dev_info(&pci_dev->dev,
614 "virtio_pci: leaving for legacy driver\n");
615 return -ENODEV;
616 }
617
618 /* If common is there, these should be too... */
619 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200620 IORESOURCE_IO | IORESOURCE_MEM,
621 &vp_dev->modern_bars);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200622 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200623 IORESOURCE_IO | IORESOURCE_MEM,
624 &vp_dev->modern_bars);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200625 if (!isr || !notify) {
626 dev_err(&pci_dev->dev,
627 "virtio_pci: missing capabilities %i/%i/%i\n",
628 common, isr, notify);
629 return -EINVAL;
630 }
631
Andy Lutomirski7a5589b2016-02-02 21:46:39 -0800632 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
633 if (err)
634 err = dma_set_mask_and_coherent(&pci_dev->dev,
635 DMA_BIT_MASK(32));
636 if (err)
637 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
638
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200639 /* Device capability is only mandatory for devices that have
640 * device-specific configuration.
641 */
642 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200643 IORESOURCE_IO | IORESOURCE_MEM,
644 &vp_dev->modern_bars);
645
646 err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars,
647 "virtio-pci-modern");
648 if (err)
649 return err;
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200650
651 err = -EINVAL;
652 vp_dev->common = map_capability(pci_dev, common,
653 sizeof(struct virtio_pci_common_cfg), 4,
654 0, sizeof(struct virtio_pci_common_cfg),
655 NULL);
656 if (!vp_dev->common)
657 goto err_map_common;
658 vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1,
659 0, 1,
660 NULL);
661 if (!vp_dev->isr)
662 goto err_map_isr;
663
664 /* Read notify_off_multiplier from config space. */
665 pci_read_config_dword(pci_dev,
666 notify + offsetof(struct virtio_pci_notify_cap,
667 notify_off_multiplier),
668 &vp_dev->notify_offset_multiplier);
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200669 /* Read notify length and offset from config space. */
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200670 pci_read_config_dword(pci_dev,
671 notify + offsetof(struct virtio_pci_notify_cap,
672 cap.length),
673 &notify_length);
674
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200675 pci_read_config_dword(pci_dev,
676 notify + offsetof(struct virtio_pci_notify_cap,
Ladi Prosek4e94ebd2016-02-01 19:36:31 +0100677 cap.offset),
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200678 &notify_offset);
679
680 /* We don't know how many VQs we'll map, ahead of the time.
681 * If notify length is small, map it all now.
682 * Otherwise, map each VQ individually later.
683 */
684 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
685 vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2,
686 0, notify_length,
687 &vp_dev->notify_len);
688 if (!vp_dev->notify_base)
689 goto err_map_notify;
690 } else {
691 vp_dev->notify_map_cap = notify;
692 }
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200693
694 /* Again, we don't know how much we should map, but PAGE_SIZE
695 * is more than enough for all existing devices.
696 */
697 if (device) {
698 vp_dev->device = map_capability(pci_dev, device, 0, 4,
699 0, PAGE_SIZE,
700 &vp_dev->device_len);
701 if (!vp_dev->device)
702 goto err_map_device;
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200703
Michael S. Tsirkind3f5f062015-01-13 16:34:58 +0200704 vp_dev->vdev.config = &virtio_pci_config_ops;
705 } else {
706 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
707 }
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200708
709 vp_dev->config_vector = vp_config_vector;
710 vp_dev->setup_vq = setup_vq;
711 vp_dev->del_vq = del_vq;
712
713 return 0;
714
715err_map_device:
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200716 if (vp_dev->notify_base)
717 pci_iounmap(pci_dev, vp_dev->notify_base);
718err_map_notify:
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200719 pci_iounmap(pci_dev, vp_dev->isr);
720err_map_isr:
721 pci_iounmap(pci_dev, vp_dev->common);
722err_map_common:
723 return err;
724}
725
726void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
727{
728 struct pci_dev *pci_dev = vp_dev->pci_dev;
729
730 if (vp_dev->device)
731 pci_iounmap(pci_dev, vp_dev->device);
Michael S. Tsirkin39092132015-01-14 18:50:55 +0200732 if (vp_dev->notify_base)
733 pci_iounmap(pci_dev, vp_dev->notify_base);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200734 pci_iounmap(pci_dev, vp_dev->isr);
735 pci_iounmap(pci_dev, vp_dev->common);
Gerd Hoffmann59a5b0f72015-06-24 07:54:15 +0200736 pci_release_selected_regions(pci_dev, vp_dev->modern_bars);
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200737}