From 2528d17199643388ba924a531dc6dcc4c66cfecf Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Thu, 7 Feb 2019 19:44:06 -0800 Subject: [PATCH] bus/vmbus: refactor secondary mapping The secondary mapping function was duplicating the code used to search the uio_resource list. Skip the unwinding since map failure already makes device unusable. Signed-off-by: Stephen Hemminger --- drivers/bus/vmbus/vmbus_common_uio.c | 154 ++++++++++++--------------- 1 file changed, 69 insertions(+), 85 deletions(-) diff --git a/drivers/bus/vmbus/vmbus_common_uio.c b/drivers/bus/vmbus/vmbus_common_uio.c index 9947f82ab1..1aa5cb2e4b 100644 --- a/drivers/bus/vmbus/vmbus_common_uio.c +++ b/drivers/bus/vmbus/vmbus_common_uio.c @@ -23,79 +23,82 @@ static struct rte_tailq_elem vmbus_tailq = { }; EAL_REGISTER_TAILQ(vmbus_tailq) +struct mapped_vmbus_resource * +vmbus_uio_find_resource(const struct rte_vmbus_device *dev) +{ + struct mapped_vmbus_resource *uio_res; + struct mapped_vmbus_res_list *uio_res_list = + RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list); + + if (dev == NULL) + return NULL; + + TAILQ_FOREACH(uio_res, uio_res_list, next) { + if (rte_uuid_compare(uio_res->id, dev->device_id) == 0) + return uio_res; + } + return NULL; +} + static int vmbus_uio_map_secondary(struct rte_vmbus_device *dev) { - int fd, i; - struct vmbus_channel *chan; struct mapped_vmbus_resource *uio_res; - struct mapped_vmbus_res_list *uio_res_list - = RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list); + struct vmbus_channel *chan; + int fd, i; - TAILQ_FOREACH(uio_res, uio_res_list, next) { - - /* skip this element if it doesn't match our UUID */ - if (rte_uuid_compare(uio_res->id, dev->device_id) != 0) - continue; - - /* open /dev/uioX */ - fd = open(uio_res->path, O_RDWR); - if (fd < 0) { - VMBUS_LOG(ERR, "Cannot open %s: %s", - uio_res->path, strerror(errno)); - return -1; - } - - for (i = 0; i != uio_res->nb_maps; i++) { - void *mapaddr; - off_t offset = i * PAGE_SIZE; - - mapaddr = vmbus_map_resource(uio_res->maps[i].addr, - fd, offset, - uio_res->maps[i].size, 0); - - if (mapaddr == uio_res->maps[i].addr) - continue; - - VMBUS_LOG(ERR, - "Cannot mmap device resource file %s to address: %p", - uio_res->path, uio_res->maps[i].addr); - - if (mapaddr != MAP_FAILED) - /* unmap addr wrongly mapped */ - vmbus_unmap_resource(mapaddr, - (size_t)uio_res->maps[i].size); - - /* unmap addrs correctly mapped */ - while (--i >= 0) - vmbus_unmap_resource(uio_res->maps[i].addr, - (size_t)uio_res->maps[i].size); - - close(fd); - return -1; - } - - /* fd is not needed in slave process, close it */ - close(fd); - - dev->primary = uio_res->primary; - if (!dev->primary) { - VMBUS_LOG(ERR, "missing primary channel"); - return -1; - } - - STAILQ_FOREACH(chan, &dev->primary->subchannel_list, next) { - if (vmbus_uio_map_secondary_subchan(dev, chan) != 0) { - VMBUS_LOG(ERR, "cannot map secondary subchan"); - return -1; - } - } - - return 0; + uio_res = vmbus_uio_find_resource(dev); + if (!uio_res) { + VMBUS_LOG(ERR, "Cannot find resource for device"); + return -1; } - VMBUS_LOG(ERR, "Cannot find resource for device"); - return 1; + /* open /dev/uioX */ + fd = open(uio_res->path, O_RDWR); + if (fd < 0) { + VMBUS_LOG(ERR, "Cannot open %s: %s", + uio_res->path, strerror(errno)); + return -1; + } + + for (i = 0; i != uio_res->nb_maps; i++) { + void *mapaddr; + off_t offset = i * PAGE_SIZE; + + mapaddr = vmbus_map_resource(uio_res->maps[i].addr, + fd, offset, + uio_res->maps[i].size, 0); + + if (mapaddr == uio_res->maps[i].addr) + continue; /* successful map */ + + if (mapaddr == MAP_FAILED) + VMBUS_LOG(ERR, + "mmap resource %d in secondary failed", i); + else + VMBUS_LOG(ERR, + "mmap resource %d address mismatch", i); + + close(fd); + return -1; + } + + /* fd is not needed in slave process, close it */ + close(fd); + + dev->primary = uio_res->primary; + if (!dev->primary) { + VMBUS_LOG(ERR, "missing primary channel"); + return -1; + } + + STAILQ_FOREACH(chan, &dev->primary->subchannel_list, next) { + if (vmbus_uio_map_secondary_subchan(dev, chan) != 0) { + VMBUS_LOG(ERR, "cannot map secondary subchan"); + return -1; + } + } + return 0; } static int @@ -136,25 +139,6 @@ vmbus_uio_map_primary(struct rte_vmbus_device *dev) return -1; } - -struct mapped_vmbus_resource * -vmbus_uio_find_resource(const struct rte_vmbus_device *dev) -{ - struct mapped_vmbus_resource *uio_res; - struct mapped_vmbus_res_list *uio_res_list = - RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list); - - if (dev == NULL) - return NULL; - - TAILQ_FOREACH(uio_res, uio_res_list, next) { - /* skip this element if it doesn't match our VMBUS address */ - if (rte_uuid_compare(uio_res->id, dev->device_id) == 0) - return uio_res; - } - return NULL; -} - /* map the VMBUS resource of a VMBUS device in virtual memory */ int vmbus_uio_map_resource(struct rte_vmbus_device *dev)