eal: allow probing a device again
[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         rte_devargs_remove(dev->devargs);
376
377         return 0;
378 }
379
380 int __rte_experimental
381 rte_dev_remove(struct rte_device *dev)
382 {
383         struct eal_dev_mp_req req;
384         char *devargs;
385         int ret;
386
387         if (!rte_dev_is_probed(dev)) {
388                 RTE_LOG(ERR, EAL, "Device is not probed\n");
389                 return -ENOENT;
390         }
391
392         ret = build_devargs(dev->devargs->bus->name, dev->name, "", &devargs);
393         if (ret != 0)
394                 return ret;
395
396         memset(&req, 0, sizeof(req));
397         req.t = EAL_DEV_REQ_TYPE_DETACH;
398         strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
399         free(devargs);
400
401         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
402                 /**
403                  * If in secondary process, just send IPC request to
404                  * primary process.
405                  */
406                 ret = eal_dev_hotplug_request_to_primary(&req);
407                 if (ret != 0) {
408                         RTE_LOG(ERR, EAL,
409                                 "Failed to send hotplug request to primary\n");
410                         return -ENOMSG;
411                 }
412                 if (req.result != 0)
413                         RTE_LOG(ERR, EAL,
414                                 "Failed to hotplug remove device\n");
415                 return req.result;
416         }
417
418         /* detach a device from primary start from here: */
419
420         /* primary send detach sync request to secondary */
421         ret = eal_dev_hotplug_request_to_secondary(&req);
422
423         /**
424          * if communication error, we need to rollback, because it is possible
425          * part of the secondary processes still detached it successfully.
426          */
427         if (ret != 0) {
428                 RTE_LOG(ERR, EAL,
429                         "Failed to send device detach request to secondary\n");
430                 ret = -ENOMSG;
431                 goto rollback;
432         }
433
434         /**
435          * if any secondary failed to detach, we need to consider if rollback
436          * is necessary.
437          */
438         if (req.result != 0) {
439                 RTE_LOG(ERR, EAL,
440                         "Failed to detach device on secondary process\n");
441                 ret = req.result;
442                 /**
443                  * if -ENOENT, we don't need to rollback, since devices is
444                  * already detached on secondary process.
445                  */
446                 if (ret != -ENOENT)
447                         goto rollback;
448         }
449
450         /* primary detach the device itself. */
451         ret = local_dev_remove(dev);
452
453         /* if primary failed, still need to consider if rollback is necessary */
454         if (ret != 0) {
455                 RTE_LOG(ERR, EAL,
456                         "Failed to detach device on primary process\n");
457                 /* if -ENOENT, we don't need to rollback */
458                 if (ret == -ENOENT)
459                         return ret;
460                 goto rollback;
461         }
462
463         return 0;
464
465 rollback:
466         req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
467
468         /* primary send rollback request to secondary. */
469         if (eal_dev_hotplug_request_to_secondary(&req) != 0)
470                 RTE_LOG(WARNING, EAL,
471                         "Failed to rollback device detach on secondary."
472                         "Devices in secondary may not sync with primary\n");
473
474         return ret;
475 }
476
477 int __rte_experimental
478 rte_dev_event_callback_register(const char *device_name,
479                                 rte_dev_event_cb_fn cb_fn,
480                                 void *cb_arg)
481 {
482         struct dev_event_callback *event_cb;
483         int ret;
484
485         if (!cb_fn)
486                 return -EINVAL;
487
488         rte_spinlock_lock(&dev_event_lock);
489
490         if (TAILQ_EMPTY(&dev_event_cbs))
491                 TAILQ_INIT(&dev_event_cbs);
492
493         TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
494                 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
495                         if (device_name == NULL && event_cb->dev_name == NULL)
496                                 break;
497                         if (device_name == NULL || event_cb->dev_name == NULL)
498                                 continue;
499                         if (!strcmp(event_cb->dev_name, device_name))
500                                 break;
501                 }
502         }
503
504         /* create a new callback. */
505         if (event_cb == NULL) {
506                 event_cb = malloc(sizeof(struct dev_event_callback));
507                 if (event_cb != NULL) {
508                         event_cb->cb_fn = cb_fn;
509                         event_cb->cb_arg = cb_arg;
510                         event_cb->active = 0;
511                         if (!device_name) {
512                                 event_cb->dev_name = NULL;
513                         } else {
514                                 event_cb->dev_name = strdup(device_name);
515                                 if (event_cb->dev_name == NULL) {
516                                         ret = -ENOMEM;
517                                         goto error;
518                                 }
519                         }
520                         TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
521                 } else {
522                         RTE_LOG(ERR, EAL,
523                                 "Failed to allocate memory for device "
524                                 "event callback.");
525                         ret = -ENOMEM;
526                         goto error;
527                 }
528         } else {
529                 RTE_LOG(ERR, EAL,
530                         "The callback is already exist, no need "
531                         "to register again.\n");
532                 ret = -EEXIST;
533         }
534
535         rte_spinlock_unlock(&dev_event_lock);
536         return 0;
537 error:
538         free(event_cb);
539         rte_spinlock_unlock(&dev_event_lock);
540         return ret;
541 }
542
543 int __rte_experimental
544 rte_dev_event_callback_unregister(const char *device_name,
545                                   rte_dev_event_cb_fn cb_fn,
546                                   void *cb_arg)
547 {
548         int ret = 0;
549         struct dev_event_callback *event_cb, *next;
550
551         if (!cb_fn)
552                 return -EINVAL;
553
554         rte_spinlock_lock(&dev_event_lock);
555         /*walk through the callbacks and remove all that match. */
556         for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
557              event_cb = next) {
558
559                 next = TAILQ_NEXT(event_cb, next);
560
561                 if (device_name != NULL && event_cb->dev_name != NULL) {
562                         if (!strcmp(event_cb->dev_name, device_name)) {
563                                 if (event_cb->cb_fn != cb_fn ||
564                                     (cb_arg != (void *)-1 &&
565                                     event_cb->cb_arg != cb_arg))
566                                         continue;
567                         }
568                 } else if (device_name != NULL) {
569                         continue;
570                 }
571
572                 /*
573                  * if this callback is not executing right now,
574                  * then remove it.
575                  */
576                 if (event_cb->active == 0) {
577                         TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
578                         free(event_cb);
579                         ret++;
580                 } else {
581                         continue;
582                 }
583         }
584         rte_spinlock_unlock(&dev_event_lock);
585         return ret;
586 }
587
588 void __rte_experimental
589 rte_dev_event_callback_process(const char *device_name,
590                                enum rte_dev_event_type event)
591 {
592         struct dev_event_callback *cb_lst;
593
594         if (device_name == NULL)
595                 return;
596
597         rte_spinlock_lock(&dev_event_lock);
598
599         TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
600                 if (cb_lst->dev_name) {
601                         if (strcmp(cb_lst->dev_name, device_name))
602                                 continue;
603                 }
604                 cb_lst->active = 1;
605                 rte_spinlock_unlock(&dev_event_lock);
606                 cb_lst->cb_fn(device_name, event,
607                                 cb_lst->cb_arg);
608                 rte_spinlock_lock(&dev_event_lock);
609                 cb_lst->active = 0;
610         }
611         rte_spinlock_unlock(&dev_event_lock);
612 }
613
614 __rte_experimental
615 int
616 rte_dev_iterator_init(struct rte_dev_iterator *it,
617                       const char *dev_str)
618 {
619         struct rte_devargs devargs;
620         struct rte_class *cls = NULL;
621         struct rte_bus *bus = NULL;
622
623         /* Having both bus_str and cls_str NULL is illegal,
624          * marking this iterator as invalid unless
625          * everything goes well.
626          */
627         it->bus_str = NULL;
628         it->cls_str = NULL;
629
630         devargs.data = dev_str;
631         if (rte_devargs_layers_parse(&devargs, dev_str))
632                 goto get_out;
633
634         bus = devargs.bus;
635         cls = devargs.cls;
636         /* The string should have at least
637          * one layer specified.
638          */
639         if (bus == NULL && cls == NULL) {
640                 RTE_LOG(ERR, EAL,
641                         "Either bus or class must be specified.\n");
642                 rte_errno = EINVAL;
643                 goto get_out;
644         }
645         if (bus != NULL && bus->dev_iterate == NULL) {
646                 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
647                 rte_errno = ENOTSUP;
648                 goto get_out;
649         }
650         if (cls != NULL && cls->dev_iterate == NULL) {
651                 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
652                 rte_errno = ENOTSUP;
653                 goto get_out;
654         }
655         it->bus_str = devargs.bus_str;
656         it->cls_str = devargs.cls_str;
657         it->dev_str = dev_str;
658         it->bus = bus;
659         it->cls = cls;
660         it->device = NULL;
661         it->class_device = NULL;
662 get_out:
663         return -rte_errno;
664 }
665
666 static char *
667 dev_str_sane_copy(const char *str)
668 {
669         size_t end;
670         char *copy;
671
672         end = strcspn(str, ",/");
673         if (str[end] == ',') {
674                 copy = strdup(&str[end + 1]);
675         } else {
676                 /* '/' or '\0' */
677                 copy = strdup("");
678         }
679         if (copy == NULL) {
680                 rte_errno = ENOMEM;
681         } else {
682                 char *slash;
683
684                 slash = strchr(copy, '/');
685                 if (slash != NULL)
686                         slash[0] = '\0';
687         }
688         return copy;
689 }
690
691 static int
692 class_next_dev_cmp(const struct rte_class *cls,
693                    const void *ctx)
694 {
695         struct rte_dev_iterator *it;
696         const char *cls_str = NULL;
697         void *dev;
698
699         if (cls->dev_iterate == NULL)
700                 return 1;
701         it = ITCTX(ctx);
702         cls_str = CLSCTX(ctx);
703         dev = it->class_device;
704         /* it->cls_str != NULL means a class
705          * was specified in the devstr.
706          */
707         if (it->cls_str != NULL && cls != it->cls)
708                 return 1;
709         /* If an error occurred previously,
710          * no need to test further.
711          */
712         if (rte_errno != 0)
713                 return -1;
714         dev = cls->dev_iterate(dev, cls_str, it);
715         it->class_device = dev;
716         return dev == NULL;
717 }
718
719 static int
720 bus_next_dev_cmp(const struct rte_bus *bus,
721                  const void *ctx)
722 {
723         struct rte_device *dev = NULL;
724         struct rte_class *cls = NULL;
725         struct rte_dev_iterator *it;
726         const char *bus_str = NULL;
727
728         if (bus->dev_iterate == NULL)
729                 return 1;
730         it = ITCTX(ctx);
731         bus_str = BUSCTX(ctx);
732         dev = it->device;
733         /* it->bus_str != NULL means a bus
734          * was specified in the devstr.
735          */
736         if (it->bus_str != NULL && bus != it->bus)
737                 return 1;
738         /* If an error occurred previously,
739          * no need to test further.
740          */
741         if (rte_errno != 0)
742                 return -1;
743         if (it->cls_str == NULL) {
744                 dev = bus->dev_iterate(dev, bus_str, it);
745                 goto end;
746         }
747         /* cls_str != NULL */
748         if (dev == NULL) {
749 next_dev_on_bus:
750                 dev = bus->dev_iterate(dev, bus_str, it);
751                 it->device = dev;
752         }
753         if (dev == NULL)
754                 return 1;
755         if (it->cls != NULL)
756                 cls = TAILQ_PREV(it->cls, rte_class_list, next);
757         cls = rte_class_find(cls, class_next_dev_cmp, ctx);
758         if (cls != NULL) {
759                 it->cls = cls;
760                 goto end;
761         }
762         goto next_dev_on_bus;
763 end:
764         it->device = dev;
765         return dev == NULL;
766 }
767 __rte_experimental
768 struct rte_device *
769 rte_dev_iterator_next(struct rte_dev_iterator *it)
770 {
771         struct rte_bus *bus = NULL;
772         int old_errno = rte_errno;
773         char *bus_str = NULL;
774         char *cls_str = NULL;
775
776         rte_errno = 0;
777         if (it->bus_str == NULL && it->cls_str == NULL) {
778                 /* Invalid iterator. */
779                 rte_errno = EINVAL;
780                 return NULL;
781         }
782         if (it->bus != NULL)
783                 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
784         if (it->bus_str != NULL) {
785                 bus_str = dev_str_sane_copy(it->bus_str);
786                 if (bus_str == NULL)
787                         goto out;
788         }
789         if (it->cls_str != NULL) {
790                 cls_str = dev_str_sane_copy(it->cls_str);
791                 if (cls_str == NULL)
792                         goto out;
793         }
794         while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
795                                    CTX(it, bus_str, cls_str)))) {
796                 if (it->device != NULL) {
797                         it->bus = bus;
798                         goto out;
799                 }
800                 if (it->bus_str != NULL ||
801                     rte_errno != 0)
802                         break;
803         }
804         if (rte_errno == 0)
805                 rte_errno = old_errno;
806 out:
807         free(bus_str);
808         free(cls_str);
809         return it->device;
810 }