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