From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pd0-f174.google.com (mail-pd0-f174.google.com [209.85.192.174]) by dpdk.org (Postfix) with ESMTP id 5AF2B9AEC for ; Wed, 25 Feb 2015 05:05:28 +0100 (CET) Received: by pdbfl12 with SMTP id fl12so1892800pdb.2 for ; Tue, 24 Feb 2015 20:05:27 -0800 (PST) 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=Dwr1tBuVmEu46v4ElCB85yLa3TaPQU5qqbRNRA6W9cQ=; b=JmOe1a+TdML3+FfjTsApjqRB4fLjaH1nq7MEugxLD6vcxvRhIbH9xW7WKjRvMJJSOq VN7x799bTte3dtX7XVPYo+smbEsJx/0BPgFSOiSnvY0z2NZ6bIdM+yMiRDrO8avapy7i P8DlwJo9TFXtBupXs/9WzjCCAT5JJyc0SnOEuXfMDNGJJjbd13qWsJvcgswnnRRCbeap wUaA0mIyFUvviS3L6qrzBGdc7mQDjFzGR6RFbRxfvueyApzX8WsX2UIJVJQhNf7Ft4HD G/1nUIdILtyPfdlleaLw4+GC67LpboXxB5W0MmLuQeBPH2QPuyTTi5sfYYqdUWVTMrqW NZ+w== X-Gm-Message-State: ALoCoQnv6dq/KYcTZ9NpPc7Vm/YSyrs1Er2gpUv6pmam5Wan3tlpCjp9M7LjWF3J6FTHHfFcGzrG X-Received: by 10.70.37.202 with SMTP id a10mr2083030pdk.108.1424837127780; Tue, 24 Feb 2015 20:05:27 -0800 (PST) Received: from localhost.localdomain (napt.igel.co.jp. [219.106.231.132]) by mx.google.com with ESMTPSA id y7sm3140021pdp.92.2015.02.24.20.05.25 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 24 Feb 2015 20:05:26 -0800 (PST) From: Tetsuya Mukawa To: dev@dpdk.org Date: Wed, 25 Feb 2015 13:04:43 +0900 Message-Id: <1424837093-5661-6-git-send-email-mukawa@igel.co.jp> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1424837093-5661-1-git-send-email-mukawa@igel.co.jp> References: <1424060073-23484-2-git-send-email-mukawa@igel.co.jp> <1424837093-5661-1-git-send-email-mukawa@igel.co.jp> Subject: [dpdk-dev] [PATCH v14 05/13] eal/pci: Consolidate pci address comparison APIs 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: Wed, 25 Feb 2015 04:05:28 -0000 This patch replaces pci_addr_comparison() and memcmp() of pci addresses by rte_eal_compare_pci_addr(). To compare PCI addresses, rte_eal_compare_pci_addr() doesn't use memcmp(). This is because sizeof(struct rte_pci_addr) returns 6, but actually this structure is like below. struct rte_pci_addr { uint16_t domain; /**< Device domain */ uint8_t bus; /**< Device bus */ uint8_t devid; /**< Device ID */ uint8_t function; /**< Device function. */ }; If the structure is dynamically allocated in a function without bzero, last 1 byte may have value. As a result, memcmp may not work. To avoid such a case, rte_eal_compare_pci_addr() compare following values. dev_addr = (addr->domain << 24) | (addr->bus << 16) | (addr->devid << 8) | addr->function; v9: - eal_compare_pci_addr() is replaced by rte_eal_compare_pci_addr(). - Fix commit log. (Thanks to Thomas Monjalon) v8: - Fix pci_scan_one() to update sysfs values. (Thanks to Qiu, Michael and Iremonger, Bernard) v5: - Fix pci_scan_one to handle pt_driver correctly. v4: - Fix calculation method of eal_compare_pci_addr(). - Add parameter checking. Signed-off-by: Tetsuya Mukawa --- lib/librte_eal/bsdapp/eal/eal_pci.c | 29 ++++++++++++-------------- lib/librte_eal/common/eal_common_pci.c | 2 +- lib/librte_eal/common/include/rte_pci.h | 34 +++++++++++++++++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_pci.c | 30 +++++++++++++-------------- lib/librte_eal/linuxapp/eal/eal_pci_uio.c | 2 +- 5 files changed, 63 insertions(+), 34 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c index 74ecce7..9193f80 100644 --- a/lib/librte_eal/bsdapp/eal/eal_pci.c +++ b/lib/librte_eal/bsdapp/eal/eal_pci.c @@ -270,20 +270,6 @@ pci_uio_map_resource(struct rte_pci_device *dev) return (0); } -/* Compare two PCI device addresses. */ -static int -pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2) -{ - uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function; - uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function; - - if (dev_addr > dev_addr2) - return 1; - else - return 0; -} - - /* Scan one pci sysfs entry, and fill the devices list from it. */ static int pci_scan_one(int dev_pci_fd, struct pci_conf *conf) @@ -356,13 +342,24 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf) } else { struct rte_pci_device *dev2 = NULL; + int ret; TAILQ_FOREACH(dev2, &pci_device_list, next) { - if (pci_addr_comparison(&dev->addr, &dev2->addr)) + ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr); + if (ret > 0) continue; - else { + else if (ret < 0) { TAILQ_INSERT_BEFORE(dev2, dev, next); return 0; + } else { /* already registered */ + /* update pt_driver */ + dev2->pt_driver = dev->pt_driver; + 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); diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c index f3c7f71..bf2793f 100644 --- a/lib/librte_eal/common/eal_common_pci.c +++ b/lib/librte_eal/common/eal_common_pci.c @@ -93,7 +93,7 @@ static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev) if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI && devargs->type != RTE_DEVTYPE_WHITELISTED_PCI) continue; - if (!memcmp(&dev->addr, &devargs->pci.addr, sizeof(dev->addr))) + if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr)) return devargs; } return NULL; diff --git a/lib/librte_eal/common/include/rte_pci.h b/lib/librte_eal/common/include/rte_pci.h index 255a77b..dcf9c81 100644 --- a/lib/librte_eal/common/include/rte_pci.h +++ b/lib/librte_eal/common/include/rte_pci.h @@ -272,6 +272,40 @@ eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr) } #undef GET_PCIADDR_FIELD +/* Compare two PCI device addresses. */ +/** + * Utility function to compare two PCI device addresses. + * + * @param addr + * The PCI Bus-Device-Function address to compare + * @param addr2 + * The PCI Bus-Device-Function address to compare + * @return + * 0 on equal PCI address. + * Positive on addr is greater than addr2. + * Negative on addr is less than addr2, or error. + */ +static inline int +rte_eal_compare_pci_addr(struct rte_pci_addr *addr, struct rte_pci_addr *addr2) +{ + uint64_t dev_addr, dev_addr2; + + if ((addr == NULL) || (addr2 == NULL)) + return -1; + + dev_addr = (addr->domain << 24) | (addr->bus << 16) | + (addr->devid << 8) | addr->function; + dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) | + (addr2->devid << 8) | addr2->function; + + if (dev_addr > dev_addr2) + return 1; + else if (dev_addr < dev_addr2) + return -1; + else + return 0; +} + /** * Probe the PCI bus for registered drivers. * diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c index 3291c68..06bfc1a 100644 --- a/lib/librte_eal/linuxapp/eal/eal_pci.c +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "rte_pci_dev_ids.h" #include "eal_filesystem.h" @@ -228,20 +229,6 @@ error: return -1; } -/* Compare two PCI device addresses. */ -static int -pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2) -{ - uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function; - uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function; - - if (dev_addr > dev_addr2) - return 1; - else - return 0; -} - - /* Scan one pci sysfs entry, and fill the devices list from it. */ static int pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus, @@ -359,13 +346,24 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus, } else { struct rte_pci_device *dev2 = NULL; + int ret; TAILQ_FOREACH(dev2, &pci_device_list, next) { - if (pci_addr_comparison(&dev->addr, &dev2->addr)) + ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr); + if (ret > 0) continue; - else { + else if (ret < 0) { TAILQ_INSERT_BEFORE(dev2, dev, next); return 0; + } else { /* already registered */ + /* update pt_driver */ + dev2->pt_driver = dev->pt_driver; + 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); diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c b/lib/librte_eal/linuxapp/eal/eal_pci_uio.c index 0a95376..c5e0cf3 100644 --- a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c +++ b/lib/librte_eal/linuxapp/eal/eal_pci_uio.c @@ -91,7 +91,7 @@ pci_uio_map_secondary(struct rte_pci_device *dev) TAILQ_FOREACH(uio_res, pci_res_list, next) { /* skip this element if it doesn't match our PCI address */ - if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr))) + if (rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr)) continue; for (i = 0; i != uio_res->nb_maps; i++) { -- 1.9.1