From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pd0-f176.google.com (mail-pd0-f176.google.com [209.85.192.176]) by dpdk.org (Postfix) with ESMTP id 1F8249AB1 for ; Thu, 12 Mar 2015 11:18:35 +0100 (CET) Received: by pdjy10 with SMTP id y10so18974461pdj.8 for ; Thu, 12 Mar 2015 03:18:34 -0700 (PDT) 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=j80Sx5iZ/er1YC8m185zQtL7o3DYf+msFaAV53xWJbA=; b=CCx/hDgXwPq1SwxS7//bYi/3/ZoAiA+UzpFxCW3f3OCXBNhGbWzdhJw7YUt1Osk3eZ H4jKRnjOBFPVEwUwYDW30mBqw8dVJDMOFS5GFfJ/syXs6VHKGZnLXsxCXcultjQXiSRs VFtVTEL/jX5Svm3HxJtyh1KG6nMuJTuoImUbHoQprcF9O9QCcuxlUMloHbHjnnmTjWGp v+jxRf/tNc3V3T4SR4sm5WoIi0aoyluoueu7lm29rOG/RemI5xE4dY+I4rffMAaLpcHi oHrjqBExZxojYoTVrgGk8m+V/QbHrkKaYMT7qrfOKKWCUhJI8YDUyyzqWo7KpsyaW4JP SVpA== X-Gm-Message-State: ALoCoQnh8ooBIpfkZFy/Qui6IZU6ChlAWFzS7BCv6FXi2c38t0DGsHECV9uNXit35ka1WV2Wrtua X-Received: by 10.70.128.15 with SMTP id nk15mr87098801pdb.121.1426155514525; Thu, 12 Mar 2015 03:18:34 -0700 (PDT) Received: from localhost.localdomain (napt.igel.co.jp. [219.106.231.132]) by mx.google.com with ESMTPSA id z4sm10080331pdi.90.2015.03.12.03.18.32 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 12 Mar 2015 03:18:34 -0700 (PDT) From: Tetsuya Mukawa To: dev@dpdk.org Date: Thu, 12 Mar 2015 19:17:48 +0900 Message-Id: <1426155474-1596-10-git-send-email-mukawa@igel.co.jp> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1426155474-1596-1-git-send-email-mukawa@igel.co.jp> References: <1425438703-18895-1-git-send-email-mukawa@igel.co.jp> <1426155474-1596-1-git-send-email-mukawa@igel.co.jp> Subject: [dpdk-dev] [PATCH v2 09/15] eal: Add pci_uio_map_uio_resource_by_index() 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: Thu, 12 Mar 2015 10:18:35 -0000 This patch adds a new function called pci_uio_map_resource_by_index(). The function hides how to map uio resource in linuxapp and bsdapp. With the function, pci_uio_map_resource() will be more abstracted. Signed-off-by: Tetsuya Mukawa --- lib/librte_eal/bsdapp/eal/eal_pci.c | 108 +++++++++++++++----------- lib/librte_eal/linuxapp/eal/eal_pci_uio.c | 124 +++++++++++++++++------------- 2 files changed, 135 insertions(+), 97 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c index f88cd15..3e63835 100644 --- a/lib/librte_eal/bsdapp/eal/eal_pci.c +++ b/lib/librte_eal/bsdapp/eal/eal_pci.c @@ -245,20 +245,73 @@ pci_uio_alloc_uio_resource(struct rte_pci_device *dev, return 0; } -/* map the PCI resource of a PCI device in virtual memory */ static int -pci_uio_map_resource(struct rte_pci_device *dev) +pci_uio_map_uio_resource_by_index(struct rte_pci_device *dev, int res_idx, + struct mapped_pci_resource *uio_res, int map_idx) { - int i, map_idx, ret; + int fd; char *devname; void *mapaddr; - uint64_t phaddr; uint64_t offset; uint64_t pagesz; + struct pci_map *maps; + + if ((dev == NULL) || (uio_res == NULL) || (uio_res->path == NULL)) + return -1; + + maps = uio_res->maps; + devname = uio_res->path; + pagesz = sysconf(_SC_PAGESIZE); + + /* allocate memory to keep path */ + maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0); + if (maps[map_idx].path == NULL) { + RTE_LOG(ERR, EAL, "Cannot allocate memory for " + "path: %s\n", strerror(errno)); + return -1; + } + + /* + * open resource file, 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; + } + + /* if matching map is found, then use it */ + offset = res_idx * pagesz; + mapaddr = pci_map_resource(NULL, fd, (off_t)offset, + (size_t)dev->mem_resource[res_idx].len, 0); + close(fd); + if (mapaddr == NULL) + goto fail; + + maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr; + maps[map_idx].size = dev->mem_resource[res_idx].len; + maps[map_idx].addr = mapaddr; + maps[map_idx].offset = offset; + strcpy(maps[map_idx].path, devname); + dev->mem_resource[res_idx].addr = mapaddr; + + return 0; + +fail: + rte_free(maps[map_idx].path); + return -1; +} + +/* map the PCI resource of a PCI device in virtual memory */ +static int +pci_uio_map_resource(struct rte_pci_device *dev) +{ + int i, map_idx, ret; + uint64_t phaddr; struct mapped_pci_resource *uio_res = NULL; struct mapped_pci_res_list *uio_res_list = RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list); - struct pci_map *maps; dev->intr_handle.fd = -1; dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN; @@ -273,51 +326,18 @@ pci_uio_map_resource(struct rte_pci_device *dev) return ret; /* Map all BARs */ - pagesz = sysconf(_SC_PAGESIZE); - devname = uio_res->path; - - maps = uio_res->maps; for (i = 0, map_idx = 0; i != PCI_MAX_RESOURCE; i++) { - int fd; - /* skip empty BAR */ phaddr = dev->mem_resource[i].phys_addr; if (phaddr == 0) continue; - /* allocate memory to keep path */ - maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0); - if (maps[map_idx].path == NULL) { - RTE_LOG(ERR, EAL, "Cannot allocate memory for " - "path: %s\n", strerror(errno)); - goto fail0; - } - - /* - * open resource file, to mmap it - */ - fd = open(devname, O_RDWR); - if (fd < 0) { - RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", - devname, strerror(errno)); - goto fail0; - } + ret = pci_uio_map_uio_resource_by_index(dev, i, + uio_res, map_idx); + if (ret != 0) + goto fail; - /* if matching map is found, then use it */ - offset = i * pagesz; - mapaddr = pci_map_resource(NULL, fd, (off_t)offset, - (size_t)dev->mem_resource[i].len, 0); - close(fd); - if (mapaddr == NULL) - goto fail1; - - maps[map_idx].phaddr = dev->mem_resource[i].phys_addr; - maps[map_idx].size = dev->mem_resource[i].len; - maps[map_idx].addr = mapaddr; - maps[map_idx].offset = offset; - strcpy(maps[map_idx].path, devname); map_idx++; - dev->mem_resource[i].addr = mapaddr; } uio_res->nb_maps = map_idx; @@ -326,9 +346,7 @@ pci_uio_map_resource(struct rte_pci_device *dev) return 0; -fail1: - rte_free(maps[map_idx].path); -fail0: +fail: rte_free(uio_res); return -1; } diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c b/lib/librte_eal/linuxapp/eal/eal_pci_uio.c index 099c413..6fca796 100644 --- a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c +++ b/lib/librte_eal/linuxapp/eal/eal_pci_uio.c @@ -327,19 +327,82 @@ pci_uio_alloc_uio_resource(struct rte_pci_device *dev, return 0; } +static int +pci_uio_map_uio_resource_by_index(struct rte_pci_device *dev, int res_idx, + struct mapped_pci_resource *uio_res, int map_idx) +{ + int fd; + char devname[PATH_MAX]; /* contains the /dev/uioX */ + void *mapaddr; + struct rte_pci_addr *loc; + struct pci_map *maps; + + if ((dev == NULL) || (uio_res == NULL) || (uio_res->path == NULL)) + return -1; + + loc = &dev->addr; + maps = uio_res->maps; + + /* update devname for mmap */ + snprintf(devname, sizeof(devname), + SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d", + loc->domain, loc->bus, loc->devid, + loc->function, res_idx); + + /* allocate memory to keep path */ + maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0); + if (maps[map_idx].path == NULL) { + RTE_LOG(ERR, EAL, "Cannot allocate memory for " + "path: %s\n", strerror(errno)); + return -1; + } + + /* + * open resource file, 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; + } + + /* try mapping somewhere close to the end of hugepages */ + if (pci_map_addr == NULL) + pci_map_addr = pci_find_max_end_va(); + + mapaddr = pci_map_resource(pci_map_addr, fd, 0, + (size_t)dev->mem_resource[res_idx].len, 0); + close(fd); + if (mapaddr == MAP_FAILED) + goto fail; + + pci_map_addr = RTE_PTR_ADD(mapaddr, + (size_t)dev->mem_resource[res_idx].len); + + maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr; + maps[map_idx].size = dev->mem_resource[res_idx].len; + maps[map_idx].addr = mapaddr; + maps[map_idx].offset = 0; + strcpy(maps[map_idx].path, devname); + dev->mem_resource[res_idx].addr = mapaddr; + + return 0; + +fail: + rte_free(maps[map_idx].path); + return -1; +} + /* map the PCI resource of a PCI device in virtual memory */ int pci_uio_map_resource(struct rte_pci_device *dev) { int i, map_idx, ret; - char devname[PATH_MAX]; /* contains the /dev/uioX */ - void *mapaddr; uint64_t phaddr; - struct rte_pci_addr *loc = &dev->addr; struct mapped_pci_resource *uio_res = NULL; struct mapped_pci_res_list *uio_res_list = RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list); - struct pci_map *maps; dev->intr_handle.fd = -1; dev->intr_handle.uio_cfg_fd = -1; @@ -355,59 +418,18 @@ pci_uio_map_resource(struct rte_pci_device *dev) return ret; /* Map all BARs */ - maps = uio_res->maps; for (i = 0, map_idx = 0; i != PCI_MAX_RESOURCE; i++) { - int fd; - /* skip empty BAR */ phaddr = dev->mem_resource[i].phys_addr; if (phaddr == 0) continue; - /* update devname for mmap */ - snprintf(devname, sizeof(devname), - SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d", - loc->domain, loc->bus, loc->devid, - loc->function, i); - - /* allocate memory to keep path */ - maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0); - if (maps[map_idx].path == NULL) { - RTE_LOG(ERR, EAL, "Cannot allocate memory for " - "path: %s\n", strerror(errno)); - goto fail0; - } - - /* - * open resource file, to mmap it - */ - fd = open(devname, O_RDWR); - if (fd < 0) { - RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", - devname, strerror(errno)); - goto fail0; - } - - /* try mapping somewhere close to the end of hugepages */ - if (pci_map_addr == NULL) - pci_map_addr = pci_find_max_end_va(); - - mapaddr = pci_map_resource(pci_map_addr, fd, 0, - (size_t)dev->mem_resource[i].len, 0); - close(fd); - if (mapaddr == MAP_FAILED) - goto fail1; + ret = pci_uio_map_uio_resource_by_index(dev, i, + uio_res, map_idx); + if (ret != 0) + goto fail; - pci_map_addr = RTE_PTR_ADD(mapaddr, - (size_t)dev->mem_resource[i].len); - - maps[map_idx].phaddr = dev->mem_resource[i].phys_addr; - maps[map_idx].size = dev->mem_resource[i].len; - maps[map_idx].addr = mapaddr; - maps[map_idx].offset = 0; - strcpy(maps[map_idx].path, devname); map_idx++; - dev->mem_resource[i].addr = mapaddr; } uio_res->nb_maps = map_idx; @@ -416,9 +438,7 @@ pci_uio_map_resource(struct rte_pci_device *dev) return 0; -fail1: - rte_free(maps[map_idx].path); -fail0: +fail: rte_free(uio_res); return -1; } -- 1.9.1