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++) {
50 off_t offset = i * PAGE_SIZE;
52 mapaddr = vmbus_map_resource(uio_res->maps[i].addr,
54 uio_res->maps[i].size, 0);
56 if (mapaddr == uio_res->maps[i].addr)
60 "Cannot mmap device resource file %s to address: %p",
61 uio_res->path, uio_res->maps[i].addr);
63 if (mapaddr != MAP_FAILED)
64 /* unmap addr wrongly mapped */
65 vmbus_unmap_resource(mapaddr,
66 (size_t)uio_res->maps[i].size);
68 /* unmap addrs correctly mapped */
70 vmbus_unmap_resource(uio_res->maps[i].addr,
71 (size_t)uio_res->maps[i].size);
77 /* fd is not needed in slave process, close it */
82 VMBUS_LOG(ERR, "Cannot find resource for device");
87 vmbus_uio_map_primary(struct rte_vmbus_device *dev)
90 struct mapped_vmbus_resource *uio_res = NULL;
91 struct mapped_vmbus_res_list *uio_res_list =
92 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
94 /* allocate uio resource */
95 ret = vmbus_uio_alloc_resource(dev, &uio_res);
99 /* Map the resources */
100 for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
101 /* stop at empty BAR */
102 if (dev->resource[i].len == 0)
105 ret = vmbus_uio_map_resource_by_index(dev, i, uio_res, 0);
110 uio_res->nb_maps = i;
112 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
117 vmbus_unmap_resource(uio_res->maps[i].addr,
118 (size_t)uio_res->maps[i].size);
120 vmbus_uio_free_resource(dev, uio_res);
125 struct mapped_vmbus_resource *
126 vmbus_uio_find_resource(const struct rte_vmbus_device *dev)
128 struct mapped_vmbus_resource *uio_res;
129 struct mapped_vmbus_res_list *uio_res_list =
130 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
135 TAILQ_FOREACH(uio_res, uio_res_list, next) {
136 /* skip this element if it doesn't match our VMBUS address */
137 if (rte_uuid_compare(uio_res->id, dev->device_id) == 0)
143 /* map the VMBUS resource of a VMBUS device in virtual memory */
145 vmbus_uio_map_resource(struct rte_vmbus_device *dev)
147 struct mapped_vmbus_resource *uio_res;
150 /* TODO: handle rescind */
151 dev->intr_handle.fd = -1;
152 dev->intr_handle.uio_cfg_fd = -1;
153 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
155 /* secondary processes - use already recorded details */
156 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
157 ret = vmbus_uio_map_secondary(dev);
159 ret = vmbus_uio_map_primary(dev);
164 uio_res = vmbus_uio_find_resource(dev);
166 VMBUS_LOG(ERR, "can not find resources!");
170 if (uio_res->nb_maps <= HV_MON_PAGE_MAP) {
171 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
176 dev->int_page = (uint32_t *)((char *)uio_res->maps[HV_INT_PAGE_MAP].addr
178 dev->monitor_page = uio_res->maps[HV_MON_PAGE_MAP].addr;
183 vmbus_uio_unmap(struct mapped_vmbus_resource *uio_res)
190 for (i = 0; i != uio_res->nb_maps; i++) {
191 vmbus_unmap_resource(uio_res->maps[i].addr,
192 (size_t)uio_res->maps[i].size);
196 /* unmap the VMBUS resource of a VMBUS device in virtual memory */
198 vmbus_uio_unmap_resource(struct rte_vmbus_device *dev)
200 struct mapped_vmbus_resource *uio_res;
201 struct mapped_vmbus_res_list *uio_res_list =
202 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
207 /* find an entry for the device */
208 uio_res = vmbus_uio_find_resource(dev);
212 /* secondary processes - just free maps */
213 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
214 return vmbus_uio_unmap(uio_res);
216 TAILQ_REMOVE(uio_res_list, uio_res, next);
218 /* unmap all resources */
219 vmbus_uio_unmap(uio_res);
221 /* free uio resource */
224 /* close fd if in primary process */
225 close(dev->intr_handle.fd);
226 if (dev->intr_handle.uio_cfg_fd >= 0) {
227 close(dev->intr_handle.uio_cfg_fd);
228 dev->intr_handle.uio_cfg_fd = -1;
231 dev->intr_handle.fd = -1;
232 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;