New upstream version 18.08
[deb_dpdk.git] / drivers / bus / vmbus / vmbus_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2018, Microsoft Corporation.
3  * All Rights Reserved.
4  */
5
6 #include <string.h>
7 #include <unistd.h>
8 #include <dirent.h>
9 #include <fcntl.h>
10 #include <sys/queue.h>
11 #include <sys/mman.h>
12
13 #include <rte_log.h>
14 #include <rte_bus.h>
15 #include <rte_eal.h>
16 #include <rte_tailq.h>
17 #include <rte_devargs.h>
18 #include <rte_malloc.h>
19 #include <rte_errno.h>
20 #include <rte_memory.h>
21 #include <rte_bus_vmbus.h>
22
23 #include "private.h"
24
25 int vmbus_logtype_bus;
26 extern struct rte_vmbus_bus rte_vmbus_bus;
27
28 /* map a particular resource from a file */
29 void *
30 vmbus_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
31                    int flags)
32 {
33         void *mapaddr;
34
35         /* Map the memory resource of device */
36         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
37                        MAP_SHARED | flags, fd, offset);
38         if (mapaddr == MAP_FAILED) {
39                 VMBUS_LOG(ERR,
40                           "mmap(%d, %p, %zu, %ld) failed: %s",
41                           fd, requested_addr, size, (long)offset,
42                           strerror(errno));
43         }
44         return mapaddr;
45 }
46
47 /* unmap a particular resource */
48 void
49 vmbus_unmap_resource(void *requested_addr, size_t size)
50 {
51         if (requested_addr == NULL)
52                 return;
53
54         /* Unmap the VMBUS memory resource of device */
55         if (munmap(requested_addr, size)) {
56                 VMBUS_LOG(ERR, "munmap(%p, 0x%lx) failed: %s",
57                         requested_addr, (unsigned long)size,
58                         strerror(errno));
59         } else
60                 VMBUS_LOG(DEBUG, "  VMBUS memory unmapped at %p",
61                           requested_addr);
62 }
63
64 /**
65  * Match the VMBUS driver and device using UUID table
66  *
67  * @param drv
68  *      VMBUS driver from which ID table would be extracted
69  * @param pci_dev
70  *      VMBUS device to match against the driver
71  * @return
72  *      true for successful match
73  *      false for unsuccessful match
74  */
75 static bool
76 vmbus_match(const struct rte_vmbus_driver *dr,
77             const struct rte_vmbus_device *dev)
78 {
79         const rte_uuid_t *id_table;
80
81         for (id_table = dr->id_table; !rte_uuid_is_null(*id_table); ++id_table) {
82                 if (rte_uuid_compare(*id_table, dev->class_id) == 0)
83                         return true;
84         }
85
86         return false;
87 }
88
89 /*
90  * If device ID match, call the devinit() function of the driver.
91  */
92 static int
93 vmbus_probe_one_driver(struct rte_vmbus_driver *dr,
94                        struct rte_vmbus_device *dev)
95 {
96         char guid[RTE_UUID_STRLEN];
97         int ret;
98
99         if (!vmbus_match(dr, dev))
100                 return 1;        /* not supported */
101
102         rte_uuid_unparse(dev->device_id, guid, sizeof(guid));
103         VMBUS_LOG(INFO, "VMBUS device %s on NUMA socket %i",
104                   guid, dev->device.numa_node);
105
106         /* TODO add blacklisted */
107
108         /* map resources for device */
109         ret = rte_vmbus_map_device(dev);
110         if (ret != 0)
111                 return ret;
112
113         /* reference driver structure */
114         dev->driver = dr;
115         dev->device.driver = &dr->driver;
116
117         if (dev->device.numa_node < 0) {
118                 VMBUS_LOG(WARNING, "  Invalid NUMA socket, default to 0");
119                 dev->device.numa_node = 0;
120         }
121
122         /* call the driver probe() function */
123         VMBUS_LOG(INFO, "  probe driver: %s", dr->driver.name);
124         ret = dr->probe(dr, dev);
125         if (ret) {
126                 dev->driver = NULL;
127                 rte_vmbus_unmap_device(dev);
128         }
129
130         return ret;
131 }
132
133 /*
134  * IF device class GUID mathces, call the probe function of
135  * registere drivers for the vmbus device.
136  * Return -1 if initialization failed,
137  * and 1 if no driver found for this device.
138  */
139 static int
140 vmbus_probe_all_drivers(struct rte_vmbus_device *dev)
141 {
142         struct rte_vmbus_driver *dr;
143         int rc;
144
145         /* Check if a driver is already loaded */
146         if (dev->driver != NULL) {
147                 VMBUS_LOG(DEBUG, "VMBUS driver already loaded");
148                 return 0;
149         }
150
151         FOREACH_DRIVER_ON_VMBUS(dr) {
152                 rc = vmbus_probe_one_driver(dr, dev);
153                 if (rc < 0) /* negative is an error */
154                         return -1;
155
156                 if (rc > 0) /* positive driver doesn't support it */
157                         continue;
158
159                 return 0;
160         }
161         return 1;
162 }
163
164 /*
165  * Scan the vmbus, and call the devinit() function for
166  * all registered drivers that have a matching entry in its id_table
167  * for discovered devices.
168  */
169 int
170 rte_vmbus_probe(void)
171 {
172         struct rte_vmbus_device *dev;
173         size_t probed = 0, failed = 0;
174         char ubuf[RTE_UUID_STRLEN];
175
176         FOREACH_DEVICE_ON_VMBUS(dev) {
177                 probed++;
178
179                 rte_uuid_unparse(dev->device_id, ubuf, sizeof(ubuf));
180
181                 /* TODO: add whitelist/blacklist */
182
183                 if (vmbus_probe_all_drivers(dev) < 0) {
184                         VMBUS_LOG(NOTICE,
185                                 "Requested device %s cannot be used", ubuf);
186                         rte_errno = errno;
187                         failed++;
188                 }
189         }
190
191         return (probed && probed == failed) ? -1 : 0;
192 }
193
194 static int
195 vmbus_parse(const char *name, void *addr)
196 {
197         rte_uuid_t guid;
198         int ret;
199
200         ret = rte_uuid_parse(name, guid);
201         if (ret == 0 && addr)
202                 memcpy(addr, &guid, sizeof(guid));
203
204         return ret;
205 }
206
207 /* register vmbus driver */
208 void
209 rte_vmbus_register(struct rte_vmbus_driver *driver)
210 {
211         VMBUS_LOG(DEBUG,
212                 "Registered driver %s", driver->driver.name);
213
214         TAILQ_INSERT_TAIL(&rte_vmbus_bus.driver_list, driver, next);
215         driver->bus = &rte_vmbus_bus;
216 }
217
218 /* unregister vmbus driver */
219 void
220 rte_vmbus_unregister(struct rte_vmbus_driver *driver)
221 {
222         TAILQ_REMOVE(&rte_vmbus_bus.driver_list, driver, next);
223         driver->bus = NULL;
224 }
225
226 /* Add a device to VMBUS bus */
227 void
228 vmbus_add_device(struct rte_vmbus_device *vmbus_dev)
229 {
230         TAILQ_INSERT_TAIL(&rte_vmbus_bus.device_list, vmbus_dev, next);
231 }
232
233 /* Insert a device into a predefined position in VMBUS bus */
234 void
235 vmbus_insert_device(struct rte_vmbus_device *exist_vmbus_dev,
236                       struct rte_vmbus_device *new_vmbus_dev)
237 {
238         TAILQ_INSERT_BEFORE(exist_vmbus_dev, new_vmbus_dev, next);
239 }
240
241 /* Remove a device from VMBUS bus */
242 void
243 vmbus_remove_device(struct rte_vmbus_device *vmbus_dev)
244 {
245         TAILQ_REMOVE(&rte_vmbus_bus.device_list, vmbus_dev, next);
246 }
247
248 /* VMBUS doesn't support hotplug */
249 static struct rte_device *
250 vmbus_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
251                   const void *data)
252 {
253         struct rte_vmbus_device *dev;
254
255         FOREACH_DEVICE_ON_VMBUS(dev) {
256                 if (start && &dev->device == start) {
257                         start = NULL;
258                         continue;
259                 }
260                 if (cmp(&dev->device, data) == 0)
261                         return &dev->device;
262         }
263
264         return NULL;
265 }
266
267
268 struct rte_vmbus_bus rte_vmbus_bus = {
269         .bus = {
270                 .scan = rte_vmbus_scan,
271                 .probe = rte_vmbus_probe,
272                 .find_device = vmbus_find_device,
273                 .parse = vmbus_parse,
274         },
275         .device_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.device_list),
276         .driver_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.driver_list),
277 };
278
279 RTE_REGISTER_BUS(vmbus, rte_vmbus_bus.bus);
280
281 RTE_INIT(vmbus_init_log)
282 {
283         vmbus_logtype_bus = rte_log_register("bus.vmbus");
284         if (vmbus_logtype_bus >= 0)
285                 rte_log_set_level(vmbus_logtype_bus, RTE_LOG_NOTICE);
286 }