bus/vmbus: refactor secondary mapping
authorStephen Hemminger <sthemmin@microsoft.com>
Fri, 8 Feb 2019 03:44:06 +0000 (19:44 -0800)
committerThomas Monjalon <thomas@monjalon.net>
Fri, 29 Mar 2019 12:44:36 +0000 (13:44 +0100)
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 <sthemmin@microsoft.com>
drivers/bus/vmbus/vmbus_common_uio.c

index 9947f82..1aa5cb2 100644 (file)
@@ -23,79 +23,82 @@ static struct rte_tailq_elem vmbus_tailq = {
 };
 EAL_REGISTER_TAILQ(vmbus_tailq)
 
-static int
-vmbus_uio_map_secondary(struct rte_vmbus_device *dev)
+struct mapped_vmbus_resource *
+vmbus_uio_find_resource(const 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 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;
+}
 
-               /* skip this element if it doesn't match our UUID */
-               if (rte_uuid_compare(uio_res->id, dev->device_id) != 0)
-                       continue;
+static int
+vmbus_uio_map_secondary(struct rte_vmbus_device *dev)
+{
+       struct mapped_vmbus_resource *uio_res;
+       struct vmbus_channel *chan;
+       int fd, i;
 
-               /* 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;
-               }
+       uio_res = vmbus_uio_find_resource(dev);
+       if (!uio_res) {
+               VMBUS_LOG(ERR,  "Cannot find resource for device");
+               return -1;
+       }
 
-               for (i = 0; i != uio_res->nb_maps; i++) {
-                       void *mapaddr;
-                       off_t offset = i * PAGE_SIZE;
+       /* 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;
+       }
 
-                       mapaddr = vmbus_map_resource(uio_res->maps[i].addr,
-                                                    fd, offset,
-                                                    uio_res->maps[i].size, 0);
+       for (i = 0; i != uio_res->nb_maps; i++) {
+               void *mapaddr;
+               off_t offset = i * PAGE_SIZE;
 
-                       if (mapaddr == uio_res->maps[i].addr)
-                               continue;
+               mapaddr = vmbus_map_resource(uio_res->maps[i].addr,
+                                            fd, offset,
+                                            uio_res->maps[i].size, 0);
 
-                       VMBUS_LOG(ERR,
-                                 "Cannot mmap device resource file %s to address: %p",
-                                 uio_res->path, uio_res->maps[i].addr);
+               if (mapaddr == uio_res->maps[i].addr)
+                       continue;       /* successful map */
 
-                       if (mapaddr != MAP_FAILED)
-                               /* unmap addr wrongly mapped */
-                               vmbus_unmap_resource(mapaddr,
-                                                    (size_t)uio_res->maps[i].size);
+               if (mapaddr == MAP_FAILED)
+                       VMBUS_LOG(ERR,
+                                 "mmap resource %d in secondary failed", i);
+               else
+                       VMBUS_LOG(ERR,
+                                 "mmap resource %d address mismatch", i);
 
-                       /* 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;
+       }
 
-                       close(fd);
-                       return -1;
-               }
+       /* fd is not needed in slave process, close it */
+       close(fd);
 
-               /* 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;
+       }
 
-               dev->primary = uio_res->primary;
-               if (!dev->primary) {
-                       VMBUS_LOG(ERR, "missing primary channel");
+       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;
                }
-
-               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;
        }
-
-       VMBUS_LOG(ERR,  "Cannot find resource for device");
-       return 1;
+       return 0;
 }
 
 static int
@@ -136,25 +139,6 @@ error:
        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)