Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation.
3 : : * Copyright(c) 2014 6WIND S.A.
4 : : */
5 : :
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <string.h>
9 : : #include <sys/queue.h>
10 : :
11 : : #include <bus_driver.h>
12 : : #include <rte_class.h>
13 : : #include <dev_driver.h>
14 : : #include <rte_devargs.h>
15 : : #include <rte_errno.h>
16 : : #include <rte_log.h>
17 : : #include <rte_spinlock.h>
18 : : #include <rte_string_fns.h>
19 : :
20 : : #include <eal_export.h>
21 : : #include "eal_private.h"
22 : : #include "hotplug_mp.h"
23 : :
24 : : RTE_EXPORT_SYMBOL(rte_driver_name)
25 : : const char *
26 : 0 : rte_driver_name(const struct rte_driver *driver)
27 : : {
28 : 0 : return driver->name;
29 : : }
30 : :
31 : : RTE_EXPORT_SYMBOL(rte_dev_bus)
32 : : const struct rte_bus *
33 : 0 : rte_dev_bus(const struct rte_device *dev)
34 : : {
35 : 0 : return dev->bus;
36 : : }
37 : :
38 : : RTE_EXPORT_SYMBOL(rte_dev_bus_info)
39 : : const char *
40 : 0 : rte_dev_bus_info(const struct rte_device *dev)
41 : : {
42 : 0 : return dev->bus_info;
43 : : }
44 : :
45 : : RTE_EXPORT_SYMBOL(rte_dev_devargs)
46 : : const struct rte_devargs *
47 : 0 : rte_dev_devargs(const struct rte_device *dev)
48 : : {
49 : 0 : return dev->devargs;
50 : : }
51 : :
52 : : RTE_EXPORT_SYMBOL(rte_dev_driver)
53 : : const struct rte_driver *
54 : 0 : rte_dev_driver(const struct rte_device *dev)
55 : : {
56 : 0 : return dev->driver;
57 : : }
58 : :
59 : : RTE_EXPORT_SYMBOL(rte_dev_name)
60 : : const char *
61 : 8 : rte_dev_name(const struct rte_device *dev)
62 : : {
63 : 8 : return dev->name;
64 : : }
65 : :
66 : : RTE_EXPORT_SYMBOL(rte_dev_numa_node)
67 : : int
68 : 0 : rte_dev_numa_node(const struct rte_device *dev)
69 : : {
70 : 0 : return dev->numa_node;
71 : : }
72 : :
73 : : /**
74 : : * The device event callback description.
75 : : *
76 : : * It contains callback address to be registered by user application,
77 : : * the pointer to the parameters for callback, and the device name.
78 : : */
79 : : struct dev_event_callback {
80 : : TAILQ_ENTRY(dev_event_callback) next; /**< Callbacks list */
81 : : rte_dev_event_cb_fn cb_fn; /**< Callback address */
82 : : void *cb_arg; /**< Callback parameter */
83 : : char *dev_name; /**< Callback device name, NULL is for all device */
84 : : uint32_t active; /**< Callback is executing */
85 : : };
86 : :
87 : : /** @internal Structure to keep track of registered callbacks */
88 : : TAILQ_HEAD(dev_event_cb_list, dev_event_callback);
89 : :
90 : : /* The device event callback list for all registered callbacks. */
91 : : static struct dev_event_cb_list dev_event_cbs;
92 : :
93 : : /* spinlock for device callbacks */
94 : : static rte_spinlock_t dev_event_lock = RTE_SPINLOCK_INITIALIZER;
95 : :
96 : : struct dev_next_ctx {
97 : : struct rte_dev_iterator *it;
98 : : const char *bus_str;
99 : : const char *cls_str;
100 : : };
101 : :
102 : : #define CTX(it, bus_str, cls_str) \
103 : : (&(const struct dev_next_ctx){ \
104 : : .it = it, \
105 : : .bus_str = bus_str, \
106 : : .cls_str = cls_str, \
107 : : })
108 : :
109 : : #define ITCTX(ptr) \
110 : : (((struct dev_next_ctx *)(intptr_t)ptr)->it)
111 : :
112 : : #define BUSCTX(ptr) \
113 : : (((struct dev_next_ctx *)(intptr_t)ptr)->bus_str)
114 : :
115 : : #define CLSCTX(ptr) \
116 : : (((struct dev_next_ctx *)(intptr_t)ptr)->cls_str)
117 : :
118 : 0 : static int cmp_dev_name(const struct rte_device *dev, const void *_name)
119 : : {
120 : : const char *name = _name;
121 : :
122 : 0 : return strcmp(dev->name, name);
123 : : }
124 : :
125 : : RTE_EXPORT_SYMBOL(rte_dev_is_probed)
126 : : int
127 : 15112 : rte_dev_is_probed(const struct rte_device *dev)
128 : : {
129 : : /* The field driver should be set only when the probe is successful. */
130 : 15112 : return dev->driver != NULL;
131 : : }
132 : :
133 : : /* helper function to build devargs, caller should free the memory */
134 : : static int
135 [ # # ]: 0 : build_devargs(const char *busname, const char *devname,
136 : : const char *drvargs, char **devargs)
137 : : {
138 : : int length;
139 : :
140 : : length = snprintf(NULL, 0, "%s:%s,%s", busname, devname, drvargs);
141 [ # # ]: 0 : if (length < 0)
142 : : return -EINVAL;
143 : :
144 : 0 : *devargs = malloc(length + 1);
145 [ # # ]: 0 : if (*devargs == NULL)
146 : : return -ENOMEM;
147 : :
148 : : length = snprintf(*devargs, length + 1, "%s:%s,%s",
149 : : busname, devname, drvargs);
150 [ # # ]: 0 : if (length < 0) {
151 : 0 : free(*devargs);
152 : 0 : return -EINVAL;
153 : : }
154 : :
155 : : return 0;
156 : : }
157 : :
158 : : RTE_EXPORT_SYMBOL(rte_eal_hotplug_add)
159 : : int
160 : 0 : rte_eal_hotplug_add(const char *busname, const char *devname,
161 : : const char *drvargs)
162 : : {
163 : :
164 : : char *devargs;
165 : : int ret;
166 : :
167 : 0 : ret = build_devargs(busname, devname, drvargs, &devargs);
168 [ # # ]: 0 : if (ret != 0)
169 : : return ret;
170 : :
171 : 0 : ret = rte_dev_probe(devargs);
172 : 0 : free(devargs);
173 : :
174 : 0 : return ret;
175 : : }
176 : :
177 : : /* probe device at local process. */
178 : : int
179 : 0 : local_dev_probe(const char *devargs, struct rte_device **new_dev)
180 : : {
181 : : struct rte_device *dev;
182 : : struct rte_driver *drv;
183 : : struct rte_devargs *da;
184 : : int ret;
185 : :
186 : 0 : *new_dev = NULL;
187 : 0 : da = calloc(1, sizeof(*da));
188 [ # # ]: 0 : if (da == NULL)
189 : : return -ENOMEM;
190 : :
191 : 0 : ret = rte_devargs_parse(da, devargs);
192 [ # # ]: 0 : if (ret)
193 : 0 : goto err_devarg;
194 : :
195 [ # # ]: 0 : if (da->bus->probe_device == NULL) {
196 : 0 : EAL_LOG(ERR, "Function probe_device not supported by bus (%s)",
197 : : da->bus->name);
198 : : ret = -ENOTSUP;
199 : 0 : goto err_devarg;
200 : : }
201 : :
202 : 0 : ret = rte_devargs_insert(&da);
203 [ # # ]: 0 : if (ret)
204 : 0 : goto err_devarg;
205 : :
206 : : /* the rte_devargs will be referenced in the matching rte_device */
207 : 0 : ret = da->bus->scan();
208 [ # # ]: 0 : if (ret)
209 : 0 : goto err_devarg;
210 : :
211 : 0 : dev = da->bus->find_device(da->bus, NULL, cmp_dev_name, da->name);
212 [ # # ]: 0 : if (dev == NULL) {
213 : 0 : EAL_LOG(ERR, "Cannot find device (%s)",
214 : : da->name);
215 : : ret = -ENODEV;
216 : 0 : goto err_devarg;
217 : : }
218 : : /* Since there is a matching device, it is now its responsibility
219 : : * to manage the devargs we've just inserted. From this point
220 : : * those devargs shouldn't be removed manually anymore.
221 : : */
222 : :
223 : : drv = NULL;
224 : 0 : next_driver:
225 : 0 : drv = rte_bus_find_driver(dev->bus, drv, dev);
226 [ # # ]: 0 : if (drv == NULL) {
227 : : ret = -ENOTSUP;
228 [ # # # # ]: 0 : } else if (rte_dev_is_probed(dev) && !dev->bus->allow_multi_probe) {
229 : 0 : EAL_LOG(INFO, "Device %s is already probed", dev->name);
230 : : ret = -EEXIST;
231 : : } else {
232 : 0 : bool was_probed = rte_dev_is_probed(dev);
233 : :
234 : : /*
235 : : * Reference driver structure.
236 : : * This needs to be before .probe_device as some bus (like PCI) use
237 : : * driver flags for adjusting configuration.
238 : : */
239 [ # # ]: 0 : if (!was_probed)
240 : 0 : dev->driver = drv;
241 : 0 : ret = dev->bus->probe_device(drv, dev);
242 [ # # ]: 0 : if (!was_probed && ret != 0)
243 : 0 : dev->driver = NULL;
244 : :
245 [ # # ]: 0 : if (ret > 0)
246 : 0 : goto next_driver;
247 : : }
248 : :
249 [ # # # # ]: 0 : if (ret && !rte_dev_is_probed(dev)) { /* if hasn't ever succeeded */
250 : 0 : EAL_LOG(ERR, "Driver cannot attach the device (%s)",
251 : : dev->name);
252 : 0 : return ret;
253 : : }
254 : :
255 : 0 : *new_dev = dev;
256 : 0 : return ret;
257 : :
258 : 0 : err_devarg:
259 [ # # ]: 0 : if (rte_devargs_remove(da) != 0) {
260 : 0 : rte_devargs_reset(da);
261 : 0 : free(da);
262 : : }
263 : : return ret;
264 : : }
265 : :
266 : : RTE_EXPORT_SYMBOL(rte_dev_probe)
267 : : int
268 : 0 : rte_dev_probe(const char *devargs)
269 : : {
270 : 0 : bool do_mp = !eal_mp_is_disabled();
271 : : struct eal_dev_mp_req req;
272 : : struct rte_device *dev;
273 : : int ret;
274 : :
275 [ # # ]: 0 : if (!do_mp)
276 : 0 : goto skip_mp_req;
277 : :
278 [ # # ]: 0 : if (strlen(devargs) >= EAL_DEV_MP_DEV_ARGS_MAX_LEN) {
279 : 0 : EAL_LOG(ERR, "devargs truncated (len %zu, max %d)",
280 : : strlen(devargs), EAL_DEV_MP_DEV_ARGS_MAX_LEN);
281 : 0 : return -E2BIG;
282 : : }
283 : :
284 : : memset(&req, 0, sizeof(req));
285 : : req.t = EAL_DEV_REQ_TYPE_ATTACH;
286 : : strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
287 : :
288 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
289 : : /**
290 : : * If in secondary process, just send IPC request to
291 : : * primary process.
292 : : */
293 : 0 : ret = eal_dev_hotplug_request_to_primary(&req);
294 [ # # ]: 0 : if (ret != 0) {
295 : 0 : EAL_LOG(ERR,
296 : : "Failed to send hotplug request to primary");
297 : 0 : return -ENOMSG;
298 : : }
299 [ # # ]: 0 : if (req.result != 0)
300 : 0 : EAL_LOG(ERR,
301 : : "Failed to hotplug add device");
302 : 0 : return req.result;
303 : : }
304 : :
305 : : /* attach a shared device from primary start from here: */
306 : :
307 : 0 : skip_mp_req:
308 : : /* primary attach the new device itself. */
309 : 0 : ret = local_dev_probe(devargs, &dev);
310 : :
311 [ # # ]: 0 : if (ret != 0) {
312 : 0 : EAL_LOG(ERR,
313 : : "Failed to attach device on primary process");
314 : :
315 : : /**
316 : : * it is possible that secondary process failed to attached a
317 : : * device that primary process have during initialization,
318 : : * so for -EEXIST case, we still need to sync with secondary
319 : : * process.
320 : : */
321 [ # # ]: 0 : if (ret != -EEXIST)
322 : : return ret;
323 : : }
324 : :
325 [ # # ]: 0 : if (!do_mp)
326 : 0 : goto skip_mp_notify;
327 : :
328 : : /* primary send attach sync request to secondary. */
329 : 0 : ret = eal_dev_hotplug_request_to_secondary(&req);
330 : :
331 : : /* if any communication error, we need to rollback. */
332 [ # # ]: 0 : if (ret != 0) {
333 : 0 : EAL_LOG(ERR,
334 : : "Failed to send hotplug add request to secondary");
335 : : ret = -ENOMSG;
336 : 0 : goto rollback;
337 : : }
338 : :
339 : : /**
340 : : * if any secondary failed to attach, we need to consider if rollback
341 : : * is necessary.
342 : : */
343 [ # # ]: 0 : if (req.result != 0) {
344 : 0 : EAL_LOG(ERR,
345 : : "Failed to attach device on secondary process");
346 : 0 : ret = req.result;
347 : :
348 : : /* for -EEXIST, we don't need to rollback. */
349 [ # # ]: 0 : if (ret == -EEXIST)
350 : : return ret;
351 : 0 : goto rollback;
352 : : }
353 : :
354 : 0 : skip_mp_notify:
355 : : return 0;
356 : :
357 : 0 : rollback:
358 [ # # ]: 0 : if (do_mp) {
359 : 0 : req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
360 : :
361 : : /* primary send rollback request to secondary. */
362 [ # # ]: 0 : if (eal_dev_hotplug_request_to_secondary(&req) != 0)
363 : 0 : EAL_LOG(WARNING,
364 : : "Failed to rollback device attach on secondary."
365 : : "Devices in secondary may not sync with primary");
366 : : }
367 : :
368 : : /* primary rollback itself. */
369 [ # # ]: 0 : if (local_dev_remove(dev) != 0)
370 : 0 : EAL_LOG(WARNING,
371 : : "Failed to rollback device attach on primary."
372 : : "Devices in secondary may not sync with primary");
373 : :
374 : : return ret;
375 : : }
376 : :
377 : : RTE_EXPORT_SYMBOL(rte_eal_hotplug_remove)
378 : : int
379 : 0 : rte_eal_hotplug_remove(const char *busname, const char *devname)
380 : : {
381 : : struct rte_device *dev;
382 : : struct rte_bus *bus;
383 : :
384 : 0 : bus = rte_bus_find_by_name(busname);
385 [ # # ]: 0 : if (bus == NULL) {
386 : 0 : EAL_LOG(ERR, "Cannot find bus (%s)", busname);
387 : 0 : return -ENOENT;
388 : : }
389 : :
390 : 0 : dev = bus->find_device(bus, NULL, cmp_dev_name, devname);
391 [ # # ]: 0 : if (dev == NULL) {
392 : 0 : EAL_LOG(ERR, "Cannot find plugged device (%s)", devname);
393 : 0 : return -EINVAL;
394 : : }
395 : :
396 : 0 : return rte_dev_remove(dev);
397 : : }
398 : :
399 : : /* remove device at local process. */
400 : : int
401 : 0 : local_dev_remove(struct rte_device *dev)
402 : : {
403 : : int ret;
404 : :
405 [ # # ]: 0 : if (dev->bus->unplug_device == NULL) {
406 : 0 : EAL_LOG(ERR, "Function unplug_device not supported by bus (%s)",
407 : : dev->bus->name);
408 : 0 : return -ENOTSUP;
409 : : }
410 : :
411 : 0 : ret = dev->bus->unplug_device(dev);
412 [ # # ]: 0 : if (ret) {
413 : 0 : EAL_LOG(ERR, "Driver cannot detach the device (%s)",
414 : : dev->name);
415 [ # # ]: 0 : return (ret < 0) ? ret : -ENOENT;
416 : : }
417 : :
418 : 0 : dev->driver = NULL;
419 : :
420 : 0 : return 0;
421 : : }
422 : :
423 : : RTE_EXPORT_SYMBOL(rte_dev_remove)
424 : : int
425 : 0 : rte_dev_remove(struct rte_device *dev)
426 : : {
427 : 0 : bool do_mp = !eal_mp_is_disabled();
428 : : struct eal_dev_mp_req req;
429 : : char *devargs;
430 : : int ret;
431 : :
432 [ # # ]: 0 : if (!rte_dev_is_probed(dev)) {
433 : 0 : EAL_LOG(ERR, "Device is not probed");
434 : 0 : return -ENOENT;
435 : : }
436 : :
437 [ # # ]: 0 : if (!do_mp)
438 : 0 : goto skip_mp_req;
439 : :
440 : 0 : ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
441 [ # # ]: 0 : if (ret != 0)
442 : : return ret;
443 : :
444 : : memset(&req, 0, sizeof(req));
445 : 0 : req.t = EAL_DEV_REQ_TYPE_DETACH;
446 : 0 : strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
447 : 0 : free(devargs);
448 : :
449 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
450 : : /**
451 : : * If in secondary process, just send IPC request to
452 : : * primary process.
453 : : */
454 : 0 : ret = eal_dev_hotplug_request_to_primary(&req);
455 [ # # ]: 0 : if (ret != 0) {
456 : 0 : EAL_LOG(ERR,
457 : : "Failed to send hotplug request to primary");
458 : 0 : return -ENOMSG;
459 : : }
460 [ # # ]: 0 : if (req.result != 0)
461 : 0 : EAL_LOG(ERR,
462 : : "Failed to hotplug remove device");
463 : 0 : return req.result;
464 : : }
465 : :
466 : : /* detach a device from primary start from here: */
467 : :
468 : : /* primary send detach sync request to secondary */
469 : 0 : ret = eal_dev_hotplug_request_to_secondary(&req);
470 : :
471 : : /**
472 : : * if communication error, we need to rollback, because it is possible
473 : : * part of the secondary processes still detached it successfully.
474 : : */
475 [ # # ]: 0 : if (ret != 0) {
476 : 0 : EAL_LOG(ERR,
477 : : "Failed to send device detach request to secondary");
478 : : ret = -ENOMSG;
479 : 0 : goto rollback;
480 : : }
481 : :
482 : : /**
483 : : * if any secondary failed to detach, we need to consider if rollback
484 : : * is necessary.
485 : : */
486 [ # # ]: 0 : if (req.result != 0) {
487 : 0 : EAL_LOG(ERR,
488 : : "Failed to detach device on secondary process");
489 : 0 : ret = req.result;
490 : : /**
491 : : * if -ENOENT, we don't need to rollback, since devices is
492 : : * already detached on secondary process.
493 : : */
494 [ # # ]: 0 : if (ret != -ENOENT)
495 : 0 : goto rollback;
496 : : }
497 : :
498 : 0 : skip_mp_req:
499 : : /* primary detach the device itself. */
500 : 0 : ret = local_dev_remove(dev);
501 : :
502 : : /* if primary failed, still need to consider if rollback is necessary */
503 [ # # ]: 0 : if (ret != 0) {
504 : 0 : EAL_LOG(ERR,
505 : : "Failed to detach device on primary process");
506 : : /* if -ENOENT, we don't need to rollback */
507 [ # # ]: 0 : if (ret == -ENOENT)
508 : : return ret;
509 : 0 : goto rollback;
510 : : }
511 : :
512 : : return 0;
513 : :
514 : 0 : rollback:
515 [ # # ]: 0 : if (do_mp) {
516 : 0 : req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
517 : :
518 : : /* primary send rollback request to secondary. */
519 [ # # ]: 0 : if (eal_dev_hotplug_request_to_secondary(&req) != 0)
520 : 0 : EAL_LOG(WARNING,
521 : : "Failed to rollback device detach on secondary."
522 : : "Devices in secondary may not sync with primary");
523 : : }
524 : :
525 : : return ret;
526 : : }
527 : :
528 : : RTE_EXPORT_SYMBOL(rte_dev_event_callback_register)
529 : : int
530 : 0 : rte_dev_event_callback_register(const char *device_name,
531 : : rte_dev_event_cb_fn cb_fn,
532 : : void *cb_arg)
533 : : {
534 : : struct dev_event_callback *event_cb;
535 : : int ret;
536 : :
537 [ # # ]: 0 : if (!cb_fn)
538 : : return -EINVAL;
539 : :
540 : : rte_spinlock_lock(&dev_event_lock);
541 : :
542 [ # # ]: 0 : if (TAILQ_EMPTY(&dev_event_cbs))
543 : 0 : TAILQ_INIT(&dev_event_cbs);
544 : :
545 [ # # ]: 0 : TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
546 [ # # # # ]: 0 : if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
547 [ # # # # ]: 0 : if (device_name == NULL && event_cb->dev_name == NULL)
548 : : break;
549 [ # # # # ]: 0 : if (device_name == NULL || event_cb->dev_name == NULL)
550 : 0 : continue;
551 [ # # ]: 0 : if (!strcmp(event_cb->dev_name, device_name))
552 : : break;
553 : : }
554 : : }
555 : :
556 : : /* create a new callback. */
557 [ # # ]: 0 : if (event_cb == NULL) {
558 : 0 : event_cb = malloc(sizeof(struct dev_event_callback));
559 [ # # ]: 0 : if (event_cb != NULL) {
560 : 0 : event_cb->cb_fn = cb_fn;
561 : 0 : event_cb->cb_arg = cb_arg;
562 : 0 : event_cb->active = 0;
563 [ # # ]: 0 : if (!device_name) {
564 : 0 : event_cb->dev_name = NULL;
565 : : } else {
566 : 0 : event_cb->dev_name = strdup(device_name);
567 [ # # ]: 0 : if (event_cb->dev_name == NULL) {
568 : : ret = -ENOMEM;
569 : 0 : goto error;
570 : : }
571 : : }
572 : 0 : TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
573 : : } else {
574 : 0 : EAL_LOG(ERR,
575 : : "Failed to allocate memory for device "
576 : : "event callback.");
577 : : ret = -ENOMEM;
578 : 0 : goto error;
579 : : }
580 : : } else {
581 : 0 : EAL_LOG(ERR,
582 : : "The callback is already exist, no need "
583 : : "to register again.");
584 : : event_cb = NULL;
585 : : ret = -EEXIST;
586 : 0 : goto error;
587 : : }
588 : :
589 : : rte_spinlock_unlock(&dev_event_lock);
590 : 0 : return 0;
591 : 0 : error:
592 : 0 : free(event_cb);
593 : : rte_spinlock_unlock(&dev_event_lock);
594 : 0 : return ret;
595 : : }
596 : :
597 : : RTE_EXPORT_SYMBOL(rte_dev_event_callback_unregister)
598 : : int
599 : 0 : rte_dev_event_callback_unregister(const char *device_name,
600 : : rte_dev_event_cb_fn cb_fn,
601 : : void *cb_arg)
602 : : {
603 : : int ret = 0;
604 : : struct dev_event_callback *event_cb, *next;
605 : :
606 [ # # ]: 0 : if (!cb_fn)
607 : : return -EINVAL;
608 : :
609 : : rte_spinlock_lock(&dev_event_lock);
610 : : /*walk through the callbacks and remove all that match. */
611 [ # # ]: 0 : for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
612 : : event_cb = next) {
613 : :
614 : 0 : next = TAILQ_NEXT(event_cb, next);
615 : :
616 [ # # # # ]: 0 : if (device_name != NULL && event_cb->dev_name != NULL) {
617 [ # # ]: 0 : if (strcmp(event_cb->dev_name, device_name))
618 : 0 : continue;
619 [ # # ]: 0 : } else if (device_name != NULL) {
620 : 0 : continue;
621 : : }
622 : :
623 : : /* Remove only matching callback with arg */
624 [ # # # # ]: 0 : if (event_cb->cb_fn != cb_fn ||
625 [ # # ]: 0 : (cb_arg != (void *)-1 && event_cb->cb_arg != cb_arg))
626 : 0 : continue;
627 : :
628 : : /*
629 : : * if this callback is not executing right now,
630 : : * then remove it.
631 : : */
632 [ # # ]: 0 : if (event_cb->active == 0) {
633 [ # # ]: 0 : TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
634 : 0 : free(event_cb->dev_name);
635 : 0 : free(event_cb);
636 : 0 : ret++;
637 : : } else {
638 : : ret = -EAGAIN;
639 : : break;
640 : : }
641 : : }
642 : :
643 : : /* this callback is not be registered */
644 [ # # ]: 0 : if (ret == 0)
645 : : ret = -ENOENT;
646 : :
647 : : rte_spinlock_unlock(&dev_event_lock);
648 : 0 : return ret;
649 : : }
650 : :
651 : : RTE_EXPORT_SYMBOL(rte_dev_event_callback_process)
652 : : void
653 : 0 : rte_dev_event_callback_process(const char *device_name,
654 : : enum rte_dev_event_type event)
655 : : {
656 : : struct dev_event_callback *cb_lst;
657 : :
658 [ # # ]: 0 : if (device_name == NULL)
659 : : return;
660 : :
661 : : rte_spinlock_lock(&dev_event_lock);
662 : :
663 [ # # ]: 0 : TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
664 [ # # ]: 0 : if (cb_lst->dev_name) {
665 [ # # ]: 0 : if (strcmp(cb_lst->dev_name, device_name))
666 : 0 : continue;
667 : : }
668 : 0 : cb_lst->active = 1;
669 : : rte_spinlock_unlock(&dev_event_lock);
670 : 0 : cb_lst->cb_fn(device_name, event,
671 : : cb_lst->cb_arg);
672 : : rte_spinlock_lock(&dev_event_lock);
673 : 0 : cb_lst->active = 0;
674 : : }
675 : : rte_spinlock_unlock(&dev_event_lock);
676 : : }
677 : :
678 : : RTE_EXPORT_SYMBOL(rte_dev_iterator_init)
679 : : int
680 : 6 : rte_dev_iterator_init(struct rte_dev_iterator *it,
681 : : const char *dev_str)
682 : : {
683 : 6 : struct rte_devargs devargs = { .bus = NULL };
684 : : struct rte_class *cls = NULL;
685 : : struct rte_bus *bus = NULL;
686 : :
687 : : /* Having both bus_str and cls_str NULL is illegal,
688 : : * marking this iterator as invalid unless
689 : : * everything goes well.
690 : : */
691 : 6 : it->bus_str = NULL;
692 : 6 : it->cls_str = NULL;
693 : :
694 : : /* Setting data field implies no malloc in parsing. */
695 : 6 : devargs.data = (void *)(intptr_t)dev_str;
696 [ - + ]: 6 : if (rte_devargs_layers_parse(&devargs, dev_str))
697 : 0 : goto get_out;
698 : :
699 : 6 : bus = devargs.bus;
700 : 6 : cls = devargs.cls;
701 : : /* The string should have at least
702 : : * one layer specified.
703 : : */
704 [ - + ]: 6 : if (bus == NULL && cls == NULL) {
705 : 0 : EAL_LOG(DEBUG, "Either bus or class must be specified.");
706 : 0 : rte_errno = EINVAL;
707 : 0 : goto get_out;
708 : : }
709 [ + - - + ]: 6 : if (bus != NULL && bus->dev_iterate == NULL) {
710 : 0 : EAL_LOG(DEBUG, "Bus %s not supported", bus->name);
711 : 0 : rte_errno = ENOTSUP;
712 : 0 : goto get_out;
713 : : }
714 [ - + - - ]: 6 : if (cls != NULL && cls->dev_iterate == NULL) {
715 : 0 : EAL_LOG(DEBUG, "Class %s not supported", cls->name);
716 : 0 : rte_errno = ENOTSUP;
717 : 0 : goto get_out;
718 : : }
719 : 6 : it->bus_str = devargs.bus_str;
720 : 6 : it->cls_str = devargs.cls_str;
721 : 6 : it->dev_str = dev_str;
722 : 6 : it->bus = bus;
723 : 6 : it->cls = cls;
724 : 6 : it->device = NULL;
725 : 6 : it->class_device = NULL;
726 : 6 : get_out:
727 : 6 : return -rte_errno;
728 : : }
729 : :
730 : : static char *
731 : 12 : dev_str_sane_copy(const char *str)
732 : : {
733 : : size_t end;
734 : : char *copy;
735 : :
736 : 12 : end = strcspn(str, ",/");
737 [ - + ]: 12 : if (str[end] == ',') {
738 : 0 : copy = strdup(&str[end + 1]);
739 : : } else {
740 : : /* '/' or '\0' */
741 : 12 : copy = strdup("");
742 : : }
743 [ - + ]: 12 : if (copy == NULL) {
744 : 0 : rte_errno = ENOMEM;
745 : : } else {
746 : : char *slash;
747 : :
748 : 12 : slash = strchr(copy, '/');
749 [ - + ]: 12 : if (slash != NULL)
750 : 0 : slash[0] = '\0';
751 : : }
752 : 12 : return copy;
753 : : }
754 : :
755 : : static int
756 : 0 : class_next_dev_cmp(const struct rte_class *cls,
757 : : const void *ctx)
758 : : {
759 : : struct rte_dev_iterator *it;
760 : : const char *cls_str = NULL;
761 : : void *dev;
762 : :
763 [ # # ]: 0 : if (cls->dev_iterate == NULL)
764 : : return 1;
765 : 0 : it = ITCTX(ctx);
766 : 0 : cls_str = CLSCTX(ctx);
767 : 0 : dev = it->class_device;
768 : : /* it->cls_str != NULL means a class
769 : : * was specified in the devstr.
770 : : */
771 [ # # # # ]: 0 : if (it->cls_str != NULL && cls != it->cls)
772 : : return 1;
773 : : /* If an error occurred previously,
774 : : * no need to test further.
775 : : */
776 [ # # ]: 0 : if (rte_errno != 0)
777 : : return -1;
778 : 0 : dev = cls->dev_iterate(dev, cls_str, it);
779 : 0 : it->class_device = dev;
780 : 0 : return dev == NULL;
781 : : }
782 : :
783 : : static int
784 : 16 : bus_next_dev_cmp(const struct rte_bus *bus,
785 : : const void *ctx)
786 : : {
787 : : struct rte_device *dev = NULL;
788 : : struct rte_class *cls = NULL;
789 : : struct rte_dev_iterator *it;
790 : : const char *bus_str = NULL;
791 : :
792 [ + + ]: 16 : if (bus->dev_iterate == NULL)
793 : : return 1;
794 : 12 : it = ITCTX(ctx);
795 : 12 : bus_str = BUSCTX(ctx);
796 : 12 : dev = it->device;
797 : : /* it->bus_str != NULL means a bus
798 : : * was specified in the devstr.
799 : : */
800 [ + - + - ]: 12 : if (it->bus_str != NULL && bus != it->bus)
801 : : return 1;
802 : : /* If an error occurred previously,
803 : : * no need to test further.
804 : : */
805 [ + - ]: 12 : if (rte_errno != 0)
806 : : return -1;
807 [ + - ]: 12 : if (it->cls_str == NULL) {
808 : 12 : dev = bus->dev_iterate(bus, dev, bus_str, it);
809 : 12 : goto end;
810 : : }
811 : : /* cls_str != NULL */
812 [ # # ]: 0 : if (dev == NULL) {
813 : 0 : next_dev_on_bus:
814 : 0 : dev = bus->dev_iterate(bus, dev, bus_str, it);
815 : 0 : it->device = dev;
816 : : }
817 [ # # ]: 0 : if (dev == NULL)
818 : : return 1;
819 [ # # ]: 0 : if (it->cls != NULL)
820 : 0 : cls = TAILQ_PREV(it->cls, rte_class_list, next);
821 : 0 : cls = rte_class_find(cls, class_next_dev_cmp, ctx);
822 [ # # ]: 0 : if (cls != NULL) {
823 : 0 : it->cls = cls;
824 : 0 : goto end;
825 : : }
826 : 0 : goto next_dev_on_bus;
827 : 12 : end:
828 : 12 : it->device = dev;
829 : 12 : return dev == NULL;
830 : : }
831 : : RTE_EXPORT_SYMBOL(rte_dev_iterator_next)
832 : : struct rte_device *
833 : 12 : rte_dev_iterator_next(struct rte_dev_iterator *it)
834 : : {
835 : : struct rte_bus *bus = NULL;
836 : 12 : int old_errno = rte_errno;
837 : : char *bus_str = NULL;
838 : : char *cls_str = NULL;
839 : :
840 : 12 : rte_errno = 0;
841 [ - + - - ]: 12 : if (it->bus_str == NULL && it->cls_str == NULL) {
842 : : /* Invalid iterator. */
843 : 0 : rte_errno = EINVAL;
844 : 0 : return NULL;
845 : : }
846 [ + - ]: 12 : if (it->bus != NULL)
847 : 12 : bus = TAILQ_PREV(it->bus, rte_bus_list, next);
848 [ + - ]: 12 : if (it->bus_str != NULL) {
849 : 12 : bus_str = dev_str_sane_copy(it->bus_str);
850 [ - + ]: 12 : if (bus_str == NULL)
851 : 0 : goto out;
852 : : }
853 [ - + ]: 12 : if (it->cls_str != NULL) {
854 : 0 : cls_str = dev_str_sane_copy(it->cls_str);
855 [ # # ]: 0 : if (cls_str == NULL)
856 : 0 : goto out;
857 : : }
858 [ + + ]: 12 : while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
859 : 12 : CTX(it, bus_str, cls_str)))) {
860 [ + - ]: 10 : if (it->device != NULL) {
861 : 10 : it->bus = bus;
862 : 10 : goto out;
863 : : }
864 [ # # ]: 0 : if (it->bus_str != NULL ||
865 [ # # ]: 0 : rte_errno != 0)
866 : : break;
867 : : }
868 [ - + ]: 2 : if (rte_errno == 0)
869 : 2 : rte_errno = old_errno;
870 : 0 : out:
871 : 12 : free(bus_str);
872 : 12 : free(cls_str);
873 : 12 : return it->device;
874 : : }
875 : :
876 : : RTE_EXPORT_SYMBOL(rte_dev_dma_map)
877 : : int
878 : 0 : rte_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova,
879 : : size_t len)
880 : : {
881 [ # # ]: 0 : if (!rte_dev_is_probed(dev)) {
882 : 0 : rte_errno = EINVAL;
883 : 0 : return -1;
884 : : }
885 [ # # # # ]: 0 : if (dev->bus->dma_map == NULL || len == 0) {
886 : 0 : rte_errno = ENOTSUP;
887 : 0 : return -1;
888 : : }
889 : : /* Memory must be registered through rte_extmem_* APIs */
890 [ # # ]: 0 : if (rte_mem_virt2memseg_list(addr) == NULL) {
891 : 0 : rte_errno = EINVAL;
892 : 0 : return -1;
893 : : }
894 : :
895 : 0 : return dev->bus->dma_map(dev, addr, iova, len);
896 : : }
897 : :
898 : : RTE_EXPORT_SYMBOL(rte_dev_dma_unmap)
899 : : int
900 : 0 : rte_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
901 : : size_t len)
902 : : {
903 [ # # ]: 0 : if (!rte_dev_is_probed(dev)) {
904 : 0 : rte_errno = EINVAL;
905 : 0 : return -1;
906 : : }
907 [ # # # # ]: 0 : if (dev->bus->dma_unmap == NULL || len == 0) {
908 : 0 : rte_errno = ENOTSUP;
909 : 0 : return -1;
910 : : }
911 : : /* Memory must be registered through rte_extmem_* APIs */
912 [ # # ]: 0 : if (rte_mem_virt2memseg_list(addr) == NULL) {
913 : 0 : rte_errno = EINVAL;
914 : 0 : return -1;
915 : : }
916 : :
917 : 0 : return dev->bus->dma_unmap(dev, addr, iova, len);
918 : : }
|