New upstream version 18.11.2
[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 vmbus_channel *chan;
31         struct mapped_vmbus_resource *uio_res;
32         struct mapped_vmbus_res_list *uio_res_list
33                 = RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
34
35         TAILQ_FOREACH(uio_res, uio_res_list, next) {
36
37                 /* skip this element if it doesn't match our UUID */
38                 if (rte_uuid_compare(uio_res->id, dev->device_id) != 0)
39                         continue;
40
41                 /* open /dev/uioX */
42                 fd = open(uio_res->path, O_RDWR);
43                 if (fd < 0) {
44                         VMBUS_LOG(ERR, "Cannot open %s: %s",
45                                   uio_res->path, strerror(errno));
46                         return -1;
47                 }
48
49                 for (i = 0; i != uio_res->nb_maps; i++) {
50                         void *mapaddr;
51                         off_t offset = i * PAGE_SIZE;
52
53                         mapaddr = vmbus_map_resource(uio_res->maps[i].addr,
54                                                      fd, offset,
55                                                      uio_res->maps[i].size, 0);
56
57                         if (mapaddr == uio_res->maps[i].addr)
58                                 continue;
59
60                         VMBUS_LOG(ERR,
61                                   "Cannot mmap device resource file %s to address: %p",
62                                   uio_res->path, uio_res->maps[i].addr);
63
64                         if (mapaddr != MAP_FAILED)
65                                 /* unmap addr wrongly mapped */
66                                 vmbus_unmap_resource(mapaddr,
67                                                      (size_t)uio_res->maps[i].size);
68
69                         /* unmap addrs correctly mapped */
70                         while (--i >= 0)
71                                 vmbus_unmap_resource(uio_res->maps[i].addr,
72                                                      (size_t)uio_res->maps[i].size);
73
74                         close(fd);
75                         return -1;
76                 }
77
78                 /* fd is not needed in slave process, close it */
79                 close(fd);
80
81                 dev->primary = uio_res->primary;
82                 if (!dev->primary) {
83                         VMBUS_LOG(ERR, "missing primary channel");
84                         return -1;
85                 }
86
87                 STAILQ_FOREACH(chan, &dev->primary->subchannel_list, next) {
88                         if (vmbus_uio_map_secondary_subchan(dev, chan) != 0) {
89                                 VMBUS_LOG(ERR, "cannot map secondary subchan");
90                                 return -1;
91                         }
92                 }
93
94                 return 0;
95         }
96
97         VMBUS_LOG(ERR,  "Cannot find resource for device");
98         return 1;
99 }
100
101 static int
102 vmbus_uio_map_primary(struct rte_vmbus_device *dev)
103 {
104         int i, ret;
105         struct mapped_vmbus_resource *uio_res = NULL;
106         struct mapped_vmbus_res_list *uio_res_list =
107                 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
108
109         /* allocate uio resource */
110         ret = vmbus_uio_alloc_resource(dev, &uio_res);
111         if (ret)
112                 return ret;
113
114         /* Map the resources */
115         for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
116                 /* stop at empty BAR */
117                 if (dev->resource[i].len == 0)
118                         break;
119
120                 ret = vmbus_uio_map_resource_by_index(dev, i, uio_res, 0);
121                 if (ret)
122                         goto error;
123         }
124
125         uio_res->nb_maps = i;
126
127         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
128
129         return 0;
130 error:
131         while (--i >= 0) {
132                 vmbus_unmap_resource(uio_res->maps[i].addr,
133                                 (size_t)uio_res->maps[i].size);
134         }
135         vmbus_uio_free_resource(dev, uio_res);
136         return -1;
137 }
138
139
140 struct mapped_vmbus_resource *
141 vmbus_uio_find_resource(const struct rte_vmbus_device *dev)
142 {
143         struct mapped_vmbus_resource *uio_res;
144         struct mapped_vmbus_res_list *uio_res_list =
145                         RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
146
147         if (dev == NULL)
148                 return NULL;
149
150         TAILQ_FOREACH(uio_res, uio_res_list, next) {
151                 /* skip this element if it doesn't match our VMBUS address */
152                 if (rte_uuid_compare(uio_res->id, dev->device_id) == 0)
153                         return uio_res;
154         }
155         return NULL;
156 }
157
158 /* map the VMBUS resource of a VMBUS device in virtual memory */
159 int
160 vmbus_uio_map_resource(struct rte_vmbus_device *dev)
161 {
162         struct mapped_vmbus_resource *uio_res;
163         int ret;
164
165         /* TODO: handle rescind */
166         dev->intr_handle.fd = -1;
167         dev->intr_handle.uio_cfg_fd = -1;
168         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
169
170         /* secondary processes - use already recorded details */
171         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
172                 ret = vmbus_uio_map_secondary(dev);
173         else
174                 ret = vmbus_uio_map_primary(dev);
175
176         if (ret != 0)
177                 return ret;
178
179         uio_res = vmbus_uio_find_resource(dev);
180         if (!uio_res) {
181                 VMBUS_LOG(ERR, "can not find resources!");
182                 return -EIO;
183         }
184
185         if (uio_res->nb_maps <= HV_MON_PAGE_MAP) {
186                 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
187                         uio_res->nb_maps);
188                 return -EINVAL;
189         }
190
191         dev->int_page = (uint32_t *)((char *)uio_res->maps[HV_INT_PAGE_MAP].addr
192                                      + (PAGE_SIZE >> 1));
193         dev->monitor_page = uio_res->maps[HV_MON_PAGE_MAP].addr;
194         return 0;
195 }
196
197 static void
198 vmbus_uio_unmap(struct mapped_vmbus_resource *uio_res)
199 {
200         int i;
201
202         if (uio_res == NULL)
203                 return;
204
205         for (i = 0; i != uio_res->nb_maps; i++) {
206                 vmbus_unmap_resource(uio_res->maps[i].addr,
207                                      (size_t)uio_res->maps[i].size);
208         }
209 }
210
211 /* unmap the VMBUS resource of a VMBUS device in virtual memory */
212 void
213 vmbus_uio_unmap_resource(struct rte_vmbus_device *dev)
214 {
215         struct mapped_vmbus_resource *uio_res;
216         struct mapped_vmbus_res_list *uio_res_list =
217                         RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
218
219         if (dev == NULL)
220                 return;
221
222         /* find an entry for the device */
223         uio_res = vmbus_uio_find_resource(dev);
224         if (uio_res == NULL)
225                 return;
226
227         /* secondary processes - just free maps */
228         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
229                 return vmbus_uio_unmap(uio_res);
230
231         TAILQ_REMOVE(uio_res_list, uio_res, next);
232
233         /* unmap all resources */
234         vmbus_uio_unmap(uio_res);
235
236         /* free uio resource */
237         rte_free(uio_res);
238
239         /* close fd if in primary process */
240         close(dev->intr_handle.fd);
241         if (dev->intr_handle.uio_cfg_fd >= 0) {
242                 close(dev->intr_handle.uio_cfg_fd);
243                 dev->intr_handle.uio_cfg_fd = -1;
244         }
245
246         dev->intr_handle.fd = -1;
247         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
248 }