net/bnxt: add SRAM manager model
[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_lcore.h>
19 #include <rte_malloc.h>
20 #include <rte_errno.h>
21 #include <rte_memory.h>
22 #include <rte_bus_vmbus.h>
23
24 #include "private.h"
25
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  * If device ID match, call the devinit() function of the driver.
90  */
91 static int
92 vmbus_probe_one_driver(struct rte_vmbus_driver *dr,
93                        struct rte_vmbus_device *dev)
94 {
95         char guid[RTE_UUID_STRLEN];
96         int ret;
97
98         if (!vmbus_match(dr, dev))
99                 return 1;        /* not supported */
100
101         rte_uuid_unparse(dev->device_id, guid, sizeof(guid));
102         VMBUS_LOG(INFO, "VMBUS device %s on NUMA socket %i",
103                   guid, dev->device.numa_node);
104
105         /* TODO add block/allow logic */
106
107         /* map resources for device */
108         ret = rte_vmbus_map_device(dev);
109         if (ret != 0)
110                 return ret;
111
112         /* reference driver structure */
113         dev->driver = dr;
114
115         if (dev->device.numa_node < 0) {
116                 if (rte_socket_count() > 1)
117                         VMBUS_LOG(INFO, "Device %s is not NUMA-aware, defaulting socket to 0",
118                                         guid);
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         } else {
129                 dev->device.driver = &dr->driver;
130         }
131
132         return ret;
133 }
134
135 /*
136  * If device class GUID matches, call the probe function of
137  * registere drivers for the vmbus device.
138  * Return -1 if initialization failed,
139  * and 1 if no driver found for this device.
140  */
141 static int
142 vmbus_probe_all_drivers(struct rte_vmbus_device *dev)
143 {
144         struct rte_vmbus_driver *dr;
145         int rc;
146
147         /* Check if a driver is already loaded */
148         if (rte_dev_is_probed(&dev->device)) {
149                 VMBUS_LOG(DEBUG, "VMBUS driver already loaded");
150                 return 0;
151         }
152
153         FOREACH_DRIVER_ON_VMBUS(dr) {
154                 rc = vmbus_probe_one_driver(dr, dev);
155                 if (rc < 0) /* negative is an error */
156                         return -1;
157
158                 if (rc > 0) /* positive driver doesn't support it */
159                         continue;
160
161                 return 0;
162         }
163         return 1;
164 }
165
166 /*
167  * Scan the vmbus, and call the devinit() function for
168  * all registered drivers that have a matching entry in its id_table
169  * for discovered devices.
170  */
171 int
172 rte_vmbus_probe(void)
173 {
174         struct rte_vmbus_device *dev;
175         size_t probed = 0, failed = 0;
176         char ubuf[RTE_UUID_STRLEN];
177
178         FOREACH_DEVICE_ON_VMBUS(dev) {
179                 probed++;
180
181                 rte_uuid_unparse(dev->device_id, ubuf, sizeof(ubuf));
182
183                 /* TODO: add allowlist/blocklist */
184
185                 if (vmbus_probe_all_drivers(dev) < 0) {
186                         VMBUS_LOG(NOTICE,
187                                 "Requested device %s cannot be used", ubuf);
188                         rte_errno = errno;
189                         failed++;
190                 }
191         }
192
193         return (probed && probed == failed) ? -1 : 0;
194 }
195
196 static int
197 vmbus_parse(const char *name, void *addr)
198 {
199         rte_uuid_t guid;
200         int ret;
201
202         ret = rte_uuid_parse(name, guid);
203         if (ret == 0 && addr)
204                 memcpy(addr, &guid, sizeof(guid));
205
206         return ret;
207 }
208
209 /*
210  * scan for matching device args on command line
211  * example:
212  *      -a 'vmbus:635a7ae3-091e-4410-ad59-667c4f8c04c3,latency=20'
213  */
214 struct rte_devargs *
215 vmbus_devargs_lookup(struct rte_vmbus_device *dev)
216 {
217         struct rte_devargs *devargs;
218         rte_uuid_t addr;
219
220         RTE_EAL_DEVARGS_FOREACH("vmbus", devargs) {
221                 vmbus_parse(devargs->name, &addr);
222
223                 if (rte_uuid_compare(dev->device_id, addr) == 0)
224                         return devargs;
225         }
226         return NULL;
227
228 }
229
230 /* register vmbus driver */
231 void
232 rte_vmbus_register(struct rte_vmbus_driver *driver)
233 {
234         VMBUS_LOG(DEBUG,
235                 "Registered driver %s", driver->driver.name);
236
237         TAILQ_INSERT_TAIL(&rte_vmbus_bus.driver_list, driver, next);
238         driver->bus = &rte_vmbus_bus;
239 }
240
241 /* unregister vmbus driver */
242 void
243 rte_vmbus_unregister(struct rte_vmbus_driver *driver)
244 {
245         TAILQ_REMOVE(&rte_vmbus_bus.driver_list, driver, next);
246         driver->bus = NULL;
247 }
248
249 /* Add a device to VMBUS bus */
250 void
251 vmbus_add_device(struct rte_vmbus_device *vmbus_dev)
252 {
253         TAILQ_INSERT_TAIL(&rte_vmbus_bus.device_list, vmbus_dev, next);
254 }
255
256 /* Insert a device into a predefined position in VMBUS bus */
257 void
258 vmbus_insert_device(struct rte_vmbus_device *exist_vmbus_dev,
259                       struct rte_vmbus_device *new_vmbus_dev)
260 {
261         TAILQ_INSERT_BEFORE(exist_vmbus_dev, new_vmbus_dev, next);
262 }
263
264 /* Remove a device from VMBUS bus */
265 void
266 vmbus_remove_device(struct rte_vmbus_device *vmbus_dev)
267 {
268         TAILQ_REMOVE(&rte_vmbus_bus.device_list, vmbus_dev, next);
269 }
270
271 /* VMBUS doesn't support hotplug */
272 static struct rte_device *
273 vmbus_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
274                   const void *data)
275 {
276         struct rte_vmbus_device *dev;
277
278         FOREACH_DEVICE_ON_VMBUS(dev) {
279                 if (start && &dev->device == start) {
280                         start = NULL;
281                         continue;
282                 }
283                 if (cmp(&dev->device, data) == 0)
284                         return &dev->device;
285         }
286
287         return NULL;
288 }
289
290
291 struct rte_vmbus_bus rte_vmbus_bus = {
292         .bus = {
293                 .scan = rte_vmbus_scan,
294                 .probe = rte_vmbus_probe,
295                 .find_device = vmbus_find_device,
296                 .parse = vmbus_parse,
297         },
298         .device_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.device_list),
299         .driver_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.driver_list),
300 };
301
302 RTE_REGISTER_BUS(vmbus, rte_vmbus_bus.bus);
303 RTE_LOG_REGISTER_DEFAULT(vmbus_logtype_bus, NOTICE);