net/virtio: introduce generic virtio header
[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 (!vtpci_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 (!vtpci_with_feature(hw, VIRTIO_F_NOTIFICATION_DATA)) {
542                 rte_write16(vq->vq_queue_index, vq->notify_addr);
543                 return;
544         }
545
546         if (vtpci_with_feature(hw, VIRTIO_F_RING_PACKED)) {
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
607 void
608 vtpci_read_dev_config(struct virtio_hw *hw, size_t offset,
609                       void *dst, int length)
610 {
611         VIRTIO_OPS(hw)->read_dev_cfg(hw, offset, dst, length);
612 }
613
614 void
615 vtpci_write_dev_config(struct virtio_hw *hw, size_t offset,
616                        const void *src, int length)
617 {
618         VIRTIO_OPS(hw)->write_dev_cfg(hw, offset, src, length);
619 }
620
621 uint64_t
622 vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features)
623 {
624         uint64_t features;
625
626         /*
627          * Limit negotiated features to what the driver, virtqueue, and
628          * host all support.
629          */
630         features = host_features & hw->guest_features;
631         VIRTIO_OPS(hw)->set_features(hw, features);
632
633         return features;
634 }
635
636 void
637 vtpci_reset(struct virtio_hw *hw)
638 {
639         VIRTIO_OPS(hw)->set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
640         /* flush status write */
641         VIRTIO_OPS(hw)->get_status(hw);
642 }
643
644 void
645 vtpci_reinit_complete(struct virtio_hw *hw)
646 {
647         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
648 }
649
650 void
651 vtpci_set_status(struct virtio_hw *hw, uint8_t status)
652 {
653         if (status != VIRTIO_CONFIG_STATUS_RESET)
654                 status |= VIRTIO_OPS(hw)->get_status(hw);
655
656         VIRTIO_OPS(hw)->set_status(hw, status);
657 }
658
659 uint8_t
660 vtpci_get_status(struct virtio_hw *hw)
661 {
662         return VIRTIO_OPS(hw)->get_status(hw);
663 }
664
665 uint8_t
666 vtpci_isr(struct virtio_hw *hw)
667 {
668         return VIRTIO_OPS(hw)->get_isr(hw);
669 }
670
671 static void *
672 get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
673 {
674         uint8_t  bar    = cap->bar;
675         uint32_t length = cap->length;
676         uint32_t offset = cap->offset;
677         uint8_t *base;
678
679         if (bar >= PCI_MAX_RESOURCE) {
680                 PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
681                 return NULL;
682         }
683
684         if (offset + length < offset) {
685                 PMD_INIT_LOG(ERR, "offset(%u) + length(%u) overflows",
686                         offset, length);
687                 return NULL;
688         }
689
690         if (offset + length > dev->mem_resource[bar].len) {
691                 PMD_INIT_LOG(ERR,
692                         "invalid cap: overflows bar space: %u > %" PRIu64,
693                         offset + length, dev->mem_resource[bar].len);
694                 return NULL;
695         }
696
697         base = dev->mem_resource[bar].addr;
698         if (base == NULL) {
699                 PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar);
700                 return NULL;
701         }
702
703         return base + offset;
704 }
705
706 static int
707 virtio_read_caps(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
708 {
709         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
710         uint8_t pos;
711         struct virtio_pci_cap cap;
712         int ret;
713
714         if (rte_pci_map_device(pci_dev)) {
715                 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
716                 return -1;
717         }
718
719         ret = rte_pci_read_config(pci_dev, &pos, 1, PCI_CAPABILITY_LIST);
720         if (ret != 1) {
721                 PMD_INIT_LOG(DEBUG,
722                              "failed to read pci capability list, ret %d", ret);
723                 return -1;
724         }
725
726         while (pos) {
727                 ret = rte_pci_read_config(pci_dev, &cap, 2, pos);
728                 if (ret != 2) {
729                         PMD_INIT_LOG(DEBUG,
730                                      "failed to read pci cap at pos: %x ret %d",
731                                      pos, ret);
732                         break;
733                 }
734
735                 if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
736                         /* Transitional devices would also have this capability,
737                          * that's why we also check if msix is enabled.
738                          * 1st byte is cap ID; 2nd byte is the position of next
739                          * cap; next two bytes are the flags.
740                          */
741                         uint16_t flags;
742
743                         ret = rte_pci_read_config(pci_dev, &flags, sizeof(flags),
744                                         pos + 2);
745                         if (ret != sizeof(flags)) {
746                                 PMD_INIT_LOG(DEBUG,
747                                              "failed to read pci cap at pos:"
748                                              " %x ret %d", pos + 2, ret);
749                                 break;
750                         }
751
752                         if (flags & PCI_MSIX_ENABLE)
753                                 hw->use_msix = VIRTIO_MSIX_ENABLED;
754                         else
755                                 hw->use_msix = VIRTIO_MSIX_DISABLED;
756                 }
757
758                 if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
759                         PMD_INIT_LOG(DEBUG,
760                                 "[%2x] skipping non VNDR cap id: %02x",
761                                 pos, cap.cap_vndr);
762                         goto next;
763                 }
764
765                 ret = rte_pci_read_config(pci_dev, &cap, sizeof(cap), pos);
766                 if (ret != sizeof(cap)) {
767                         PMD_INIT_LOG(DEBUG,
768                                      "failed to read pci cap at pos: %x ret %d",
769                                      pos, ret);
770                         break;
771                 }
772
773                 PMD_INIT_LOG(DEBUG,
774                         "[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
775                         pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
776
777                 switch (cap.cfg_type) {
778                 case VIRTIO_PCI_CAP_COMMON_CFG:
779                         dev->common_cfg = get_cfg_addr(pci_dev, &cap);
780                         break;
781                 case VIRTIO_PCI_CAP_NOTIFY_CFG:
782                         ret = rte_pci_read_config(pci_dev,
783                                         &dev->notify_off_multiplier,
784                                         4, pos + sizeof(cap));
785                         if (ret != 4)
786                                 PMD_INIT_LOG(DEBUG,
787                                         "failed to read notify_off_multiplier, ret %d",
788                                         ret);
789                         else
790                                 dev->notify_base = get_cfg_addr(pci_dev, &cap);
791                         break;
792                 case VIRTIO_PCI_CAP_DEVICE_CFG:
793                         dev->dev_cfg = get_cfg_addr(pci_dev, &cap);
794                         break;
795                 case VIRTIO_PCI_CAP_ISR_CFG:
796                         dev->isr = get_cfg_addr(pci_dev, &cap);
797                         break;
798                 }
799
800 next:
801                 pos = cap.cap_next;
802         }
803
804         if (dev->common_cfg == NULL || dev->notify_base == NULL ||
805             dev->dev_cfg == NULL    || dev->isr == NULL) {
806                 PMD_INIT_LOG(INFO, "no modern virtio pci device found.");
807                 return -1;
808         }
809
810         PMD_INIT_LOG(INFO, "found modern virtio pci device.");
811
812         PMD_INIT_LOG(DEBUG, "common cfg mapped at: %p", dev->common_cfg);
813         PMD_INIT_LOG(DEBUG, "device cfg mapped at: %p", dev->dev_cfg);
814         PMD_INIT_LOG(DEBUG, "isr cfg mapped at: %p", dev->isr);
815         PMD_INIT_LOG(DEBUG, "notify base: %p, notify off multiplier: %u",
816                 dev->notify_base, dev->notify_off_multiplier);
817
818         return 0;
819 }
820
821 /*
822  * Return -1:
823  *   if there is error mapping with VFIO/UIO.
824  *   if port map error when driver type is KDRV_NONE.
825  *   if marked as allowed but driver type is KDRV_UNKNOWN.
826  * Return 1 if kernel driver is managing the device.
827  * Return 0 on success.
828  */
829 int
830 vtpci_init(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev)
831 {
832         struct virtio_hw *hw = &dev->hw;
833
834         RTE_BUILD_BUG_ON(offsetof(struct virtio_pci_dev, hw) != 0);
835
836         dev->pci_dev = pci_dev;
837
838         /*
839          * Try if we can succeed reading virtio pci caps, which exists
840          * only on modern pci device. If failed, we fallback to legacy
841          * virtio handling.
842          */
843         if (virtio_read_caps(pci_dev, hw) == 0) {
844                 PMD_INIT_LOG(INFO, "modern virtio pci detected.");
845                 VIRTIO_OPS(hw) = &modern_ops;
846                 dev->modern = true;
847                 goto msix_detect;
848         }
849
850         PMD_INIT_LOG(INFO, "trying with legacy virtio pci.");
851         if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0) {
852                 rte_pci_unmap_device(pci_dev);
853                 if (pci_dev->kdrv == RTE_PCI_KDRV_UNKNOWN &&
854                     (!pci_dev->device.devargs ||
855                      pci_dev->device.devargs->bus !=
856                      rte_bus_find_by_name("pci"))) {
857                         PMD_INIT_LOG(INFO,
858                                 "skip kernel managed virtio device.");
859                         return 1;
860                 }
861                 return -1;
862         }
863
864         VIRTIO_OPS(hw) = &legacy_ops;
865         dev->modern = false;
866
867 msix_detect:
868         VIRTIO_OPS(hw)->intr_detect(hw);
869
870         return 0;
871 }
872
873 void vtpci_legacy_ioport_unmap(struct virtio_hw *hw)
874 {
875         rte_pci_ioport_unmap(VTPCI_IO(hw));
876 }
877
878 int vtpci_legacy_ioport_map(struct virtio_hw *hw)
879 {
880         struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
881
882         return rte_pci_ioport_map(dev->pci_dev, 0, VTPCI_IO(hw));
883 }