From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f48.google.com (mail-wm0-f48.google.com [74.125.82.48]) by dpdk.org (Postfix) with ESMTP id 1B1DF919D for ; Fri, 22 Jan 2016 16:27:54 +0100 (CET) Received: by mail-wm0-f48.google.com with SMTP id u188so23035614wmu.1 for ; Fri, 22 Jan 2016 07:27:54 -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=B61tmHUyH2Fv5IQ1MImFl4V0D9Ks0Sm9WqTiTBnihMM=; b=qxDYUZ3FtZe+XV17epSKTGlcIDIaXcpaKjHQrOaPjN5UnZSZMLkdDm8XE1HGIE4jy6 XZftIufKKKBZYPERTBrrlIra7lLHYfDiMY8vubQnRgpzTP7qMySfU0GyHY4MNxH56kZc uvSIxF9qTu+hvHZf7Wa5NBTVH3CHsmwjxq5lwyV30fwwVyiBgcP/kHwbRGcRPdDxF8JM xu+vSKq3ZZmSUcUNlhYbouhuuUG76Z35Ycmlha+r+K1f1CF0boUI6tPLhMNl1+ktS+Zx 3CrXldOT2wRlx04opX0BuZWiKS9/4Ul5f7TuuzrfVz4X+0EXp8bmbk5EPRBIhxPmKR6B F5nQ== 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=B61tmHUyH2Fv5IQ1MImFl4V0D9Ks0Sm9WqTiTBnihMM=; b=akdyf34swmbihZ6/iOUf6rDFGRBr3Kuo6yVBvO+MWlBKOBHzfIDqQlsGqkr7bs5wv1 k3mGMs1QzLVEJm6yr6ikC4PKufZwurCoJ8ogkW1ADxq60er3tht5ZiY47lcVnt3rztdh Fyqgpzmp4GBTp6yt2QC9No0B4kjf2Ue3dB9PZBrgO7+Wavoqt4LNTDt2K+Eq+cTQvC9S +4xzoE1TcwlT8vDNNc9hTp8jw4DQ2oDfP95CqmY+kOCn8jNAX03K+X4FcJ+jtG4jX8dT 4xvK5KhGSlezhKy8bzGNzAn3TyQnKUaHMhppB6xj7HeI8TH43Ci+EHSEKMkhxxZOMYsq Dpnw== X-Gm-Message-State: AG10YOTAHhEgcLzWqrkQ7AhCsxzzmtPUrvYK6KAnUFvTt6MNfmAlPerFyi5T8X0w69zuZpLD X-Received: by 10.194.193.106 with SMTP id hn10mr3736136wjc.134.1453476473858; Fri, 22 Jan 2016 07:27:53 -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 w73sm3359464wmw.21.2016.01.22.07.27.52 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 22 Jan 2016 07:27:53 -0800 (PST) From: David Marchand To: dev@dpdk.org Date: Fri, 22 Jan 2016 16:27:37 +0100 Message-Id: <1453476464-31767-3-git-send-email-david.marchand@6wind.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453476464-31767-1-git-send-email-david.marchand@6wind.com> References: <1453476464-31767-1-git-send-email-david.marchand@6wind.com> Cc: viktorin@rehivetech.com Subject: [dpdk-dev] [PATCH 2/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, 22 Jan 2016 15:27:54 -0000 Remove duplicate code by moving this to helpers in common. Signed-off-by: David Marchand --- lib/librte_eal/bsdapp/eal/eal_pci.c | 34 +++++----------- lib/librte_eal/common/eal_common_pci.c | 70 ++++++++++++++++++++++++--------- lib/librte_eal/common/include/rte_pci.h | 24 +++++++++++ lib/librte_eal/linuxapp/eal/eal_pci.c | 34 +++++----------- 4 files changed, 94 insertions(+), 68 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c index 4584522..401cda3 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 1e12776..2528775 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. @@ -332,16 +367,15 @@ rte_eal_pci_probe_one(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_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, "Requested device " PCI_PRI_FMT @@ -362,18 +396,16 @@ 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 diff --git a/lib/librte_eal/common/include/rte_pci.h b/lib/librte_eal/common/include/rte_pci.h index 334c12e..4ba7192 100644 --- a/lib/librte_eal/common/include/rte_pci.h +++ b/lib/librte_eal/common/include/rte_pci.h @@ -399,6 +399,30 @@ void *pci_map_resource(void *requested_addr, int fd, off_t offset, void pci_unmap_resource(void *requested_addr, size_t size); /** + * @internal + * Add a pci device to sorted pci device list + * + * @param dev + * The PCI device object. + * @return + * - 0 on success. + * - negative on error. + */ +int pci_add_device(struct rte_pci_device *dev); + +/** + * @internal + * Find a pci device object based on a PCI Bus-Device-Function address. + * + * @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); + +/** * Probe the single PCI device. * * Scan the content of the PCI bus, and find the pci device specified by pci diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c index a354f76..83eec5d 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