9e729ff72332ee0ba95d8f974245ce527b6d3c2b
[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         /* eventfd for used ring interrupt */
70         int intr_fd[IFCVF_MAX_QUEUES * 2];
71 };
72
73 struct internal_list {
74         TAILQ_ENTRY(internal_list) next;
75         struct ifcvf_internal *internal;
76 };
77
78 TAILQ_HEAD(internal_list_head, internal_list);
79 static struct internal_list_head internal_list =
80         TAILQ_HEAD_INITIALIZER(internal_list);
81
82 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
83
84 static struct internal_list *
85 find_internal_resource_by_did(int did)
86 {
87         int found = 0;
88         struct internal_list *list;
89
90         pthread_mutex_lock(&internal_list_lock);
91
92         TAILQ_FOREACH(list, &internal_list, next) {
93                 if (did == list->internal->did) {
94                         found = 1;
95                         break;
96                 }
97         }
98
99         pthread_mutex_unlock(&internal_list_lock);
100
101         if (!found)
102                 return NULL;
103
104         return list;
105 }
106
107 static struct internal_list *
108 find_internal_resource_by_dev(struct rte_pci_device *pdev)
109 {
110         int found = 0;
111         struct internal_list *list;
112
113         pthread_mutex_lock(&internal_list_lock);
114
115         TAILQ_FOREACH(list, &internal_list, next) {
116                 if (pdev == list->internal->pdev) {
117                         found = 1;
118                         break;
119                 }
120         }
121
122         pthread_mutex_unlock(&internal_list_lock);
123
124         if (!found)
125                 return NULL;
126
127         return list;
128 }
129
130 static int
131 ifcvf_vfio_setup(struct ifcvf_internal *internal)
132 {
133         struct rte_pci_device *dev = internal->pdev;
134         char devname[RTE_DEV_NAME_MAX_LEN] = {0};
135         int iommu_group_num;
136         int i;
137
138         internal->vfio_dev_fd = -1;
139         internal->vfio_group_fd = -1;
140         internal->vfio_container_fd = -1;
141
142         rte_pci_device_name(&dev->addr, devname, RTE_DEV_NAME_MAX_LEN);
143         rte_vfio_get_group_num(rte_pci_get_sysfs_path(), devname,
144                         &iommu_group_num);
145
146         internal->vfio_container_fd = rte_vfio_container_create();
147         if (internal->vfio_container_fd < 0)
148                 return -1;
149
150         internal->vfio_group_fd = rte_vfio_container_group_bind(
151                         internal->vfio_container_fd, iommu_group_num);
152         if (internal->vfio_group_fd < 0)
153                 goto err;
154
155         if (rte_pci_map_device(dev))
156                 goto err;
157
158         internal->vfio_dev_fd = dev->intr_handle.vfio_dev_fd;
159
160         for (i = 0; i < RTE_MIN(PCI_MAX_RESOURCE, IFCVF_PCI_MAX_RESOURCE);
161                         i++) {
162                 internal->hw.mem_resource[i].addr =
163                         internal->pdev->mem_resource[i].addr;
164                 internal->hw.mem_resource[i].phys_addr =
165                         internal->pdev->mem_resource[i].phys_addr;
166                 internal->hw.mem_resource[i].len =
167                         internal->pdev->mem_resource[i].len;
168         }
169
170         return 0;
171
172 err:
173         rte_vfio_container_destroy(internal->vfio_container_fd);
174         return -1;
175 }
176
177 static int
178 ifcvf_dma_map(struct ifcvf_internal *internal, int do_map)
179 {
180         uint32_t i;
181         int ret;
182         struct rte_vhost_memory *mem = NULL;
183         int vfio_container_fd;
184
185         ret = rte_vhost_get_mem_table(internal->vid, &mem);
186         if (ret < 0) {
187                 DRV_LOG(ERR, "failed to get VM memory layout.");
188                 goto exit;
189         }
190
191         vfio_container_fd = internal->vfio_container_fd;
192
193         for (i = 0; i < mem->nregions; i++) {
194                 struct rte_vhost_mem_region *reg;
195
196                 reg = &mem->regions[i];
197                 DRV_LOG(INFO, "%s, region %u: HVA 0x%" PRIx64 ", "
198                         "GPA 0x%" PRIx64 ", size 0x%" PRIx64 ".",
199                         do_map ? "DMA map" : "DMA unmap", i,
200                         reg->host_user_addr, reg->guest_phys_addr, reg->size);
201
202                 if (do_map) {
203                         ret = rte_vfio_container_dma_map(vfio_container_fd,
204                                 reg->host_user_addr, reg->guest_phys_addr,
205                                 reg->size);
206                         if (ret < 0) {
207                                 DRV_LOG(ERR, "DMA map failed.");
208                                 goto exit;
209                         }
210                 } else {
211                         ret = rte_vfio_container_dma_unmap(vfio_container_fd,
212                                 reg->host_user_addr, reg->guest_phys_addr,
213                                 reg->size);
214                         if (ret < 0) {
215                                 DRV_LOG(ERR, "DMA unmap failed.");
216                                 goto exit;
217                         }
218                 }
219         }
220
221 exit:
222         if (mem)
223                 free(mem);
224         return ret;
225 }
226
227 static uint64_t
228 hva_to_gpa(int vid, uint64_t hva)
229 {
230         struct rte_vhost_memory *mem = NULL;
231         struct rte_vhost_mem_region *reg;
232         uint32_t i;
233         uint64_t gpa = 0;
234
235         if (rte_vhost_get_mem_table(vid, &mem) < 0)
236                 goto exit;
237
238         for (i = 0; i < mem->nregions; i++) {
239                 reg = &mem->regions[i];
240
241                 if (hva >= reg->host_user_addr &&
242                                 hva < reg->host_user_addr + reg->size) {
243                         gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
244                         break;
245                 }
246         }
247
248 exit:
249         if (mem)
250                 free(mem);
251         return gpa;
252 }
253
254 static int
255 vdpa_ifcvf_start(struct ifcvf_internal *internal)
256 {
257         struct ifcvf_hw *hw = &internal->hw;
258         int i, nr_vring;
259         int vid;
260         struct rte_vhost_vring vq;
261         uint64_t gpa;
262
263         vid = internal->vid;
264         nr_vring = rte_vhost_get_vring_num(vid);
265         rte_vhost_get_negotiated_features(vid, &hw->req_features);
266
267         for (i = 0; i < nr_vring; i++) {
268                 rte_vhost_get_vhost_vring(vid, i, &vq);
269                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
270                 if (gpa == 0) {
271                         DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
272                         return -1;
273                 }
274                 hw->vring[i].desc = gpa;
275
276                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
277                 if (gpa == 0) {
278                         DRV_LOG(ERR, "Fail to get GPA for available ring.");
279                         return -1;
280                 }
281                 hw->vring[i].avail = gpa;
282
283                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
284                 if (gpa == 0) {
285                         DRV_LOG(ERR, "Fail to get GPA for used ring.");
286                         return -1;
287                 }
288                 hw->vring[i].used = gpa;
289
290                 hw->vring[i].size = vq.size;
291                 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
292                                 &hw->vring[i].last_used_idx);
293         }
294         hw->nr_vring = i;
295
296         return ifcvf_start_hw(&internal->hw);
297 }
298
299 static void
300 vdpa_ifcvf_stop(struct ifcvf_internal *internal)
301 {
302         struct ifcvf_hw *hw = &internal->hw;
303         uint32_t i;
304         int vid;
305         uint64_t features;
306         uint64_t log_base, log_size;
307         uint64_t len;
308
309         vid = internal->vid;
310         ifcvf_stop_hw(hw);
311
312         for (i = 0; i < hw->nr_vring; i++)
313                 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
314                                 hw->vring[i].last_used_idx);
315
316         if (internal->sw_lm)
317                 return;
318
319         rte_vhost_get_negotiated_features(vid, &features);
320         if (RTE_VHOST_NEED_LOG(features)) {
321                 ifcvf_disable_logging(hw);
322                 rte_vhost_get_log_base(internal->vid, &log_base, &log_size);
323                 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
324                                 log_base, IFCVF_LOG_BASE, log_size);
325                 /*
326                  * IFCVF marks dirty memory pages for only packet buffer,
327                  * SW helps to mark the used ring as dirty after device stops.
328                  */
329                 for (i = 0; i < hw->nr_vring; i++) {
330                         len = IFCVF_USED_RING_LEN(hw->vring[i].size);
331                         rte_vhost_log_used_vring(vid, i, 0, len);
332                 }
333         }
334 }
335
336 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
337                 sizeof(int) * (IFCVF_MAX_QUEUES * 2 + 1))
338 static int
339 vdpa_enable_vfio_intr(struct ifcvf_internal *internal, bool m_rx)
340 {
341         int ret;
342         uint32_t i, nr_vring;
343         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
344         struct vfio_irq_set *irq_set;
345         int *fd_ptr;
346         struct rte_vhost_vring vring;
347         int fd;
348
349         nr_vring = rte_vhost_get_vring_num(internal->vid);
350
351         irq_set = (struct vfio_irq_set *)irq_set_buf;
352         irq_set->argsz = sizeof(irq_set_buf);
353         irq_set->count = nr_vring + 1;
354         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
355                          VFIO_IRQ_SET_ACTION_TRIGGER;
356         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
357         irq_set->start = 0;
358         fd_ptr = (int *)&irq_set->data;
359         fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = internal->pdev->intr_handle.fd;
360
361         for (i = 0; i < nr_vring; i++)
362                 internal->intr_fd[i] = -1;
363
364         for (i = 0; i < nr_vring; i++) {
365                 rte_vhost_get_vhost_vring(internal->vid, i, &vring);
366                 fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = vring.callfd;
367                 if ((i & 1) == 0 && m_rx == true) {
368                         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
369                         if (fd < 0) {
370                                 DRV_LOG(ERR, "can't setup eventfd: %s",
371                                         strerror(errno));
372                                 return -1;
373                         }
374                         internal->intr_fd[i] = fd;
375                         fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = fd;
376                 }
377         }
378
379         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
380         if (ret) {
381                 DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s",
382                                 strerror(errno));
383                 return -1;
384         }
385
386         return 0;
387 }
388
389 static int
390 vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
391 {
392         int ret;
393         uint32_t i, nr_vring;
394         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
395         struct vfio_irq_set *irq_set;
396
397         irq_set = (struct vfio_irq_set *)irq_set_buf;
398         irq_set->argsz = sizeof(irq_set_buf);
399         irq_set->count = 0;
400         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
401         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
402         irq_set->start = 0;
403
404         nr_vring = rte_vhost_get_vring_num(internal->vid);
405         for (i = 0; i < nr_vring; i++) {
406                 if (internal->intr_fd[i] >= 0)
407                         close(internal->intr_fd[i]);
408                 internal->intr_fd[i] = -1;
409         }
410
411         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
412         if (ret) {
413                 DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s",
414                                 strerror(errno));
415                 return -1;
416         }
417
418         return 0;
419 }
420
421 static void *
422 notify_relay(void *arg)
423 {
424         int i, kickfd, epfd, nfds = 0;
425         uint32_t qid, q_num;
426         struct epoll_event events[IFCVF_MAX_QUEUES * 2];
427         struct epoll_event ev;
428         uint64_t buf;
429         int nbytes;
430         struct rte_vhost_vring vring;
431         struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
432         struct ifcvf_hw *hw = &internal->hw;
433
434         q_num = rte_vhost_get_vring_num(internal->vid);
435
436         epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
437         if (epfd < 0) {
438                 DRV_LOG(ERR, "failed to create epoll instance.");
439                 return NULL;
440         }
441         internal->epfd = epfd;
442
443         for (qid = 0; qid < q_num; qid++) {
444                 ev.events = EPOLLIN | EPOLLPRI;
445                 rte_vhost_get_vhost_vring(internal->vid, qid, &vring);
446                 ev.data.u64 = qid | (uint64_t)vring.kickfd << 32;
447                 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
448                         DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
449                         return NULL;
450                 }
451         }
452
453         for (;;) {
454                 nfds = epoll_wait(epfd, events, q_num, -1);
455                 if (nfds < 0) {
456                         if (errno == EINTR)
457                                 continue;
458                         DRV_LOG(ERR, "epoll_wait return fail\n");
459                         return NULL;
460                 }
461
462                 for (i = 0; i < nfds; i++) {
463                         qid = events[i].data.u32;
464                         kickfd = (uint32_t)(events[i].data.u64 >> 32);
465                         do {
466                                 nbytes = read(kickfd, &buf, 8);
467                                 if (nbytes < 0) {
468                                         if (errno == EINTR ||
469                                             errno == EWOULDBLOCK ||
470                                             errno == EAGAIN)
471                                                 continue;
472                                         DRV_LOG(INFO, "Error reading "
473                                                 "kickfd: %s",
474                                                 strerror(errno));
475                                 }
476                                 break;
477                         } while (1);
478
479                         ifcvf_notify_queue(hw, qid);
480                 }
481         }
482
483         return NULL;
484 }
485
486 static int
487 setup_notify_relay(struct ifcvf_internal *internal)
488 {
489         int ret;
490
491         ret = pthread_create(&internal->tid, NULL, notify_relay,
492                         (void *)internal);
493         if (ret) {
494                 DRV_LOG(ERR, "failed to create notify relay pthread.");
495                 return -1;
496         }
497         return 0;
498 }
499
500 static int
501 unset_notify_relay(struct ifcvf_internal *internal)
502 {
503         void *status;
504
505         if (internal->tid) {
506                 pthread_cancel(internal->tid);
507                 pthread_join(internal->tid, &status);
508         }
509         internal->tid = 0;
510
511         if (internal->epfd >= 0)
512                 close(internal->epfd);
513         internal->epfd = -1;
514
515         return 0;
516 }
517
518 static int
519 update_datapath(struct ifcvf_internal *internal)
520 {
521         int ret;
522
523         rte_spinlock_lock(&internal->lock);
524
525         if (!rte_atomic32_read(&internal->running) &&
526             (rte_atomic32_read(&internal->started) &&
527              rte_atomic32_read(&internal->dev_attached))) {
528                 ret = ifcvf_dma_map(internal, 1);
529                 if (ret)
530                         goto err;
531
532                 ret = vdpa_enable_vfio_intr(internal, 0);
533                 if (ret)
534                         goto err;
535
536                 ret = vdpa_ifcvf_start(internal);
537                 if (ret)
538                         goto err;
539
540                 ret = setup_notify_relay(internal);
541                 if (ret)
542                         goto err;
543
544                 rte_atomic32_set(&internal->running, 1);
545         } else if (rte_atomic32_read(&internal->running) &&
546                    (!rte_atomic32_read(&internal->started) ||
547                     !rte_atomic32_read(&internal->dev_attached))) {
548                 ret = unset_notify_relay(internal);
549                 if (ret)
550                         goto err;
551
552                 vdpa_ifcvf_stop(internal);
553
554                 ret = vdpa_disable_vfio_intr(internal);
555                 if (ret)
556                         goto err;
557
558                 ret = ifcvf_dma_map(internal, 0);
559                 if (ret)
560                         goto err;
561
562                 rte_atomic32_set(&internal->running, 0);
563         }
564
565         rte_spinlock_unlock(&internal->lock);
566         return 0;
567 err:
568         rte_spinlock_unlock(&internal->lock);
569         return ret;
570 }
571
572 static int
573 m_ifcvf_start(struct ifcvf_internal *internal)
574 {
575         struct ifcvf_hw *hw = &internal->hw;
576         uint32_t i, nr_vring;
577         int vid, ret;
578         struct rte_vhost_vring vq;
579         void *vring_buf;
580         uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
581         uint64_t size;
582         uint64_t gpa;
583
584         vid = internal->vid;
585         nr_vring = rte_vhost_get_vring_num(vid);
586         rte_vhost_get_negotiated_features(vid, &hw->req_features);
587
588         for (i = 0; i < nr_vring; i++) {
589                 rte_vhost_get_vhost_vring(vid, i, &vq);
590
591                 size = RTE_ALIGN_CEIL(vring_size(vq.size, PAGE_SIZE),
592                                 PAGE_SIZE);
593                 vring_buf = rte_zmalloc("ifcvf", size, PAGE_SIZE);
594                 vring_init(&internal->m_vring[i], vq.size, vring_buf,
595                                 PAGE_SIZE);
596
597                 ret = rte_vfio_container_dma_map(internal->vfio_container_fd,
598                         (uint64_t)(uintptr_t)vring_buf, m_vring_iova, size);
599                 if (ret < 0) {
600                         DRV_LOG(ERR, "mediated vring DMA map failed.");
601                         goto error;
602                 }
603
604                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
605                 if (gpa == 0) {
606                         DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
607                         return -1;
608                 }
609                 hw->vring[i].desc = gpa;
610
611                 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
612                 if (gpa == 0) {
613                         DRV_LOG(ERR, "Fail to get GPA for available ring.");
614                         return -1;
615                 }
616                 hw->vring[i].avail = gpa;
617
618                 /* Direct I/O for Tx queue, relay for Rx queue */
619                 if (i & 1) {
620                         gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
621                         if (gpa == 0) {
622                                 DRV_LOG(ERR, "Fail to get GPA for used ring.");
623                                 return -1;
624                         }
625                         hw->vring[i].used = gpa;
626                 } else {
627                         hw->vring[i].used = m_vring_iova +
628                                 (char *)internal->m_vring[i].used -
629                                 (char *)internal->m_vring[i].desc;
630                 }
631
632                 hw->vring[i].size = vq.size;
633
634                 rte_vhost_get_vring_base(vid, i,
635                                 &internal->m_vring[i].avail->idx,
636                                 &internal->m_vring[i].used->idx);
637
638                 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
639                                 &hw->vring[i].last_used_idx);
640
641                 m_vring_iova += size;
642         }
643         hw->nr_vring = nr_vring;
644
645         return ifcvf_start_hw(&internal->hw);
646
647 error:
648         for (i = 0; i < nr_vring; i++)
649                 if (internal->m_vring[i].desc)
650                         rte_free(internal->m_vring[i].desc);
651
652         return -1;
653 }
654
655 static int
656 m_ifcvf_stop(struct ifcvf_internal *internal)
657 {
658         int vid;
659         uint32_t i;
660         struct rte_vhost_vring vq;
661         struct ifcvf_hw *hw = &internal->hw;
662         uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
663         uint64_t size, len;
664
665         vid = internal->vid;
666         ifcvf_stop_hw(hw);
667
668         for (i = 0; i < hw->nr_vring; i++) {
669                 rte_vhost_get_vhost_vring(vid, i, &vq);
670                 len = IFCVF_USED_RING_LEN(vq.size);
671                 rte_vhost_log_used_vring(vid, i, 0, len);
672
673                 size = RTE_ALIGN_CEIL(vring_size(vq.size, PAGE_SIZE),
674                                 PAGE_SIZE);
675                 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
676                         (uint64_t)(uintptr_t)internal->m_vring[i].desc,
677                         m_vring_iova, size);
678
679                 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
680                                 hw->vring[i].last_used_idx);
681                 rte_free(internal->m_vring[i].desc);
682                 m_vring_iova += size;
683         }
684
685         return 0;
686 }
687
688 static void
689 update_used_ring(struct ifcvf_internal *internal, uint16_t qid)
690 {
691         rte_vdpa_relay_vring_used(internal->vid, qid, &internal->m_vring[qid]);
692         rte_vhost_vring_call(internal->vid, qid);
693 }
694
695 static void *
696 vring_relay(void *arg)
697 {
698         int i, vid, epfd, fd, nfds;
699         struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
700         struct rte_vhost_vring vring;
701         uint16_t qid, q_num;
702         struct epoll_event events[IFCVF_MAX_QUEUES * 4];
703         struct epoll_event ev;
704         int nbytes;
705         uint64_t buf;
706
707         vid = internal->vid;
708         q_num = rte_vhost_get_vring_num(vid);
709
710         /* add notify fd and interrupt fd to epoll */
711         epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
712         if (epfd < 0) {
713                 DRV_LOG(ERR, "failed to create epoll instance.");
714                 return NULL;
715         }
716         internal->epfd = epfd;
717
718         for (qid = 0; qid < q_num; qid++) {
719                 ev.events = EPOLLIN | EPOLLPRI;
720                 rte_vhost_get_vhost_vring(vid, qid, &vring);
721                 ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
722                 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
723                         DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
724                         return NULL;
725                 }
726         }
727
728         for (qid = 0; qid < q_num; qid += 2) {
729                 ev.events = EPOLLIN | EPOLLPRI;
730                 /* leave a flag to mark it's for interrupt */
731                 ev.data.u64 = 1 | qid << 1 |
732                         (uint64_t)internal->intr_fd[qid] << 32;
733                 if (epoll_ctl(epfd, EPOLL_CTL_ADD, internal->intr_fd[qid], &ev)
734                                 < 0) {
735                         DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
736                         return NULL;
737                 }
738         }
739
740         /* start relay with a first kick */
741         for (qid = 0; qid < q_num; qid++)
742                 ifcvf_notify_queue(&internal->hw, qid);
743
744         /* listen to the events and react accordingly */
745         for (;;) {
746                 nfds = epoll_wait(epfd, events, q_num * 2, -1);
747                 if (nfds < 0) {
748                         if (errno == EINTR)
749                                 continue;
750                         DRV_LOG(ERR, "epoll_wait return fail\n");
751                         return NULL;
752                 }
753
754                 for (i = 0; i < nfds; i++) {
755                         fd = (uint32_t)(events[i].data.u64 >> 32);
756                         do {
757                                 nbytes = read(fd, &buf, 8);
758                                 if (nbytes < 0) {
759                                         if (errno == EINTR ||
760                                             errno == EWOULDBLOCK ||
761                                             errno == EAGAIN)
762                                                 continue;
763                                         DRV_LOG(INFO, "Error reading "
764                                                 "kickfd: %s",
765                                                 strerror(errno));
766                                 }
767                                 break;
768                         } while (1);
769
770                         qid = events[i].data.u32 >> 1;
771
772                         if (events[i].data.u32 & 1)
773                                 update_used_ring(internal, qid);
774                         else
775                                 ifcvf_notify_queue(&internal->hw, qid);
776                 }
777         }
778
779         return NULL;
780 }
781
782 static int
783 setup_vring_relay(struct ifcvf_internal *internal)
784 {
785         int ret;
786
787         ret = pthread_create(&internal->tid, NULL, vring_relay,
788                         (void *)internal);
789         if (ret) {
790                 DRV_LOG(ERR, "failed to create ring relay pthread.");
791                 return -1;
792         }
793         return 0;
794 }
795
796 static int
797 unset_vring_relay(struct ifcvf_internal *internal)
798 {
799         void *status;
800
801         if (internal->tid) {
802                 pthread_cancel(internal->tid);
803                 pthread_join(internal->tid, &status);
804         }
805         internal->tid = 0;
806
807         if (internal->epfd >= 0)
808                 close(internal->epfd);
809         internal->epfd = -1;
810
811         return 0;
812 }
813
814 static int
815 ifcvf_sw_fallback_switchover(struct ifcvf_internal *internal)
816 {
817         int ret;
818         int vid = internal->vid;
819
820         /* stop the direct IO data path */
821         unset_notify_relay(internal);
822         vdpa_ifcvf_stop(internal);
823         vdpa_disable_vfio_intr(internal);
824
825         ret = rte_vhost_host_notifier_ctrl(vid, false);
826         if (ret && ret != -ENOTSUP)
827                 goto error;
828
829         /* set up interrupt for interrupt relay */
830         ret = vdpa_enable_vfio_intr(internal, 1);
831         if (ret)
832                 goto unmap;
833
834         /* config the VF */
835         ret = m_ifcvf_start(internal);
836         if (ret)
837                 goto unset_intr;
838
839         /* set up vring relay thread */
840         ret = setup_vring_relay(internal);
841         if (ret)
842                 goto stop_vf;
843
844         rte_vhost_host_notifier_ctrl(vid, true);
845
846         internal->sw_fallback_running = true;
847
848         return 0;
849
850 stop_vf:
851         m_ifcvf_stop(internal);
852 unset_intr:
853         vdpa_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                 vdpa_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 }