1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018, Microsoft Corporation.
13 #include <rte_tailq.h>
15 #include <rte_malloc.h>
17 #include <rte_bus_vmbus.h>
21 static struct rte_tailq_elem vmbus_tailq = {
22 .name = "VMBUS_RESOURCE_LIST",
24 EAL_REGISTER_TAILQ(vmbus_tailq)
27 vmbus_uio_map_secondary(struct rte_vmbus_device *dev)
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);
34 TAILQ_FOREACH(uio_res, uio_res_list, next) {
36 /* skip this element if it doesn't match our UUID */
37 if (rte_uuid_compare(uio_res->id, dev->device_id) != 0)
41 fd = open(uio_res->path, O_RDWR);
43 VMBUS_LOG(ERR, "Cannot open %s: %s",
44 uio_res->path, strerror(errno));
48 for (i = 0; i != uio_res->nb_maps; i++) {
51 mapaddr = vmbus_map_resource(uio_res->maps[i].addr,
53 uio_res->maps[i].size, 0);
55 if (mapaddr == uio_res->maps[i].addr)
59 "Cannot mmap device resource file %s to address: %p",
60 uio_res->path, uio_res->maps[i].addr);
62 if (mapaddr != MAP_FAILED)
63 /* unmap addr wrongly mapped */
64 vmbus_unmap_resource(mapaddr,
65 (size_t)uio_res->maps[i].size);
67 /* unmap addrs correctly mapped */
69 vmbus_unmap_resource(uio_res->maps[i].addr,
70 (size_t)uio_res->maps[i].size);
76 /* fd is not needed in slave process, close it */
81 VMBUS_LOG(ERR, "Cannot find resource for device");
86 vmbus_uio_map_primary(struct rte_vmbus_device *dev)
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);
93 /* allocate uio resource */
94 ret = vmbus_uio_alloc_resource(dev, &uio_res);
98 /* Map the resources */
99 for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
101 if (dev->resource[i].len == 0)
104 ret = vmbus_uio_map_resource_by_index(dev, i, uio_res, 0);
109 uio_res->nb_maps = i;
111 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
116 vmbus_unmap_resource(uio_res->maps[i].addr,
117 (size_t)uio_res->maps[i].size);
119 vmbus_uio_free_resource(dev, uio_res);
124 struct mapped_vmbus_resource *
125 vmbus_uio_find_resource(const struct rte_vmbus_device *dev)
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);
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)
142 /* map the VMBUS resource of a VMBUS device in virtual memory */
144 vmbus_uio_map_resource(struct rte_vmbus_device *dev)
146 struct mapped_vmbus_resource *uio_res;
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;
154 /* secondary processes - use already recorded details */
155 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
156 ret = vmbus_uio_map_secondary(dev);
158 ret = vmbus_uio_map_primary(dev);
163 uio_res = vmbus_uio_find_resource(dev);
165 VMBUS_LOG(ERR, "can not find resources!");
169 if (uio_res->nb_maps <= HV_MON_PAGE_MAP) {
170 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
175 dev->int_page = (uint32_t *)((char *)uio_res->maps[HV_INT_PAGE_MAP].addr
177 dev->monitor_page = uio_res->maps[HV_MON_PAGE_MAP].addr;
182 vmbus_uio_unmap(struct mapped_vmbus_resource *uio_res)
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);
195 /* unmap the VMBUS resource of a VMBUS device in virtual memory */
197 vmbus_uio_unmap_resource(struct rte_vmbus_device *dev)
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);
206 /* find an entry for the device */
207 uio_res = vmbus_uio_find_resource(dev);
211 /* secondary processes - just free maps */
212 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
213 return vmbus_uio_unmap(uio_res);
215 TAILQ_REMOVE(uio_res_list, uio_res, next);
217 /* unmap all resources */
218 vmbus_uio_unmap(uio_res);
220 /* free uio resource */
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;
230 dev->intr_handle.fd = -1;
231 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;