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