From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by dpdk.org (Postfix) with ESMTP id C2EDCB0C6 for ; Wed, 28 May 2014 16:38:47 +0200 (CEST) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP; 28 May 2014 07:38:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.98,928,1392192000"; d="scan'208";a="546191675" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga002.fm.intel.com with ESMTP; 28 May 2014 07:38:31 -0700 Received: from sivswdev02.ir.intel.com (sivswdev02.ir.intel.com [10.237.217.46]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id s4SEcUuK017276; Wed, 28 May 2014 15:38:30 +0100 Received: from sivswdev02.ir.intel.com (localhost [127.0.0.1]) by sivswdev02.ir.intel.com with ESMTP id s4SEcUf6008712; Wed, 28 May 2014 15:38:30 +0100 Received: (from aburakov@localhost) by sivswdev02.ir.intel.com with id s4SEcUB0008708; Wed, 28 May 2014 15:38:30 +0100 From: Anatoly Burakov To: dev@dpdk.org Date: Wed, 28 May 2014 15:37:58 +0100 Message-Id: <89a46e4b7e06d94b3ebdd90f5c9af36d5aa3edfb.1401287651.git.anatoly.burakov@intel.com> X-Mailer: git-send-email 1.7.0.7 In-Reply-To: <1400514709-24087-1-git-send-email-anatoly.burakov@intel.com> References: <1400514709-24087-1-git-send-email-anatoly.burakov@intel.com> In-Reply-To: References: Subject: [dpdk-dev] [PATCH v3 01/20] pci: move open() out of pci_map_resource, rename structs 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, 28 May 2014 14:38:49 -0000 Separating mapping code and calls to open. This is a preparatory work for VFIO patch since it'll need to map BARs too but it doesn't use path in mapped_pci_resource. Also, renaming structs to be more generic. Signed-off-by: Anatoly Burakov --- lib/librte_eal/linuxapp/eal/eal_pci.c | 125 ++++++++++++++++------------------ 1 file changed, 58 insertions(+), 67 deletions(-) diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c index ac2c1fe..fd88bd0 100644 --- a/lib/librte_eal/linuxapp/eal/eal_pci.c +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c @@ -31,39 +31,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include #include -#include -#include -#include -#include #include #include -#include -#include #include -#include -#include #include -#include -#include #include #include -#include -#include -#include -#include #include -#include #include -#include -#include #include -#include -#include #include #include "rte_pci_dev_ids.h" @@ -74,15 +52,12 @@ * @file * PCI probing under linux * - * This code is used to simulate a PCI probe by parsing information in - * sysfs. Moreover, when a registered driver matches a device, the - * kernel driver currently using it is unloaded and replaced by - * igb_uio module, which is a very minimal userland driver for Intel - * network card, only providing access to PCI BAR to applications, and - * enabling bus master. + * This code is used to simulate a PCI probe by parsing information in sysfs. + * When a registered device matches a driver, it is then initialized with + * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it). */ -struct uio_map { +struct pci_map { void *addr; uint64_t offset; uint64_t size; @@ -93,18 +68,18 @@ struct uio_map { * For multi-process we need to reproduce all PCI mappings in secondary * processes, so save them in a tailq. */ -struct uio_resource { - TAILQ_ENTRY(uio_resource) next; +struct mapped_pci_resource { + TAILQ_ENTRY(mapped_pci_resource) next; struct rte_pci_addr pci_addr; char path[PATH_MAX]; - size_t nb_maps; - struct uio_map maps[PCI_MAX_RESOURCE]; + int nb_maps; + struct pci_map maps[PCI_MAX_RESOURCE]; }; -TAILQ_HEAD(uio_res_list, uio_resource); +TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource); +static struct mapped_pci_res_list *pci_res_list; -static struct uio_res_list *uio_res_list = NULL; static int pci_parse_sysfs_value(const char *filename, uint64_t *val); /* unbind kernel driver for this device */ @@ -148,30 +123,17 @@ error: /* map a particular resource from a file */ static void * -pci_map_resource(void *requested_addr, const char *devname, off_t offset, - size_t size) +pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size) { - int fd; void *mapaddr; - /* - * open devname, to mmap it - */ - fd = open(devname, O_RDWR); - if (fd < 0) { - RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", - devname, strerror(errno)); - goto fail; - } - /* Map the PCI memory resource of device */ mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); - close(fd); if (mapaddr == MAP_FAILED || (requested_addr != NULL && mapaddr != requested_addr)) { - RTE_LOG(ERR, EAL, "%s(): cannot mmap(%s(%d), %p, 0x%lx, 0x%lx):" - " %s (%p)\n", __func__, devname, fd, requested_addr, + RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n", + __func__, fd, requested_addr, (unsigned long)size, (unsigned long)offset, strerror(errno), mapaddr); goto fail; @@ -186,10 +148,10 @@ fail: } #define OFF_MAX ((uint64_t)(off_t)-1) -static ssize_t -pci_uio_get_mappings(const char *devname, struct uio_map maps[], size_t nb_maps) +static int +pci_uio_get_mappings(const char *devname, struct pci_map maps[], int nb_maps) { - size_t i; + int i; char dirname[PATH_MAX]; char filename[PATH_MAX]; uint64_t offset, size; @@ -249,25 +211,37 @@ pci_uio_get_mappings(const char *devname, struct uio_map maps[], size_t nb_maps) static int pci_uio_map_secondary(struct rte_pci_device *dev) { - size_t i; - struct uio_resource *uio_res; + int fd, i; + struct mapped_pci_resource *uio_res; - TAILQ_FOREACH(uio_res, uio_res_list, next) { + 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))) continue; for (i = 0; i != uio_res->nb_maps; i++) { - if (pci_map_resource(uio_res->maps[i].addr, - uio_res->path, + /* + * open devname, to mmap it + */ + fd = open(uio_res->path, O_RDWR); + if (fd < 0) { + RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", + uio_res->path, strerror(errno)); + return -1; + } + + if (pci_map_resource(uio_res->maps[i].addr, fd, (off_t)uio_res->maps[i].offset, (size_t)uio_res->maps[i].size) != uio_res->maps[i].addr) { RTE_LOG(ERR, EAL, "Cannot mmap device resource\n"); + close(fd); return (-1); } + /* fd is not needed in slave process, close it */ + close(fd); } return (0); } @@ -276,7 +250,8 @@ pci_uio_map_secondary(struct rte_pci_device *dev) return -1; } -static int pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num) +static int +pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num) { FILE *f; char filename[PATH_MAX]; @@ -323,7 +298,8 @@ static int pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num) * sysfs. On error, return a negative value. In this case dstbuf is * invalid. */ -static int pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf, +static int +pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf, unsigned int buflen) { struct rte_pci_addr *loc = &dev->addr; @@ -405,10 +381,10 @@ pci_uio_map_resource(struct rte_pci_device *dev) uint64_t phaddr; uint64_t offset; uint64_t pagesz; - ssize_t nb_maps; + int nb_maps; struct rte_pci_addr *loc = &dev->addr; - struct uio_resource *uio_res; - struct uio_map *maps; + struct mapped_pci_resource *uio_res; + struct pci_map *maps; dev->intr_handle.fd = -1; dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN; @@ -460,6 +436,7 @@ pci_uio_map_resource(struct rte_pci_device *dev) maps = uio_res->maps; for (i = 0; i != PCI_MAX_RESOURCE; i++) { + int fd; /* skip empty BAR */ if ((phaddr = dev->mem_resource[i].phys_addr) == 0) @@ -473,14 +450,27 @@ pci_uio_map_resource(struct rte_pci_device *dev) /* if matching map is found, then use it */ if (j != nb_maps) { offset = j * pagesz; + + /* + * open devname, to mmap it + */ + fd = open(devname, O_RDWR); + if (fd < 0) { + RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", + devname, strerror(errno)); + return -1; + } + if (maps[j].addr != NULL || - (mapaddr = pci_map_resource(NULL, devname, + (mapaddr = pci_map_resource(NULL, fd, (off_t)offset, (size_t)maps[j].size) ) == NULL) { rte_free(uio_res); + close(fd); return (-1); } + close(fd); maps[j].addr = mapaddr; maps[j].offset = offset; @@ -488,7 +478,7 @@ pci_uio_map_resource(struct rte_pci_device *dev) } } - TAILQ_INSERT_TAIL(uio_res_list, uio_res, next); + TAILQ_INSERT_TAIL(pci_res_list, uio_res, next); return (0); } @@ -866,7 +856,8 @@ rte_eal_pci_init(void) { TAILQ_INIT(&pci_driver_list); TAILQ_INIT(&pci_device_list); - uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list); + pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, + mapped_pci_res_list); /* for debug purposes, PCI can be disabled */ if (internal_config.no_pci) -- 1.8.1.4