15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   Copyright(c) 2014 6WIND S.A.
65129044dSC.J. Collier *   All rights reserved.
75129044dSC.J. Collier *
85129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
95129044dSC.J. Collier *   modification, are permitted provided that the following conditions
105129044dSC.J. Collier *   are met:
115129044dSC.J. Collier *
125129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
135129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
145129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
155129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
165129044dSC.J. Collier *       the documentation and/or other materials provided with the
175129044dSC.J. Collier *       distribution.
185129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
195129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
205129044dSC.J. Collier *       from this software without specific prior written permission.
215129044dSC.J. Collier *
225129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
235129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
245129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
255129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
265129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
275129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
285129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
295129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
305129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
315129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
325129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
335129044dSC.J. Collier */
345129044dSC.J. Collier
355129044dSC.J. Collier#include <stdio.h>
365129044dSC.J. Collier#include <string.h>
375129044dSC.J. Collier#include <inttypes.h>
385129044dSC.J. Collier#include <sys/queue.h>
395129044dSC.J. Collier
405129044dSC.J. Collier#include <rte_dev.h>
415129044dSC.J. Collier#include <rte_devargs.h>
425129044dSC.J. Collier#include <rte_debug.h>
435129044dSC.J. Collier#include <rte_devargs.h>
445129044dSC.J. Collier#include <rte_log.h>
455129044dSC.J. Collier
465129044dSC.J. Collier#include "eal_private.h"
475129044dSC.J. Collier
485129044dSC.J. Collier/** Global list of device drivers. */
495129044dSC.J. Collierstatic struct rte_driver_list dev_driver_list =
505129044dSC.J. Collier	TAILQ_HEAD_INITIALIZER(dev_driver_list);
513d9b7210SChristian Ehrhardt/** Global list of device drivers. */
523d9b7210SChristian Ehrhardtstatic struct rte_device_list dev_device_list =
533d9b7210SChristian Ehrhardt	TAILQ_HEAD_INITIALIZER(dev_device_list);
545129044dSC.J. Collier
555129044dSC.J. Collier/* register a driver */
565129044dSC.J. Colliervoid
575129044dSC.J. Collierrte_eal_driver_register(struct rte_driver *driver)
585129044dSC.J. Collier{
595129044dSC.J. Collier	TAILQ_INSERT_TAIL(&dev_driver_list, driver, next);
605129044dSC.J. Collier}
615129044dSC.J. Collier
625129044dSC.J. Collier/* unregister a driver */
635129044dSC.J. Colliervoid
645129044dSC.J. Collierrte_eal_driver_unregister(struct rte_driver *driver)
655129044dSC.J. Collier{
665129044dSC.J. Collier	TAILQ_REMOVE(&dev_driver_list, driver, next);
675129044dSC.J. Collier}
685129044dSC.J. Collier
693d9b7210SChristian Ehrhardtvoid rte_eal_device_insert(struct rte_device *dev)
705129044dSC.J. Collier{
713d9b7210SChristian Ehrhardt	TAILQ_INSERT_TAIL(&dev_device_list, dev, next);
723d9b7210SChristian Ehrhardt}
735129044dSC.J. Collier
743d9b7210SChristian Ehrhardtvoid rte_eal_device_remove(struct rte_device *dev)
753d9b7210SChristian Ehrhardt{
763d9b7210SChristian Ehrhardt	TAILQ_REMOVE(&dev_device_list, dev, next);
775129044dSC.J. Collier}
785129044dSC.J. Collier
795129044dSC.J. Collierint
805129044dSC.J. Collierrte_eal_dev_init(void)
815129044dSC.J. Collier{
825129044dSC.J. Collier	struct rte_devargs *devargs;
835129044dSC.J. Collier
845129044dSC.J. Collier	/*
855129044dSC.J. Collier	 * Note that the dev_driver_list is populated here
865129044dSC.J. Collier	 * from calls made to rte_eal_driver_register from constructor functions
873d9b7210SChristian Ehrhardt	 * embedded into PMD modules via the RTE_PMD_REGISTER_VDEV macro
885129044dSC.J. Collier	 */
895129044dSC.J. Collier
905129044dSC.J. Collier	/* call the init function for each virtual device */
915129044dSC.J. Collier	TAILQ_FOREACH(devargs, &devargs_list, next) {
925129044dSC.J. Collier
935129044dSC.J. Collier		if (devargs->type != RTE_DEVTYPE_VIRTUAL)
945129044dSC.J. Collier			continue;
955129044dSC.J. Collier
965129044dSC.J. Collier		if (rte_eal_vdev_init(devargs->virt.drv_name,
975129044dSC.J. Collier					devargs->args)) {
985129044dSC.J. Collier			RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
995129044dSC.J. Collier					devargs->virt.drv_name);
1005129044dSC.J. Collier			return -1;
1015129044dSC.J. Collier		}
1025129044dSC.J. Collier	}
1035129044dSC.J. Collier
1045129044dSC.J. Collier	return 0;
1055129044dSC.J. Collier}
1065129044dSC.J. Collier
1073d9b7210SChristian Ehrhardtint rte_eal_dev_attach(const char *name, const char *devargs)
1085129044dSC.J. Collier{
1093d9b7210SChristian Ehrhardt	struct rte_pci_addr addr;
1105129044dSC.J. Collier
1113d9b7210SChristian Ehrhardt	if (name == NULL || devargs == NULL) {
1123d9b7210SChristian Ehrhardt		RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
1135129044dSC.J. Collier		return -EINVAL;
1143d9b7210SChristian Ehrhardt	}
1155129044dSC.J. Collier
1163d9b7210SChristian Ehrhardt	if (eal_parse_pci_DomBDF(name, &addr) == 0) {
1173d9b7210SChristian Ehrhardt		if (rte_eal_pci_probe_one(&addr) < 0)
1183d9b7210SChristian Ehrhardt			goto err;
1193d9b7210SChristian Ehrhardt
1203d9b7210SChristian Ehrhardt	} else {
1213d9b7210SChristian Ehrhardt		if (rte_eal_vdev_init(name, devargs))
1223d9b7210SChristian Ehrhardt			goto err;
1233d9b7210SChristian Ehrhardt	}
1243d9b7210SChristian Ehrhardt
1253d9b7210SChristian Ehrhardt	return 0;
1263d9b7210SChristian Ehrhardt
1273d9b7210SChristian Ehrhardterr:
1283d9b7210SChristian Ehrhardt	RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n", name);
1293d9b7210SChristian Ehrhardt	return -EINVAL;
1303d9b7210SChristian Ehrhardt}
1313d9b7210SChristian Ehrhardt
1323d9b7210SChristian Ehrhardtint rte_eal_dev_detach(const char *name)
1333d9b7210SChristian Ehrhardt{
1343d9b7210SChristian Ehrhardt	struct rte_pci_addr addr;
1355129044dSC.J. Collier
1363d9b7210SChristian Ehrhardt	if (name == NULL) {
1373d9b7210SChristian Ehrhardt		RTE_LOG(ERR, EAL, "Invalid device provided.\n");
1383d9b7210SChristian Ehrhardt		return -EINVAL;
1395129044dSC.J. Collier	}
1405129044dSC.J. Collier
1413d9b7210SChristian Ehrhardt	if (eal_parse_pci_DomBDF(name, &addr) == 0) {
1423d9b7210SChristian Ehrhardt		if (rte_eal_pci_detach(&addr) < 0)
1433d9b7210SChristian Ehrhardt			goto err;
1443d9b7210SChristian Ehrhardt	} else {
1453d9b7210SChristian Ehrhardt		if (rte_eal_vdev_uninit(name))
1463d9b7210SChristian Ehrhardt			goto err;
1473d9b7210SChristian Ehrhardt	}
1483d9b7210SChristian Ehrhardt	return 0;
1493d9b7210SChristian Ehrhardt
1503d9b7210SChristian Ehrhardterr:
1513d9b7210SChristian Ehrhardt	RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n", name);
1525129044dSC.J. Collier	return -EINVAL;
1535129044dSC.J. Collier}
154