From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f45.google.com (mail-wm0-f45.google.com [74.125.82.45]) by dpdk.org (Postfix) with ESMTP id C364EC60C for ; Fri, 29 Jan 2016 15:49:19 +0100 (CET) Received: by mail-wm0-f45.google.com with SMTP id p63so71319439wmp.1 for ; Fri, 29 Jan 2016 06:49:19 -0800 (PST) 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; bh=vEfVBr/1mwrbRNLCuclx4iHC//e7rjQrJvs36VvKJQ4=; b=ut8tGydph8MC3WnYssloPBs3jJWQfJCz4W2XTgDh5l0Uer5SXunzBXBxGaWeKIif1b rROfZPkCyH8dwU+ZZAv9rwYO3IhYRl7TWyjFV14tyd8LrX2lGBn4MYmPaIfjdXYzB8eY pdD9PdM19LyDnndufgRqnki/yZ5cQQBmo9ieZ5aeRCK18DUDV2qk3yrOAhJcaS2/CLjn 2JNW8ij2RX9jNmX6JEPDS+dm9jRP6qWcKkzyrz4HlEJhHKUoOPTIPkIUP2rgs2kCHx4g YNQzJ67izZOKIQQ7RF7nlzb1RRA6iZAUhTOpx+IlFtqK+V1v9nxYX0oEJEc8kJ0UPu8b 1ZSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vEfVBr/1mwrbRNLCuclx4iHC//e7rjQrJvs36VvKJQ4=; b=NWm9vqzCu6ifTKGLwFrXibr0VK5JzA8qsW8VyTvwIATEAXQYfap1Fa6gr3yXthLzH2 f/vQBfCdim4b/Ez1mjwGGm2KMydYYnnFGiuByaOwdR44L5hRsyzfD353H+Jo6BTRh0Z1 2xQkPDmvC2I9vOPyHnU61S1JOZdr1uMRuLgK9xUvCB2H+54V1xCTIwZhxx26moXMUok8 GS/RdXgcdAl7XjIL382WadTgydMWKr1FyW9oJclpcjctbDUUtiK5Y/xSL/XSOuz5q1HW nmkqmohNUD+MwV1SjtdzDmS3DHWWlHyjJBkv0F/ltlQtIFv/0h1XJ02IK0k9iQalPia3 8o/g== X-Gm-Message-State: AG10YOTba+m+KMHdPkLW2uZCID9vZnC9ByydadFGGJt0gvK0BgrSb9G0ht29LGyBRGGSV98Q X-Received: by 10.28.228.87 with SMTP id b84mr9068330wmh.81.1454078959629; Fri, 29 Jan 2016 06:49:19 -0800 (PST) Received: from gloops.dev.6wind.com (144.77.126.78.rev.sfr.net. [78.126.77.144]) by smtp.gmail.com with ESMTPSA id y188sm7811294wmy.11.2016.01.29.06.49.18 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 29 Jan 2016 06:49:18 -0800 (PST) From: David Marchand To: dev@dpdk.org Date: Fri, 29 Jan 2016 15:49:05 +0100 Message-Id: <1454078953-23744-2-git-send-email-david.marchand@6wind.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1454078953-23744-1-git-send-email-david.marchand@6wind.com> References: <1453476464-31767-1-git-send-email-david.marchand@6wind.com> <1454078953-23744-1-git-send-email-david.marchand@6wind.com> Cc: viktorin@rehivetech.com Subject: [dpdk-dev] [PATCH v2 1/9] pci: add internal device list helpers X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Jan 2016 14:49:20 -0000 Remove duplicate code by moving this to helpers in common. Signed-off-by: David Marchand --- Changes since v1: - moved helpers to eal_private.h --- lib/librte_eal/bsdapp/eal/eal_pci.c | 34 ++++----------- lib/librte_eal/common/eal_common_pci.c | 76 ++++++++++++++++++++++++---------- lib/librte_eal/common/eal_private.h | 26 ++++++++++++ lib/librte_eal/linuxapp/eal/eal_pci.c | 34 +++++---------- 4 files changed, 99 insertions(+), 71 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c index 5dd89e3..e95249b 100644 --- a/lib/librte_eal/bsdapp/eal/eal_pci.c +++ b/lib/librte_eal/bsdapp/eal/eal_pci.c @@ -247,6 +247,7 @@ static int pci_scan_one(int dev_pci_fd, struct pci_conf *conf) { struct rte_pci_device *dev; + struct rte_pci_device *dev2; struct pci_bar_io bar; unsigned i, max; @@ -311,32 +312,15 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf) dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf); } - /* device is valid, add in list (sorted) */ - if (TAILQ_EMPTY(&pci_device_list)) { - TAILQ_INSERT_TAIL(&pci_device_list, dev, next); - } + dev2 = pci_find_device(&dev->addr); + if (!dev2) + pci_add_device(dev); else { - struct rte_pci_device *dev2 = NULL; - int ret; - - TAILQ_FOREACH(dev2, &pci_device_list, next) { - ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr); - if (ret > 0) - continue; - else if (ret < 0) { - TAILQ_INSERT_BEFORE(dev2, dev, next); - return 0; - } else { /* already registered */ - dev2->kdrv = dev->kdrv; - dev2->max_vfs = dev->max_vfs; - memmove(dev2->mem_resource, - dev->mem_resource, - sizeof(dev->mem_resource)); - free(dev); - return 0; - } - } - TAILQ_INSERT_TAIL(&pci_device_list, dev, next); + dev2->kdrv = dev->kdrv; + dev2->max_vfs = dev->max_vfs; + memmove(dev2->mem_resource, dev->mem_resource, + sizeof(dev->mem_resource)); + free(dev); } return 0; diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c index d9aa66b..c3a33c6 100644 --- a/lib/librte_eal/common/eal_common_pci.c +++ b/lib/librte_eal/common/eal_common_pci.c @@ -139,6 +139,41 @@ pci_unmap_resource(void *requested_addr, size_t size) requested_addr); } +struct rte_pci_device * +pci_find_device(const struct rte_pci_addr *addr) +{ + struct rte_pci_device *dev; + + TAILQ_FOREACH(dev, &pci_device_list, next) { + if (!rte_eal_compare_pci_addr(&dev->addr, addr)) + break; + } + + return dev; +} + +int +pci_add_device(struct rte_pci_device *dev) +{ + struct rte_pci_device *dev2; + int ret; + + TAILQ_FOREACH(dev2, &pci_device_list, next) { + ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr); + if (ret > 0) + continue; + + if (ret == 0) + return -1; + + TAILQ_INSERT_BEFORE(dev2, dev, next); + return 0; + } + + TAILQ_INSERT_TAIL(&pci_device_list, dev, next); + return 0; +} + /* * If vendor/device ID match, call the devinit() function of the * driver. @@ -337,16 +372,15 @@ rte_eal_pci_probe_one(const struct rte_pci_addr *addr) if (pci_refresh_device(addr) < 0) goto err_return; - TAILQ_FOREACH(dev, &pci_device_list, next) { - if (rte_eal_compare_pci_addr(&dev->addr, addr)) - continue; + dev = pci_find_device(addr); + if (!dev) + goto err_return; - ret = pci_probe_all_drivers(dev); - if (ret < 0) - goto err_return; - return 0; - } - return -1; + ret = pci_probe_all_drivers(dev); + if (ret < 0) + goto err_return; + + return 0; err_return: RTE_LOG(WARNING, EAL, @@ -367,23 +401,21 @@ rte_eal_pci_detach(const struct rte_pci_addr *addr) if (addr == NULL) return -1; - TAILQ_FOREACH(dev, &pci_device_list, next) { - if (rte_eal_compare_pci_addr(&dev->addr, addr)) - continue; + dev = pci_find_device(addr); + if (!dev) + goto err_return; - ret = pci_detach_all_drivers(dev); - if (ret < 0) - goto err_return; + ret = pci_detach_all_drivers(dev); + if (ret < 0) + goto err_return; - TAILQ_REMOVE(&pci_device_list, dev, next); - return 0; - } - return -1; + TAILQ_REMOVE(&pci_device_list, dev, next); + return 0; 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); + RTE_LOG(WARNING, EAL, + "Requested device " PCI_PRI_FMT " cannot be detached\n", + addr->domain, addr->bus, addr->devid, addr->function); return -1; } diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index ed1903f..e44a448 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -155,6 +155,32 @@ struct rte_pci_driver; struct rte_pci_device; /** + * Add a pci device to sorted pci device list + * + * This function is private to EAL. + * + * @param dev + * The PCI device object. + * @return + * - 0 on success. + * - negative on error. + */ +int pci_add_device(struct rte_pci_device *dev); + +/** + * Find a pci device object based on a PCI Bus-Device-Function address. + * + * This function is private to EAL. + * + * @param addr + * The PCI Bus-Device-Function address to look for + * @return + * - pointer to the object, + * - NULL when not found. + */ +struct rte_pci_device *pci_find_device(const struct rte_pci_addr *addr); + +/** * Refresh a pci device object by asking the kernel for the latest information. * * This function is private to EAL. diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c index 4fe8b60..d9d6de7 100644 --- a/lib/librte_eal/linuxapp/eal/eal_pci.c +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c @@ -259,6 +259,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus, char filename[PATH_MAX]; unsigned long tmp; struct rte_pci_device *dev; + struct rte_pci_device *dev2; char driver[PATH_MAX]; int ret; @@ -364,30 +365,15 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus, } else dev->kdrv = RTE_KDRV_UNKNOWN; - /* device is valid, add in list (sorted) */ - if (TAILQ_EMPTY(&pci_device_list)) { - TAILQ_INSERT_TAIL(&pci_device_list, dev, next); - } else { - struct rte_pci_device *dev2; - int ret; - - TAILQ_FOREACH(dev2, &pci_device_list, next) { - ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr); - if (ret > 0) - continue; - - if (ret < 0) { - TAILQ_INSERT_BEFORE(dev2, dev, next); - } else { /* already registered */ - dev2->kdrv = dev->kdrv; - dev2->max_vfs = dev->max_vfs; - memmove(dev2->mem_resource, dev->mem_resource, - sizeof(dev->mem_resource)); - free(dev); - } - return 0; - } - TAILQ_INSERT_TAIL(&pci_device_list, dev, next); + dev2 = pci_find_device(&dev->addr); + if (!dev2) + pci_add_device(dev); + else { + dev2->kdrv = dev->kdrv; + dev2->max_vfs = dev->max_vfs; + memmove(dev2->mem_resource, dev->mem_resource, + sizeof(dev->mem_resource)); + free(dev); } return 0; -- 1.9.1