Imported Upstream version 16.11.2
[deb_dpdk.git] / drivers / net / virtio / virtio_pci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdint.h>
34
35 #ifdef RTE_EXEC_ENV_LINUXAPP
36  #include <dirent.h>
37  #include <fcntl.h>
38 #endif
39
40 #include "virtio_pci.h"
41 #include "virtio_logs.h"
42 #include "virtqueue.h"
43
44 /*
45  * Following macros are derived from linux/pci_regs.h, however,
46  * we can't simply include that header here, as there is no such
47  * file for non-Linux platform.
48  */
49 #define PCI_CAPABILITY_LIST     0x34
50 #define PCI_CAP_ID_VNDR         0x09
51 #define PCI_CAP_ID_MSIX         0x11
52
53 /*
54  * The remaining space is defined by each driver as the per-driver
55  * configuration space.
56  */
57 #define VIRTIO_PCI_CONFIG(hw) (((hw)->use_msix) ? 24 : 20)
58
59 static inline int
60 check_vq_phys_addr_ok(struct virtqueue *vq)
61 {
62         /* Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
63          * and only accepts 32 bit page frame number.
64          * Check if the allocated physical memory exceeds 16TB.
65          */
66         if ((vq->vq_ring_mem + vq->vq_ring_size - 1) >>
67                         (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
68                 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
69                 return 0;
70         }
71
72         return 1;
73 }
74
75 /*
76  * Since we are in legacy mode:
77  * http://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf
78  *
79  * "Note that this is possible because while the virtio header is PCI (i.e.
80  * little) endian, the device-specific region is encoded in the native endian of
81  * the guest (where such distinction is applicable)."
82  *
83  * For powerpc which supports both, qemu supposes that cpu is big endian and
84  * enforces this for the virtio-net stuff.
85  */
86 static void
87 legacy_read_dev_config(struct virtio_hw *hw, size_t offset,
88                        void *dst, int length)
89 {
90 #ifdef RTE_ARCH_PPC_64
91         int size;
92
93         while (length > 0) {
94                 if (length >= 4) {
95                         size = 4;
96                         rte_eal_pci_ioport_read(VTPCI_IO(hw), dst, size,
97                                 VIRTIO_PCI_CONFIG(hw) + offset);
98                         *(uint32_t *)dst = rte_be_to_cpu_32(*(uint32_t *)dst);
99                 } else if (length >= 2) {
100                         size = 2;
101                         rte_eal_pci_ioport_read(VTPCI_IO(hw), dst, size,
102                                 VIRTIO_PCI_CONFIG(hw) + offset);
103                         *(uint16_t *)dst = rte_be_to_cpu_16(*(uint16_t *)dst);
104                 } else {
105                         size = 1;
106                         rte_eal_pci_ioport_read(VTPCI_IO(hw), dst, size,
107                                 VIRTIO_PCI_CONFIG(hw) + offset);
108                 }
109
110                 dst = (char *)dst + size;
111                 offset += size;
112                 length -= size;
113         }
114 #else
115         rte_eal_pci_ioport_read(VTPCI_IO(hw), dst, length,
116                                 VIRTIO_PCI_CONFIG(hw) + offset);
117 #endif
118 }
119
120 static void
121 legacy_write_dev_config(struct virtio_hw *hw, size_t offset,
122                         const void *src, int length)
123 {
124 #ifdef RTE_ARCH_PPC_64
125         union {
126                 uint32_t u32;
127                 uint16_t u16;
128         } tmp;
129         int size;
130
131         while (length > 0) {
132                 if (length >= 4) {
133                         size = 4;
134                         tmp.u32 = rte_cpu_to_be_32(*(const uint32_t *)src);
135                         rte_eal_pci_ioport_write(VTPCI_IO(hw), &tmp.u32, size,
136                                 VIRTIO_PCI_CONFIG(hw) + offset);
137                 } else if (length >= 2) {
138                         size = 2;
139                         tmp.u16 = rte_cpu_to_be_16(*(const uint16_t *)src);
140                         rte_eal_pci_ioport_write(VTPCI_IO(hw), &tmp.u16, size,
141                                 VIRTIO_PCI_CONFIG(hw) + offset);
142                 } else {
143                         size = 1;
144                         rte_eal_pci_ioport_write(VTPCI_IO(hw), src, size,
145                                 VIRTIO_PCI_CONFIG(hw) + offset);
146                 }
147
148                 src = (const char *)src + size;
149                 offset += size;
150                 length -= size;
151         }
152 #else
153         rte_eal_pci_ioport_write(VTPCI_IO(hw), src, length,
154                                  VIRTIO_PCI_CONFIG(hw) + offset);
155 #endif
156 }
157
158 static uint64_t
159 legacy_get_features(struct virtio_hw *hw)
160 {
161         uint32_t dst;
162
163         rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 4,
164                                 VIRTIO_PCI_HOST_FEATURES);
165         return dst;
166 }
167
168 static void
169 legacy_set_features(struct virtio_hw *hw, uint64_t features)
170 {
171         if ((features >> 32) != 0) {
172                 PMD_DRV_LOG(ERR,
173                         "only 32 bit features are allowed for legacy virtio!");
174                 return;
175         }
176         rte_eal_pci_ioport_write(VTPCI_IO(hw), &features, 4,
177                                  VIRTIO_PCI_GUEST_FEATURES);
178 }
179
180 static uint8_t
181 legacy_get_status(struct virtio_hw *hw)
182 {
183         uint8_t dst;
184
185         rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_STATUS);
186         return dst;
187 }
188
189 static void
190 legacy_set_status(struct virtio_hw *hw, uint8_t status)
191 {
192         rte_eal_pci_ioport_write(VTPCI_IO(hw), &status, 1, VIRTIO_PCI_STATUS);
193 }
194
195 static void
196 legacy_reset(struct virtio_hw *hw)
197 {
198         legacy_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
199 }
200
201 static uint8_t
202 legacy_get_isr(struct virtio_hw *hw)
203 {
204         uint8_t dst;
205
206         rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 1, VIRTIO_PCI_ISR);
207         return dst;
208 }
209
210 /* Enable one vector (0) for Link State Intrerrupt */
211 static uint16_t
212 legacy_set_config_irq(struct virtio_hw *hw, uint16_t vec)
213 {
214         uint16_t dst;
215
216         rte_eal_pci_ioport_write(VTPCI_IO(hw), &vec, 2,
217                                  VIRTIO_MSI_CONFIG_VECTOR);
218         rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 2,
219                                 VIRTIO_MSI_CONFIG_VECTOR);
220         return dst;
221 }
222
223 static uint16_t
224 legacy_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
225 {
226         uint16_t dst;
227
228         rte_eal_pci_ioport_write(VTPCI_IO(hw), &queue_id, 2,
229                                  VIRTIO_PCI_QUEUE_SEL);
230         rte_eal_pci_ioport_read(VTPCI_IO(hw), &dst, 2, VIRTIO_PCI_QUEUE_NUM);
231         return dst;
232 }
233
234 static int
235 legacy_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
236 {
237         uint32_t src;
238
239         if (!check_vq_phys_addr_ok(vq))
240                 return -1;
241
242         rte_eal_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
243                          VIRTIO_PCI_QUEUE_SEL);
244         src = vq->vq_ring_mem >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
245         rte_eal_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
246
247         return 0;
248 }
249
250 static void
251 legacy_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
252 {
253         uint32_t src = 0;
254
255         rte_eal_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
256                          VIRTIO_PCI_QUEUE_SEL);
257         rte_eal_pci_ioport_write(VTPCI_IO(hw), &src, 4, VIRTIO_PCI_QUEUE_PFN);
258 }
259
260 static void
261 legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
262 {
263         rte_eal_pci_ioport_write(VTPCI_IO(hw), &vq->vq_queue_index, 2,
264                          VIRTIO_PCI_QUEUE_NOTIFY);
265 }
266
267 #ifdef RTE_EXEC_ENV_LINUXAPP
268 static int
269 legacy_virtio_has_msix(const struct rte_pci_addr *loc)
270 {
271         DIR *d;
272         char dirname[PATH_MAX];
273
274         snprintf(dirname, sizeof(dirname),
275                      "%s/" PCI_PRI_FMT "/msi_irqs", pci_get_sysfs_path(),
276                      loc->domain, loc->bus, loc->devid, loc->function);
277
278         d = opendir(dirname);
279         if (d)
280                 closedir(d);
281
282         return d != NULL;
283 }
284 #else
285 static int
286 legacy_virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
287 {
288         /* nic_uio does not enable interrupts, return 0 (false). */
289         return 0;
290 }
291 #endif
292
293 static int
294 legacy_virtio_resource_init(struct rte_pci_device *pci_dev,
295                             struct virtio_hw *hw, uint32_t *dev_flags)
296 {
297         if (rte_eal_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
298                 return -1;
299
300         if (pci_dev->intr_handle.type != RTE_INTR_HANDLE_UNKNOWN)
301                 *dev_flags |= RTE_ETH_DEV_INTR_LSC;
302         else
303                 *dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
304
305         return 0;
306 }
307
308 const struct virtio_pci_ops legacy_ops = {
309         .read_dev_cfg   = legacy_read_dev_config,
310         .write_dev_cfg  = legacy_write_dev_config,
311         .reset          = legacy_reset,
312         .get_status     = legacy_get_status,
313         .set_status     = legacy_set_status,
314         .get_features   = legacy_get_features,
315         .set_features   = legacy_set_features,
316         .get_isr        = legacy_get_isr,
317         .set_config_irq = legacy_set_config_irq,
318         .get_queue_num  = legacy_get_queue_num,
319         .setup_queue    = legacy_setup_queue,
320         .del_queue      = legacy_del_queue,
321         .notify_queue   = legacy_notify_queue,
322 };
323
324
325 static inline uint8_t
326 io_read8(uint8_t *addr)
327 {
328         return *(volatile uint8_t *)addr;
329 }
330
331 static inline void
332 io_write8(uint8_t val, uint8_t *addr)
333 {
334         *(volatile uint8_t *)addr = val;
335 }
336
337 static inline uint16_t
338 io_read16(uint16_t *addr)
339 {
340         return *(volatile uint16_t *)addr;
341 }
342
343 static inline void
344 io_write16(uint16_t val, uint16_t *addr)
345 {
346         *(volatile uint16_t *)addr = val;
347 }
348
349 static inline uint32_t
350 io_read32(uint32_t *addr)
351 {
352         return *(volatile uint32_t *)addr;
353 }
354
355 static inline void
356 io_write32(uint32_t val, uint32_t *addr)
357 {
358         *(volatile uint32_t *)addr = val;
359 }
360
361 static inline void
362 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi)
363 {
364         io_write32(val & ((1ULL << 32) - 1), lo);
365         io_write32(val >> 32,                hi);
366 }
367
368 static void
369 modern_read_dev_config(struct virtio_hw *hw, size_t offset,
370                        void *dst, int length)
371 {
372         int i;
373         uint8_t *p;
374         uint8_t old_gen, new_gen;
375
376         do {
377                 old_gen = io_read8(&hw->common_cfg->config_generation);
378
379                 p = dst;
380                 for (i = 0;  i < length; i++)
381                         *p++ = io_read8((uint8_t *)hw->dev_cfg + offset + i);
382
383                 new_gen = io_read8(&hw->common_cfg->config_generation);
384         } while (old_gen != new_gen);
385 }
386
387 static void
388 modern_write_dev_config(struct virtio_hw *hw, size_t offset,
389                         const void *src, int length)
390 {
391         int i;
392         const uint8_t *p = src;
393
394         for (i = 0;  i < length; i++)
395                 io_write8(*p++, (uint8_t *)hw->dev_cfg + offset + i);
396 }
397
398 static uint64_t
399 modern_get_features(struct virtio_hw *hw)
400 {
401         uint32_t features_lo, features_hi;
402
403         io_write32(0, &hw->common_cfg->device_feature_select);
404         features_lo = io_read32(&hw->common_cfg->device_feature);
405
406         io_write32(1, &hw->common_cfg->device_feature_select);
407         features_hi = io_read32(&hw->common_cfg->device_feature);
408
409         return ((uint64_t)features_hi << 32) | features_lo;
410 }
411
412 static void
413 modern_set_features(struct virtio_hw *hw, uint64_t features)
414 {
415         io_write32(0, &hw->common_cfg->guest_feature_select);
416         io_write32(features & ((1ULL << 32) - 1),
417                 &hw->common_cfg->guest_feature);
418
419         io_write32(1, &hw->common_cfg->guest_feature_select);
420         io_write32(features >> 32,
421                 &hw->common_cfg->guest_feature);
422 }
423
424 static uint8_t
425 modern_get_status(struct virtio_hw *hw)
426 {
427         return io_read8(&hw->common_cfg->device_status);
428 }
429
430 static void
431 modern_set_status(struct virtio_hw *hw, uint8_t status)
432 {
433         io_write8(status, &hw->common_cfg->device_status);
434 }
435
436 static void
437 modern_reset(struct virtio_hw *hw)
438 {
439         modern_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
440         modern_get_status(hw);
441 }
442
443 static uint8_t
444 modern_get_isr(struct virtio_hw *hw)
445 {
446         return io_read8(hw->isr);
447 }
448
449 static uint16_t
450 modern_set_config_irq(struct virtio_hw *hw, uint16_t vec)
451 {
452         io_write16(vec, &hw->common_cfg->msix_config);
453         return io_read16(&hw->common_cfg->msix_config);
454 }
455
456 static uint16_t
457 modern_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
458 {
459         io_write16(queue_id, &hw->common_cfg->queue_select);
460         return io_read16(&hw->common_cfg->queue_size);
461 }
462
463 static int
464 modern_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
465 {
466         uint64_t desc_addr, avail_addr, used_addr;
467         uint16_t notify_off;
468
469         if (!check_vq_phys_addr_ok(vq))
470                 return -1;
471
472         desc_addr = vq->vq_ring_mem;
473         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
474         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
475                                                          ring[vq->vq_nentries]),
476                                    VIRTIO_PCI_VRING_ALIGN);
477
478         io_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
479
480         io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo,
481                                       &hw->common_cfg->queue_desc_hi);
482         io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo,
483                                        &hw->common_cfg->queue_avail_hi);
484         io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo,
485                                       &hw->common_cfg->queue_used_hi);
486
487         notify_off = io_read16(&hw->common_cfg->queue_notify_off);
488         vq->notify_addr = (void *)((uint8_t *)hw->notify_base +
489                                 notify_off * hw->notify_off_multiplier);
490
491         io_write16(1, &hw->common_cfg->queue_enable);
492
493         PMD_INIT_LOG(DEBUG, "queue %u addresses:", vq->vq_queue_index);
494         PMD_INIT_LOG(DEBUG, "\t desc_addr: %" PRIx64, desc_addr);
495         PMD_INIT_LOG(DEBUG, "\t aval_addr: %" PRIx64, avail_addr);
496         PMD_INIT_LOG(DEBUG, "\t used_addr: %" PRIx64, used_addr);
497         PMD_INIT_LOG(DEBUG, "\t notify addr: %p (notify offset: %u)",
498                 vq->notify_addr, notify_off);
499
500         return 0;
501 }
502
503 static void
504 modern_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
505 {
506         io_write16(vq->vq_queue_index, &hw->common_cfg->queue_select);
507
508         io_write64_twopart(0, &hw->common_cfg->queue_desc_lo,
509                                   &hw->common_cfg->queue_desc_hi);
510         io_write64_twopart(0, &hw->common_cfg->queue_avail_lo,
511                                   &hw->common_cfg->queue_avail_hi);
512         io_write64_twopart(0, &hw->common_cfg->queue_used_lo,
513                                   &hw->common_cfg->queue_used_hi);
514
515         io_write16(0, &hw->common_cfg->queue_enable);
516 }
517
518 static void
519 modern_notify_queue(struct virtio_hw *hw __rte_unused, struct virtqueue *vq)
520 {
521         io_write16(vq->vq_queue_index, vq->notify_addr);
522 }
523
524 const struct virtio_pci_ops modern_ops = {
525         .read_dev_cfg   = modern_read_dev_config,
526         .write_dev_cfg  = modern_write_dev_config,
527         .reset          = modern_reset,
528         .get_status     = modern_get_status,
529         .set_status     = modern_set_status,
530         .get_features   = modern_get_features,
531         .set_features   = modern_set_features,
532         .get_isr        = modern_get_isr,
533         .set_config_irq = modern_set_config_irq,
534         .get_queue_num  = modern_get_queue_num,
535         .setup_queue    = modern_setup_queue,
536         .del_queue      = modern_del_queue,
537         .notify_queue   = modern_notify_queue,
538 };
539
540
541 void
542 vtpci_read_dev_config(struct virtio_hw *hw, size_t offset,
543                       void *dst, int length)
544 {
545         VTPCI_OPS(hw)->read_dev_cfg(hw, offset, dst, length);
546 }
547
548 void
549 vtpci_write_dev_config(struct virtio_hw *hw, size_t offset,
550                        const void *src, int length)
551 {
552         VTPCI_OPS(hw)->write_dev_cfg(hw, offset, src, length);
553 }
554
555 uint64_t
556 vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features)
557 {
558         uint64_t features;
559
560         /*
561          * Limit negotiated features to what the driver, virtqueue, and
562          * host all support.
563          */
564         features = host_features & hw->guest_features;
565         VTPCI_OPS(hw)->set_features(hw, features);
566
567         return features;
568 }
569
570 void
571 vtpci_reset(struct virtio_hw *hw)
572 {
573         VTPCI_OPS(hw)->set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
574         /* flush status write */
575         VTPCI_OPS(hw)->get_status(hw);
576 }
577
578 void
579 vtpci_reinit_complete(struct virtio_hw *hw)
580 {
581         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
582 }
583
584 void
585 vtpci_set_status(struct virtio_hw *hw, uint8_t status)
586 {
587         if (status != VIRTIO_CONFIG_STATUS_RESET)
588                 status |= VTPCI_OPS(hw)->get_status(hw);
589
590         VTPCI_OPS(hw)->set_status(hw, status);
591 }
592
593 uint8_t
594 vtpci_get_status(struct virtio_hw *hw)
595 {
596         return VTPCI_OPS(hw)->get_status(hw);
597 }
598
599 uint8_t
600 vtpci_isr(struct virtio_hw *hw)
601 {
602         return VTPCI_OPS(hw)->get_isr(hw);
603 }
604
605
606 /* Enable one vector (0) for Link State Intrerrupt */
607 uint16_t
608 vtpci_irq_config(struct virtio_hw *hw, uint16_t vec)
609 {
610         return VTPCI_OPS(hw)->set_config_irq(hw, vec);
611 }
612
613 static void *
614 get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
615 {
616         uint8_t  bar    = cap->bar;
617         uint32_t length = cap->length;
618         uint32_t offset = cap->offset;
619         uint8_t *base;
620
621         if (bar > 5) {
622                 PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
623                 return NULL;
624         }
625
626         if (offset + length < offset) {
627                 PMD_INIT_LOG(ERR, "offset(%u) + length(%u) overflows",
628                         offset, length);
629                 return NULL;
630         }
631
632         if (offset + length > dev->mem_resource[bar].len) {
633                 PMD_INIT_LOG(ERR,
634                         "invalid cap: overflows bar space: %u > %" PRIu64,
635                         offset + length, dev->mem_resource[bar].len);
636                 return NULL;
637         }
638
639         base = dev->mem_resource[bar].addr;
640         if (base == NULL) {
641                 PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar);
642                 return NULL;
643         }
644
645         return base + offset;
646 }
647
648 static int
649 virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
650 {
651         uint8_t pos;
652         struct virtio_pci_cap cap;
653         int ret;
654
655         if (rte_eal_pci_map_device(dev)) {
656                 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
657                 return -1;
658         }
659
660         ret = rte_eal_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
661         if (ret < 0) {
662                 PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
663                 return -1;
664         }
665
666         while (pos) {
667                 ret = rte_eal_pci_read_config(dev, &cap, sizeof(cap), pos);
668                 if (ret < 0) {
669                         PMD_INIT_LOG(ERR,
670                                 "failed to read pci cap at pos: %x", pos);
671                         break;
672                 }
673
674                 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
675                         hw->use_msix = 1;
676
677                 if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
678                         PMD_INIT_LOG(DEBUG,
679                                 "[%2x] skipping non VNDR cap id: %02x",
680                                 pos, cap.cap_vndr);
681                         goto next;
682                 }
683
684                 PMD_INIT_LOG(DEBUG,
685                         "[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
686                         pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
687
688                 switch (cap.cfg_type) {
689                 case VIRTIO_PCI_CAP_COMMON_CFG:
690                         hw->common_cfg = get_cfg_addr(dev, &cap);
691                         break;
692                 case VIRTIO_PCI_CAP_NOTIFY_CFG:
693                         rte_eal_pci_read_config(dev, &hw->notify_off_multiplier,
694                                                 4, pos + sizeof(cap));
695                         hw->notify_base = get_cfg_addr(dev, &cap);
696                         break;
697                 case VIRTIO_PCI_CAP_DEVICE_CFG:
698                         hw->dev_cfg = get_cfg_addr(dev, &cap);
699                         break;
700                 case VIRTIO_PCI_CAP_ISR_CFG:
701                         hw->isr = get_cfg_addr(dev, &cap);
702                         break;
703                 }
704
705 next:
706                 pos = cap.cap_next;
707         }
708
709         if (hw->common_cfg == NULL || hw->notify_base == NULL ||
710             hw->dev_cfg == NULL    || hw->isr == NULL) {
711                 PMD_INIT_LOG(INFO, "no modern virtio pci device found.");
712                 return -1;
713         }
714
715         PMD_INIT_LOG(INFO, "found modern virtio pci device.");
716
717         PMD_INIT_LOG(DEBUG, "common cfg mapped at: %p", hw->common_cfg);
718         PMD_INIT_LOG(DEBUG, "device cfg mapped at: %p", hw->dev_cfg);
719         PMD_INIT_LOG(DEBUG, "isr cfg mapped at: %p", hw->isr);
720         PMD_INIT_LOG(DEBUG, "notify base: %p, notify off multiplier: %u",
721                 hw->notify_base, hw->notify_off_multiplier);
722
723         return 0;
724 }
725
726 /*
727  * Return -1:
728  *   if there is error mapping with VFIO/UIO.
729  *   if port map error when driver type is KDRV_NONE.
730  *   if whitelisted but driver type is KDRV_UNKNOWN.
731  * Return 1 if kernel driver is managing the device.
732  * Return 0 on success.
733  */
734 int
735 vtpci_init(struct rte_pci_device *dev, struct virtio_hw *hw,
736            uint32_t *dev_flags)
737 {
738         hw->dev = dev;
739
740         /*
741          * Try if we can succeed reading virtio pci caps, which exists
742          * only on modern pci device. If failed, we fallback to legacy
743          * virtio handling.
744          */
745         if (virtio_read_caps(dev, hw) == 0) {
746                 PMD_INIT_LOG(INFO, "modern virtio pci detected.");
747                 virtio_hw_internal[hw->port_id].vtpci_ops = &modern_ops;
748                 hw->modern = 1;
749                 *dev_flags |= RTE_ETH_DEV_INTR_LSC;
750                 return 0;
751         }
752
753         PMD_INIT_LOG(INFO, "trying with legacy virtio pci.");
754         if (legacy_virtio_resource_init(dev, hw, dev_flags) < 0) {
755                 if (dev->kdrv == RTE_KDRV_UNKNOWN &&
756                     (!dev->device.devargs ||
757                      dev->device.devargs->type !=
758                         RTE_DEVTYPE_WHITELISTED_PCI)) {
759                         PMD_INIT_LOG(INFO,
760                                 "skip kernel managed virtio device.");
761                         return 1;
762                 }
763                 return -1;
764         }
765
766         virtio_hw_internal[hw->port_id].vtpci_ops = &legacy_ops;
767         hw->use_msix = legacy_virtio_has_msix(&dev->addr);
768         hw->modern   = 0;
769
770         return 0;
771 }