1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
10 #include <linux/virtio_net.h>
13 #include <rte_malloc.h>
14 #include <rte_memory.h>
15 #include <rte_bus_pci.h>
16 #include <rte_vhost.h>
19 #include <rte_spinlock.h>
21 #include <rte_kvargs.h>
22 #include <rte_devargs.h>
24 #include "base/ifcvf.h"
26 #define DRV_LOG(level, fmt, args...) \
27 rte_log(RTE_LOG_ ## level, ifcvf_vdpa_logtype, \
28 "IFCVF %s(): " fmt "\n", __func__, ##args)
31 #define PAGE_SIZE 4096
34 #define IFCVF_USED_RING_LEN(size) \
35 ((size) * sizeof(struct vring_used_elem) + sizeof(uint16_t) * 3)
37 #define IFCVF_VDPA_MODE "vdpa"
38 #define IFCVF_SW_FALLBACK_LM "sw-live-migration"
40 static const char * const ifcvf_valid_arguments[] = {
46 static int ifcvf_vdpa_logtype;
48 struct ifcvf_internal {
49 struct rte_vdpa_dev_addr dev_addr;
50 struct rte_pci_device *pdev;
52 int vfio_container_fd;
55 pthread_t tid; /* thread for notify relay */
61 rte_atomic32_t started;
62 rte_atomic32_t dev_attached;
63 rte_atomic32_t running;
66 bool sw_fallback_running;
67 /* mediated vring for sw fallback */
68 struct vring m_vring[IFCVF_MAX_QUEUES * 2];
71 struct internal_list {
72 TAILQ_ENTRY(internal_list) next;
73 struct ifcvf_internal *internal;
76 TAILQ_HEAD(internal_list_head, internal_list);
77 static struct internal_list_head internal_list =
78 TAILQ_HEAD_INITIALIZER(internal_list);
80 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
82 static struct internal_list *
83 find_internal_resource_by_did(int did)
86 struct internal_list *list;
88 pthread_mutex_lock(&internal_list_lock);
90 TAILQ_FOREACH(list, &internal_list, next) {
91 if (did == list->internal->did) {
97 pthread_mutex_unlock(&internal_list_lock);
105 static struct internal_list *
106 find_internal_resource_by_dev(struct rte_pci_device *pdev)
109 struct internal_list *list;
111 pthread_mutex_lock(&internal_list_lock);
113 TAILQ_FOREACH(list, &internal_list, next) {
114 if (pdev == list->internal->pdev) {
120 pthread_mutex_unlock(&internal_list_lock);
129 ifcvf_vfio_setup(struct ifcvf_internal *internal)
131 struct rte_pci_device *dev = internal->pdev;
132 char devname[RTE_DEV_NAME_MAX_LEN] = {0};
136 internal->vfio_dev_fd = -1;
137 internal->vfio_group_fd = -1;
138 internal->vfio_container_fd = -1;
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,
144 internal->vfio_container_fd = rte_vfio_container_create();
145 if (internal->vfio_container_fd < 0)
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)
153 if (rte_pci_map_device(dev))
156 internal->vfio_dev_fd = dev->intr_handle.vfio_dev_fd;
158 for (i = 0; i < RTE_MIN(PCI_MAX_RESOURCE, IFCVF_PCI_MAX_RESOURCE);
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;
171 rte_vfio_container_destroy(internal->vfio_container_fd);
176 ifcvf_dma_map(struct ifcvf_internal *internal, int do_map)
180 struct rte_vhost_memory *mem = NULL;
181 int vfio_container_fd;
183 ret = rte_vhost_get_mem_table(internal->vid, &mem);
185 DRV_LOG(ERR, "failed to get VM memory layout.");
189 vfio_container_fd = internal->vfio_container_fd;
191 for (i = 0; i < mem->nregions; i++) {
192 struct rte_vhost_mem_region *reg;
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);
201 ret = rte_vfio_container_dma_map(vfio_container_fd,
202 reg->host_user_addr, reg->guest_phys_addr,
205 DRV_LOG(ERR, "DMA map failed.");
209 ret = rte_vfio_container_dma_unmap(vfio_container_fd,
210 reg->host_user_addr, reg->guest_phys_addr,
213 DRV_LOG(ERR, "DMA unmap failed.");
226 hva_to_gpa(int vid, uint64_t hva)
228 struct rte_vhost_memory *mem = NULL;
229 struct rte_vhost_mem_region *reg;
233 if (rte_vhost_get_mem_table(vid, &mem) < 0)
236 for (i = 0; i < mem->nregions; i++) {
237 reg = &mem->regions[i];
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;
253 vdpa_ifcvf_start(struct ifcvf_internal *internal)
255 struct ifcvf_hw *hw = &internal->hw;
258 struct rte_vhost_vring vq;
262 nr_vring = rte_vhost_get_vring_num(vid);
263 rte_vhost_get_negotiated_features(vid, &hw->req_features);
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);
269 DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
272 hw->vring[i].desc = gpa;
274 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
276 DRV_LOG(ERR, "Fail to get GPA for available ring.");
279 hw->vring[i].avail = gpa;
281 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
283 DRV_LOG(ERR, "Fail to get GPA for used ring.");
286 hw->vring[i].used = gpa;
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);
294 return ifcvf_start_hw(&internal->hw);
298 vdpa_ifcvf_stop(struct ifcvf_internal *internal)
300 struct ifcvf_hw *hw = &internal->hw;
304 uint64_t log_base, log_size;
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);
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);
324 * IFCVF marks dirty memory pages for only packet buffer,
325 * SW helps to mark the used ring as dirty after device stops.
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);
334 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
335 sizeof(int) * (IFCVF_MAX_QUEUES * 2 + 1))
337 vdpa_enable_vfio_intr(struct ifcvf_internal *internal)
340 uint32_t i, nr_vring;
341 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
342 struct vfio_irq_set *irq_set;
344 struct rte_vhost_vring vring;
346 nr_vring = rte_vhost_get_vring_num(internal->vid);
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;
355 fd_ptr = (int *)&irq_set->data;
356 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = internal->pdev->intr_handle.fd;
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;
363 ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
365 DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s",
374 vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
377 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
378 struct vfio_irq_set *irq_set;
380 irq_set = (struct vfio_irq_set *)irq_set_buf;
381 irq_set->argsz = sizeof(irq_set_buf);
383 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
384 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
387 ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
389 DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s",
398 notify_relay(void *arg)
400 int i, kickfd, epfd, nfds = 0;
402 struct epoll_event events[IFCVF_MAX_QUEUES * 2];
403 struct epoll_event ev;
406 struct rte_vhost_vring vring;
407 struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
408 struct ifcvf_hw *hw = &internal->hw;
410 q_num = rte_vhost_get_vring_num(internal->vid);
412 epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
414 DRV_LOG(ERR, "failed to create epoll instance.");
417 internal->epfd = epfd;
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));
430 nfds = epoll_wait(epfd, events, q_num, -1);
434 DRV_LOG(ERR, "epoll_wait return fail\n");
438 for (i = 0; i < nfds; i++) {
439 qid = events[i].data.u32;
440 kickfd = (uint32_t)(events[i].data.u64 >> 32);
442 nbytes = read(kickfd, &buf, 8);
444 if (errno == EINTR ||
445 errno == EWOULDBLOCK ||
448 DRV_LOG(INFO, "Error reading "
455 ifcvf_notify_queue(hw, qid);
463 setup_notify_relay(struct ifcvf_internal *internal)
467 ret = pthread_create(&internal->tid, NULL, notify_relay,
470 DRV_LOG(ERR, "failed to create notify relay pthread.");
477 unset_notify_relay(struct ifcvf_internal *internal)
482 pthread_cancel(internal->tid);
483 pthread_join(internal->tid, &status);
487 if (internal->epfd >= 0)
488 close(internal->epfd);
495 update_datapath(struct ifcvf_internal *internal)
499 rte_spinlock_lock(&internal->lock);
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);
508 ret = vdpa_enable_vfio_intr(internal);
512 ret = vdpa_ifcvf_start(internal);
516 ret = setup_notify_relay(internal);
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);
528 vdpa_ifcvf_stop(internal);
530 ret = vdpa_disable_vfio_intr(internal);
534 ret = ifcvf_dma_map(internal, 0);
538 rte_atomic32_set(&internal->running, 0);
541 rte_spinlock_unlock(&internal->lock);
544 rte_spinlock_unlock(&internal->lock);
549 m_ifcvf_start(struct ifcvf_internal *internal)
551 struct ifcvf_hw *hw = &internal->hw;
552 uint32_t i, nr_vring;
554 struct rte_vhost_vring vq;
556 uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
561 nr_vring = rte_vhost_get_vring_num(vid);
562 rte_vhost_get_negotiated_features(vid, &hw->req_features);
564 for (i = 0; i < nr_vring; i++) {
565 rte_vhost_get_vhost_vring(vid, i, &vq);
567 size = RTE_ALIGN_CEIL(vring_size(vq.size, PAGE_SIZE),
569 vring_buf = rte_zmalloc("ifcvf", size, PAGE_SIZE);
570 vring_init(&internal->m_vring[i], vq.size, vring_buf,
573 ret = rte_vfio_container_dma_map(internal->vfio_container_fd,
574 (uint64_t)(uintptr_t)vring_buf, m_vring_iova, size);
576 DRV_LOG(ERR, "mediated vring DMA map failed.");
580 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
582 DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
585 hw->vring[i].desc = gpa;
587 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
589 DRV_LOG(ERR, "Fail to get GPA for available ring.");
592 hw->vring[i].avail = gpa;
594 hw->vring[i].used = m_vring_iova +
595 (char *)internal->m_vring[i].used -
596 (char *)internal->m_vring[i].desc;
598 hw->vring[i].size = vq.size;
600 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
601 &hw->vring[i].last_used_idx);
603 m_vring_iova += size;
605 hw->nr_vring = nr_vring;
607 return ifcvf_start_hw(&internal->hw);
610 for (i = 0; i < nr_vring; i++)
611 if (internal->m_vring[i].desc)
612 rte_free(internal->m_vring[i].desc);
618 m_ifcvf_stop(struct ifcvf_internal *internal)
622 struct rte_vhost_vring vq;
623 struct ifcvf_hw *hw = &internal->hw;
624 uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
630 for (i = 0; i < hw->nr_vring; i++) {
631 rte_vhost_get_vhost_vring(vid, i, &vq);
632 len = IFCVF_USED_RING_LEN(vq.size);
633 rte_vhost_log_used_vring(vid, i, 0, len);
635 size = RTE_ALIGN_CEIL(vring_size(vq.size, PAGE_SIZE),
637 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
638 (uint64_t)(uintptr_t)internal->m_vring[i].desc,
641 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
642 hw->vring[i].last_used_idx);
643 rte_free(internal->m_vring[i].desc);
644 m_vring_iova += size;
651 m_enable_vfio_intr(struct ifcvf_internal *internal)
654 struct rte_intr_handle *intr_handle = &internal->pdev->intr_handle;
657 nr_vring = rte_vhost_get_vring_num(internal->vid);
659 ret = rte_intr_efd_enable(intr_handle, nr_vring);
663 ret = rte_intr_enable(intr_handle);
671 m_disable_vfio_intr(struct ifcvf_internal *internal)
673 struct rte_intr_handle *intr_handle = &internal->pdev->intr_handle;
675 rte_intr_efd_disable(intr_handle);
676 rte_intr_disable(intr_handle);
680 update_used_ring(struct ifcvf_internal *internal, uint16_t qid)
682 rte_vdpa_relay_vring_used(internal->vid, qid, &internal->m_vring[qid]);
683 rte_vhost_vring_call(internal->vid, qid);
687 vring_relay(void *arg)
689 int i, vid, epfd, fd, nfds;
690 struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
691 struct rte_vhost_vring vring;
692 struct rte_intr_handle *intr_handle;
694 struct epoll_event events[IFCVF_MAX_QUEUES * 4];
695 struct epoll_event ev;
700 q_num = rte_vhost_get_vring_num(vid);
701 /* prepare the mediated vring */
702 for (qid = 0; qid < q_num; qid++)
703 rte_vhost_get_vring_base(vid, qid,
704 &internal->m_vring[qid].avail->idx,
705 &internal->m_vring[qid].used->idx);
707 /* add notify fd and interrupt fd to epoll */
708 epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
710 DRV_LOG(ERR, "failed to create epoll instance.");
713 internal->epfd = epfd;
715 for (qid = 0; qid < q_num; qid++) {
716 ev.events = EPOLLIN | EPOLLPRI;
717 rte_vhost_get_vhost_vring(vid, qid, &vring);
718 ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
719 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
720 DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
725 intr_handle = &internal->pdev->intr_handle;
726 for (qid = 0; qid < q_num; qid++) {
727 ev.events = EPOLLIN | EPOLLPRI;
728 ev.data.u64 = 1 | qid << 1 |
729 (uint64_t)intr_handle->efds[qid] << 32;
730 if (epoll_ctl(epfd, EPOLL_CTL_ADD, intr_handle->efds[qid], &ev)
732 DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
737 /* start relay with a first kick */
738 for (qid = 0; qid < q_num; qid++)
739 ifcvf_notify_queue(&internal->hw, qid);
741 /* listen to the events and react accordingly */
743 nfds = epoll_wait(epfd, events, q_num * 2, -1);
747 DRV_LOG(ERR, "epoll_wait return fail\n");
751 for (i = 0; i < nfds; i++) {
752 fd = (uint32_t)(events[i].data.u64 >> 32);
754 nbytes = read(fd, &buf, 8);
756 if (errno == EINTR ||
757 errno == EWOULDBLOCK ||
760 DRV_LOG(INFO, "Error reading "
767 qid = events[i].data.u32 >> 1;
769 if (events[i].data.u32 & 1)
770 update_used_ring(internal, qid);
772 ifcvf_notify_queue(&internal->hw, qid);
780 setup_vring_relay(struct ifcvf_internal *internal)
784 ret = pthread_create(&internal->tid, NULL, vring_relay,
787 DRV_LOG(ERR, "failed to create ring relay pthread.");
794 unset_vring_relay(struct ifcvf_internal *internal)
799 pthread_cancel(internal->tid);
800 pthread_join(internal->tid, &status);
804 if (internal->epfd >= 0)
805 close(internal->epfd);
812 ifcvf_sw_fallback_switchover(struct ifcvf_internal *internal)
815 int vid = internal->vid;
817 /* stop the direct IO data path */
818 unset_notify_relay(internal);
819 vdpa_ifcvf_stop(internal);
820 vdpa_disable_vfio_intr(internal);
822 ret = rte_vhost_host_notifier_ctrl(vid, false);
823 if (ret && ret != -ENOTSUP)
826 /* set up interrupt for interrupt relay */
827 ret = m_enable_vfio_intr(internal);
832 ret = m_ifcvf_start(internal);
836 /* set up vring relay thread */
837 ret = setup_vring_relay(internal);
841 rte_vhost_host_notifier_ctrl(vid, true);
843 internal->sw_fallback_running = true;
848 m_ifcvf_stop(internal);
850 m_disable_vfio_intr(internal);
852 ifcvf_dma_map(internal, 0);
858 ifcvf_dev_config(int vid)
861 struct internal_list *list;
862 struct ifcvf_internal *internal;
864 did = rte_vhost_get_vdpa_device_id(vid);
865 list = find_internal_resource_by_did(did);
867 DRV_LOG(ERR, "Invalid device id: %d", did);
871 internal = list->internal;
873 rte_atomic32_set(&internal->dev_attached, 1);
874 update_datapath(internal);
876 if (rte_vhost_host_notifier_ctrl(vid, true) != 0)
877 DRV_LOG(NOTICE, "vDPA (%d): software relay is used.", did);
883 ifcvf_dev_close(int vid)
886 struct internal_list *list;
887 struct ifcvf_internal *internal;
889 did = rte_vhost_get_vdpa_device_id(vid);
890 list = find_internal_resource_by_did(did);
892 DRV_LOG(ERR, "Invalid device id: %d", did);
896 internal = list->internal;
898 if (internal->sw_fallback_running) {
899 /* unset ring relay */
900 unset_vring_relay(internal);
903 m_ifcvf_stop(internal);
905 /* remove interrupt setting */
906 m_disable_vfio_intr(internal);
908 /* unset DMA map for guest memory */
909 ifcvf_dma_map(internal, 0);
911 internal->sw_fallback_running = false;
913 rte_atomic32_set(&internal->dev_attached, 0);
914 update_datapath(internal);
921 ifcvf_set_features(int vid)
925 struct internal_list *list;
926 struct ifcvf_internal *internal;
927 uint64_t log_base, log_size;
929 did = rte_vhost_get_vdpa_device_id(vid);
930 list = find_internal_resource_by_did(did);
932 DRV_LOG(ERR, "Invalid device id: %d", did);
936 internal = list->internal;
937 rte_vhost_get_negotiated_features(vid, &features);
939 if (!RTE_VHOST_NEED_LOG(features))
942 if (internal->sw_lm) {
943 ifcvf_sw_fallback_switchover(internal);
945 rte_vhost_get_log_base(vid, &log_base, &log_size);
946 rte_vfio_container_dma_map(internal->vfio_container_fd,
947 log_base, IFCVF_LOG_BASE, log_size);
948 ifcvf_enable_logging(&internal->hw, IFCVF_LOG_BASE, log_size);
955 ifcvf_get_vfio_group_fd(int vid)
958 struct internal_list *list;
960 did = rte_vhost_get_vdpa_device_id(vid);
961 list = find_internal_resource_by_did(did);
963 DRV_LOG(ERR, "Invalid device id: %d", did);
967 return list->internal->vfio_group_fd;
971 ifcvf_get_vfio_device_fd(int vid)
974 struct internal_list *list;
976 did = rte_vhost_get_vdpa_device_id(vid);
977 list = find_internal_resource_by_did(did);
979 DRV_LOG(ERR, "Invalid device id: %d", did);
983 return list->internal->vfio_dev_fd;
987 ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
990 struct internal_list *list;
991 struct ifcvf_internal *internal;
992 struct vfio_region_info reg = { .argsz = sizeof(reg) };
995 did = rte_vhost_get_vdpa_device_id(vid);
996 list = find_internal_resource_by_did(did);
998 DRV_LOG(ERR, "Invalid device id: %d", did);
1002 internal = list->internal;
1004 reg.index = ifcvf_get_notify_region(&internal->hw);
1005 ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
1007 DRV_LOG(ERR, "Get not get device region info: %s",
1012 *offset = ifcvf_get_queue_notify_off(&internal->hw, qid) + reg.offset;
1019 ifcvf_get_queue_num(int did, uint32_t *queue_num)
1021 struct internal_list *list;
1023 list = find_internal_resource_by_did(did);
1025 DRV_LOG(ERR, "Invalid device id: %d", did);
1029 *queue_num = list->internal->max_queues;
1035 ifcvf_get_vdpa_features(int did, uint64_t *features)
1037 struct internal_list *list;
1039 list = find_internal_resource_by_did(did);
1041 DRV_LOG(ERR, "Invalid device id: %d", did);
1045 *features = list->internal->features;
1050 #define VDPA_SUPPORTED_PROTOCOL_FEATURES \
1051 (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
1052 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ | \
1053 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD | \
1054 1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER | \
1055 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1057 ifcvf_get_protocol_features(int did __rte_unused, uint64_t *features)
1059 *features = VDPA_SUPPORTED_PROTOCOL_FEATURES;
1063 static struct rte_vdpa_dev_ops ifcvf_ops = {
1064 .get_queue_num = ifcvf_get_queue_num,
1065 .get_features = ifcvf_get_vdpa_features,
1066 .get_protocol_features = ifcvf_get_protocol_features,
1067 .dev_conf = ifcvf_dev_config,
1068 .dev_close = ifcvf_dev_close,
1069 .set_vring_state = NULL,
1070 .set_features = ifcvf_set_features,
1071 .migration_done = NULL,
1072 .get_vfio_group_fd = ifcvf_get_vfio_group_fd,
1073 .get_vfio_device_fd = ifcvf_get_vfio_device_fd,
1074 .get_notify_area = ifcvf_get_notify_area,
1078 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1080 uint16_t *n = extra_args;
1082 if (value == NULL || extra_args == NULL)
1085 *n = (uint16_t)strtoul(value, NULL, 0);
1086 if (*n == USHRT_MAX && errno == ERANGE)
1093 ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1094 struct rte_pci_device *pci_dev)
1097 struct ifcvf_internal *internal = NULL;
1098 struct internal_list *list = NULL;
1100 int sw_fallback_lm = 0;
1101 struct rte_kvargs *kvlist = NULL;
1104 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1107 kvlist = rte_kvargs_parse(pci_dev->device.devargs->args,
1108 ifcvf_valid_arguments);
1112 /* probe only when vdpa mode is specified */
1113 if (rte_kvargs_count(kvlist, IFCVF_VDPA_MODE) == 0) {
1114 rte_kvargs_free(kvlist);
1118 ret = rte_kvargs_process(kvlist, IFCVF_VDPA_MODE, &open_int,
1120 if (ret < 0 || vdpa_mode == 0) {
1121 rte_kvargs_free(kvlist);
1125 list = rte_zmalloc("ifcvf", sizeof(*list), 0);
1129 internal = rte_zmalloc("ifcvf", sizeof(*internal), 0);
1130 if (internal == NULL)
1133 internal->pdev = pci_dev;
1134 rte_spinlock_init(&internal->lock);
1136 if (ifcvf_vfio_setup(internal) < 0) {
1137 DRV_LOG(ERR, "failed to setup device %s", pci_dev->name);
1141 if (ifcvf_init_hw(&internal->hw, internal->pdev) < 0) {
1142 DRV_LOG(ERR, "failed to init device %s", pci_dev->name);
1146 internal->max_queues = IFCVF_MAX_QUEUES;
1147 features = ifcvf_get_features(&internal->hw);
1148 internal->features = (features &
1149 ~(1ULL << VIRTIO_F_IOMMU_PLATFORM)) |
1150 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
1151 (1ULL << VIRTIO_NET_F_CTRL_VQ) |
1152 (1ULL << VIRTIO_NET_F_STATUS) |
1153 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
1154 (1ULL << VHOST_F_LOG_ALL);
1156 internal->dev_addr.pci_addr = pci_dev->addr;
1157 internal->dev_addr.type = PCI_ADDR;
1158 list->internal = internal;
1160 if (rte_kvargs_count(kvlist, IFCVF_SW_FALLBACK_LM)) {
1161 ret = rte_kvargs_process(kvlist, IFCVF_SW_FALLBACK_LM,
1162 &open_int, &sw_fallback_lm);
1166 internal->sw_lm = sw_fallback_lm;
1168 internal->did = rte_vdpa_register_device(&internal->dev_addr,
1170 if (internal->did < 0) {
1171 DRV_LOG(ERR, "failed to register device %s", pci_dev->name);
1175 pthread_mutex_lock(&internal_list_lock);
1176 TAILQ_INSERT_TAIL(&internal_list, list, next);
1177 pthread_mutex_unlock(&internal_list_lock);
1179 rte_atomic32_set(&internal->started, 1);
1180 update_datapath(internal);
1182 rte_kvargs_free(kvlist);
1186 rte_kvargs_free(kvlist);
1193 ifcvf_pci_remove(struct rte_pci_device *pci_dev)
1195 struct ifcvf_internal *internal;
1196 struct internal_list *list;
1198 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1201 list = find_internal_resource_by_dev(pci_dev);
1203 DRV_LOG(ERR, "Invalid device: %s", pci_dev->name);
1207 internal = list->internal;
1208 rte_atomic32_set(&internal->started, 0);
1209 update_datapath(internal);
1211 rte_pci_unmap_device(internal->pdev);
1212 rte_vfio_container_destroy(internal->vfio_container_fd);
1213 rte_vdpa_unregister_device(internal->did);
1215 pthread_mutex_lock(&internal_list_lock);
1216 TAILQ_REMOVE(&internal_list, list, next);
1217 pthread_mutex_unlock(&internal_list_lock);
1226 * IFCVF has the same vendor ID and device ID as virtio net PCI
1227 * device, with its specific subsystem vendor ID and device ID.
1229 static const struct rte_pci_id pci_id_ifcvf_map[] = {
1230 { .class_id = RTE_CLASS_ANY_ID,
1231 .vendor_id = IFCVF_VENDOR_ID,
1232 .device_id = IFCVF_DEVICE_ID,
1233 .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1234 .subsystem_device_id = IFCVF_SUBSYS_DEVICE_ID,
1237 { .vendor_id = 0, /* sentinel */
1241 static struct rte_pci_driver rte_ifcvf_vdpa = {
1242 .id_table = pci_id_ifcvf_map,
1244 .probe = ifcvf_pci_probe,
1245 .remove = ifcvf_pci_remove,
1248 RTE_PMD_REGISTER_PCI(net_ifcvf, rte_ifcvf_vdpa);
1249 RTE_PMD_REGISTER_PCI_TABLE(net_ifcvf, pci_id_ifcvf_map);
1250 RTE_PMD_REGISTER_KMOD_DEP(net_ifcvf, "* vfio-pci");
1252 RTE_INIT(ifcvf_vdpa_init_log)
1254 ifcvf_vdpa_logtype = rte_log_register("pmd.net.ifcvf_vdpa");
1255 if (ifcvf_vdpa_logtype >= 0)
1256 rte_log_set_level(ifcvf_vdpa_logtype, RTE_LOG_NOTICE);