app/procinfo: add --show-mempool
[dpdk.git] / drivers / net / ifc / ifcvf_vdpa.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <pthread.h>
7 #include <fcntl.h>
8 #include <sys/ioctl.h>
9 #include <sys/epoll.h>
10 #include <linux/virtio_net.h>
11 #include <stdbool.h>
12
13 #include <rte_malloc.h>
14 #include <rte_memory.h>
15 #include <rte_bus_pci.h>
16 #include <rte_vhost.h>
17 #include <rte_vdpa.h>
18 #include <rte_vfio.h>
19 #include <rte_spinlock.h>
20 #include <rte_log.h>
21 #include <rte_kvargs.h>
22 #include <rte_devargs.h>
23
24 #include "base/ifcvf.h"
25
26 #define DRV_LOG(level, fmt, args...) \
27         rte_log(RTE_LOG_ ## level, ifcvf_vdpa_logtype, \
28                 "IFCVF %s(): " fmt "\n", __func__, ##args)
29
30 #ifndef PAGE_SIZE
31 #define PAGE_SIZE 4096
32 #endif
33
34 #define IFCVF_USED_RING_LEN(size) \
35         ((size) * sizeof(struct vring_used_elem) + sizeof(uint16_t) * 3)
36
37 #define IFCVF_VDPA_MODE         "vdpa"
38 #define IFCVF_SW_FALLBACK_LM    "sw-live-migration"
39
40 static const char * const ifcvf_valid_arguments[] = {
41         IFCVF_VDPA_MODE,
42         IFCVF_SW_FALLBACK_LM,
43         NULL
44 };
45
46 static int ifcvf_vdpa_logtype;
47
48 struct ifcvf_internal {
49         struct rte_vdpa_dev_addr dev_addr;
50         struct rte_pci_device *pdev;
51         struct ifcvf_hw hw;
52         int vfio_container_fd;
53         int vfio_group_fd;
54         int vfio_dev_fd;
55         pthread_t tid;  /* thread for notify relay */
56         int epfd;
57         int vid;
58         int did;
59         uint16_t max_queues;
60         uint64_t features;
61         rte_atomic32_t started;
62         rte_atomic32_t dev_attached;
63         rte_atomic32_t running;
64         rte_spinlock_t lock;
65         bool sw_lm;
66         bool sw_fallback_running;
67         /* mediated vring for sw fallback */
68         struct vring m_vring[IFCVF_MAX_QUEUES * 2];
69 };
70
71 struct internal_list {
72         TAILQ_ENTRY(internal_list) next;
73         struct ifcvf_internal *internal;
74 };
75
76 TAILQ_HEAD(internal_list_head, internal_list);
77 static struct internal_list_head internal_list =
78         TAILQ_HEAD_INITIALIZER(internal_list);
79
80 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
81
82 static struct internal_list *
83 find_internal_resource_by_did(int did)
84 {
85         int found = 0;
86         struct internal_list *list;
87
88         pthread_mutex_lock(&internal_list_lock);
89
90         TAILQ_FOREACH(list, &internal_list, next) {
91                 if (did == list->internal->did) {
92                         found = 1;
93                         break;
94                 }
95         }
96
97         pthread_mutex_unlock(&internal_list_lock);
98
99         if (!found)
100                 return NULL;
101
102         return list;
103 }
104
105 static struct internal_list *
106 find_internal_resource_by_dev(struct rte_pci_device *pdev)
107 {
108         int found = 0;
109         struct internal_list *list;
110
111         pthread_mutex_lock(&internal_list_lock);
112
113         TAILQ_FOREACH(list, &internal_list, next) {
114                 if (pdev == list->internal->pdev) {
115                         found = 1;
116                         break;
117                 }
118         }
119
120         pthread_mutex_unlock(&internal_list_lock);
121
122         if (!found)
123                 return NULL;
124
125         return list;
126 }
127
128 static int
129 ifcvf_vfio_setup(struct ifcvf_internal *internal)
130 {
131         struct rte_pci_device *dev = internal->pdev;
132         char devname[RTE_DEV_NAME_MAX_LEN] = {0};
133         int iommu_group_num;
134         int i;
135
136         internal->vfio_dev_fd = -1;
137         internal->vfio_group_fd = -1;
138         internal->vfio_container_fd = -1;
139
140         rte_pci_device_name(&dev->addr, devname, RTE_DEV_NAME_MAX_LEN);
141         rte_vfio_get_group_num(rte_pci_get_sysfs_path(), devname,
142                         &iommu_group_num);
143
144         internal->vfio_container_fd = rte_vfio_container_create();
145         if (internal->vfio_container_fd < 0)
146                 return -1;
147
148         internal->vfio_group_fd = rte_vfio_container_group_bind(
149                         internal->vfio_container_fd, iommu_group_num);
150         if (internal->vfio_group_fd < 0)
151                 goto err;
152
153         if (rte_pci_map_device(dev))
154                 goto err;
155
156         internal->vfio_dev_fd = dev->intr_handle.vfio_dev_fd;
157
158         for (i = 0; i < RTE_MIN(PCI_MAX_RESOURCE, IFCVF_PCI_MAX_RESOURCE);
159                         i++) {
160                 internal->hw.mem_resource[i].addr =
161                         internal->pdev->mem_resource[i].addr;
162                 internal->hw.mem_resource[i].phys_addr =
163                         internal->pdev->mem_resource[i].phys_addr;
164                 internal->hw.mem_resource[i].len =
165                         internal->pdev->mem_resource[i].len;
166         }
167
168         return 0;
169
170 err:
171         rte_vfio_container_destroy(internal->vfio_container_fd);
172         return -1;
173 }
174
175 static int
176 ifcvf_dma_map(struct ifcvf_internal *internal, int do_map)
177 {
178         uint32_t i;
179         int ret;
180         struct rte_vhost_memory *mem = NULL;
181         int vfio_container_fd;
182
183         ret = rte_vhost_get_mem_table(internal->vid, &mem);
184         if (ret < 0) {
185                 DRV_LOG(ERR, "failed to get VM memory layout.");
186                 goto exit;
187         }
188
189         vfio_container_fd = internal->vfio_container_fd;
190
191         for (i = 0; i < mem->nregions; i++) {
192                 struct rte_vhost_mem_region *reg;
193
194                 reg = &mem->regions[i];
195                 DRV_LOG(INFO, "%s, region %u: HVA 0x%" PRIx64 ", "
196                         "GPA 0x%" PRIx64 ", size 0x%" PRIx64 ".",
197                         do_map ? "DMA map" : "DMA unmap", i,
198                         reg->host_user_addr, reg->guest_phys_addr, reg->size);
199
200                 if (do_map) {
201                         ret = rte_vfio_container_dma_map(vfio_container_fd,
202                                 reg->host_user_addr, reg->guest_phys_addr,
203                                 reg->size);
204                         if (ret < 0) {
205                                 DRV_LOG(ERR, "DMA map failed.");
206                                 goto exit;
207                         }
208                 } else {
209                         ret = rte_vfio_container_dma_unmap(vfio_container_fd,
210                                 reg->host_user_addr, reg->guest_phys_addr,
211                                 reg->size);
212                         if (ret < 0) {
213                                 DRV_LOG(ERR, "DMA unmap failed.");
214                                 goto exit;
215                         }
216                 }
217         }
218
219 exit:
220         if (mem)
221                 free(mem);
222         return ret;
223 }
224
225 static uint64_t
226 hva_to_gpa(int vid, uint64_t hva)
227 {
228         struct rte_vhost_memory *mem = NULL;
229         struct rte_vhost_mem_region *reg;
230         uint32_t i;
231         uint64_t gpa = 0;
232
233         if (rte_vhost_get_mem_table(vid, &mem) < 0)
234                 goto exit;
235
236         for (i = 0; i < mem->nregions; i++) {
237                 reg = &mem->regions[i];
238
239                 if (hva >= reg->host_user_addr &&
240                                 hva < reg->host_user_addr + reg->size) {
241                         gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
242                         break;
243                 }
244         }
245
246 exit:
247         if (mem)
248                 free(mem);
249         return gpa;
250 }
251
252 static int
253 vdpa_ifcvf_start(struct ifcvf_internal *internal)
254 {
255         struct ifcvf_hw *hw = &internal->hw;
256         int i, nr_vring;
257         int vid;
258         struct rte_vhost_vring vq;
259         uint64_t gpa;
260
261         vid = internal->vid;
262         nr_vring = rte_vhost_get_vring_num(vid);
263         rte_vhost_get_negotiated_features(vid, &hw->req_features);
264
265         for (i = 0; i < nr_vring; i++) {
266                 rte_vhost_get_vhost_vring(vid, i, &vq);
267                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
268                 if (gpa == 0) {
269                         DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
270                         return -1;
271                 }
272                 hw->vring[i].desc = gpa;
273
274                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
275                 if (gpa == 0) {
276                         DRV_LOG(ERR, "Fail to get GPA for available ring.");
277                         return -1;
278                 }
279                 hw->vring[i].avail = gpa;
280
281                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
282                 if (gpa == 0) {
283                         DRV_LOG(ERR, "Fail to get GPA for used ring.");
284                         return -1;
285                 }
286                 hw->vring[i].used = gpa;
287
288                 hw->vring[i].size = vq.size;
289                 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
290                                 &hw->vring[i].last_used_idx);
291         }
292         hw->nr_vring = i;
293
294         return ifcvf_start_hw(&internal->hw);
295 }
296
297 static void
298 vdpa_ifcvf_stop(struct ifcvf_internal *internal)
299 {
300         struct ifcvf_hw *hw = &internal->hw;
301         uint32_t i;
302         int vid;
303         uint64_t features;
304         uint64_t log_base, log_size;
305         uint64_t len;
306
307         vid = internal->vid;
308         ifcvf_stop_hw(hw);
309
310         for (i = 0; i < hw->nr_vring; i++)
311                 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
312                                 hw->vring[i].last_used_idx);
313
314         if (internal->sw_lm)
315                 return;
316
317         rte_vhost_get_negotiated_features(vid, &features);
318         if (RTE_VHOST_NEED_LOG(features)) {
319                 ifcvf_disable_logging(hw);
320                 rte_vhost_get_log_base(internal->vid, &log_base, &log_size);
321                 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
322                                 log_base, IFCVF_LOG_BASE, log_size);
323                 /*
324                  * IFCVF marks dirty memory pages for only packet buffer,
325                  * SW helps to mark the used ring as dirty after device stops.
326                  */
327                 for (i = 0; i < hw->nr_vring; i++) {
328                         len = IFCVF_USED_RING_LEN(hw->vring[i].size);
329                         rte_vhost_log_used_vring(vid, i, 0, len);
330                 }
331         }
332 }
333
334 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
335                 sizeof(int) * (IFCVF_MAX_QUEUES * 2 + 1))
336 static int
337 vdpa_enable_vfio_intr(struct ifcvf_internal *internal)
338 {
339         int ret;
340         uint32_t i, nr_vring;
341         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
342         struct vfio_irq_set *irq_set;
343         int *fd_ptr;
344         struct rte_vhost_vring vring;
345
346         nr_vring = rte_vhost_get_vring_num(internal->vid);
347
348         irq_set = (struct vfio_irq_set *)irq_set_buf;
349         irq_set->argsz = sizeof(irq_set_buf);
350         irq_set->count = nr_vring + 1;
351         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
352                          VFIO_IRQ_SET_ACTION_TRIGGER;
353         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
354         irq_set->start = 0;
355         fd_ptr = (int *)&irq_set->data;
356         fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = internal->pdev->intr_handle.fd;
357
358         for (i = 0; i < nr_vring; i++) {
359                 rte_vhost_get_vhost_vring(internal->vid, i, &vring);
360                 fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = vring.callfd;
361         }
362
363         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
364         if (ret) {
365                 DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s",
366                                 strerror(errno));
367                 return -1;
368         }
369
370         return 0;
371 }
372
373 static int
374 vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
375 {
376         int ret;
377         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
378         struct vfio_irq_set *irq_set;
379
380         irq_set = (struct vfio_irq_set *)irq_set_buf;
381         irq_set->argsz = sizeof(irq_set_buf);
382         irq_set->count = 0;
383         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
384         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
385         irq_set->start = 0;
386
387         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
388         if (ret) {
389                 DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s",
390                                 strerror(errno));
391                 return -1;
392         }
393
394         return 0;
395 }
396
397 static void *
398 notify_relay(void *arg)
399 {
400         int i, kickfd, epfd, nfds = 0;
401         uint32_t qid, q_num;
402         struct epoll_event events[IFCVF_MAX_QUEUES * 2];
403         struct epoll_event ev;
404         uint64_t buf;
405         int nbytes;
406         struct rte_vhost_vring vring;
407         struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
408         struct ifcvf_hw *hw = &internal->hw;
409
410         q_num = rte_vhost_get_vring_num(internal->vid);
411
412         epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
413         if (epfd < 0) {
414                 DRV_LOG(ERR, "failed to create epoll instance.");
415                 return NULL;
416         }
417         internal->epfd = epfd;
418
419         for (qid = 0; qid < q_num; qid++) {
420                 ev.events = EPOLLIN | EPOLLPRI;
421                 rte_vhost_get_vhost_vring(internal->vid, qid, &vring);
422                 ev.data.u64 = qid | (uint64_t)vring.kickfd << 32;
423                 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
424                         DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
425                         return NULL;
426                 }
427         }
428
429         for (;;) {
430                 nfds = epoll_wait(epfd, events, q_num, -1);
431                 if (nfds < 0) {
432                         if (errno == EINTR)
433                                 continue;
434                         DRV_LOG(ERR, "epoll_wait return fail\n");
435                         return NULL;
436                 }
437
438                 for (i = 0; i < nfds; i++) {
439                         qid = events[i].data.u32;
440                         kickfd = (uint32_t)(events[i].data.u64 >> 32);
441                         do {
442                                 nbytes = read(kickfd, &buf, 8);
443                                 if (nbytes < 0) {
444                                         if (errno == EINTR ||
445                                             errno == EWOULDBLOCK ||
446                                             errno == EAGAIN)
447                                                 continue;
448                                         DRV_LOG(INFO, "Error reading "
449                                                 "kickfd: %s",
450                                                 strerror(errno));
451                                 }
452                                 break;
453                         } while (1);
454
455                         ifcvf_notify_queue(hw, qid);
456                 }
457         }
458
459         return NULL;
460 }
461
462 static int
463 setup_notify_relay(struct ifcvf_internal *internal)
464 {
465         int ret;
466
467         ret = pthread_create(&internal->tid, NULL, notify_relay,
468                         (void *)internal);
469         if (ret) {
470                 DRV_LOG(ERR, "failed to create notify relay pthread.");
471                 return -1;
472         }
473         return 0;
474 }
475
476 static int
477 unset_notify_relay(struct ifcvf_internal *internal)
478 {
479         void *status;
480
481         if (internal->tid) {
482                 pthread_cancel(internal->tid);
483                 pthread_join(internal->tid, &status);
484         }
485         internal->tid = 0;
486
487         if (internal->epfd >= 0)
488                 close(internal->epfd);
489         internal->epfd = -1;
490
491         return 0;
492 }
493
494 static int
495 update_datapath(struct ifcvf_internal *internal)
496 {
497         int ret;
498
499         rte_spinlock_lock(&internal->lock);
500
501         if (!rte_atomic32_read(&internal->running) &&
502             (rte_atomic32_read(&internal->started) &&
503              rte_atomic32_read(&internal->dev_attached))) {
504                 ret = ifcvf_dma_map(internal, 1);
505                 if (ret)
506                         goto err;
507
508                 ret = vdpa_enable_vfio_intr(internal);
509                 if (ret)
510                         goto err;
511
512                 ret = vdpa_ifcvf_start(internal);
513                 if (ret)
514                         goto err;
515
516                 ret = setup_notify_relay(internal);
517                 if (ret)
518                         goto err;
519
520                 rte_atomic32_set(&internal->running, 1);
521         } else if (rte_atomic32_read(&internal->running) &&
522                    (!rte_atomic32_read(&internal->started) ||
523                     !rte_atomic32_read(&internal->dev_attached))) {
524                 ret = unset_notify_relay(internal);
525                 if (ret)
526                         goto err;
527
528                 vdpa_ifcvf_stop(internal);
529
530                 ret = vdpa_disable_vfio_intr(internal);
531                 if (ret)
532                         goto err;
533
534                 ret = ifcvf_dma_map(internal, 0);
535                 if (ret)
536                         goto err;
537
538                 rte_atomic32_set(&internal->running, 0);
539         }
540
541         rte_spinlock_unlock(&internal->lock);
542         return 0;
543 err:
544         rte_spinlock_unlock(&internal->lock);
545         return ret;
546 }
547
548 static int
549 m_ifcvf_start(struct ifcvf_internal *internal)
550 {
551         struct ifcvf_hw *hw = &internal->hw;
552         uint32_t i, nr_vring;
553         int vid, ret;
554         struct rte_vhost_vring vq;
555         void *vring_buf;
556         uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
557         uint64_t size;
558         uint64_t gpa;
559
560         vid = internal->vid;
561         nr_vring = rte_vhost_get_vring_num(vid);
562         rte_vhost_get_negotiated_features(vid, &hw->req_features);
563
564         for (i = 0; i < nr_vring; i++) {
565                 rte_vhost_get_vhost_vring(vid, i, &vq);
566
567                 size = RTE_ALIGN_CEIL(vring_size(vq.size, PAGE_SIZE),
568                                 PAGE_SIZE);
569                 vring_buf = rte_zmalloc("ifcvf", size, PAGE_SIZE);
570                 vring_init(&internal->m_vring[i], vq.size, vring_buf,
571                                 PAGE_SIZE);
572
573                 ret = rte_vfio_container_dma_map(internal->vfio_container_fd,
574                         (uint64_t)(uintptr_t)vring_buf, m_vring_iova, size);
575                 if (ret < 0) {
576                         DRV_LOG(ERR, "mediated vring DMA map failed.");
577                         goto error;
578                 }
579
580                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
581                 if (gpa == 0) {
582                         DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
583                         return -1;
584                 }
585                 hw->vring[i].desc = gpa;
586
587                 hw->vring[i].avail = m_vring_iova +
588                         (char *)internal->m_vring[i].avail -
589                         (char *)internal->m_vring[i].desc;
590
591                 hw->vring[i].used = m_vring_iova +
592                         (char *)internal->m_vring[i].used -
593                         (char *)internal->m_vring[i].desc;
594
595                 hw->vring[i].size = vq.size;
596
597                 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
598                                 &hw->vring[i].last_used_idx);
599
600                 m_vring_iova += size;
601         }
602         hw->nr_vring = nr_vring;
603
604         return ifcvf_start_hw(&internal->hw);
605
606 error:
607         for (i = 0; i < nr_vring; i++)
608                 if (internal->m_vring[i].desc)
609                         rte_free(internal->m_vring[i].desc);
610
611         return -1;
612 }
613
614 static int
615 m_ifcvf_stop(struct ifcvf_internal *internal)
616 {
617         int vid;
618         uint32_t i;
619         struct rte_vhost_vring vq;
620         struct ifcvf_hw *hw = &internal->hw;
621         uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
622         uint64_t size, len;
623
624         vid = internal->vid;
625         ifcvf_stop_hw(hw);
626
627         for (i = 0; i < hw->nr_vring; i++) {
628                 rte_vhost_get_vhost_vring(vid, i, &vq);
629                 len = IFCVF_USED_RING_LEN(vq.size);
630                 rte_vhost_log_used_vring(vid, i, 0, len);
631
632                 size = RTE_ALIGN_CEIL(vring_size(vq.size, PAGE_SIZE),
633                                 PAGE_SIZE);
634                 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
635                         (uint64_t)(uintptr_t)internal->m_vring[i].desc,
636                         m_vring_iova, size);
637
638                 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
639                                 hw->vring[i].last_used_idx);
640                 rte_free(internal->m_vring[i].desc);
641                 m_vring_iova += size;
642         }
643
644         return 0;
645 }
646
647 static int
648 m_enable_vfio_intr(struct ifcvf_internal *internal)
649 {
650         uint32_t nr_vring;
651         struct rte_intr_handle *intr_handle = &internal->pdev->intr_handle;
652         int ret;
653
654         nr_vring = rte_vhost_get_vring_num(internal->vid);
655
656         ret = rte_intr_efd_enable(intr_handle, nr_vring);
657         if (ret)
658                 return -1;
659
660         ret = rte_intr_enable(intr_handle);
661         if (ret)
662                 return -1;
663
664         return 0;
665 }
666
667 static void
668 m_disable_vfio_intr(struct ifcvf_internal *internal)
669 {
670         struct rte_intr_handle *intr_handle = &internal->pdev->intr_handle;
671
672         rte_intr_efd_disable(intr_handle);
673         rte_intr_disable(intr_handle);
674 }
675
676 static void
677 update_avail_ring(struct ifcvf_internal *internal, uint16_t qid)
678 {
679         rte_vdpa_relay_vring_avail(internal->vid, qid, &internal->m_vring[qid]);
680         ifcvf_notify_queue(&internal->hw, qid);
681 }
682
683 static void
684 update_used_ring(struct ifcvf_internal *internal, uint16_t qid)
685 {
686         rte_vdpa_relay_vring_used(internal->vid, qid, &internal->m_vring[qid]);
687         rte_vhost_vring_call(internal->vid, qid);
688 }
689
690 static void *
691 vring_relay(void *arg)
692 {
693         int i, vid, epfd, fd, nfds;
694         struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
695         struct rte_vhost_vring vring;
696         struct rte_intr_handle *intr_handle;
697         uint16_t qid, q_num;
698         struct epoll_event events[IFCVF_MAX_QUEUES * 4];
699         struct epoll_event ev;
700         int nbytes;
701         uint64_t buf;
702
703         vid = internal->vid;
704         q_num = rte_vhost_get_vring_num(vid);
705         /* prepare the mediated vring */
706         for (qid = 0; qid < q_num; qid++) {
707                 rte_vhost_get_vring_base(vid, qid,
708                                 &internal->m_vring[qid].avail->idx,
709                                 &internal->m_vring[qid].used->idx);
710                 rte_vdpa_relay_vring_avail(vid, qid, &internal->m_vring[qid]);
711         }
712
713         /* add notify fd and interrupt fd to epoll */
714         epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
715         if (epfd < 0) {
716                 DRV_LOG(ERR, "failed to create epoll instance.");
717                 return NULL;
718         }
719         internal->epfd = epfd;
720
721         for (qid = 0; qid < q_num; qid++) {
722                 ev.events = EPOLLIN | EPOLLPRI;
723                 rte_vhost_get_vhost_vring(vid, qid, &vring);
724                 ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
725                 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
726                         DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
727                         return NULL;
728                 }
729         }
730
731         intr_handle = &internal->pdev->intr_handle;
732         for (qid = 0; qid < q_num; qid++) {
733                 ev.events = EPOLLIN | EPOLLPRI;
734                 ev.data.u64 = 1 | qid << 1 |
735                         (uint64_t)intr_handle->efds[qid] << 32;
736                 if (epoll_ctl(epfd, EPOLL_CTL_ADD, intr_handle->efds[qid], &ev)
737                                 < 0) {
738                         DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
739                         return NULL;
740                 }
741         }
742
743         /* start relay with a first kick */
744         for (qid = 0; qid < q_num; qid++)
745                 ifcvf_notify_queue(&internal->hw, qid);
746
747         /* listen to the events and react accordingly */
748         for (;;) {
749                 nfds = epoll_wait(epfd, events, q_num * 2, -1);
750                 if (nfds < 0) {
751                         if (errno == EINTR)
752                                 continue;
753                         DRV_LOG(ERR, "epoll_wait return fail\n");
754                         return NULL;
755                 }
756
757                 for (i = 0; i < nfds; i++) {
758                         fd = (uint32_t)(events[i].data.u64 >> 32);
759                         do {
760                                 nbytes = read(fd, &buf, 8);
761                                 if (nbytes < 0) {
762                                         if (errno == EINTR ||
763                                             errno == EWOULDBLOCK ||
764                                             errno == EAGAIN)
765                                                 continue;
766                                         DRV_LOG(INFO, "Error reading "
767                                                 "kickfd: %s",
768                                                 strerror(errno));
769                                 }
770                                 break;
771                         } while (1);
772
773                         qid = events[i].data.u32 >> 1;
774
775                         if (events[i].data.u32 & 1)
776                                 update_used_ring(internal, qid);
777                         else
778                                 update_avail_ring(internal, qid);
779                 }
780         }
781
782         return NULL;
783 }
784
785 static int
786 setup_vring_relay(struct ifcvf_internal *internal)
787 {
788         int ret;
789
790         ret = pthread_create(&internal->tid, NULL, vring_relay,
791                         (void *)internal);
792         if (ret) {
793                 DRV_LOG(ERR, "failed to create ring relay pthread.");
794                 return -1;
795         }
796         return 0;
797 }
798
799 static int
800 unset_vring_relay(struct ifcvf_internal *internal)
801 {
802         void *status;
803
804         if (internal->tid) {
805                 pthread_cancel(internal->tid);
806                 pthread_join(internal->tid, &status);
807         }
808         internal->tid = 0;
809
810         if (internal->epfd >= 0)
811                 close(internal->epfd);
812         internal->epfd = -1;
813
814         return 0;
815 }
816
817 static int
818 ifcvf_sw_fallback_switchover(struct ifcvf_internal *internal)
819 {
820         int ret;
821
822         /* stop the direct IO data path */
823         unset_notify_relay(internal);
824         vdpa_ifcvf_stop(internal);
825         vdpa_disable_vfio_intr(internal);
826
827         ret = rte_vhost_host_notifier_ctrl(internal->vid, false);
828         if (ret && ret != -ENOTSUP)
829                 goto error;
830
831         /* set up interrupt for interrupt relay */
832         ret = m_enable_vfio_intr(internal);
833         if (ret)
834                 goto unmap;
835
836         /* config the VF */
837         ret = m_ifcvf_start(internal);
838         if (ret)
839                 goto unset_intr;
840
841         /* set up vring relay thread */
842         ret = setup_vring_relay(internal);
843         if (ret)
844                 goto stop_vf;
845
846         internal->sw_fallback_running = true;
847
848         return 0;
849
850 stop_vf:
851         m_ifcvf_stop(internal);
852 unset_intr:
853         m_disable_vfio_intr(internal);
854 unmap:
855         ifcvf_dma_map(internal, 0);
856 error:
857         return -1;
858 }
859
860 static int
861 ifcvf_dev_config(int vid)
862 {
863         int did;
864         struct internal_list *list;
865         struct ifcvf_internal *internal;
866
867         did = rte_vhost_get_vdpa_device_id(vid);
868         list = find_internal_resource_by_did(did);
869         if (list == NULL) {
870                 DRV_LOG(ERR, "Invalid device id: %d", did);
871                 return -1;
872         }
873
874         internal = list->internal;
875         internal->vid = vid;
876         rte_atomic32_set(&internal->dev_attached, 1);
877         update_datapath(internal);
878
879         if (rte_vhost_host_notifier_ctrl(vid, true) != 0)
880                 DRV_LOG(NOTICE, "vDPA (%d): software relay is used.", did);
881
882         return 0;
883 }
884
885 static int
886 ifcvf_dev_close(int vid)
887 {
888         int did;
889         struct internal_list *list;
890         struct ifcvf_internal *internal;
891
892         did = rte_vhost_get_vdpa_device_id(vid);
893         list = find_internal_resource_by_did(did);
894         if (list == NULL) {
895                 DRV_LOG(ERR, "Invalid device id: %d", did);
896                 return -1;
897         }
898
899         internal = list->internal;
900
901         if (internal->sw_fallback_running) {
902                 /* unset ring relay */
903                 unset_vring_relay(internal);
904
905                 /* reset VF */
906                 m_ifcvf_stop(internal);
907
908                 /* remove interrupt setting */
909                 m_disable_vfio_intr(internal);
910
911                 /* unset DMA map for guest memory */
912                 ifcvf_dma_map(internal, 0);
913
914                 internal->sw_fallback_running = false;
915         } else {
916                 rte_atomic32_set(&internal->dev_attached, 0);
917                 update_datapath(internal);
918         }
919
920         return 0;
921 }
922
923 static int
924 ifcvf_set_features(int vid)
925 {
926         uint64_t features;
927         int did;
928         struct internal_list *list;
929         struct ifcvf_internal *internal;
930         uint64_t log_base, log_size;
931
932         did = rte_vhost_get_vdpa_device_id(vid);
933         list = find_internal_resource_by_did(did);
934         if (list == NULL) {
935                 DRV_LOG(ERR, "Invalid device id: %d", did);
936                 return -1;
937         }
938
939         internal = list->internal;
940         rte_vhost_get_negotiated_features(vid, &features);
941
942         if (!RTE_VHOST_NEED_LOG(features))
943                 return 0;
944
945         if (internal->sw_lm) {
946                 ifcvf_sw_fallback_switchover(internal);
947         } else {
948                 rte_vhost_get_log_base(vid, &log_base, &log_size);
949                 rte_vfio_container_dma_map(internal->vfio_container_fd,
950                                 log_base, IFCVF_LOG_BASE, log_size);
951                 ifcvf_enable_logging(&internal->hw, IFCVF_LOG_BASE, log_size);
952         }
953
954         return 0;
955 }
956
957 static int
958 ifcvf_get_vfio_group_fd(int vid)
959 {
960         int did;
961         struct internal_list *list;
962
963         did = rte_vhost_get_vdpa_device_id(vid);
964         list = find_internal_resource_by_did(did);
965         if (list == NULL) {
966                 DRV_LOG(ERR, "Invalid device id: %d", did);
967                 return -1;
968         }
969
970         return list->internal->vfio_group_fd;
971 }
972
973 static int
974 ifcvf_get_vfio_device_fd(int vid)
975 {
976         int did;
977         struct internal_list *list;
978
979         did = rte_vhost_get_vdpa_device_id(vid);
980         list = find_internal_resource_by_did(did);
981         if (list == NULL) {
982                 DRV_LOG(ERR, "Invalid device id: %d", did);
983                 return -1;
984         }
985
986         return list->internal->vfio_dev_fd;
987 }
988
989 static int
990 ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
991 {
992         int did;
993         struct internal_list *list;
994         struct ifcvf_internal *internal;
995         struct vfio_region_info reg = { .argsz = sizeof(reg) };
996         int ret;
997
998         did = rte_vhost_get_vdpa_device_id(vid);
999         list = find_internal_resource_by_did(did);
1000         if (list == NULL) {
1001                 DRV_LOG(ERR, "Invalid device id: %d", did);
1002                 return -1;
1003         }
1004
1005         internal = list->internal;
1006
1007         reg.index = ifcvf_get_notify_region(&internal->hw);
1008         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
1009         if (ret) {
1010                 DRV_LOG(ERR, "Get not get device region info: %s",
1011                                 strerror(errno));
1012                 return -1;
1013         }
1014
1015         *offset = ifcvf_get_queue_notify_off(&internal->hw, qid) + reg.offset;
1016         *size = 0x1000;
1017
1018         return 0;
1019 }
1020
1021 static int
1022 ifcvf_get_queue_num(int did, uint32_t *queue_num)
1023 {
1024         struct internal_list *list;
1025
1026         list = find_internal_resource_by_did(did);
1027         if (list == NULL) {
1028                 DRV_LOG(ERR, "Invalid device id: %d", did);
1029                 return -1;
1030         }
1031
1032         *queue_num = list->internal->max_queues;
1033
1034         return 0;
1035 }
1036
1037 static int
1038 ifcvf_get_vdpa_features(int did, uint64_t *features)
1039 {
1040         struct internal_list *list;
1041
1042         list = find_internal_resource_by_did(did);
1043         if (list == NULL) {
1044                 DRV_LOG(ERR, "Invalid device id: %d", did);
1045                 return -1;
1046         }
1047
1048         *features = list->internal->features;
1049
1050         return 0;
1051 }
1052
1053 #define VDPA_SUPPORTED_PROTOCOL_FEATURES \
1054                 (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
1055                  1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ | \
1056                  1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD | \
1057                  1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER | \
1058                  1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1059 static int
1060 ifcvf_get_protocol_features(int did __rte_unused, uint64_t *features)
1061 {
1062         *features = VDPA_SUPPORTED_PROTOCOL_FEATURES;
1063         return 0;
1064 }
1065
1066 static struct rte_vdpa_dev_ops ifcvf_ops = {
1067         .get_queue_num = ifcvf_get_queue_num,
1068         .get_features = ifcvf_get_vdpa_features,
1069         .get_protocol_features = ifcvf_get_protocol_features,
1070         .dev_conf = ifcvf_dev_config,
1071         .dev_close = ifcvf_dev_close,
1072         .set_vring_state = NULL,
1073         .set_features = ifcvf_set_features,
1074         .migration_done = NULL,
1075         .get_vfio_group_fd = ifcvf_get_vfio_group_fd,
1076         .get_vfio_device_fd = ifcvf_get_vfio_device_fd,
1077         .get_notify_area = ifcvf_get_notify_area,
1078 };
1079
1080 static inline int
1081 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1082 {
1083         uint16_t *n = extra_args;
1084
1085         if (value == NULL || extra_args == NULL)
1086                 return -EINVAL;
1087
1088         *n = (uint16_t)strtoul(value, NULL, 0);
1089         if (*n == USHRT_MAX && errno == ERANGE)
1090                 return -1;
1091
1092         return 0;
1093 }
1094
1095 static int
1096 ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1097                 struct rte_pci_device *pci_dev)
1098 {
1099         uint64_t features;
1100         struct ifcvf_internal *internal = NULL;
1101         struct internal_list *list = NULL;
1102         int vdpa_mode = 0;
1103         int sw_fallback_lm = 0;
1104         struct rte_kvargs *kvlist = NULL;
1105         int ret = 0;
1106
1107         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1108                 return 0;
1109
1110         kvlist = rte_kvargs_parse(pci_dev->device.devargs->args,
1111                         ifcvf_valid_arguments);
1112         if (kvlist == NULL)
1113                 return 1;
1114
1115         /* probe only when vdpa mode is specified */
1116         if (rte_kvargs_count(kvlist, IFCVF_VDPA_MODE) == 0) {
1117                 rte_kvargs_free(kvlist);
1118                 return 1;
1119         }
1120
1121         ret = rte_kvargs_process(kvlist, IFCVF_VDPA_MODE, &open_int,
1122                         &vdpa_mode);
1123         if (ret < 0 || vdpa_mode == 0) {
1124                 rte_kvargs_free(kvlist);
1125                 return 1;
1126         }
1127
1128         list = rte_zmalloc("ifcvf", sizeof(*list), 0);
1129         if (list == NULL)
1130                 goto error;
1131
1132         internal = rte_zmalloc("ifcvf", sizeof(*internal), 0);
1133         if (internal == NULL)
1134                 goto error;
1135
1136         internal->pdev = pci_dev;
1137         rte_spinlock_init(&internal->lock);
1138
1139         if (ifcvf_vfio_setup(internal) < 0) {
1140                 DRV_LOG(ERR, "failed to setup device %s", pci_dev->name);
1141                 goto error;
1142         }
1143
1144         if (ifcvf_init_hw(&internal->hw, internal->pdev) < 0) {
1145                 DRV_LOG(ERR, "failed to init device %s", pci_dev->name);
1146                 goto error;
1147         }
1148
1149         internal->max_queues = IFCVF_MAX_QUEUES;
1150         features = ifcvf_get_features(&internal->hw);
1151         internal->features = (features &
1152                 ~(1ULL << VIRTIO_F_IOMMU_PLATFORM)) |
1153                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
1154                 (1ULL << VIRTIO_NET_F_CTRL_VQ) |
1155                 (1ULL << VIRTIO_NET_F_STATUS) |
1156                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
1157                 (1ULL << VHOST_F_LOG_ALL);
1158
1159         internal->dev_addr.pci_addr = pci_dev->addr;
1160         internal->dev_addr.type = PCI_ADDR;
1161         list->internal = internal;
1162
1163         if (rte_kvargs_count(kvlist, IFCVF_SW_FALLBACK_LM)) {
1164                 ret = rte_kvargs_process(kvlist, IFCVF_SW_FALLBACK_LM,
1165                                 &open_int, &sw_fallback_lm);
1166                 if (ret < 0)
1167                         goto error;
1168         }
1169         internal->sw_lm = sw_fallback_lm;
1170
1171         internal->did = rte_vdpa_register_device(&internal->dev_addr,
1172                                 &ifcvf_ops);
1173         if (internal->did < 0) {
1174                 DRV_LOG(ERR, "failed to register device %s", pci_dev->name);
1175                 goto error;
1176         }
1177
1178         pthread_mutex_lock(&internal_list_lock);
1179         TAILQ_INSERT_TAIL(&internal_list, list, next);
1180         pthread_mutex_unlock(&internal_list_lock);
1181
1182         rte_atomic32_set(&internal->started, 1);
1183         update_datapath(internal);
1184
1185         rte_kvargs_free(kvlist);
1186         return 0;
1187
1188 error:
1189         rte_kvargs_free(kvlist);
1190         rte_free(list);
1191         rte_free(internal);
1192         return -1;
1193 }
1194
1195 static int
1196 ifcvf_pci_remove(struct rte_pci_device *pci_dev)
1197 {
1198         struct ifcvf_internal *internal;
1199         struct internal_list *list;
1200
1201         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1202                 return 0;
1203
1204         list = find_internal_resource_by_dev(pci_dev);
1205         if (list == NULL) {
1206                 DRV_LOG(ERR, "Invalid device: %s", pci_dev->name);
1207                 return -1;
1208         }
1209
1210         internal = list->internal;
1211         rte_atomic32_set(&internal->started, 0);
1212         update_datapath(internal);
1213
1214         rte_pci_unmap_device(internal->pdev);
1215         rte_vfio_container_destroy(internal->vfio_container_fd);
1216         rte_vdpa_unregister_device(internal->did);
1217
1218         pthread_mutex_lock(&internal_list_lock);
1219         TAILQ_REMOVE(&internal_list, list, next);
1220         pthread_mutex_unlock(&internal_list_lock);
1221
1222         rte_free(list);
1223         rte_free(internal);
1224
1225         return 0;
1226 }
1227
1228 /*
1229  * IFCVF has the same vendor ID and device ID as virtio net PCI
1230  * device, with its specific subsystem vendor ID and device ID.
1231  */
1232 static const struct rte_pci_id pci_id_ifcvf_map[] = {
1233         { .class_id = RTE_CLASS_ANY_ID,
1234           .vendor_id = IFCVF_VENDOR_ID,
1235           .device_id = IFCVF_DEVICE_ID,
1236           .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1237           .subsystem_device_id = IFCVF_SUBSYS_DEVICE_ID,
1238         },
1239
1240         { .vendor_id = 0, /* sentinel */
1241         },
1242 };
1243
1244 static struct rte_pci_driver rte_ifcvf_vdpa = {
1245         .id_table = pci_id_ifcvf_map,
1246         .drv_flags = 0,
1247         .probe = ifcvf_pci_probe,
1248         .remove = ifcvf_pci_remove,
1249 };
1250
1251 RTE_PMD_REGISTER_PCI(net_ifcvf, rte_ifcvf_vdpa);
1252 RTE_PMD_REGISTER_PCI_TABLE(net_ifcvf, pci_id_ifcvf_map);
1253 RTE_PMD_REGISTER_KMOD_DEP(net_ifcvf, "* vfio-pci");
1254
1255 RTE_INIT(ifcvf_vdpa_init_log)
1256 {
1257         ifcvf_vdpa_logtype = rte_log_register("pmd.net.ifcvf_vdpa");
1258         if (ifcvf_vdpa_logtype >= 0)
1259                 rte_log_set_level(ifcvf_vdpa_logtype, RTE_LOG_NOTICE);
1260 }