vfio: use static window sizing for sPAPR IOMMU
[dpdk.git] / lib / librte_eal / linux / eal_dev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <sys/socket.h>
10 #include <linux/netlink.h>
11
12 #include <rte_string_fns.h>
13 #include <rte_log.h>
14 #include <rte_compat.h>
15 #include <rte_dev.h>
16 #include <rte_malloc.h>
17 #include <rte_interrupts.h>
18 #include <rte_alarm.h>
19 #include <rte_bus.h>
20 #include <rte_eal.h>
21 #include <rte_spinlock.h>
22 #include <rte_errno.h>
23
24 #include "eal_private.h"
25
26 static struct rte_intr_handle intr_handle = {.fd = -1 };
27 static bool monitor_started;
28 static bool hotplug_handle;
29
30 #define EAL_UEV_MSG_LEN 4096
31 #define EAL_UEV_MSG_ELEM_LEN 128
32
33 /*
34  * spinlock for device hot-unplug failure handling. If it try to access bus or
35  * device, such as handle sigbus on bus or handle memory failure for device
36  * just need to use this lock. It could protect the bus and the device to avoid
37  * race condition.
38  */
39 static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
40
41 static struct sigaction sigbus_action_old;
42
43 static int sigbus_need_recover;
44
45 static void dev_uev_handler(__rte_unused void *param);
46
47 /* identify the system layer which reports this event. */
48 enum eal_dev_event_subsystem {
49         EAL_DEV_EVENT_SUBSYSTEM_PCI, /* PCI bus device event */
50         EAL_DEV_EVENT_SUBSYSTEM_UIO, /* UIO driver device event */
51         EAL_DEV_EVENT_SUBSYSTEM_VFIO, /* VFIO driver device event */
52         EAL_DEV_EVENT_SUBSYSTEM_MAX
53 };
54
55 static void
56 sigbus_action_recover(void)
57 {
58         if (sigbus_need_recover) {
59                 sigaction(SIGBUS, &sigbus_action_old, NULL);
60                 sigbus_need_recover = 0;
61         }
62 }
63
64 static void sigbus_handler(int signum, siginfo_t *info,
65                                 void *ctx __rte_unused)
66 {
67         int ret;
68
69         RTE_LOG(DEBUG, EAL, "Thread catch SIGBUS, fault address:%p\n",
70                 info->si_addr);
71
72         rte_spinlock_lock(&failure_handle_lock);
73         ret = rte_bus_sigbus_handler(info->si_addr);
74         rte_spinlock_unlock(&failure_handle_lock);
75         if (ret == -1) {
76                 rte_exit(EXIT_FAILURE,
77                          "Failed to handle SIGBUS for hot-unplug, "
78                          "(rte_errno: %s)!", strerror(rte_errno));
79         } else if (ret == 1) {
80                 if (sigbus_action_old.sa_flags == SA_SIGINFO
81                     && sigbus_action_old.sa_sigaction) {
82                         (*(sigbus_action_old.sa_sigaction))(signum,
83                                                             info, ctx);
84                 } else if (sigbus_action_old.sa_flags != SA_SIGINFO
85                            && sigbus_action_old.sa_handler) {
86                         (*(sigbus_action_old.sa_handler))(signum);
87                 } else {
88                         rte_exit(EXIT_FAILURE,
89                                  "Failed to handle generic SIGBUS!");
90                 }
91         }
92
93         RTE_LOG(DEBUG, EAL, "Success to handle SIGBUS for hot-unplug!\n");
94 }
95
96 static int cmp_dev_name(const struct rte_device *dev,
97         const void *_name)
98 {
99         const char *name = _name;
100
101         return strcmp(dev->name, name);
102 }
103
104 static int
105 dev_uev_socket_fd_create(void)
106 {
107         struct sockaddr_nl addr;
108         int ret;
109
110         intr_handle.fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC |
111                         SOCK_NONBLOCK,
112                         NETLINK_KOBJECT_UEVENT);
113         if (intr_handle.fd < 0) {
114                 RTE_LOG(ERR, EAL, "create uevent fd failed.\n");
115                 return -1;
116         }
117
118         memset(&addr, 0, sizeof(addr));
119         addr.nl_family = AF_NETLINK;
120         addr.nl_pid = 0;
121         addr.nl_groups = 0xffffffff;
122
123         ret = bind(intr_handle.fd, (struct sockaddr *) &addr, sizeof(addr));
124         if (ret < 0) {
125                 RTE_LOG(ERR, EAL, "Failed to bind uevent socket.\n");
126                 goto err;
127         }
128
129         return 0;
130 err:
131         close(intr_handle.fd);
132         intr_handle.fd = -1;
133         return ret;
134 }
135
136 struct rte_dev_event {
137         enum rte_dev_event_type type;   /**< device event type */
138         int subsystem;                  /**< subsystem id */
139         char *devname;                  /**< device name */
140 };
141
142 static int
143 dev_uev_parse(const char *buf, struct rte_dev_event *event, int length)
144 {
145         char action[EAL_UEV_MSG_ELEM_LEN];
146         char subsystem[EAL_UEV_MSG_ELEM_LEN];
147         char pci_slot_name[EAL_UEV_MSG_ELEM_LEN];
148         int i = 0;
149
150         memset(action, 0, EAL_UEV_MSG_ELEM_LEN);
151         memset(subsystem, 0, EAL_UEV_MSG_ELEM_LEN);
152         memset(pci_slot_name, 0, EAL_UEV_MSG_ELEM_LEN);
153
154         while (i < length) {
155                 for (; i < length; i++) {
156                         if (*buf)
157                                 break;
158                         buf++;
159                 }
160                 /**
161                  * check device uevent from kernel side, no need to check
162                  * uevent from udev.
163                  */
164                 if (!strncmp(buf, "libudev", 7)) {
165                         buf += 7;
166                         i += 7;
167                         return -1;
168                 }
169                 if (!strncmp(buf, "ACTION=", 7)) {
170                         buf += 7;
171                         i += 7;
172                         strlcpy(action, buf, sizeof(action));
173                 } else if (!strncmp(buf, "SUBSYSTEM=", 10)) {
174                         buf += 10;
175                         i += 10;
176                         strlcpy(subsystem, buf, sizeof(subsystem));
177                 } else if (!strncmp(buf, "PCI_SLOT_NAME=", 14)) {
178                         buf += 14;
179                         i += 14;
180                         strlcpy(pci_slot_name, buf, sizeof(subsystem));
181                         event->devname = strdup(pci_slot_name);
182                 }
183                 for (; i < length; i++) {
184                         if (*buf == '\0')
185                                 break;
186                         buf++;
187                 }
188         }
189
190         /* parse the subsystem layer */
191         if (!strncmp(subsystem, "uio", 3))
192                 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_UIO;
193         else if (!strncmp(subsystem, "pci", 3))
194                 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_PCI;
195         else if (!strncmp(subsystem, "vfio", 4))
196                 event->subsystem = EAL_DEV_EVENT_SUBSYSTEM_VFIO;
197         else
198                 goto err;
199
200         /* parse the action type */
201         if (!strncmp(action, "add", 3))
202                 event->type = RTE_DEV_EVENT_ADD;
203         else if (!strncmp(action, "remove", 6))
204                 event->type = RTE_DEV_EVENT_REMOVE;
205         else
206                 goto err;
207         return 0;
208 err:
209         free(event->devname);
210         return -1;
211 }
212
213 static void
214 dev_delayed_unregister(void *param)
215 {
216         rte_intr_callback_unregister(&intr_handle, dev_uev_handler, param);
217         close(intr_handle.fd);
218         intr_handle.fd = -1;
219 }
220
221 static void
222 dev_uev_handler(__rte_unused void *param)
223 {
224         struct rte_dev_event uevent;
225         int ret;
226         char buf[EAL_UEV_MSG_LEN];
227         struct rte_bus *bus;
228         struct rte_device *dev;
229         const char *busname = "";
230
231         memset(&uevent, 0, sizeof(struct rte_dev_event));
232         memset(buf, 0, EAL_UEV_MSG_LEN);
233
234         ret = recv(intr_handle.fd, buf, EAL_UEV_MSG_LEN, MSG_DONTWAIT);
235         if (ret < 0 && errno == EAGAIN)
236                 return;
237         else if (ret <= 0) {
238                 /* connection is closed or broken, can not up again. */
239                 RTE_LOG(ERR, EAL, "uevent socket connection is broken.\n");
240                 rte_eal_alarm_set(1, dev_delayed_unregister, NULL);
241                 return;
242         }
243
244         ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
245         if (ret < 0) {
246                 RTE_LOG(DEBUG, EAL, "Ignoring uevent '%s'\n", buf);
247                 return;
248         }
249
250         RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
251                 uevent.devname, uevent.type, uevent.subsystem);
252
253         switch (uevent.subsystem) {
254         case EAL_DEV_EVENT_SUBSYSTEM_PCI:
255         case EAL_DEV_EVENT_SUBSYSTEM_UIO:
256                 busname = "pci";
257                 break;
258         default:
259                 break;
260         }
261
262         if (uevent.devname) {
263                 if (uevent.type == RTE_DEV_EVENT_REMOVE && hotplug_handle) {
264                         rte_spinlock_lock(&failure_handle_lock);
265                         bus = rte_bus_find_by_name(busname);
266                         if (bus == NULL) {
267                                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
268                                         busname);
269                                 goto failure_handle_err;
270                         }
271
272                         dev = bus->find_device(NULL, cmp_dev_name,
273                                                uevent.devname);
274                         if (dev == NULL) {
275                                 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
276                                         "bus (%s)\n", uevent.devname, busname);
277                                 goto failure_handle_err;
278                         }
279
280                         ret = bus->hot_unplug_handler(dev);
281                         if (ret) {
282                                 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
283                                         "for device (%s)\n", dev->name);
284                         }
285                         rte_spinlock_unlock(&failure_handle_lock);
286                 }
287                 rte_dev_event_callback_process(uevent.devname, uevent.type);
288                 free(uevent.devname);
289         }
290
291         return;
292
293 failure_handle_err:
294         rte_spinlock_unlock(&failure_handle_lock);
295         free(uevent.devname);
296 }
297
298 int
299 rte_dev_event_monitor_start(void)
300 {
301         int ret;
302
303         if (monitor_started)
304                 return 0;
305
306         ret = dev_uev_socket_fd_create();
307         if (ret) {
308                 RTE_LOG(ERR, EAL, "error create device event fd.\n");
309                 return -1;
310         }
311
312         intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
313         ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
314
315         if (ret) {
316                 RTE_LOG(ERR, EAL, "fail to register uevent callback.\n");
317                 return -1;
318         }
319
320         monitor_started = true;
321
322         return 0;
323 }
324
325 int
326 rte_dev_event_monitor_stop(void)
327 {
328         int ret;
329
330         if (!monitor_started)
331                 return 0;
332
333         ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
334                                            (void *)-1);
335         if (ret < 0) {
336                 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
337                 return ret;
338         }
339
340         close(intr_handle.fd);
341         intr_handle.fd = -1;
342         monitor_started = false;
343
344         return 0;
345 }
346
347 int
348 dev_sigbus_handler_register(void)
349 {
350         sigset_t mask;
351         struct sigaction action;
352
353         rte_errno = 0;
354
355         if (sigbus_need_recover)
356                 return 0;
357
358         sigemptyset(&mask);
359         sigaddset(&mask, SIGBUS);
360         action.sa_flags = SA_SIGINFO;
361         action.sa_mask = mask;
362         action.sa_sigaction = sigbus_handler;
363         sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
364
365         return rte_errno;
366 }
367
368 int
369 dev_sigbus_handler_unregister(void)
370 {
371         rte_errno = 0;
372
373         sigbus_action_recover();
374
375         return rte_errno;
376 }
377
378 int
379 rte_dev_hotplug_handle_enable(void)
380 {
381         int ret = 0;
382
383         ret = dev_sigbus_handler_register();
384         if (ret < 0)
385                 RTE_LOG(ERR, EAL,
386                         "fail to register sigbus handler for devices.\n");
387
388         hotplug_handle = true;
389
390         return ret;
391 }
392
393 int
394 rte_dev_hotplug_handle_disable(void)
395 {
396         int ret = 0;
397
398         ret = dev_sigbus_handler_unregister();
399         if (ret < 0)
400                 RTE_LOG(ERR, EAL,
401                         "fail to unregister sigbus handler for devices.\n");
402
403         hotplug_handle = false;
404
405         return ret;
406 }