New upstream version 18.08
[deb_dpdk.git] / drivers / bus / vmbus / vmbus_common_uio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2018, Microsoft Corporation.
3  * All Rights Reserved.
4  */
5
6 #include <fcntl.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/mman.h>
11
12 #include <rte_eal.h>
13 #include <rte_tailq.h>
14 #include <rte_log.h>
15 #include <rte_malloc.h>
16 #include <rte_bus.h>
17 #include <rte_bus_vmbus.h>
18
19 #include "private.h"
20
21 static struct rte_tailq_elem vmbus_tailq = {
22         .name = "VMBUS_RESOURCE_LIST",
23 };
24 EAL_REGISTER_TAILQ(vmbus_tailq)
25
26 static int
27 vmbus_uio_map_secondary(struct rte_vmbus_device *dev)
28 {
29         int fd, i;
30         struct mapped_vmbus_resource *uio_res;
31         struct mapped_vmbus_res_list *uio_res_list
32                 = RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
33
34         TAILQ_FOREACH(uio_res, uio_res_list, next) {
35
36                 /* skip this element if it doesn't match our UUID */
37                 if (rte_uuid_compare(uio_res->id, dev->device_id) != 0)
38                         continue;
39
40                 /* open /dev/uioX */
41                 fd = open(uio_res->path, O_RDWR);
42                 if (fd < 0) {
43                         VMBUS_LOG(ERR, "Cannot open %s: %s",
44                                   uio_res->path, strerror(errno));
45                         return -1;
46                 }
47
48                 for (i = 0; i != uio_res->nb_maps; i++) {
49                         void *mapaddr;
50
51                         mapaddr = vmbus_map_resource(uio_res->maps[i].addr,
52                                                      fd, 0,
53                                                      uio_res->maps[i].size, 0);
54
55                         if (mapaddr == uio_res->maps[i].addr)
56                                 continue;
57
58                         VMBUS_LOG(ERR,
59                                   "Cannot mmap device resource file %s to address: %p",
60                                   uio_res->path, uio_res->maps[i].addr);
61
62                         if (mapaddr != MAP_FAILED)
63                                 /* unmap addr wrongly mapped */
64                                 vmbus_unmap_resource(mapaddr,
65                                                      (size_t)uio_res->maps[i].size);
66
67                         /* unmap addrs correctly mapped */
68                         while (--i >= 0)
69                                 vmbus_unmap_resource(uio_res->maps[i].addr,
70                                                      (size_t)uio_res->maps[i].size);
71
72                         close(fd);
73                         return -1;
74                 }
75
76                 /* fd is not needed in slave process, close it */
77                 close(fd);
78                 return 0;
79         }
80
81         VMBUS_LOG(ERR,  "Cannot find resource for device");
82         return 1;
83 }
84
85 static int
86 vmbus_uio_map_primary(struct rte_vmbus_device *dev)
87 {
88         int i, ret;
89         struct mapped_vmbus_resource *uio_res = NULL;
90         struct mapped_vmbus_res_list *uio_res_list =
91                 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
92
93         /* allocate uio resource */
94         ret = vmbus_uio_alloc_resource(dev, &uio_res);
95         if (ret)
96                 return ret;
97
98         /* Map the resources */
99         for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
100                 /* skip empty BAR */
101                 if (dev->resource[i].len == 0)
102                         continue;
103
104                 ret = vmbus_uio_map_resource_by_index(dev, i, uio_res, 0);
105                 if (ret)
106                         goto error;
107         }
108
109         uio_res->nb_maps = i;
110
111         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
112
113         return 0;
114 error:
115         while (--i >= 0) {
116                 vmbus_unmap_resource(uio_res->maps[i].addr,
117                                 (size_t)uio_res->maps[i].size);
118         }
119         vmbus_uio_free_resource(dev, uio_res);
120         return -1;
121 }
122
123
124 struct mapped_vmbus_resource *
125 vmbus_uio_find_resource(const struct rte_vmbus_device *dev)
126 {
127         struct mapped_vmbus_resource *uio_res;
128         struct mapped_vmbus_res_list *uio_res_list =
129                         RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
130
131         if (dev == NULL)
132                 return NULL;
133
134         TAILQ_FOREACH(uio_res, uio_res_list, next) {
135                 /* skip this element if it doesn't match our VMBUS address */
136                 if (rte_uuid_compare(uio_res->id, dev->device_id) == 0)
137                         return uio_res;
138         }
139         return NULL;
140 }
141
142 /* map the VMBUS resource of a VMBUS device in virtual memory */
143 int
144 vmbus_uio_map_resource(struct rte_vmbus_device *dev)
145 {
146         struct mapped_vmbus_resource *uio_res;
147         int ret;
148
149         /* TODO: handle rescind */
150         dev->intr_handle.fd = -1;
151         dev->intr_handle.uio_cfg_fd = -1;
152         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
153
154         /* secondary processes - use already recorded details */
155         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
156                 ret = vmbus_uio_map_secondary(dev);
157         else
158                 ret = vmbus_uio_map_primary(dev);
159
160         if (ret != 0)
161                 return ret;
162
163         uio_res = vmbus_uio_find_resource(dev);
164         if (!uio_res) {
165                 VMBUS_LOG(ERR, "can not find resources!");
166                 return -EIO;
167         }
168
169         if (uio_res->nb_maps <= HV_MON_PAGE_MAP) {
170                 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
171                         uio_res->nb_maps);
172                 return -EINVAL;
173         }
174
175         dev->int_page = (uint32_t *)((char *)uio_res->maps[HV_INT_PAGE_MAP].addr
176                                      + (PAGE_SIZE >> 1));
177         dev->monitor_page = uio_res->maps[HV_MON_PAGE_MAP].addr;
178         return 0;
179 }
180
181 static void
182 vmbus_uio_unmap(struct mapped_vmbus_resource *uio_res)
183 {
184         int i;
185
186         if (uio_res == NULL)
187                 return;
188
189         for (i = 0; i != uio_res->nb_maps; i++) {
190                 vmbus_unmap_resource(uio_res->maps[i].addr,
191                                      (size_t)uio_res->maps[i].size);
192         }
193 }
194
195 /* unmap the VMBUS resource of a VMBUS device in virtual memory */
196 void
197 vmbus_uio_unmap_resource(struct rte_vmbus_device *dev)
198 {
199         struct mapped_vmbus_resource *uio_res;
200         struct mapped_vmbus_res_list *uio_res_list =
201                         RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
202
203         if (dev == NULL)
204                 return;
205
206         /* find an entry for the device */
207         uio_res = vmbus_uio_find_resource(dev);
208         if (uio_res == NULL)
209                 return;
210
211         /* secondary processes - just free maps */
212         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
213                 return vmbus_uio_unmap(uio_res);
214
215         TAILQ_REMOVE(uio_res_list, uio_res, next);
216
217         /* unmap all resources */
218         vmbus_uio_unmap(uio_res);
219
220         /* free uio resource */
221         rte_free(uio_res);
222
223         /* close fd if in primary process */
224         close(dev->intr_handle.fd);
225         if (dev->intr_handle.uio_cfg_fd >= 0) {
226                 close(dev->intr_handle.uio_cfg_fd);
227                 dev->intr_handle.uio_cfg_fd = -1;
228         }
229
230         dev->intr_handle.fd = -1;
231         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
232 }