/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2016 NXP
 */

#include <stdio.h>
#include <string.h>
#include <sys/queue.h>

#include <bus_driver.h>
#include <rte_debug.h>
#include <rte_devargs.h>
#include <rte_kvargs.h>
#include <rte_string_fns.h>
#include <rte_errno.h>

#include <eal_export.h>
#include "eal_private.h"

static struct rte_bus_list rte_bus_list =
	TAILQ_HEAD_INITIALIZER(rte_bus_list);

RTE_EXPORT_SYMBOL(rte_bus_name)
const char *
rte_bus_name(const struct rte_bus *bus)
{
	return bus->name;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_register)
void
rte_bus_register(struct rte_bus *bus)
{
	RTE_VERIFY(bus);
	RTE_VERIFY(rte_bus_name(bus) && strlen(rte_bus_name(bus)));
	/* A bus should mandatorily have the scan implemented */
	RTE_VERIFY(bus->scan);
	RTE_VERIFY(bus->probe);
	RTE_VERIFY(bus->find_device);

	/* A bus providing probe_device requires match. */
	RTE_VERIFY(!bus->probe_device || bus->match);

	TAILQ_INIT(&bus->device_list);
	TAILQ_INIT(&bus->driver_list);
	TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
	EAL_LOG(DEBUG, "Registered [%s] bus.", rte_bus_name(bus));
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_unregister)
void
rte_bus_unregister(struct rte_bus *bus)
{
	TAILQ_REMOVE(&rte_bus_list, bus, next);
	EAL_LOG(DEBUG, "Unregistered [%s] bus.", rte_bus_name(bus));
}

/* Scan all the buses for registered devices */
RTE_EXPORT_SYMBOL(rte_bus_scan)
int
rte_bus_scan(void)
{
	int ret;
	struct rte_bus *bus = NULL;

	TAILQ_FOREACH(bus, &rte_bus_list, next) {
		ret = bus->scan();
		if (ret)
			EAL_LOG(ERR, "Scan for (%s) bus failed.",
				rte_bus_name(bus));
	}

	return 0;
}

/*
 * Generic probe function for buses.
 * Iterates through all devices on the bus, finds matching drivers,
 * and calls bus->probe_device() for each device.
 */
RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_generic_probe)
int
rte_bus_generic_probe(struct rte_bus *bus)
{
	size_t probed = 0, failed = 0;
	struct rte_device *dev;

	TAILQ_FOREACH(dev, &bus->device_list, next) {
		struct rte_driver *drv = NULL;
		bool was_probed;
		int ret;

		if (rte_bus_device_is_ignored(bus, dev->name))
			continue;

		if (rte_dev_is_probed(dev) && !bus->allow_multi_probe) {
			EAL_LOG(INFO, "Device %s is already probed", dev->name);
			continue;
		}

		probed++;
next_driver:
		drv = rte_bus_find_driver(bus, drv, dev);
		if (drv == NULL)
			continue;

		was_probed = rte_dev_is_probed(dev);
		if (!was_probed)
			dev->driver = drv;
		ret = bus->probe_device(drv, dev);
		if (!was_probed && ret != 0)
			dev->driver = NULL;

		if (ret > 0)
			goto next_driver;

		if (ret < 0) {
			if (ret == -EEXIST)
				continue;
			EAL_LOG(ERR, "Failed to probe device %s", dev->name);
			failed++;
		}
	}

	return (probed && probed == failed) ? -1 : 0;
}

/*
 * Generic cleanup function for buses.
 * Iterates through all devices on the bus, unplugs probed devices,
 * removes devargs, removes devices from the bus list, and frees device structures.
 */
RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_generic_cleanup)
int
rte_bus_generic_cleanup(struct rte_bus *bus)
{
	struct rte_device *dev;
	int error = 0;

	RTE_VERIFY(bus->free_device);
	RTE_VERIFY(bus->unplug_device);

	while ((dev = TAILQ_FIRST(&bus->device_list)) != NULL) {
		if (rte_dev_is_probed(dev)) {
			if (bus->unplug_device(dev) < 0) {
				rte_errno = errno;
				error = -1;
			}
		}

		rte_devargs_remove(dev->devargs);
		rte_bus_remove_device(bus, dev);
		bus->free_device(dev);
	}

	return error;
}

/* Probe all devices of all buses */
RTE_EXPORT_SYMBOL(rte_bus_probe)
int
rte_bus_probe(void)
{
	int ret;
	struct rte_bus *bus, *vbus = NULL;

	TAILQ_FOREACH(bus, &rte_bus_list, next) {
		if (!strcmp(rte_bus_name(bus), "vdev")) {
			vbus = bus;
			continue;
		}

		ret = bus->probe(bus);
		if (ret)
			EAL_LOG(ERR, "Bus (%s) probe failed.",
				rte_bus_name(bus));
	}

	if (vbus) {
		ret = vbus->probe(vbus);
		if (ret)
			EAL_LOG(ERR, "Bus (%s) probe failed.",
				rte_bus_name(vbus));
	}

	return 0;
}

/* Clean up all devices of all buses */
int
eal_bus_cleanup(void)
{
	int ret = 0;
	struct rte_bus *bus;

	TAILQ_FOREACH(bus, &rte_bus_list, next) {
		if (bus->cleanup == NULL)
			continue;
		if (bus->cleanup(bus) != 0)
			ret = -1;
	}

	return ret;
}

/* Dump information of a single bus */
static int
bus_dump_one(FILE *f, struct rte_bus *bus)
{
	int ret;

	/* For now, dump only the bus name */
	ret = fprintf(f, " %s\n", rte_bus_name(bus));

	/* Error in case of inability in writing to stream */
	if (ret < 0)
		return ret;

	return 0;
}

RTE_EXPORT_SYMBOL(rte_bus_dump)
void
rte_bus_dump(FILE *f)
{
	int ret;
	struct rte_bus *bus;

	TAILQ_FOREACH(bus, &rte_bus_list, next) {
		ret = bus_dump_one(f, bus);
		if (ret) {
			EAL_LOG(ERR, "Unable to write to stream (%d)",
				ret);
			break;
		}
	}
}

RTE_EXPORT_SYMBOL(rte_bus_find)
struct rte_bus *
rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
	     const void *data)
{
	struct rte_bus *bus;

	if (start != NULL)
		bus = TAILQ_NEXT(start, next);
	else
		bus = TAILQ_FIRST(&rte_bus_list);
	while (bus != NULL) {
		if (cmp(bus, data) == 0)
			break;
		bus = TAILQ_NEXT(bus, next);
	}
	return bus;
}

static int
cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
{
	const struct rte_device *dev2 = _dev2;

	return dev1 != dev2;
}

static int
bus_find_device(const struct rte_bus *bus, const void *_dev)
{
	struct rte_device *dev;

	dev = bus->find_device(bus, NULL, cmp_rte_device, _dev);
	return dev == NULL;
}

RTE_EXPORT_SYMBOL(rte_bus_find_by_device)
struct rte_bus *
rte_bus_find_by_device(const struct rte_device *dev)
{
	return rte_bus_find(NULL, bus_find_device, (const void *)dev);
}

static int
cmp_bus_name(const struct rte_bus *bus, const void *_name)
{
	const char *name = _name;

	return strcmp(rte_bus_name(bus), name);
}

RTE_EXPORT_SYMBOL(rte_bus_find_by_name)
struct rte_bus *
rte_bus_find_by_name(const char *busname)
{
	return rte_bus_find(NULL, cmp_bus_name, (const void *)busname);
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_find_devargs)
struct rte_devargs *
rte_bus_find_devargs(const struct rte_bus *bus, const char *name)
{
	rte_bus_dev_compare_t cmp = bus->dev_compare;
	const char *bus_name = rte_bus_name(bus);
	struct rte_devargs *devargs;

	if (cmp == NULL)
		cmp = strcmp;

	RTE_EAL_DEVARGS_FOREACH(bus_name, devargs) {
		const char *devargs_name = devargs->name;

		/* The name in the devargs is usually prefixed with <bus>: */
		if (strncmp(devargs_name, bus_name, strlen(bus_name)) == 0)
			devargs_name += strlen(bus_name) + 1;
		if (cmp(name, devargs_name) != 0)
			continue;
		EAL_LOG(DEBUG, "found devargs for %s:%s", bus_name, name);
		return devargs;
	}

	EAL_LOG(DEBUG, "no devargs for %s:%s", bus_name, name);
	return NULL;
}

static int
bus_can_parse(const struct rte_bus *bus, const void *_name)
{
	const char *name = _name;

	return !(bus->parse && bus->parse(name, NULL) == 0);
}

struct rte_bus *
rte_bus_find_by_device_name(const char *str)
{
	char name[RTE_DEV_NAME_MAX_LEN];
	char *c;

	strlcpy(name, str, sizeof(name));
	c = strchr(name, ',');
	if (c != NULL)
		c[0] = '\0';
	return rte_bus_find(NULL, bus_can_parse, name);
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_device_is_ignored)
bool
rte_bus_device_is_ignored(const struct rte_bus *bus, const char *dev_name)
{
	const struct internal_config *internal_conf = eal_get_internal_configuration();
	struct rte_devargs *devargs = rte_bus_find_devargs(bus, dev_name);
	enum rte_bus_scan_mode scan_mode = bus->conf.scan_mode;

	if (scan_mode == RTE_BUS_SCAN_UNDEFINED) {
		if (internal_conf->no_auto_probing != 0)
			scan_mode = RTE_BUS_SCAN_ALLOWLIST;
		else
			scan_mode = RTE_BUS_SCAN_BLOCKLIST;
	}

	if (scan_mode == RTE_BUS_SCAN_ALLOWLIST) {
		if (devargs && devargs->policy == RTE_DEV_ALLOWED)
			return false;
	} else {
		if (devargs == NULL || devargs->policy != RTE_DEV_BLOCKED)
			return false;
	}

	EAL_LOG(DEBUG, "device %s:%s is ignored", rte_bus_name(bus), dev_name);
	return true;
}

/*
 * Get iommu class of devices on the bus.
 */
RTE_EXPORT_SYMBOL(rte_bus_get_iommu_class)
enum rte_iova_mode
rte_bus_get_iommu_class(void)
{
	enum rte_iova_mode mode = RTE_IOVA_DC;
	bool buses_want_va = false;
	bool buses_want_pa = false;
	struct rte_bus *bus;

	TAILQ_FOREACH(bus, &rte_bus_list, next) {
		enum rte_iova_mode bus_iova_mode;

		if (bus->get_iommu_class == NULL)
			continue;

		bus_iova_mode = bus->get_iommu_class();
		EAL_LOG(DEBUG, "Bus %s wants IOVA as '%s'",
			rte_bus_name(bus),
			bus_iova_mode == RTE_IOVA_DC ? "DC" :
			(bus_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
		if (bus_iova_mode == RTE_IOVA_PA) {
			buses_want_pa = true;
			if (!RTE_IOVA_IN_MBUF)
				EAL_LOG(WARNING,
					"Bus %s wants IOVA as PA not compatible with 'enable_iova_as_pa=false' build option.",
					rte_bus_name(bus));
		} else if (bus_iova_mode == RTE_IOVA_VA)
			buses_want_va = true;
	}
	if (buses_want_va && !buses_want_pa) {
		mode = RTE_IOVA_VA;
	} else if (buses_want_pa && !buses_want_va) {
		mode = RTE_IOVA_PA;
	} else {
		mode = RTE_IOVA_DC;
		if (buses_want_va) {
			EAL_LOG(WARNING, "Some buses want 'VA' but forcing 'DC' because other buses want 'PA'.");
			EAL_LOG(WARNING, "Depending on the final decision by the EAL, not all buses may be able to initialize.");
		}
	}

	return mode;
}

static int
bus_handle_sigbus(const struct rte_bus *bus,
			const void *failure_addr)
{
	int ret;

	if (!bus->sigbus_handler)
		return -1;

	ret = bus->sigbus_handler(failure_addr);

	/* find bus but handle failed, keep the errno be set. */
	if (ret < 0 && rte_errno == 0)
		rte_errno = ENOTSUP;

	return ret > 0;
}

int
rte_bus_sigbus_handler(const void *failure_addr)
{
	struct rte_bus *bus;

	int ret = 0;
	int old_errno = rte_errno;

	rte_errno = 0;

	bus = rte_bus_find(NULL, bus_handle_sigbus, failure_addr);
	/* can not find bus. */
	if (!bus)
		return 1;
	/* find bus but handle failed, pass on the new errno. */
	else if (rte_errno != 0)
		return -1;

	/* restore the old errno. */
	rte_errno = old_errno;

	return ret;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_add_device)
void
rte_bus_add_device(struct rte_bus *bus, struct rte_device *dev)
{
	TAILQ_INSERT_TAIL(&bus->device_list, dev, next);
	dev->bus = bus;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_remove_device)
void
rte_bus_remove_device(struct rte_bus *bus, struct rte_device *dev)
{
	TAILQ_REMOVE(&bus->device_list, dev, next);
	dev->bus = NULL;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_insert_device)
void
rte_bus_insert_device(struct rte_bus *bus,
		      struct rte_device *exist_dev,
		      struct rte_device *new_dev)
{
	TAILQ_INSERT_BEFORE(exist_dev, new_dev, next);
	new_dev->bus = bus;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_generic_find_device)
struct rte_device *
rte_bus_generic_find_device(const struct rte_bus *bus, const struct rte_device *start,
		    rte_dev_cmp_t cmp, const void *data)
{
	struct rte_device *dev;

	if (start != NULL)
		dev = TAILQ_NEXT(start, next);
	else
		dev = TAILQ_FIRST(&bus->device_list);
	while (dev != NULL) {
		if (cmp(dev, data) == 0)
			return dev;
		dev = TAILQ_NEXT(dev, next);
	}
	return NULL;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_add_driver)
void
rte_bus_add_driver(struct rte_bus *bus, struct rte_driver *driver)
{
	TAILQ_INSERT_TAIL(&bus->driver_list, driver, next);
	driver->bus = bus;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_remove_driver)
void
rte_bus_remove_driver(struct rte_bus *bus, struct rte_driver *driver)
{
	TAILQ_REMOVE(&bus->driver_list, driver, next);
	driver->bus = NULL;
}

static int
bus_dev_match_by_name(const struct rte_device *dev, const void *_kvlist)
{
	const struct rte_kvargs *kvlist = _kvlist;
	const char *name;

	if (kvlist == NULL)
		return 0;

	name = rte_kvargs_get(kvlist, "name");
	if (name != NULL && strcmp(name, dev->name))
		return -1;

	return 0;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_generic_dev_iterate)
void *
rte_bus_generic_dev_iterate(const struct rte_bus *bus,
			     const void *start,
			     const char *str,
			     const struct rte_dev_iterator *it __rte_unused)
{
	static const char * const params_keys[] = { "name", NULL };
	struct rte_kvargs *kvargs = NULL;
	struct rte_device *dev;

	if (str != NULL) {
		kvargs = rte_kvargs_parse(str, params_keys);
		if (kvargs == NULL) {
			rte_errno = EINVAL;
			return NULL;
		}
	}

	dev = rte_bus_generic_find_device(bus, start, bus_dev_match_by_name, kvargs);
	rte_kvargs_free(kvargs);
	return dev;
}

RTE_EXPORT_INTERNAL_SYMBOL(rte_bus_find_driver)
struct rte_driver *
rte_bus_find_driver(const struct rte_bus *bus, const struct rte_driver *start,
	const struct rte_device *dev)
{
	struct rte_driver *drv;

	if (start != NULL)
		drv = TAILQ_NEXT(start, next);
	else
		drv = TAILQ_FIRST(&bus->driver_list);
	while (drv != NULL) {
		if (bus->match(drv, dev))
			break;
		drv = TAILQ_NEXT(drv, next);
	}
	return drv;
}
