199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright 2018 Gaëtan Rivet
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
599a2dd95SBruce Richardson #include <stdio.h>
699a2dd95SBruce Richardson #include <string.h>
799a2dd95SBruce Richardson #include <sys/queue.h>
899a2dd95SBruce Richardson
999a2dd95SBruce Richardson #include <rte_class.h>
1099a2dd95SBruce Richardson #include <rte_debug.h>
1199a2dd95SBruce Richardson
12*ae67895bSDavid Marchand #include "eal_private.h"
13*ae67895bSDavid Marchand
1499a2dd95SBruce Richardson static struct rte_class_list rte_class_list =
1599a2dd95SBruce Richardson TAILQ_HEAD_INITIALIZER(rte_class_list);
1699a2dd95SBruce Richardson
1799a2dd95SBruce Richardson void
rte_class_register(struct rte_class * class)1899a2dd95SBruce Richardson rte_class_register(struct rte_class *class)
1999a2dd95SBruce Richardson {
2099a2dd95SBruce Richardson RTE_VERIFY(class);
2199a2dd95SBruce Richardson RTE_VERIFY(class->name && strlen(class->name));
2299a2dd95SBruce Richardson
2399a2dd95SBruce Richardson TAILQ_INSERT_TAIL(&rte_class_list, class, next);
24*ae67895bSDavid Marchand EAL_LOG(DEBUG, "Registered [%s] device class.", class->name);
2599a2dd95SBruce Richardson }
2699a2dd95SBruce Richardson
2799a2dd95SBruce Richardson void
rte_class_unregister(struct rte_class * class)2899a2dd95SBruce Richardson rte_class_unregister(struct rte_class *class)
2999a2dd95SBruce Richardson {
3099a2dd95SBruce Richardson TAILQ_REMOVE(&rte_class_list, class, next);
31*ae67895bSDavid Marchand EAL_LOG(DEBUG, "Unregistered [%s] device class.", class->name);
3299a2dd95SBruce Richardson }
3399a2dd95SBruce Richardson
3499a2dd95SBruce Richardson struct rte_class *
rte_class_find(const struct rte_class * start,rte_class_cmp_t cmp,const void * data)3599a2dd95SBruce Richardson rte_class_find(const struct rte_class *start, rte_class_cmp_t cmp,
3699a2dd95SBruce Richardson const void *data)
3799a2dd95SBruce Richardson {
3899a2dd95SBruce Richardson struct rte_class *cls;
3999a2dd95SBruce Richardson
4099a2dd95SBruce Richardson if (start != NULL)
4199a2dd95SBruce Richardson cls = TAILQ_NEXT(start, next);
4299a2dd95SBruce Richardson else
4399a2dd95SBruce Richardson cls = TAILQ_FIRST(&rte_class_list);
4499a2dd95SBruce Richardson while (cls != NULL) {
4599a2dd95SBruce Richardson if (cmp(cls, data) == 0)
4699a2dd95SBruce Richardson break;
4799a2dd95SBruce Richardson cls = TAILQ_NEXT(cls, next);
4899a2dd95SBruce Richardson }
4999a2dd95SBruce Richardson return cls;
5099a2dd95SBruce Richardson }
5199a2dd95SBruce Richardson
5299a2dd95SBruce Richardson static int
cmp_class_name(const struct rte_class * class,const void * _name)5399a2dd95SBruce Richardson cmp_class_name(const struct rte_class *class, const void *_name)
5499a2dd95SBruce Richardson {
5599a2dd95SBruce Richardson const char *name = _name;
5699a2dd95SBruce Richardson
5799a2dd95SBruce Richardson return strcmp(class->name, name);
5899a2dd95SBruce Richardson }
5999a2dd95SBruce Richardson
6099a2dd95SBruce Richardson struct rte_class *
rte_class_find_by_name(const char * name)6199a2dd95SBruce Richardson rte_class_find_by_name(const char *name)
6299a2dd95SBruce Richardson {
6399a2dd95SBruce Richardson return rte_class_find(NULL, cmp_class_name, (const void *)name);
6499a2dd95SBruce Richardson }
65