68f6cc5742f886439273d9ddafe1b99c29d7ce04
[dpdk.git] / drivers / bus / vmbus / linux / vmbus_bus.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/mman.h>
11 #include <sys/stat.h>
12
13 #include <rte_eal.h>
14 #include <rte_uuid.h>
15 #include <rte_tailq.h>
16 #include <rte_log.h>
17 #include <rte_devargs.h>
18 #include <rte_memory.h>
19 #include <rte_malloc.h>
20 #include <rte_bus_vmbus.h>
21
22 #include "eal_filesystem.h"
23 #include "private.h"
24
25 /** Pathname of VMBUS devices directory. */
26 #define SYSFS_VMBUS_DEVICES "/sys/bus/vmbus/devices"
27
28 /*
29  * GUID associated with network devices
30  * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
31  */
32 static const rte_uuid_t vmbus_nic_uuid = {
33         0xf8, 0x61, 0x51, 0x63,
34         0xdf, 0x3e,
35         0x46, 0xc5,
36         0x91, 0x3f,
37         0xf2, 0xd2, 0xf9, 0x65, 0xed, 0xe
38 };
39
40 extern struct rte_vmbus_bus rte_vmbus_bus;
41
42 /* Read sysfs file to get UUID */
43 static int
44 parse_sysfs_uuid(const char *filename, rte_uuid_t uu)
45 {
46         char buf[BUFSIZ];
47         char *cp, *in = buf;
48         FILE *f;
49
50         f = fopen(filename, "r");
51         if (f == NULL) {
52                 VMBUS_LOG(ERR, "cannot open sysfs value %s: %s",
53                           filename, strerror(errno));
54                 return -1;
55         }
56
57         if (fgets(buf, sizeof(buf), f) == NULL) {
58                 VMBUS_LOG(ERR, "cannot read sysfs value %s",
59                                 filename);
60                 fclose(f);
61                 return -1;
62         }
63         fclose(f);
64
65         cp = strchr(buf, '\n');
66         if (cp)
67                 *cp = '\0';
68
69         /* strip { } notation */
70         if (buf[0] == '{') {
71                 in = buf + 1;
72                 cp = strchr(in, '}');
73                 if (cp)
74                         *cp = '\0';
75         }
76
77         if (rte_uuid_parse(in, uu) < 0) {
78                 VMBUS_LOG(ERR, "%s %s not a valid UUID",
79                         filename, buf);
80                 return -1;
81         }
82
83         return 0;
84 }
85
86 static int
87 get_sysfs_string(const char *filename, char *buf, size_t buflen)
88 {
89         char *cp;
90         FILE *f;
91
92         f = fopen(filename, "r");
93         if (f == NULL) {
94                 VMBUS_LOG(ERR, "cannot open sysfs value %s:%s",
95                           filename, strerror(errno));
96                 return -1;
97         }
98
99         if (fgets(buf, buflen, f) == NULL) {
100                 VMBUS_LOG(ERR, "cannot read sysfs value %s",
101                                 filename);
102                 fclose(f);
103                 return -1;
104         }
105         fclose(f);
106
107         /* remove trailing newline */
108         cp = memchr(buf, '\n', buflen);
109         if (cp)
110                 *cp = '\0';
111
112         return 0;
113 }
114
115 static int
116 vmbus_get_uio_dev(const struct rte_vmbus_device *dev,
117                   char *dstbuf, size_t buflen)
118 {
119         char dirname[PATH_MAX];
120         unsigned int uio_num;
121         struct dirent *e;
122         DIR *dir;
123
124         /* Assume recent kernel where uio is in uio/uioX */
125         snprintf(dirname, sizeof(dirname),
126                  SYSFS_VMBUS_DEVICES "/%s/uio", dev->device.name);
127
128         dir = opendir(dirname);
129         if (dir == NULL)
130                 return -1; /* Not a UIO device */
131
132         /* take the first file starting with "uio" */
133         while ((e = readdir(dir)) != NULL) {
134                 const int prefix_len = 3;
135                 char *endptr;
136
137                 if (strncmp(e->d_name, "uio", prefix_len) != 0)
138                         continue;
139
140                 /* try uio%d */
141                 errno = 0;
142                 uio_num = strtoull(e->d_name + prefix_len, &endptr, 10);
143                 if (errno == 0 && endptr != (e->d_name + prefix_len)) {
144                         snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
145                         break;
146                 }
147         }
148         closedir(dir);
149
150         if (e == NULL)
151                 return -1;
152
153         return uio_num;
154 }
155
156 /* Check map names with kernel names */
157 static const char *map_names[VMBUS_MAX_RESOURCE] = {
158         [HV_TXRX_RING_MAP] = "txrx_rings",
159         [HV_INT_PAGE_MAP]  = "int_page",
160         [HV_MON_PAGE_MAP]  = "monitor_page",
161         [HV_RECV_BUF_MAP]  = "recv:",
162         [HV_SEND_BUF_MAP]  = "send:",
163 };
164
165
166 /* map the resources of a vmbus device in virtual memory */
167 int
168 rte_vmbus_map_device(struct rte_vmbus_device *dev)
169 {
170         char uioname[PATH_MAX], filename[PATH_MAX];
171         char dirname[PATH_MAX], mapname[64];
172         int i;
173
174         dev->uio_num = vmbus_get_uio_dev(dev, uioname, sizeof(uioname));
175         if (dev->uio_num < 0) {
176                 VMBUS_LOG(DEBUG, "Not managed by UIO driver, skipped");
177                 return 1;
178         }
179
180         /* Extract resource value */
181         for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
182                 struct rte_mem_resource *res = &dev->resource[i];
183                 unsigned long len, gpad = 0;
184                 char *cp;
185
186                 snprintf(dirname, sizeof(dirname),
187                          "%s/maps/map%d", uioname, i);
188
189                 snprintf(filename, sizeof(filename),
190                          "%s/name", dirname);
191
192                 if (get_sysfs_string(filename, mapname, sizeof(mapname)) < 0) {
193                         VMBUS_LOG(ERR, "could not read %s", filename);
194                         return -1;
195                 }
196
197                 if (strncmp(map_names[i], mapname, strlen(map_names[i])) != 0) {
198                         VMBUS_LOG(ERR,
199                                 "unexpected resource %s (expected %s)",
200                                 mapname, map_names[i]);
201                         return -1;
202                 }
203
204                 snprintf(filename, sizeof(filename),
205                          "%s/size", dirname);
206                 if (eal_parse_sysfs_value(filename, &len) < 0) {
207                         VMBUS_LOG(ERR,
208                                 "could not read %s", filename);
209                         return -1;
210                 }
211                 res->len = len;
212
213                 /* both send and receive buffers have gpad in name */
214                 cp = memchr(mapname, ':', sizeof(mapname));
215                 if (cp)
216                         gpad = strtoul(cp+1, NULL, 0);
217
218                 /* put the GPAD value in physical address */
219                 res->phys_addr = gpad;
220         }
221
222         return vmbus_uio_map_resource(dev);
223 }
224
225 void
226 rte_vmbus_unmap_device(struct rte_vmbus_device *dev)
227 {
228         vmbus_uio_unmap_resource(dev);
229 }
230
231 /* Scan one vmbus sysfs entry, and fill the devices list from it. */
232 static int
233 vmbus_scan_one(const char *name)
234 {
235         struct rte_vmbus_device *dev, *dev2;
236         char filename[PATH_MAX];
237         char dirname[PATH_MAX];
238         unsigned long tmp;
239         char *dev_name;
240
241         dev = calloc(1, sizeof(*dev));
242         if (dev == NULL)
243                 return -1;
244
245         dev->device.bus = &rte_vmbus_bus.bus;
246         dev->device.name = dev_name = strdup(name);
247         if (!dev->device.name)
248                 goto error;
249
250         /* sysfs base directory
251          *   /sys/bus/vmbus/devices/7a08391f-f5a0-4ac0-9802-d13fd964f8df
252          * or on older kernel
253          *   /sys/bus/vmbus/devices/vmbus_1
254          */
255         snprintf(dirname, sizeof(dirname), "%s/%s",
256                  SYSFS_VMBUS_DEVICES, name);
257
258         /* get device class  */
259         snprintf(filename, sizeof(filename), "%s/class_id", dirname);
260         if (parse_sysfs_uuid(filename, dev->class_id) < 0)
261                 goto error;
262
263         /* skip non-network devices */
264         if (rte_uuid_compare(dev->class_id, vmbus_nic_uuid) != 0) {
265                 free(dev_name);
266                 free(dev);
267                 return 0;
268         }
269
270         /* get device id */
271         snprintf(filename, sizeof(filename), "%s/device_id", dirname);
272         if (parse_sysfs_uuid(filename, dev->device_id) < 0)
273                 goto error;
274
275         /* get relid */
276         snprintf(filename, sizeof(filename), "%s/id", dirname);
277         if (eal_parse_sysfs_value(filename, &tmp) < 0)
278                 goto error;
279         dev->relid = tmp;
280
281         /* get monitor id */
282         snprintf(filename, sizeof(filename), "%s/monitor_id", dirname);
283         if (eal_parse_sysfs_value(filename, &tmp) < 0)
284                 goto error;
285         dev->monitor_id = tmp;
286
287         /* get numa node (if present) */
288         snprintf(filename, sizeof(filename), "%s/numa_node",
289                  dirname);
290
291         if (access(filename, R_OK) == 0) {
292                 if (eal_parse_sysfs_value(filename, &tmp) < 0)
293                         goto error;
294                 dev->device.numa_node = tmp;
295         } else {
296                 /* if no NUMA support, set default to 0 */
297                 dev->device.numa_node = SOCKET_ID_ANY;
298         }
299
300         dev->device.devargs = vmbus_devargs_lookup(dev);
301
302         /* device is valid, add in list (sorted) */
303         VMBUS_LOG(DEBUG, "Adding vmbus device %s", name);
304
305         TAILQ_FOREACH(dev2, &rte_vmbus_bus.device_list, next) {
306                 int ret;
307
308                 ret = rte_uuid_compare(dev->device_id, dev2->device_id);
309                 if (ret > 0)
310                         continue;
311
312                 if (ret < 0) {
313                         vmbus_insert_device(dev2, dev);
314                 } else { /* already registered */
315                         VMBUS_LOG(NOTICE,
316                                 "%s already registered", name);
317                         free(dev_name);
318                         free(dev);
319                 }
320                 return 0;
321         }
322
323         vmbus_add_device(dev);
324         return 0;
325 error:
326         VMBUS_LOG(DEBUG, "failed");
327
328         free(dev_name);
329         free(dev);
330         return -1;
331 }
332
333 /*
334  * Scan the content of the vmbus, and the devices in the devices list
335  */
336 int
337 rte_vmbus_scan(void)
338 {
339         struct dirent *e;
340         DIR *dir;
341
342         dir = opendir(SYSFS_VMBUS_DEVICES);
343         if (dir == NULL) {
344                 if (errno == ENOENT)
345                         return 0;
346
347                 VMBUS_LOG(ERR, "opendir %s failed: %s",
348                           SYSFS_VMBUS_DEVICES, strerror(errno));
349                 return -1;
350         }
351
352         while ((e = readdir(dir)) != NULL) {
353                 if (e->d_name[0] == '.')
354                         continue;
355
356                 if (vmbus_scan_one(e->d_name) < 0)
357                         goto error;
358         }
359         closedir(dir);
360         return 0;
361
362 error:
363         closedir(dir);
364         return -1;
365 }
366
367 void rte_vmbus_irq_mask(struct rte_vmbus_device *device)
368 {
369         vmbus_uio_irq_control(device, 1);
370 }
371
372 void rte_vmbus_irq_unmask(struct rte_vmbus_device *device)
373 {
374         vmbus_uio_irq_control(device, 0);
375 }
376
377 int rte_vmbus_irq_read(struct rte_vmbus_device *device)
378 {
379         return vmbus_uio_irq_read(device);
380 }