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 * rte_mem_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);
81 vmbus_unmap_resource(mapaddr, uio_res->maps[i].size);
88 /* fd is not needed in secondary process, close it */
91 dev->primary = uio_res->primary;
93 VMBUS_LOG(ERR, "missing primary channel");
97 STAILQ_FOREACH(chan, &dev->primary->subchannel_list, next) {
98 if (vmbus_uio_map_secondary_subchan(dev, chan) != 0) {
99 VMBUS_LOG(ERR, "cannot map secondary subchan");
107 vmbus_uio_map_primary(struct rte_vmbus_device *dev)
110 struct mapped_vmbus_resource *uio_res = NULL;
111 struct mapped_vmbus_res_list *uio_res_list =
112 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
114 /* allocate uio resource */
115 ret = vmbus_uio_alloc_resource(dev, &uio_res);
119 /* Map the resources */
120 for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
121 /* stop at empty BAR */
122 if (dev->resource[i].len == 0)
125 ret = vmbus_uio_map_resource_by_index(dev, i, uio_res, 0);
130 uio_res->nb_maps = i;
132 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
137 vmbus_unmap_resource(uio_res->maps[i].addr,
138 (size_t)uio_res->maps[i].size);
140 vmbus_uio_free_resource(dev, uio_res);
144 /* map the VMBUS resource of a VMBUS device in virtual memory */
146 vmbus_uio_map_resource(struct rte_vmbus_device *dev)
148 struct mapped_vmbus_resource *uio_res;
151 /* TODO: handle rescind */
152 dev->intr_handle.fd = -1;
153 dev->intr_handle.uio_cfg_fd = -1;
154 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
156 /* secondary processes - use already recorded details */
157 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
158 ret = vmbus_uio_map_secondary(dev);
160 ret = vmbus_uio_map_primary(dev);
165 uio_res = vmbus_uio_find_resource(dev);
167 VMBUS_LOG(ERR, "can not find resources!");
171 if (uio_res->nb_maps <= HV_MON_PAGE_MAP) {
172 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
177 dev->int_page = (uint32_t *)((char *)uio_res->maps[HV_INT_PAGE_MAP].addr
178 + (rte_mem_page_size() >> 1));
179 dev->monitor_page = uio_res->maps[HV_MON_PAGE_MAP].addr;
184 vmbus_uio_unmap(struct mapped_vmbus_resource *uio_res)
191 for (i = 0; i != uio_res->nb_maps; i++) {
192 vmbus_unmap_resource(uio_res->maps[i].addr,
193 (size_t)uio_res->maps[i].size);
197 /* unmap the VMBUS resource of a VMBUS device in virtual memory */
199 vmbus_uio_unmap_resource(struct rte_vmbus_device *dev)
201 struct mapped_vmbus_resource *uio_res;
202 struct mapped_vmbus_res_list *uio_res_list =
203 RTE_TAILQ_CAST(vmbus_tailq.head, mapped_vmbus_res_list);
208 /* find an entry for the device */
209 uio_res = vmbus_uio_find_resource(dev);
213 /* secondary processes - just free maps */
214 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
215 return vmbus_uio_unmap(uio_res);
217 TAILQ_REMOVE(uio_res_list, uio_res, next);
219 /* unmap all resources */
220 vmbus_uio_unmap(uio_res);
222 /* free uio resource */
225 /* close fd if in primary process */
226 close(dev->intr_handle.fd);
227 if (dev->intr_handle.uio_cfg_fd >= 0) {
228 close(dev->intr_handle.uio_cfg_fd);
229 dev->intr_handle.uio_cfg_fd = -1;
232 dev->intr_handle.fd = -1;
233 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;