common/octeontx2: fix memory mapping API usage
[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                 return -1;
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                 return -1;
207         return 0;
208 }
209
210 static void
211 dev_delayed_unregister(void *param)
212 {
213         rte_intr_callback_unregister(&intr_handle, dev_uev_handler, param);
214         close(intr_handle.fd);
215         intr_handle.fd = -1;
216 }
217
218 static void
219 dev_uev_handler(__rte_unused void *param)
220 {
221         struct rte_dev_event uevent;
222         int ret;
223         char buf[EAL_UEV_MSG_LEN];
224         struct rte_bus *bus;
225         struct rte_device *dev;
226         const char *busname = "";
227
228         memset(&uevent, 0, sizeof(struct rte_dev_event));
229         memset(buf, 0, EAL_UEV_MSG_LEN);
230
231         ret = recv(intr_handle.fd, buf, EAL_UEV_MSG_LEN, MSG_DONTWAIT);
232         if (ret < 0 && errno == EAGAIN)
233                 return;
234         else if (ret <= 0) {
235                 /* connection is closed or broken, can not up again. */
236                 RTE_LOG(ERR, EAL, "uevent socket connection is broken.\n");
237                 rte_eal_alarm_set(1, dev_delayed_unregister, NULL);
238                 return;
239         }
240
241         ret = dev_uev_parse(buf, &uevent, EAL_UEV_MSG_LEN);
242         if (ret < 0) {
243                 RTE_LOG(DEBUG, EAL, "Ignoring uevent '%s'\n", buf);
244                 return;
245         }
246
247         RTE_LOG(DEBUG, EAL, "receive uevent(name:%s, type:%d, subsystem:%d)\n",
248                 uevent.devname, uevent.type, uevent.subsystem);
249
250         switch (uevent.subsystem) {
251         case EAL_DEV_EVENT_SUBSYSTEM_PCI:
252         case EAL_DEV_EVENT_SUBSYSTEM_UIO:
253                 busname = "pci";
254                 break;
255         default:
256                 break;
257         }
258
259         if (uevent.devname) {
260                 if (uevent.type == RTE_DEV_EVENT_REMOVE && hotplug_handle) {
261                         rte_spinlock_lock(&failure_handle_lock);
262                         bus = rte_bus_find_by_name(busname);
263                         if (bus == NULL) {
264                                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n",
265                                         busname);
266                                 goto failure_handle_err;
267                         }
268
269                         dev = bus->find_device(NULL, cmp_dev_name,
270                                                uevent.devname);
271                         if (dev == NULL) {
272                                 RTE_LOG(ERR, EAL, "Cannot find device (%s) on "
273                                         "bus (%s)\n", uevent.devname, busname);
274                                 goto failure_handle_err;
275                         }
276
277                         ret = bus->hot_unplug_handler(dev);
278                         if (ret) {
279                                 RTE_LOG(ERR, EAL, "Can not handle hot-unplug "
280                                         "for device (%s)\n", dev->name);
281                         }
282                         rte_spinlock_unlock(&failure_handle_lock);
283                 }
284                 rte_dev_event_callback_process(uevent.devname, uevent.type);
285         }
286
287         return;
288
289 failure_handle_err:
290         rte_spinlock_unlock(&failure_handle_lock);
291 }
292
293 int
294 rte_dev_event_monitor_start(void)
295 {
296         int ret;
297
298         if (monitor_started)
299                 return 0;
300
301         ret = dev_uev_socket_fd_create();
302         if (ret) {
303                 RTE_LOG(ERR, EAL, "error create device event fd.\n");
304                 return -1;
305         }
306
307         intr_handle.type = RTE_INTR_HANDLE_DEV_EVENT;
308         ret = rte_intr_callback_register(&intr_handle, dev_uev_handler, NULL);
309
310         if (ret) {
311                 RTE_LOG(ERR, EAL, "fail to register uevent callback.\n");
312                 return -1;
313         }
314
315         monitor_started = true;
316
317         return 0;
318 }
319
320 int
321 rte_dev_event_monitor_stop(void)
322 {
323         int ret;
324
325         if (!monitor_started)
326                 return 0;
327
328         ret = rte_intr_callback_unregister(&intr_handle, dev_uev_handler,
329                                            (void *)-1);
330         if (ret < 0) {
331                 RTE_LOG(ERR, EAL, "fail to unregister uevent callback.\n");
332                 return ret;
333         }
334
335         close(intr_handle.fd);
336         intr_handle.fd = -1;
337         monitor_started = false;
338
339         return 0;
340 }
341
342 int
343 dev_sigbus_handler_register(void)
344 {
345         sigset_t mask;
346         struct sigaction action;
347
348         rte_errno = 0;
349
350         if (sigbus_need_recover)
351                 return 0;
352
353         sigemptyset(&mask);
354         sigaddset(&mask, SIGBUS);
355         action.sa_flags = SA_SIGINFO;
356         action.sa_mask = mask;
357         action.sa_sigaction = sigbus_handler;
358         sigbus_need_recover = !sigaction(SIGBUS, &action, &sigbus_action_old);
359
360         return rte_errno;
361 }
362
363 int
364 dev_sigbus_handler_unregister(void)
365 {
366         rte_errno = 0;
367
368         sigbus_action_recover();
369
370         return rte_errno;
371 }
372
373 int
374 rte_dev_hotplug_handle_enable(void)
375 {
376         int ret = 0;
377
378         ret = dev_sigbus_handler_register();
379         if (ret < 0)
380                 RTE_LOG(ERR, EAL,
381                         "fail to register sigbus handler for devices.\n");
382
383         hotplug_handle = true;
384
385         return ret;
386 }
387
388 int
389 rte_dev_hotplug_handle_disable(void)
390 {
391         int ret = 0;
392
393         ret = dev_sigbus_handler_unregister();
394         if (ret < 0)
395                 RTE_LOG(ERR, EAL,
396                         "fail to unregister sigbus handler for devices.\n");
397
398         hotplug_handle = false;
399
400         return ret;
401 }