From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f177.google.com (mail-wr0-f177.google.com [209.85.128.177]) by dpdk.org (Postfix) with ESMTP id D98722BCD for ; Tue, 11 Apr 2017 13:07:56 +0200 (CEST) Received: by mail-wr0-f177.google.com with SMTP id o21so138076951wrb.2 for ; Tue, 11 Apr 2017 04:07:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=Ktvoc60UX6AfRZuXfO2ZoYQowSBdEo1c71DQMOJ7F1g=; b=wlhDMdcahPmfM4scumA6NyxIAxT6rMYm+MvwuYzOjcWFF13DDdMWRCPprY54Eavu1q 0Gy9Y4bhx5PeLRV/SNnCw7PmBxwEMP3qysw8j+rm3cOKden9fvhiHTdtZP8d7ujTucuM ayweiyqHmjzpygLZOB71GdBr1CnCADViPdVdv8ctWn1yQnypRD6/tSVs0KL1zO1he+q2 zFrt6zNc5PcN+FFmSzNYybU5DczbcUz0ZaBkz323nUvpZ0BA4dLfbGHt1dx7eMzJRn4+ GDtShU9I+XnH1/UX6hp5GrB+SPQbZ3S7pHrqe/ubguotG2BdfeAeQSVVZHCBnItwwmy7 62EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=Ktvoc60UX6AfRZuXfO2ZoYQowSBdEo1c71DQMOJ7F1g=; b=PSxSFerZpAxPN783kbl8L6zxBpM14UZIqcqUgZk8GfcZjm5pqk0QfmO3gylBRVtnVj rqaOnUMunrUkd3bh7NSwwX0deToZZ/1tmmhVYQaTmkQ29ruSvsQcqb2l/qsq6Da4YgJM 2xKq9mAKCbA8nWkRmvzYh9W0scpQG2onS6y086pTMzSNc+ZtV0vkBXdYpD81Qbykj1TO K69puprYIYsE/ucuB3s1C283PGqgqKyQImGs7K8VZYLtwhr7fiVUE4lPtenR5uBkf5so Q+CxsMO4sce0FEMWb2Jqv6u+0ZdWb4P+3Mo5kdTha3LhtpeDfq/HwOvccCUqb95ynUgr n6JQ== X-Gm-Message-State: AN3rC/5Sl3FgU+suan1uc3jquxrZ1x95qFJ3w43u+f0sI9izSTMJ4QOoJwWdU0CrvDk+ct3e X-Received: by 10.223.131.2 with SMTP id 2mr876558wrd.87.1491908876167; Tue, 11 Apr 2017 04:07:56 -0700 (PDT) Received: from bidouze.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id g15sm1151735wmc.14.2017.04.11.04.07.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 11 Apr 2017 04:07:55 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Shreyansh Jain Date: Tue, 11 Apr 2017 13:07:33 +0200 Message-Id: X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH v13 6/7] pci: use bus driver for attach/detach X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Apr 2017 11:07:57 -0000 From: Shreyansh Jain Given a bus, attach and detach callbacks allow the implementation to handles calls from EAL for attaching or detaching a named device. Signed-off-by: Shreyansh Jain Reviewed-by: Ferruh Yigit Signed-off-by: Gaetan Rivet --- lib/librte_eal/common/eal_bus.h | 45 ++++++++++++++++ lib/librte_eal/common/eal_common_dev.c | 57 ++++++++++++++++----- lib/librte_eal/common/eal_common_pci.c | 91 +++++++++++++++++++++++++++++++-- lib/librte_eal/common/include/rte_bus.h | 29 +++++++++++ lib/librte_eal/common/include/rte_pci.h | 45 ++++++++++------ 5 files changed, 235 insertions(+), 32 deletions(-) create mode 100644 lib/librte_eal/common/eal_bus.h diff --git a/lib/librte_eal/common/eal_bus.h b/lib/librte_eal/common/eal_bus.h new file mode 100644 index 0000000..4148f16 --- /dev/null +++ b/lib/librte_eal/common/eal_bus.h @@ -0,0 +1,45 @@ +/*- + * BSD LICENSE + * + * Copyright 2017 6WIND S.A. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of 6WIND S.A. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _EAL_BUS_H_ +#define _EAL_BUS_H_ + +#include + +#include + +/* Private bus list */ +extern struct rte_bus_list rte_bus_list; + +#define FOREACH_BUS(bus_p) TAILQ_FOREACH(bus_p, &rte_bus_list, next) + +#endif /* _EAL_BUS_H_ */ diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c index 9889997..d7d1f3d 100644 --- a/lib/librte_eal/common/eal_common_dev.c +++ b/lib/librte_eal/common/eal_common_dev.c @@ -38,12 +38,14 @@ #include #include +#include #include #include #include #include #include "eal_private.h" +#include "eal_bus.h" /** Global list of device drivers. */ static struct rte_driver_list dev_driver_list = @@ -107,23 +109,37 @@ rte_eal_dev_init(void) int rte_eal_dev_attach(const char *name, const char *devargs) { - struct rte_pci_addr addr; + int ret = 1; + struct rte_bus *bus; if (name == NULL || devargs == NULL) { RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n"); return -EINVAL; } - if (eal_parse_pci_DomBDF(name, &addr) == 0) { - if (rte_eal_pci_probe_one(&addr) < 0) + FOREACH_BUS(bus) { + if (!bus->attach) { + RTE_LOG(DEBUG, EAL, "Bus (%s) doesn't implement" + " attach.\n", bus->name); + continue; + } + ret = bus->attach(name); + if (!ret) /* device successfully attached */ + return ret; + if (ret > 0) /* device not found on bus */ + continue; + else goto err; + } - } else { - if (rte_eal_vdev_init(name, devargs)) + if (ret > 0) { + /* In case the device was not found on any bus, search VDEV */ + ret = rte_eal_vdev_init(name, devargs); + if (ret) goto err; } - return 0; + return ret; err: RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n", name); @@ -132,21 +148,38 @@ int rte_eal_dev_attach(const char *name, const char *devargs) int rte_eal_dev_detach(const char *name) { - struct rte_pci_addr addr; + int ret = 1; + struct rte_bus *bus; if (name == NULL) { RTE_LOG(ERR, EAL, "Invalid device provided.\n"); return -EINVAL; } - if (eal_parse_pci_DomBDF(name, &addr) == 0) { - if (rte_eal_pci_detach(&addr) < 0) + FOREACH_BUS(bus) { + if (!bus->detach) { + RTE_LOG(DEBUG, EAL, "Bus (%s) doesn't implement" + " detach.\n", bus->name); + continue; + } + + ret = bus->detach(name); + if (!ret) /* device successfully detached */ + return ret; + if (ret > 0) /* device not found on the bus */ + continue; + else goto err; - } else { - if (rte_eal_vdev_uninit(name)) + } + + if (ret > 0) { + /* In case the device was not found on any bus, search VDEV */ + ret = rte_eal_vdev_uninit(name); + if (ret) goto err; } - return 0; + + return ret; err: RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n", name); diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c index 6f0d4d8..c5168d8 100644 --- a/lib/librte_eal/common/eal_common_pci.c +++ b/lib/librte_eal/common/eal_common_pci.c @@ -362,19 +362,98 @@ rte_eal_pci_probe_one(const struct rte_pci_addr *addr) } /* + * Attach device specific by its name + */ +int +rte_eal_pci_attach(const char *device_name) +{ + struct rte_pci_device *dev = NULL; + struct rte_pci_driver *drv = NULL; + struct rte_pci_addr addr; + int ret; + + if (!device_name) + return -1; + + memset(&addr, 0, sizeof(addr)); + if (eal_parse_pci_DomBDF(device_name, &addr)) { + /* Device doesn't match PCI BDF naming format */ + return 1; + } + + if (pci_update_device(&addr) < 0) + goto err_return; + + FOREACH_DEVICE_ON_PCIBUS(dev) { + if (rte_eal_compare_pci_addr(&dev->addr, &addr)) + continue; + + FOREACH_DRIVER_ON_PCIBUS(drv) { + ret = rte_pci_match(drv, dev); + if (ret) { + /* Match of device and driver failed */ + RTE_LOG(DEBUG, EAL, "Driver (%s) doesn't match" + " the device (%s)\n", drv->driver.name, + device_name); + continue; + } + + RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", + dev->id.vendor_id, dev->id.device_id, + drv->driver.name); + + if (drv->drv_flags & RTE_PCI_DRV_NEED_MAPPING) { + /* map resources for devices that use + * igb_uio + */ + ret = rte_eal_pci_map_device(dev); + if (ret != 0) + goto err_return; + } + + /* reference driver structure */ + dev->driver = drv; + + /* call the driver probe() function */ + ret = drv->probe(drv, dev); + if (ret) { + dev->driver = NULL; + if (drv->drv_flags & RTE_PCI_DRV_NEED_MAPPING) + rte_eal_pci_unmap_device(dev); + } + return ret; + } + } + + return 1; +err_return: + RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT + " cannot be used\n", dev->addr.domain, dev->addr.bus, + dev->addr.devid, dev->addr.function); + return -1; +} + +/* * Detach device specified by its pci address. */ int -rte_eal_pci_detach(const struct rte_pci_addr *addr) +rte_eal_pci_detach(const char *device_name) { struct rte_pci_device *dev = NULL; - int ret = 0; + struct rte_pci_addr addr; + int ret = 1; - if (addr == NULL) + if (!device_name) return -1; + memset(&addr, 0, sizeof(addr)); + if (eal_parse_pci_DomBDF(device_name, &addr)) { + /* Device doesn't match PCI BDF naming format */ + return 1; + } + FOREACH_DEVICE_ON_PCIBUS(dev) { - if (rte_eal_compare_pci_addr(&dev->addr, addr)) + if (rte_eal_compare_pci_addr(&dev->addr, &addr)) continue; ret = rte_eal_pci_detach_dev(dev); @@ -389,8 +468,8 @@ rte_eal_pci_detach(const struct rte_pci_addr *addr) free(dev); return 0; } - return -1; + return ret; err_return: RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT " cannot be used\n", dev->addr.domain, dev->addr.bus, @@ -515,6 +594,8 @@ struct rte_pci_bus rte_pci_bus = { .bus = { .scan = rte_eal_pci_scan, .probe = rte_eal_pci_probe, + .attach = rte_eal_pci_attach, + .detach = rte_eal_pci_detach, }, .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list), diff --git a/lib/librte_eal/common/include/rte_bus.h b/lib/librte_eal/common/include/rte_bus.h index 7c36969..e23b9e5 100644 --- a/lib/librte_eal/common/include/rte_bus.h +++ b/lib/librte_eal/common/include/rte_bus.h @@ -82,6 +82,33 @@ typedef int (*rte_bus_scan_t)(void); typedef int (*rte_bus_probe_t)(void); /** + * Attach a device to a bus, assuming it is 'connected' to the bus. + * A bus is responsible for scanning for devices. Attaching a new device is + * for reenabling the device after being detached/removed. + * + * @param device_name + * Name of the device to attach. + * + * @return + * 0 for successful attach + * !0 for unsuccessful attach (or incorrect device name) + */ +typedef int (*rte_bus_attach_t)(const char *device_name); + +/** + * Detach a named device from a bus. Implementation would check the existence + * of device on the bus and detach it. + * + * @param device_name + * Name of the device to detach + * + * @return + * 0 for successful detaching + * !0 if device not found or can't detach + */ +typedef int (*rte_bus_detach_t)(const char *device_name); + +/** * A structure describing a generic bus. */ struct rte_bus { @@ -89,6 +116,8 @@ struct rte_bus { const char *name; /**< Name of the bus */ rte_bus_scan_t scan; /**< Scan for devices attached to bus */ rte_bus_probe_t probe; /**< Probe devices on bus */ + rte_bus_attach_t attach; /**< Attach a named device */ + rte_bus_detach_t detach; /**< Detach a named device */ }; /** diff --git a/lib/librte_eal/common/include/rte_pci.h b/lib/librte_eal/common/include/rte_pci.h index 5980dd1..de0641a 100644 --- a/lib/librte_eal/common/include/rte_pci.h +++ b/lib/librte_eal/common/include/rte_pci.h @@ -403,6 +403,36 @@ int rte_eal_pci_probe(void); /** + * Search and attach a PCI device to PCI Bus + * Implements rte_bus->attach + * + * @param device_name + * Name of the device to search and attach + * + * @return + * 0 for successful removal of device + * >0 if device not found on bus + * <0 in case of error in removal. + */ +int +rte_eal_pci_attach(const char *device_name); + +/** + * Search and detach a PCI device from PCI Bus + * Implements rte_bus->detach + * + * @param device_name + * Name of the device to search and detach + * + * @return + * 0 for successful detaching of device + * >0 if device not found on bus + * <0 in case of error in removal. + */ +int +rte_eal_pci_detach(const char *device_name); + +/** * Map the PCI device resources in user space virtual memory address * * Note that driver should not call this function when flag @@ -476,21 +506,6 @@ void pci_unmap_resource(void *requested_addr, size_t size); int rte_eal_pci_probe_one(const struct rte_pci_addr *addr); /** - * Close the single PCI device. - * - * Scan the content of the PCI bus, and find the pci device specified by pci - * address, then call the remove() function for registered driver that has a - * matching entry in its id_table for discovered device. - * - * @param addr - * The PCI Bus-Device-Function address to close. - * @return - * - 0 on success. - * - Negative on error. - */ -int rte_eal_pci_detach(const struct rte_pci_addr *addr); - -/** * Dump the content of the PCI bus. * * @param f -- 2.1.4