New upstream version 18.11-rc1
[deb_dpdk.git] / lib / librte_vhost / vdpa.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 /**
6  * @file
7  *
8  * Device specific vhost lib
9  */
10
11 #include <stdbool.h>
12
13 #include <rte_malloc.h>
14 #include "rte_vdpa.h"
15 #include "vhost.h"
16
17 static struct rte_vdpa_device *vdpa_devices[MAX_VHOST_DEVICE];
18 static uint32_t vdpa_device_num;
19
20 static bool
21 is_same_vdpa_device(struct rte_vdpa_dev_addr *a,
22                 struct rte_vdpa_dev_addr *b)
23 {
24         bool ret = true;
25
26         if (a->type != b->type)
27                 return false;
28
29         switch (a->type) {
30         case PCI_ADDR:
31                 if (a->pci_addr.domain != b->pci_addr.domain ||
32                                 a->pci_addr.bus != b->pci_addr.bus ||
33                                 a->pci_addr.devid != b->pci_addr.devid ||
34                                 a->pci_addr.function != b->pci_addr.function)
35                         ret = false;
36                 break;
37         default:
38                 break;
39         }
40
41         return ret;
42 }
43
44 int
45 rte_vdpa_register_device(struct rte_vdpa_dev_addr *addr,
46                 struct rte_vdpa_dev_ops *ops)
47 {
48         struct rte_vdpa_device *dev;
49         char device_name[MAX_VDPA_NAME_LEN];
50         int i;
51
52         if (vdpa_device_num >= MAX_VHOST_DEVICE)
53                 return -1;
54
55         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
56                 dev = vdpa_devices[i];
57                 if (dev && is_same_vdpa_device(&dev->addr, addr))
58                         return -1;
59         }
60
61         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
62                 if (vdpa_devices[i] == NULL)
63                         break;
64         }
65
66         sprintf(device_name, "vdpa-dev-%d", i);
67         dev = rte_zmalloc(device_name, sizeof(struct rte_vdpa_device),
68                         RTE_CACHE_LINE_SIZE);
69         if (!dev)
70                 return -1;
71
72         memcpy(&dev->addr, addr, sizeof(struct rte_vdpa_dev_addr));
73         dev->ops = ops;
74         vdpa_devices[i] = dev;
75         vdpa_device_num++;
76
77         return i;
78 }
79
80 int
81 rte_vdpa_unregister_device(int did)
82 {
83         if (did < 0 || did >= MAX_VHOST_DEVICE || vdpa_devices[did] == NULL)
84                 return -1;
85
86         rte_free(vdpa_devices[did]);
87         vdpa_devices[did] = NULL;
88         vdpa_device_num--;
89
90         return did;
91 }
92
93 int
94 rte_vdpa_find_device_id(struct rte_vdpa_dev_addr *addr)
95 {
96         struct rte_vdpa_device *dev;
97         int i;
98
99         for (i = 0; i < MAX_VHOST_DEVICE; ++i) {
100                 dev = vdpa_devices[i];
101                 if (dev && is_same_vdpa_device(&dev->addr, addr))
102                         return i;
103         }
104
105         return -1;
106 }
107
108 struct rte_vdpa_device *
109 rte_vdpa_get_device(int did)
110 {
111         if (did < 0 || did >= MAX_VHOST_DEVICE)
112                 return NULL;
113
114         return vdpa_devices[did];
115 }
116
117 int
118 rte_vdpa_get_device_num(void)
119 {
120         return vdpa_device_num;
121 }