net/ifc: fix build with Linux < 3.19
[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
12 #include <rte_malloc.h>
13 #include <rte_memory.h>
14 #include <rte_bus_pci.h>
15 #include <rte_vhost.h>
16 #include <rte_vdpa.h>
17 #include <rte_vfio.h>
18 #include <rte_spinlock.h>
19 #include <rte_log.h>
20
21 #include "base/ifcvf.h"
22
23 #define DRV_LOG(level, fmt, args...) \
24         rte_log(RTE_LOG_ ## level, ifcvf_vdpa_logtype, \
25                 "%s(): " fmt "\n", __func__, ##args)
26
27 #ifndef PAGE_SIZE
28 #define PAGE_SIZE 4096
29 #endif
30
31 static int ifcvf_vdpa_logtype;
32
33 struct ifcvf_internal {
34         struct rte_vdpa_dev_addr dev_addr;
35         struct rte_pci_device *pdev;
36         struct ifcvf_hw hw;
37         int vfio_container_fd;
38         int vfio_group_fd;
39         int vfio_dev_fd;
40         pthread_t tid;  /* thread for notify relay */
41         int epfd;
42         int vid;
43         int did;
44         uint16_t max_queues;
45         uint64_t features;
46         rte_atomic32_t started;
47         rte_atomic32_t dev_attached;
48         rte_atomic32_t running;
49         rte_spinlock_t lock;
50 };
51
52 struct internal_list {
53         TAILQ_ENTRY(internal_list) next;
54         struct ifcvf_internal *internal;
55 };
56
57 TAILQ_HEAD(internal_list_head, internal_list);
58 static struct internal_list_head internal_list =
59         TAILQ_HEAD_INITIALIZER(internal_list);
60
61 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
62
63 static struct internal_list *
64 find_internal_resource_by_did(int did)
65 {
66         int found = 0;
67         struct internal_list *list;
68
69         pthread_mutex_lock(&internal_list_lock);
70
71         TAILQ_FOREACH(list, &internal_list, next) {
72                 if (did == list->internal->did) {
73                         found = 1;
74                         break;
75                 }
76         }
77
78         pthread_mutex_unlock(&internal_list_lock);
79
80         if (!found)
81                 return NULL;
82
83         return list;
84 }
85
86 static struct internal_list *
87 find_internal_resource_by_dev(struct rte_pci_device *pdev)
88 {
89         int found = 0;
90         struct internal_list *list;
91
92         pthread_mutex_lock(&internal_list_lock);
93
94         TAILQ_FOREACH(list, &internal_list, next) {
95                 if (pdev == list->internal->pdev) {
96                         found = 1;
97                         break;
98                 }
99         }
100
101         pthread_mutex_unlock(&internal_list_lock);
102
103         if (!found)
104                 return NULL;
105
106         return list;
107 }
108
109 static int
110 ifcvf_vfio_setup(struct ifcvf_internal *internal)
111 {
112         struct rte_pci_device *dev = internal->pdev;
113         char devname[RTE_DEV_NAME_MAX_LEN] = {0};
114         int iommu_group_num;
115         int ret = 0;
116         int i;
117
118         internal->vfio_dev_fd = -1;
119         internal->vfio_group_fd = -1;
120         internal->vfio_container_fd = -1;
121
122         rte_pci_device_name(&dev->addr, devname, RTE_DEV_NAME_MAX_LEN);
123         rte_vfio_get_group_num(rte_pci_get_sysfs_path(), devname,
124                         &iommu_group_num);
125
126         internal->vfio_container_fd = rte_vfio_container_create();
127         if (internal->vfio_container_fd < 0)
128                 return -1;
129
130         internal->vfio_group_fd = rte_vfio_container_group_bind(
131                         internal->vfio_container_fd, iommu_group_num);
132         if (internal->vfio_group_fd < 0)
133                 goto err;
134
135         if (rte_pci_map_device(dev))
136                 goto err;
137
138         internal->vfio_dev_fd = dev->intr_handle.vfio_dev_fd;
139
140         for (i = 0; i < RTE_MIN(PCI_MAX_RESOURCE, IFCVF_PCI_MAX_RESOURCE);
141                         i++) {
142                 internal->hw.mem_resource[i].addr =
143                         internal->pdev->mem_resource[i].addr;
144                 internal->hw.mem_resource[i].phys_addr =
145                         internal->pdev->mem_resource[i].phys_addr;
146                 internal->hw.mem_resource[i].len =
147                         internal->pdev->mem_resource[i].len;
148         }
149         ret = ifcvf_init_hw(&internal->hw, internal->pdev);
150
151         return ret;
152
153 err:
154         rte_vfio_container_destroy(internal->vfio_container_fd);
155         return -1;
156 }
157
158 static int
159 ifcvf_dma_map(struct ifcvf_internal *internal, int do_map)
160 {
161         uint32_t i;
162         int ret;
163         struct rte_vhost_memory *mem = NULL;
164         int vfio_container_fd;
165
166         ret = rte_vhost_get_mem_table(internal->vid, &mem);
167         if (ret < 0) {
168                 DRV_LOG(ERR, "failed to get VM memory layout.");
169                 goto exit;
170         }
171
172         vfio_container_fd = internal->vfio_container_fd;
173
174         for (i = 0; i < mem->nregions; i++) {
175                 struct rte_vhost_mem_region *reg;
176
177                 reg = &mem->regions[i];
178                 DRV_LOG(INFO, "%s, region %u: HVA 0x%" PRIx64 ", "
179                         "GPA 0x%" PRIx64 ", size 0x%" PRIx64 ".",
180                         do_map ? "DMA map" : "DMA unmap", i,
181                         reg->host_user_addr, reg->guest_phys_addr, reg->size);
182
183                 if (do_map) {
184                         ret = rte_vfio_container_dma_map(vfio_container_fd,
185                                 reg->host_user_addr, reg->guest_phys_addr,
186                                 reg->size);
187                         if (ret < 0) {
188                                 DRV_LOG(ERR, "DMA map failed.");
189                                 goto exit;
190                         }
191                 } else {
192                         ret = rte_vfio_container_dma_unmap(vfio_container_fd,
193                                 reg->host_user_addr, reg->guest_phys_addr,
194                                 reg->size);
195                         if (ret < 0) {
196                                 DRV_LOG(ERR, "DMA unmap failed.");
197                                 goto exit;
198                         }
199                 }
200         }
201
202 exit:
203         if (mem)
204                 free(mem);
205         return ret;
206 }
207
208 static uint64_t
209 qva_to_gpa(int vid, uint64_t qva)
210 {
211         struct rte_vhost_memory *mem = NULL;
212         struct rte_vhost_mem_region *reg;
213         uint32_t i;
214         uint64_t gpa = 0;
215
216         if (rte_vhost_get_mem_table(vid, &mem) < 0)
217                 goto exit;
218
219         for (i = 0; i < mem->nregions; i++) {
220                 reg = &mem->regions[i];
221
222                 if (qva >= reg->host_user_addr &&
223                                 qva < reg->host_user_addr + reg->size) {
224                         gpa = qva - reg->host_user_addr + reg->guest_phys_addr;
225                         break;
226                 }
227         }
228
229 exit:
230         if (mem)
231                 free(mem);
232         return gpa;
233 }
234
235 static int
236 vdpa_ifcvf_start(struct ifcvf_internal *internal)
237 {
238         struct ifcvf_hw *hw = &internal->hw;
239         int i, nr_vring;
240         int vid;
241         struct rte_vhost_vring vq;
242         uint64_t gpa;
243
244         vid = internal->vid;
245         nr_vring = rte_vhost_get_vring_num(vid);
246         rte_vhost_get_negotiated_features(vid, &hw->req_features);
247
248         for (i = 0; i < nr_vring; i++) {
249                 rte_vhost_get_vhost_vring(vid, i, &vq);
250                 gpa = qva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
251                 if (gpa == 0) {
252                         DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
253                         return -1;
254                 }
255                 hw->vring[i].desc = gpa;
256
257                 gpa = qva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
258                 if (gpa == 0) {
259                         DRV_LOG(ERR, "Fail to get GPA for available ring.");
260                         return -1;
261                 }
262                 hw->vring[i].avail = gpa;
263
264                 gpa = qva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
265                 if (gpa == 0) {
266                         DRV_LOG(ERR, "Fail to get GPA for used ring.");
267                         return -1;
268                 }
269                 hw->vring[i].used = gpa;
270
271                 hw->vring[i].size = vq.size;
272                 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
273                                 &hw->vring[i].last_used_idx);
274         }
275         hw->nr_vring = i;
276
277         return ifcvf_start_hw(&internal->hw);
278 }
279
280 static void
281 ifcvf_used_ring_log(struct ifcvf_hw *hw, uint32_t queue, uint8_t *log_buf)
282 {
283         uint32_t i, size;
284         uint64_t pfn;
285
286         pfn = hw->vring[queue].used / PAGE_SIZE;
287         size = hw->vring[queue].size * sizeof(struct vring_used_elem) +
288                         sizeof(uint16_t) * 3;
289
290         for (i = 0; i <= size / PAGE_SIZE; i++)
291                 __sync_fetch_and_or_8(&log_buf[(pfn + i) / 8],
292                                 1 << ((pfn + i) % 8));
293 }
294
295 static void
296 vdpa_ifcvf_stop(struct ifcvf_internal *internal)
297 {
298         struct ifcvf_hw *hw = &internal->hw;
299         uint32_t i;
300         int vid;
301         uint64_t features;
302         uint64_t log_base, log_size;
303         uint8_t *log_buf;
304
305         vid = internal->vid;
306         ifcvf_stop_hw(hw);
307
308         for (i = 0; i < hw->nr_vring; i++)
309                 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
310                                 hw->vring[i].last_used_idx);
311
312         rte_vhost_get_negotiated_features(vid, &features);
313         if (RTE_VHOST_NEED_LOG(features)) {
314                 ifcvf_disable_logging(hw);
315                 rte_vhost_get_log_base(internal->vid, &log_base, &log_size);
316                 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
317                                 log_base, IFCVF_LOG_BASE, log_size);
318                 /*
319                  * IFCVF marks dirty memory pages for only packet buffer,
320                  * SW helps to mark the used ring as dirty after device stops.
321                  */
322                 log_buf = (uint8_t *)(uintptr_t)log_base;
323                 for (i = 0; i < hw->nr_vring; i++)
324                         ifcvf_used_ring_log(hw, i, log_buf);
325         }
326 }
327
328 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
329                 sizeof(int) * (IFCVF_MAX_QUEUES * 2 + 1))
330 static int
331 vdpa_enable_vfio_intr(struct ifcvf_internal *internal)
332 {
333         int ret;
334         uint32_t i, nr_vring;
335         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
336         struct vfio_irq_set *irq_set;
337         int *fd_ptr;
338         struct rte_vhost_vring vring;
339
340         nr_vring = rte_vhost_get_vring_num(internal->vid);
341
342         irq_set = (struct vfio_irq_set *)irq_set_buf;
343         irq_set->argsz = sizeof(irq_set_buf);
344         irq_set->count = nr_vring + 1;
345         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
346                          VFIO_IRQ_SET_ACTION_TRIGGER;
347         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
348         irq_set->start = 0;
349         fd_ptr = (int *)&irq_set->data;
350         fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = internal->pdev->intr_handle.fd;
351
352         for (i = 0; i < nr_vring; i++) {
353                 rte_vhost_get_vhost_vring(internal->vid, i, &vring);
354                 fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = vring.callfd;
355         }
356
357         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
358         if (ret) {
359                 DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s",
360                                 strerror(errno));
361                 return -1;
362         }
363
364         return 0;
365 }
366
367 static int
368 vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
369 {
370         int ret;
371         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
372         struct vfio_irq_set *irq_set;
373
374         irq_set = (struct vfio_irq_set *)irq_set_buf;
375         irq_set->argsz = sizeof(irq_set_buf);
376         irq_set->count = 0;
377         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
378         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
379         irq_set->start = 0;
380
381         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
382         if (ret) {
383                 DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s",
384                                 strerror(errno));
385                 return -1;
386         }
387
388         return 0;
389 }
390
391 static void *
392 notify_relay(void *arg)
393 {
394         int i, kickfd, epfd, nfds = 0;
395         uint32_t qid, q_num;
396         struct epoll_event events[IFCVF_MAX_QUEUES * 2];
397         struct epoll_event ev;
398         uint64_t buf;
399         int nbytes;
400         struct rte_vhost_vring vring;
401         struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
402         struct ifcvf_hw *hw = &internal->hw;
403
404         q_num = rte_vhost_get_vring_num(internal->vid);
405
406         epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
407         if (epfd < 0) {
408                 DRV_LOG(ERR, "failed to create epoll instance.");
409                 return NULL;
410         }
411         internal->epfd = epfd;
412
413         for (qid = 0; qid < q_num; qid++) {
414                 ev.events = EPOLLIN | EPOLLPRI;
415                 rte_vhost_get_vhost_vring(internal->vid, qid, &vring);
416                 ev.data.u64 = qid | (uint64_t)vring.kickfd << 32;
417                 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
418                         DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
419                         return NULL;
420                 }
421         }
422
423         for (;;) {
424                 nfds = epoll_wait(epfd, events, q_num, -1);
425                 if (nfds < 0) {
426                         if (errno == EINTR)
427                                 continue;
428                         DRV_LOG(ERR, "epoll_wait return fail\n");
429                         return NULL;
430                 }
431
432                 for (i = 0; i < nfds; i++) {
433                         qid = events[i].data.u32;
434                         kickfd = (uint32_t)(events[i].data.u64 >> 32);
435                         do {
436                                 nbytes = read(kickfd, &buf, 8);
437                                 if (nbytes < 0) {
438                                         if (errno == EINTR ||
439                                             errno == EWOULDBLOCK ||
440                                             errno == EAGAIN)
441                                                 continue;
442                                         DRV_LOG(INFO, "Error reading "
443                                                 "kickfd: %s",
444                                                 strerror(errno));
445                                 }
446                                 break;
447                         } while (1);
448
449                         ifcvf_notify_queue(hw, qid);
450                 }
451         }
452
453         return NULL;
454 }
455
456 static int
457 setup_notify_relay(struct ifcvf_internal *internal)
458 {
459         int ret;
460
461         ret = pthread_create(&internal->tid, NULL, notify_relay,
462                         (void *)internal);
463         if (ret) {
464                 DRV_LOG(ERR, "failed to create notify relay pthread.");
465                 return -1;
466         }
467         return 0;
468 }
469
470 static int
471 unset_notify_relay(struct ifcvf_internal *internal)
472 {
473         void *status;
474
475         if (internal->tid) {
476                 pthread_cancel(internal->tid);
477                 pthread_join(internal->tid, &status);
478         }
479         internal->tid = 0;
480
481         if (internal->epfd >= 0)
482                 close(internal->epfd);
483         internal->epfd = -1;
484
485         return 0;
486 }
487
488 static int
489 update_datapath(struct ifcvf_internal *internal)
490 {
491         int ret;
492
493         rte_spinlock_lock(&internal->lock);
494
495         if (!rte_atomic32_read(&internal->running) &&
496             (rte_atomic32_read(&internal->started) &&
497              rte_atomic32_read(&internal->dev_attached))) {
498                 ret = ifcvf_dma_map(internal, 1);
499                 if (ret)
500                         goto err;
501
502                 ret = vdpa_enable_vfio_intr(internal);
503                 if (ret)
504                         goto err;
505
506                 ret = vdpa_ifcvf_start(internal);
507                 if (ret)
508                         goto err;
509
510                 ret = setup_notify_relay(internal);
511                 if (ret)
512                         goto err;
513
514                 rte_atomic32_set(&internal->running, 1);
515         } else if (rte_atomic32_read(&internal->running) &&
516                    (!rte_atomic32_read(&internal->started) ||
517                     !rte_atomic32_read(&internal->dev_attached))) {
518                 ret = unset_notify_relay(internal);
519                 if (ret)
520                         goto err;
521
522                 vdpa_ifcvf_stop(internal);
523
524                 ret = vdpa_disable_vfio_intr(internal);
525                 if (ret)
526                         goto err;
527
528                 ret = ifcvf_dma_map(internal, 0);
529                 if (ret)
530                         goto err;
531
532                 rte_atomic32_set(&internal->running, 0);
533         }
534
535         rte_spinlock_unlock(&internal->lock);
536         return 0;
537 err:
538         rte_spinlock_unlock(&internal->lock);
539         return ret;
540 }
541
542 static int
543 ifcvf_dev_config(int vid)
544 {
545         int did;
546         struct internal_list *list;
547         struct ifcvf_internal *internal;
548
549         did = rte_vhost_get_vdpa_device_id(vid);
550         list = find_internal_resource_by_did(did);
551         if (list == NULL) {
552                 DRV_LOG(ERR, "Invalid device id: %d", did);
553                 return -1;
554         }
555
556         internal = list->internal;
557         internal->vid = vid;
558         rte_atomic32_set(&internal->dev_attached, 1);
559         update_datapath(internal);
560
561         return 0;
562 }
563
564 static int
565 ifcvf_dev_close(int vid)
566 {
567         int did;
568         struct internal_list *list;
569         struct ifcvf_internal *internal;
570
571         did = rte_vhost_get_vdpa_device_id(vid);
572         list = find_internal_resource_by_did(did);
573         if (list == NULL) {
574                 DRV_LOG(ERR, "Invalid device id: %d", did);
575                 return -1;
576         }
577
578         internal = list->internal;
579         rte_atomic32_set(&internal->dev_attached, 0);
580         update_datapath(internal);
581
582         return 0;
583 }
584
585 static int
586 ifcvf_set_features(int vid)
587 {
588         uint64_t features;
589         int did;
590         struct internal_list *list;
591         struct ifcvf_internal *internal;
592         uint64_t log_base, log_size;
593
594         did = rte_vhost_get_vdpa_device_id(vid);
595         list = find_internal_resource_by_did(did);
596         if (list == NULL) {
597                 DRV_LOG(ERR, "Invalid device id: %d", did);
598                 return -1;
599         }
600
601         internal = list->internal;
602         rte_vhost_get_negotiated_features(vid, &features);
603
604         if (RTE_VHOST_NEED_LOG(features)) {
605                 rte_vhost_get_log_base(vid, &log_base, &log_size);
606                 rte_vfio_container_dma_map(internal->vfio_container_fd,
607                                 log_base, IFCVF_LOG_BASE, log_size);
608                 ifcvf_enable_logging(&internal->hw, IFCVF_LOG_BASE, log_size);
609         }
610
611         return 0;
612 }
613
614 static int
615 ifcvf_get_vfio_group_fd(int vid)
616 {
617         int did;
618         struct internal_list *list;
619
620         did = rte_vhost_get_vdpa_device_id(vid);
621         list = find_internal_resource_by_did(did);
622         if (list == NULL) {
623                 DRV_LOG(ERR, "Invalid device id: %d", did);
624                 return -1;
625         }
626
627         return list->internal->vfio_group_fd;
628 }
629
630 static int
631 ifcvf_get_vfio_device_fd(int vid)
632 {
633         int did;
634         struct internal_list *list;
635
636         did = rte_vhost_get_vdpa_device_id(vid);
637         list = find_internal_resource_by_did(did);
638         if (list == NULL) {
639                 DRV_LOG(ERR, "Invalid device id: %d", did);
640                 return -1;
641         }
642
643         return list->internal->vfio_dev_fd;
644 }
645
646 static int
647 ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
648 {
649         int did;
650         struct internal_list *list;
651         struct ifcvf_internal *internal;
652         struct vfio_region_info reg = { .argsz = sizeof(reg) };
653         int ret;
654
655         did = rte_vhost_get_vdpa_device_id(vid);
656         list = find_internal_resource_by_did(did);
657         if (list == NULL) {
658                 DRV_LOG(ERR, "Invalid device id: %d", did);
659                 return -1;
660         }
661
662         internal = list->internal;
663
664         reg.index = ifcvf_get_notify_region(&internal->hw);
665         ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
666         if (ret) {
667                 DRV_LOG(ERR, "Get not get device region info: %s",
668                                 strerror(errno));
669                 return -1;
670         }
671
672         *offset = ifcvf_get_queue_notify_off(&internal->hw, qid) + reg.offset;
673         *size = 0x1000;
674
675         return 0;
676 }
677
678 static int
679 ifcvf_get_queue_num(int did, uint32_t *queue_num)
680 {
681         struct internal_list *list;
682
683         list = find_internal_resource_by_did(did);
684         if (list == NULL) {
685                 DRV_LOG(ERR, "Invalid device id: %d", did);
686                 return -1;
687         }
688
689         *queue_num = list->internal->max_queues;
690
691         return 0;
692 }
693
694 static int
695 ifcvf_get_vdpa_features(int did, uint64_t *features)
696 {
697         struct internal_list *list;
698
699         list = find_internal_resource_by_did(did);
700         if (list == NULL) {
701                 DRV_LOG(ERR, "Invalid device id: %d", did);
702                 return -1;
703         }
704
705         *features = list->internal->features;
706
707         return 0;
708 }
709
710 #define VDPA_SUPPORTED_PROTOCOL_FEATURES \
711                 (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
712                  1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ | \
713                  1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD | \
714                  1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER | \
715                  1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD)
716 static int
717 ifcvf_get_protocol_features(int did __rte_unused, uint64_t *features)
718 {
719         *features = VDPA_SUPPORTED_PROTOCOL_FEATURES;
720         return 0;
721 }
722
723 struct rte_vdpa_dev_ops ifcvf_ops = {
724         .get_queue_num = ifcvf_get_queue_num,
725         .get_features = ifcvf_get_vdpa_features,
726         .get_protocol_features = ifcvf_get_protocol_features,
727         .dev_conf = ifcvf_dev_config,
728         .dev_close = ifcvf_dev_close,
729         .set_vring_state = NULL,
730         .set_features = ifcvf_set_features,
731         .migration_done = NULL,
732         .get_vfio_group_fd = ifcvf_get_vfio_group_fd,
733         .get_vfio_device_fd = ifcvf_get_vfio_device_fd,
734         .get_notify_area = ifcvf_get_notify_area,
735 };
736
737 static int
738 ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
739                 struct rte_pci_device *pci_dev)
740 {
741         uint64_t features;
742         struct ifcvf_internal *internal = NULL;
743         struct internal_list *list = NULL;
744
745         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
746                 return 0;
747
748         list = rte_zmalloc("ifcvf", sizeof(*list), 0);
749         if (list == NULL)
750                 goto error;
751
752         internal = rte_zmalloc("ifcvf", sizeof(*internal), 0);
753         if (internal == NULL)
754                 goto error;
755
756         internal->pdev = pci_dev;
757         rte_spinlock_init(&internal->lock);
758         if (ifcvf_vfio_setup(internal) < 0)
759                 return -1;
760
761         internal->max_queues = IFCVF_MAX_QUEUES;
762         features = ifcvf_get_features(&internal->hw);
763         internal->features = (features &
764                 ~(1ULL << VIRTIO_F_IOMMU_PLATFORM)) |
765                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
766                 (1ULL << VIRTIO_NET_F_CTRL_VQ) |
767                 (1ULL << VIRTIO_NET_F_STATUS) |
768                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
769                 (1ULL << VHOST_F_LOG_ALL);
770
771         internal->dev_addr.pci_addr = pci_dev->addr;
772         internal->dev_addr.type = PCI_ADDR;
773         list->internal = internal;
774
775         pthread_mutex_lock(&internal_list_lock);
776         TAILQ_INSERT_TAIL(&internal_list, list, next);
777         pthread_mutex_unlock(&internal_list_lock);
778
779         internal->did = rte_vdpa_register_device(&internal->dev_addr,
780                                 &ifcvf_ops);
781         if (internal->did < 0)
782                 goto error;
783
784         rte_atomic32_set(&internal->started, 1);
785         update_datapath(internal);
786
787         return 0;
788
789 error:
790         rte_free(list);
791         rte_free(internal);
792         return -1;
793 }
794
795 static int
796 ifcvf_pci_remove(struct rte_pci_device *pci_dev)
797 {
798         struct ifcvf_internal *internal;
799         struct internal_list *list;
800
801         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
802                 return 0;
803
804         list = find_internal_resource_by_dev(pci_dev);
805         if (list == NULL) {
806                 DRV_LOG(ERR, "Invalid device: %s", pci_dev->name);
807                 return -1;
808         }
809
810         internal = list->internal;
811         rte_atomic32_set(&internal->started, 0);
812         update_datapath(internal);
813
814         rte_pci_unmap_device(internal->pdev);
815         rte_vfio_container_destroy(internal->vfio_container_fd);
816         rte_vdpa_unregister_device(internal->did);
817
818         pthread_mutex_lock(&internal_list_lock);
819         TAILQ_REMOVE(&internal_list, list, next);
820         pthread_mutex_unlock(&internal_list_lock);
821
822         rte_free(list);
823         rte_free(internal);
824
825         return 0;
826 }
827
828 /*
829  * IFCVF has the same vendor ID and device ID as virtio net PCI
830  * device, with its specific subsystem vendor ID and device ID.
831  */
832 static const struct rte_pci_id pci_id_ifcvf_map[] = {
833         { .class_id = RTE_CLASS_ANY_ID,
834           .vendor_id = IFCVF_VENDOR_ID,
835           .device_id = IFCVF_DEVICE_ID,
836           .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
837           .subsystem_device_id = IFCVF_SUBSYS_DEVICE_ID,
838         },
839
840         { .vendor_id = 0, /* sentinel */
841         },
842 };
843
844 static struct rte_pci_driver rte_ifcvf_vdpa = {
845         .id_table = pci_id_ifcvf_map,
846         .drv_flags = 0,
847         .probe = ifcvf_pci_probe,
848         .remove = ifcvf_pci_remove,
849 };
850
851 RTE_PMD_REGISTER_PCI(net_ifcvf, rte_ifcvf_vdpa);
852 RTE_PMD_REGISTER_PCI_TABLE(net_ifcvf, pci_id_ifcvf_map);
853 RTE_PMD_REGISTER_KMOD_DEP(net_ifcvf, "* vfio-pci");
854
855 RTE_INIT(ifcvf_vdpa_init_log)
856 {
857         ifcvf_vdpa_logtype = rte_log_register("pmd.net.ifcvf_vdpa");
858         if (ifcvf_vdpa_logtype >= 0)
859                 rte_log_set_level(ifcvf_vdpa_logtype, RTE_LOG_NOTICE);
860 }