baadc79c43031fa86707d0c0b412a8156fc2e213
[dpdk.git] / drivers / net / virtio / virtio_pci.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <stdint.h>
5
6 #ifdef RTE_EXEC_ENV_LINUX
7  #include <dirent.h>
8  #include <fcntl.h>
9 #endif
10
11 #include <rte_io.h>
12 #include <rte_bus.h>
13
14 #include "virtio_pci.h"
15 #include "virtio_logs.h"
16 #include "virtqueue.h"
17
18 /*
19  * Following macros are derived from linux/pci_regs.h, however,
20  * we can't simply include that header here, as there is no such
21  * file for non-Linux platform.
22  */
23 #define PCI_CAPABILITY_LIST     0x34
24 #define PCI_CAP_ID_VNDR         0x09
25 #define PCI_CAP_ID_MSIX         0x11
26
27 /*
28  * The remaining space is defined by each driver as the per-driver
29  * configuration space.
30  */
31 #define VIRTIO_PCI_CONFIG(hw) \
32                 (((hw)->use_msix == VIRTIO_MSIX_ENABLED) ? 24 : 20)
33
34
35 struct virtio_pci_internal {
36         struct rte_pci_ioport io;
37 };
38
39 #define VTPCI_IO(hw) (&virtio_pci_internal[(hw)->port_id].io)
40
41 struct virtio_pci_internal virtio_pci_internal[RTE_MAX_ETHPORTS];
42
43 static inline int
44 check_vq_phys_addr_ok(struct virtqueue *vq)
45 {
46         /* Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
47          * and only accepts 32 bit page frame number.
48          * Check if the allocated physical memory exceeds 16TB.
49          */
50         if ((vq->vq_ring_mem + vq->vq_ring_size - 1) >>
51                         (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
52                 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
53                 return 0;
54         }
55
56         return 1;
57 }
58
59 #define PCI_MSIX_ENABLE 0x8000
60
61 static enum virtio_msix_status
62 vtpci_msix_detect(struct rte_pci_device *dev)
63 {
64         uint8_t pos;
65         int ret;
66
67         ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
68         if (ret != 1) {
69                 PMD_INIT_LOG(DEBUG,
70                              "failed to read pci capability list, ret %d", ret);
71                 return VIRTIO_MSIX_NONE;
72         }
73
74         while (pos) {
75                 uint8_t cap[2];
76
77                 ret = rte_pci_read_config(dev, cap, sizeof(cap), pos);
78                 if (ret != sizeof(cap)) {
79                         PMD_INIT_LOG(DEBUG,
80                                      "failed to read pci cap at pos: %x ret %d",
81                                      pos, ret);
82                         break;
83                 }
84
85                 if (cap[0] == PCI_CAP_ID_MSIX) {
86                         uint16_t flags;
87
88                         ret = rte_pci_read_config(dev, &flags, sizeof(flags),
89                                         pos + sizeof(cap));
90                         if (ret != sizeof(flags)) {
91                                 PMD_INIT_LOG(DEBUG,
92                                              "failed to read pci cap at pos:"
93                                              " %x ret %d", pos + 2, ret);
94                                 break;
95                         }
96
97                         if (flags & PCI_MSIX_ENABLE)
98                                 return VIRTIO_MSIX_ENABLED;
99                         else
100                                 return VIRTIO_MSIX_DISABLED;
101                 }
102
103                 pos = cap[1];
104         }
105
106         return VIRTIO_MSIX_NONE;
107 }
108
109 /*
110  * Since we are in legacy mode:
111  * http://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf
112  *
113  * "Note that this is possible because while the virtio header is PCI (i.e.
114  * little) endian, the device-specific region is encoded in the native endian of
115  * the guest (where such distinction is applicable)."
116  *
117  * For powerpc which supports both, qemu supposes that cpu is big endian and
118  * enforces this for the virtio-net stuff.
119  */
120 static void
121 legacy_read_dev_config(struct virtio_hw *hw, size_t offset,
122                        void *dst, int length)
123 {
124 #ifdef RTE_ARCH_PPC_64
125         int size;
126
127         while (length > 0) {
128                 if (length >= 4) {
129                         size = 4;
130                         rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
131                                 VIRTIO_PCI_CONFIG(hw) + offset);
132                         *(uint32_t *)dst = rte_be_to_cpu_32(*(uint32_t *)dst);
133                 } else if (length >= 2) {
134                         size = 2;
135                         rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
136                                 VIRTIO_PCI_CONFIG(hw) + offset);
137                         *(uint16_t *)dst = rte_be_to_cpu_16(*(uint16_t *)dst);
138                 } else {
139                         size = 1;
140                         rte_pci_ioport_read(VTPCI_IO(hw), dst, size,
141                                 VIRTIO_PCI_CONFIG(hw) + offset);
142                 }
143
144                 dst = (char *)dst + size;
145                 offset += size;
146                 length -= size;
147         }
148 #else
149         rte_pci_ioport_read(VTPCI_IO(hw), dst, length,
150                 VIRTIO_PCI_CONFIG(hw) + offset);
151 #endif
152 }
153
154 static void
155 legacy_write_dev_config(struct virtio_hw *hw, size_t offset,
156                         const void *src, int length)
157 {
158 #ifdef RTE_ARCH_PPC_64
159         union {
160                 uint32_t u32;
161                 uint16_t u16;
162         } tmp;
163         int size;
164
165         while (length > 0) {
166                 if (length >= 4) {
167                         size = 4;
168                         tmp.u32 = rte_cpu_to_be_32(*(const uint32_t *)src);
169                         rte_pci_ioport_write(VTPCI_IO(hw), &tmp.u32, size,
170                                 VIRTIO_PCI_CONFIG(hw) + offset);
171                 } else if (length >= 2) {
172                         size = 2;
173                         tmp.u16 = rte_cpu_to_be_16(*(const uint16_t *)src);
174                         rte_pci_ioport_write(VTPCI_IO(hw), &tmp.u16, size,
175                                 VIRTIO_PCI_CONFIG(hw) + offset);
176                 } else {
177                         size = 1;
178                         rte_pci_ioport_write(VTPCI_IO(hw), src, size,
179                                 VIRTIO_PCI_CONFIG(hw) + offset);
180                 }
181
182                 src = (const char *)src + size;
183                 offset += size;
184                 length -= size;
185         }
186 #else
187         rte_pci_ioport_write(VTPCI_IO(hw), src, length,
188                 VIRTIO_PCI_CONFIG(hw) + offset);
189 #endif
190 }
191
192 static uint64_t
193 legacy_get_features(struct virtio_hw *hw)
194 {
195         uint32_t dst;
196
197         rte_pci_ioport_read(VTPCI_IO(hw), &dst, 4, VIRTIO_PCI_HOST_FEATURES);
198         return dst;
199 }
200
201 static void
202 legacy_set_features(struct virtio_hw *hw, uint64_t features)
203 {
204         if ((features >> 32) != 0) {
205                 PMD_DRV_LOG(ERR,
206                         "only 32 bit features are allowed for legacy virtio!");
207                 return;
208         }
209         rte_pci_ioport_write(VTPCI_IO(hw), &features, 4,
210                 VIRTIO_PCI_GUEST_FEATURES);
211 }
212
213 static int
214 legacy_features_ok(struct virtio_hw *hw __rte_unused)
215 {
216         return 0;
217 }
218
219 static uint8_t
220 legacy_get_status(struct virtio_hw *hw)
221 {
222         uint8_t dst;
223
224         rte_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_STATUS);
225         return dst;
226 }
227
228 static void
229 legacy_set_status(struct virtio_hw *hw, uint8_t status)
230 {
231         rte_pci_ioport_write(VTPCI_IO(hw), &status, 1, VIRTIO_PCI_STATUS);
232 }
233
234 static uint8_t
235 legacy_get_isr(struct virtio_hw *hw)
236 {
237         uint8_t dst;
238
239         rte_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_ISR);
240         return dst;
241 }
242
243 /* Enable one vector (0) for Link State Intrerrupt */
244 static uint16_t
245 legacy_set_config_irq(struct virtio_hw *hw, uint16_t vec)
246 {
247         uint16_t dst;
248
249         rte_pci_ioport_write(VTPCI_IO(hw), &vec, 2, VIRTIO_MSI_CONFIG_VECTOR);
250         rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_MSI_CONFIG_VECTOR);
251         return dst;
252 }
253
254 static uint16_t
255 legacy_set_queue_irq(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec)
256 {
257         uint16_t dst;
258
259         rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
260                 VIRTIO_PCI_QUEUE_SEL);
261         rte_pci_ioport_write(VTPCI_IO(hw), &vec, 2, VIRTIO_MSI_QUEUE_VECTOR);
262         rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_MSI_QUEUE_VECTOR);
263         return dst;
264 }
265
266 static uint16_t
267 legacy_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
268 {
269         uint16_t dst;
270
271         rte_pci_ioport_write(VTPCI_IO(hw), &queue_id, 2, VIRTIO_PCI_QUEUE_SEL);
272         rte_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_PCI_QUEUE_NUM);
273         return dst;
274 }
275
276 static int
277 legacy_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
278 {
279         uint32_t src;
280
281         if (!check_vq_phys_addr_ok(vq))
282                 return -1;
283
284         rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
285                 VIRTIO_PCI_QUEUE_SEL);
286         src = vq->vq_ring_mem >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
287         rte_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
288
289         return 0;
290 }
291
292 static void
293 legacy_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
294 {
295         uint32_t src = 0;
296
297         rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
298                 VIRTIO_PCI_QUEUE_SEL);
299         rte_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
300 }
301
302 static void
303 legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
304 {
305         rte_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
306                 VIRTIO_PCI_QUEUE_NOTIFY);
307 }
308
309 static void
310 legacy_intr_detect(struct virtio_hw *hw)
311 {
312         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
313
314         hw->use_msix = vtpci_msix_detect(dev->pci_dev);
315 }
316
317 static int
318 legacy_dev_close(struct virtio_hw *hw)
319 {
320         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
321
322         rte_pci_unmap_device(dev->pci_dev);
323         rte_pci_ioport_unmap(VTPCI_IO(hw));
324
325         return 0;
326 }
327
328 const struct virtio_ops legacy_ops = {
329         .read_dev_cfg   = legacy_read_dev_config,
330         .write_dev_cfg  = legacy_write_dev_config,
331         .get_status     = legacy_get_status,
332         .set_status     = legacy_set_status,
333         .get_features   = legacy_get_features,
334         .set_features   = legacy_set_features,
335         .features_ok    = legacy_features_ok,
336         .get_isr        = legacy_get_isr,
337         .set_config_irq = legacy_set_config_irq,
338         .set_queue_irq  = legacy_set_queue_irq,
339         .get_queue_num  = legacy_get_queue_num,
340         .setup_queue    = legacy_setup_queue,
341         .del_queue      = legacy_del_queue,
342         .notify_queue   = legacy_notify_queue,
343         .intr_detect    = legacy_intr_detect,
344         .dev_close      = legacy_dev_close,
345 };
346
347 static inline void
348 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi)
349 {
350         rte_write32(val & ((1ULL << 32) - 1), lo);
351         rte_write32(val >> 32,               hi);
352 }
353
354 static void
355 modern_read_dev_config(struct virtio_hw *hw, size_t offset,
356                        void *dst, int length)
357 {
358         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
359         int i;
360         uint8_t *p;
361         uint8_t old_gen, new_gen;
362
363         do {
364                 old_gen = rte_read8(&dev->common_cfg->config_generation);
365
366                 p = dst;
367                 for (i = 0;  i < length; i++)
368                         *p++ = rte_read8((uint8_t *)dev->dev_cfg + offset + i);
369
370                 new_gen = rte_read8(&dev->common_cfg->config_generation);
371         } while (old_gen != new_gen);
372 }
373
374 static void
375 modern_write_dev_config(struct virtio_hw *hw, size_t offset,
376                         const void *src, int length)
377 {
378         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
379         int i;
380         const uint8_t *p = src;
381
382         for (i = 0;  i < length; i++)
383                 rte_write8((*p++), (((uint8_t *)dev->dev_cfg) + offset + i));
384 }
385
386 static uint64_t
387 modern_get_features(struct virtio_hw *hw)
388 {
389         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
390         uint32_t features_lo, features_hi;
391
392         rte_write32(0, &dev->common_cfg->device_feature_select);
393         features_lo = rte_read32(&dev->common_cfg->device_feature);
394
395         rte_write32(1, &dev->common_cfg->device_feature_select);
396         features_hi = rte_read32(&dev->common_cfg->device_feature);
397
398         return ((uint64_t)features_hi << 32) | features_lo;
399 }
400
401 static void
402 modern_set_features(struct virtio_hw *hw, uint64_t features)
403 {
404         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
405
406         rte_write32(0, &dev->common_cfg->guest_feature_select);
407         rte_write32(features & ((1ULL << 32) - 1),
408                     &dev->common_cfg->guest_feature);
409
410         rte_write32(1, &dev->common_cfg->guest_feature_select);
411         rte_write32(features >> 32,
412                     &dev->common_cfg->guest_feature);
413 }
414
415 static int
416 modern_features_ok(struct virtio_hw *hw)
417 {
418         if (!virtio_with_feature(hw, VIRTIO_F_VERSION_1)) {
419                 PMD_INIT_LOG(ERR, "Version 1+ required with modern devices\n");
420                 return -1;
421         }
422
423         return 0;
424 }
425
426 static uint8_t
427 modern_get_status(struct virtio_hw *hw)
428 {
429         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
430
431         return rte_read8(&dev->common_cfg->device_status);
432 }
433
434 static void
435 modern_set_status(struct virtio_hw *hw, uint8_t status)
436 {
437         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
438
439         rte_write8(status, &dev->common_cfg->device_status);
440 }
441
442 static uint8_t
443 modern_get_isr(struct virtio_hw *hw)
444 {
445         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
446
447         return rte_read8(dev->isr);
448 }
449
450 static uint16_t
451 modern_set_config_irq(struct virtio_hw *hw, uint16_t vec)
452 {
453         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
454
455         rte_write16(vec, &dev->common_cfg->msix_config);
456         return rte_read16(&dev->common_cfg->msix_config);
457 }
458
459 static uint16_t
460 modern_set_queue_irq(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec)
461 {
462         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
463
464         rte_write16(vq->vq_queue_index, &dev->common_cfg->queue_select);
465         rte_write16(vec, &dev->common_cfg->queue_msix_vector);
466         return rte_read16(&dev->common_cfg->queue_msix_vector);
467 }
468
469 static uint16_t
470 modern_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
471 {
472         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
473
474         rte_write16(queue_id, &dev->common_cfg->queue_select);
475         return rte_read16(&dev->common_cfg->queue_size);
476 }
477
478 static int
479 modern_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
480 {
481         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
482         uint64_t desc_addr, avail_addr, used_addr;
483         uint16_t notify_off;
484
485         if (!check_vq_phys_addr_ok(vq))
486                 return -1;
487
488         desc_addr = vq->vq_ring_mem;
489         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
490         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
491                                                          ring[vq->vq_nentries]),
492                                    VIRTIO_PCI_VRING_ALIGN);
493
494         rte_write16(vq->vq_queue_index, &dev->common_cfg->queue_select);
495
496         io_write64_twopart(desc_addr, &dev->common_cfg->queue_desc_lo,
497                                       &dev->common_cfg->queue_desc_hi);
498         io_write64_twopart(avail_addr, &dev->common_cfg->queue_avail_lo,
499                                        &dev->common_cfg->queue_avail_hi);
500         io_write64_twopart(used_addr, &dev->common_cfg->queue_used_lo,
501                                       &dev->common_cfg->queue_used_hi);
502
503         notify_off = rte_read16(&dev->common_cfg->queue_notify_off);
504         vq->notify_addr = (void *)((uint8_t *)dev->notify_base +
505                                 notify_off * dev->notify_off_multiplier);
506
507         rte_write16(1, &dev->common_cfg->queue_enable);
508
509         PMD_INIT_LOG(DEBUG, "queue %u addresses:", vq->vq_queue_index);
510         PMD_INIT_LOG(DEBUG, "\t desc_addr: %" PRIx64, desc_addr);
511         PMD_INIT_LOG(DEBUG, "\t aval_addr: %" PRIx64, avail_addr);
512         PMD_INIT_LOG(DEBUG, "\t used_addr: %" PRIx64, used_addr);
513         PMD_INIT_LOG(DEBUG, "\t notify addr: %p (notify offset: %u)",
514                 vq->notify_addr, notify_off);
515
516         return 0;
517 }
518
519 static void
520 modern_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
521 {
522         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
523
524         rte_write16(vq->vq_queue_index, &dev->common_cfg->queue_select);
525
526         io_write64_twopart(0, &dev->common_cfg->queue_desc_lo,
527                                   &dev->common_cfg->queue_desc_hi);
528         io_write64_twopart(0, &dev->common_cfg->queue_avail_lo,
529                                   &dev->common_cfg->queue_avail_hi);
530         io_write64_twopart(0, &dev->common_cfg->queue_used_lo,
531                                   &dev->common_cfg->queue_used_hi);
532
533         rte_write16(0, &dev->common_cfg->queue_enable);
534 }
535
536 static void
537 modern_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
538 {
539         uint32_t notify_data;
540
541         if (!virtio_with_feature(hw, VIRTIO_F_NOTIFICATION_DATA)) {
542                 rte_write16(vq->vq_queue_index, vq->notify_addr);
543                 return;
544         }
545
546         if (virtio_with_packed_queue(hw)) {
547                 /*
548                  * Bit[0:15]: vq queue index
549                  * Bit[16:30]: avail index
550                  * Bit[31]: avail wrap counter
551                  */
552                 notify_data = ((uint32_t)(!!(vq->vq_packed.cached_flags &
553                                 VRING_PACKED_DESC_F_AVAIL)) << 31) |
554                                 ((uint32_t)vq->vq_avail_idx << 16) |
555                                 vq->vq_queue_index;
556         } else {
557                 /*
558                  * Bit[0:15]: vq queue index
559                  * Bit[16:31]: avail index
560                  */
561                 notify_data = ((uint32_t)vq->vq_avail_idx << 16) |
562                                 vq->vq_queue_index;
563         }
564         rte_write32(notify_data, vq->notify_addr);
565 }
566
567
568
569 static void
570 modern_intr_detect(struct virtio_hw *hw)
571 {
572         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
573
574         hw->use_msix = vtpci_msix_detect(dev->pci_dev);
575 }
576
577 static int
578 modern_dev_close(struct virtio_hw *hw)
579 {
580         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
581
582         rte_pci_unmap_device(dev->pci_dev);
583
584         return 0;
585 }
586
587 const struct virtio_ops modern_ops = {
588         .read_dev_cfg   = modern_read_dev_config,
589         .write_dev_cfg  = modern_write_dev_config,
590         .get_status     = modern_get_status,
591         .set_status     = modern_set_status,
592         .get_features   = modern_get_features,
593         .set_features   = modern_set_features,
594         .features_ok    = modern_features_ok,
595         .get_isr        = modern_get_isr,
596         .set_config_irq = modern_set_config_irq,
597         .set_queue_irq  = modern_set_queue_irq,
598         .get_queue_num  = modern_get_queue_num,
599         .setup_queue    = modern_setup_queue,
600         .del_queue      = modern_del_queue,
601         .notify_queue   = modern_notify_queue,
602         .intr_detect    = modern_intr_detect,
603         .dev_close      = modern_dev_close,
604 };
605
606 uint8_t
607 vtpci_isr(struct virtio_hw *hw)
608 {
609         return VIRTIO_OPS(hw)->get_isr(hw);
610 }
611
612 static void *
613 get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
614 {
615         uint8_t  bar    = cap->bar;
616         uint32_t length = cap->length;
617         uint32_t offset = cap->offset;
618         uint8_t *base;
619
620         if (bar >= PCI_MAX_RESOURCE) {
621                 PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
622                 return NULL;
623         }
624
625         if (offset + length < offset) {
626                 PMD_INIT_LOG(ERR, "offset(%u) + length(%u) overflows",
627                         offset, length);
628                 return NULL;
629         }
630
631         if (offset + length > dev->mem_resource[bar].len) {
632                 PMD_INIT_LOG(ERR,
633                         "invalid cap: overflows bar space: %u > %" PRIu64,
634                         offset + length, dev->mem_resource[bar].len);
635                 return NULL;
636         }
637
638         base = dev->mem_resource[bar].addr;
639         if (base == NULL) {
640                 PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar);
641                 return NULL;
642         }
643
644         return base + offset;
645 }
646
647 static int
648 virtio_read_caps(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
649 {
650         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
651         uint8_t pos;
652         struct virtio_pci_cap cap;
653         int ret;
654
655         if (rte_pci_map_device(pci_dev)) {
656                 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
657                 return -1;
658         }
659
660         ret = rte_pci_read_config(pci_dev, &pos, 1, PCI_CAPABILITY_LIST);
661         if (ret != 1) {
662                 PMD_INIT_LOG(DEBUG,
663                              "failed to read pci capability list, ret %d", ret);
664                 return -1;
665         }
666
667         while (pos) {
668                 ret = rte_pci_read_config(pci_dev, &cap, 2, pos);
669                 if (ret != 2) {
670                         PMD_INIT_LOG(DEBUG,
671                                      "failed to read pci cap at pos: %x ret %d",
672                                      pos, ret);
673                         break;
674                 }
675
676                 if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
677                         /* Transitional devices would also have this capability,
678                          * that's why we also check if msix is enabled.
679                          * 1st byte is cap ID; 2nd byte is the position of next
680                          * cap; next two bytes are the flags.
681                          */
682                         uint16_t flags;
683
684                         ret = rte_pci_read_config(pci_dev, &flags, sizeof(flags),
685                                         pos + 2);
686                         if (ret != sizeof(flags)) {
687                                 PMD_INIT_LOG(DEBUG,
688                                              "failed to read pci cap at pos:"
689                                              " %x ret %d", pos + 2, ret);
690                                 break;
691                         }
692
693                         if (flags & PCI_MSIX_ENABLE)
694                                 hw->use_msix = VIRTIO_MSIX_ENABLED;
695                         else
696                                 hw->use_msix = VIRTIO_MSIX_DISABLED;
697                 }
698
699                 if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
700                         PMD_INIT_LOG(DEBUG,
701                                 "[%2x] skipping non VNDR cap id: %02x",
702                                 pos, cap.cap_vndr);
703                         goto next;
704                 }
705
706                 ret = rte_pci_read_config(pci_dev, &cap, sizeof(cap), pos);
707                 if (ret != sizeof(cap)) {
708                         PMD_INIT_LOG(DEBUG,
709                                      "failed to read pci cap at pos: %x ret %d",
710                                      pos, ret);
711                         break;
712                 }
713
714                 PMD_INIT_LOG(DEBUG,
715                         "[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
716                         pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
717
718                 switch (cap.cfg_type) {
719                 case VIRTIO_PCI_CAP_COMMON_CFG:
720                         dev->common_cfg = get_cfg_addr(pci_dev, &cap);
721                         break;
722                 case VIRTIO_PCI_CAP_NOTIFY_CFG:
723                         ret = rte_pci_read_config(pci_dev,
724                                         &dev->notify_off_multiplier,
725                                         4, pos + sizeof(cap));
726                         if (ret != 4)
727                                 PMD_INIT_LOG(DEBUG,
728                                         "failed to read notify_off_multiplier, ret %d",
729                                         ret);
730                         else
731                                 dev->notify_base = get_cfg_addr(pci_dev, &cap);
732                         break;
733                 case VIRTIO_PCI_CAP_DEVICE_CFG:
734                         dev->dev_cfg = get_cfg_addr(pci_dev, &cap);
735                         break;
736                 case VIRTIO_PCI_CAP_ISR_CFG:
737                         dev->isr = get_cfg_addr(pci_dev, &cap);
738                         break;
739                 }
740
741 next:
742                 pos = cap.cap_next;
743         }
744
745         if (dev->common_cfg == NULL || dev->notify_base == NULL ||
746             dev->dev_cfg == NULL    || dev->isr == NULL) {
747                 PMD_INIT_LOG(INFO, "no modern virtio pci device found.");
748                 return -1;
749         }
750
751         PMD_INIT_LOG(INFO, "found modern virtio pci device.");
752
753         PMD_INIT_LOG(DEBUG, "common cfg mapped at: %p", dev->common_cfg);
754         PMD_INIT_LOG(DEBUG, "device cfg mapped at: %p", dev->dev_cfg);
755         PMD_INIT_LOG(DEBUG, "isr cfg mapped at: %p", dev->isr);
756         PMD_INIT_LOG(DEBUG, "notify base: %p, notify off multiplier: %u",
757                 dev->notify_base, dev->notify_off_multiplier);
758
759         return 0;
760 }
761
762 /*
763  * Return -1:
764  *   if there is error mapping with VFIO/UIO.
765  *   if port map error when driver type is KDRV_NONE.
766  *   if marked as allowed but driver type is KDRV_UNKNOWN.
767  * Return 1 if kernel driver is managing the device.
768  * Return 0 on success.
769  */
770 int
771 vtpci_init(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev)
772 {
773         struct virtio_hw *hw = &dev->hw;
774
775         RTE_BUILD_BUG_ON(offsetof(struct virtio_pci_dev, hw) != 0);
776
777         dev->pci_dev = pci_dev;
778
779         /*
780          * Try if we can succeed reading virtio pci caps, which exists
781          * only on modern pci device. If failed, we fallback to legacy
782          * virtio handling.
783          */
784         if (virtio_read_caps(pci_dev, hw) == 0) {
785                 PMD_INIT_LOG(INFO, "modern virtio pci detected.");
786                 VIRTIO_OPS(hw) = &modern_ops;
787                 dev->modern = true;
788                 goto msix_detect;
789         }
790
791         PMD_INIT_LOG(INFO, "trying with legacy virtio pci.");
792         if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0) {
793                 rte_pci_unmap_device(pci_dev);
794                 if (pci_dev->kdrv == RTE_PCI_KDRV_UNKNOWN &&
795                     (!pci_dev->device.devargs ||
796                      pci_dev->device.devargs->bus !=
797                      rte_bus_find_by_name("pci"))) {
798                         PMD_INIT_LOG(INFO,
799                                 "skip kernel managed virtio device.");
800                         return 1;
801                 }
802                 return -1;
803         }
804
805         VIRTIO_OPS(hw) = &legacy_ops;
806         dev->modern = false;
807
808 msix_detect:
809         VIRTIO_OPS(hw)->intr_detect(hw);
810
811         return 0;
812 }
813
814 void vtpci_legacy_ioport_unmap(struct virtio_hw *hw)
815 {
816         rte_pci_ioport_unmap(VTPCI_IO(hw));
817 }
818
819 int vtpci_legacy_ioport_map(struct virtio_hw *hw)
820 {
821         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
822
823         return rte_pci_ioport_map(dev->pci_dev, 0, VTPCI_IO(hw));
824 }