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)
26 struct mapped_vmbus_resource *
27 vmbus_uio_find_resource(const struct rte_vmbus_device *dev)
29 struct mapped_vmbus_resource *uio_res;
30 struct mapped_vmbus_res_list *uio_res_list =
31 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
36 TAILQ_FOREACH(uio_res, uio_res_list, next) {
37 if (rte_uuid_compare(uio_res->id, dev->device_id) == 0)
44 vmbus_uio_map_secondary(struct rte_vmbus_device *dev)
46 struct mapped_vmbus_resource *uio_res;
47 struct vmbus_channel *chan;
50 uio_res = vmbus_uio_find_resource(dev);
52 VMBUS_LOG(ERR, "Cannot find resource for device");
57 fd = open(uio_res->path, O_RDWR);
59 VMBUS_LOG(ERR, "Cannot open %s: %s",
60 uio_res->path, strerror(errno));
64 for (i = 0; i != uio_res->nb_maps; i++) {
66 off_t offset = i * PAGE_SIZE;
68 mapaddr = vmbus_map_resource(uio_res->maps[i].addr,
70 uio_res->maps[i].size, 0);
72 if (mapaddr == uio_res->maps[i].addr)
73 continue; /* successful map */
75 if (mapaddr == MAP_FAILED)
77 "mmap resource %d in secondary failed", i);
80 "mmap resource %d address mismatch", i);
86 /* fd is not needed in slave process, close it */
89 dev->primary = uio_res->primary;
91 VMBUS_LOG(ERR, "missing primary channel");
95 STAILQ_FOREACH(chan, &dev->primary->subchannel_list, next) {
96 if (vmbus_uio_map_secondary_subchan(dev, chan) != 0) {
97 VMBUS_LOG(ERR, "cannot map secondary subchan");
105 vmbus_uio_map_primary(struct rte_vmbus_device *dev)
108 struct mapped_vmbus_resource *uio_res = NULL;
109 struct mapped_vmbus_res_list *uio_res_list =
110 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
112 /* allocate uio resource */
113 ret = vmbus_uio_alloc_resource(dev, &uio_res);
117 /* Map the resources */
118 for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
119 /* stop at empty BAR */
120 if (dev->resource[i].len == 0)
123 ret = vmbus_uio_map_resource_by_index(dev, i, uio_res, 0);
128 uio_res->nb_maps = i;
130 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
135 vmbus_unmap_resource(uio_res->maps[i].addr,
136 (size_t)uio_res->maps[i].size);
138 vmbus_uio_free_resource(dev, uio_res);
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;