1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018, Microsoft Corporation.
16 #include <rte_memory.h>
17 #include <rte_eal_memconfig.h>
18 #include <rte_common.h>
19 #include <rte_malloc.h>
20 #include <rte_bus_vmbus.h>
21 #include <rte_string_fns.h>
25 /** Pathname of VMBUS devices directory. */
26 #define SYSFS_VMBUS_DEVICES "/sys/bus/vmbus/devices"
28 static void *vmbus_map_addr;
30 /* Control interrupts */
31 void vmbus_uio_irq_control(struct rte_vmbus_device *dev, int32_t onoff)
33 if (write(dev->intr_handle.fd, &onoff, sizeof(onoff)) < 0) {
34 VMBUS_LOG(ERR, "cannot write to %d:%s",
35 dev->intr_handle.fd, strerror(errno));
39 int vmbus_uio_irq_read(struct rte_vmbus_device *dev)
43 if (read(dev->intr_handle.fd, &count, sizeof(count)) < 0) {
44 VMBUS_LOG(ERR, "cannot read to %d:%s",
45 dev->intr_handle.fd, strerror(errno));
53 vmbus_uio_free_resource(struct rte_vmbus_device *dev,
54 struct mapped_vmbus_resource *uio_res)
58 if (dev->intr_handle.uio_cfg_fd >= 0) {
59 close(dev->intr_handle.uio_cfg_fd);
60 dev->intr_handle.uio_cfg_fd = -1;
63 if (dev->intr_handle.fd >= 0) {
64 close(dev->intr_handle.fd);
65 dev->intr_handle.fd = -1;
66 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
71 vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
72 struct mapped_vmbus_resource **uio_res)
74 char devname[PATH_MAX]; /* contains the /dev/uioX */
76 /* save fd if in primary process */
77 snprintf(devname, sizeof(devname), "/dev/uio%u", dev->uio_num);
78 dev->intr_handle.fd = open(devname, O_RDWR);
79 if (dev->intr_handle.fd < 0) {
80 VMBUS_LOG(ERR, "Cannot open %s: %s",
81 devname, strerror(errno));
84 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
86 /* allocate the mapping details for secondary processes*/
87 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
88 if (*uio_res == NULL) {
89 VMBUS_LOG(ERR, "cannot store uio mmap details");
93 strlcpy((*uio_res)->path, devname, PATH_MAX);
94 rte_uuid_copy((*uio_res)->id, dev->device_id);
99 vmbus_uio_free_resource(dev, *uio_res);
104 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
106 size_t sz = msl->memseg_arr.len * msl->page_sz;
107 void *end_va = RTE_PTR_ADD(msl->base_va, sz);
110 if (*max_va < end_va)
116 * TODO: this should be part of memseg api.
117 * code is duplicated from PCI.
120 vmbus_find_max_end_va(void)
124 rte_memseg_list_walk(find_max_end_va, &va);
129 vmbus_uio_map_resource_by_index(struct rte_vmbus_device *dev, int idx,
130 struct mapped_vmbus_resource *uio_res,
133 size_t size = dev->resource[idx].len;
134 struct vmbus_map *maps = uio_res->maps;
139 /* devname for mmap */
140 fd = open(uio_res->path, O_RDWR);
142 VMBUS_LOG(ERR, "Cannot open %s: %s",
143 uio_res->path, strerror(errno));
147 /* try mapping somewhere close to the end of hugepages */
148 if (vmbus_map_addr == NULL)
149 vmbus_map_addr = vmbus_find_max_end_va();
151 /* offset is special in uio it indicates which resource */
152 offset = idx * PAGE_SIZE;
154 mapaddr = vmbus_map_resource(vmbus_map_addr, fd, offset, size, flags);
157 if (mapaddr == MAP_FAILED)
160 dev->resource[idx].addr = mapaddr;
161 vmbus_map_addr = RTE_PTR_ADD(mapaddr, size);
163 /* Record result of sucessful mapping for use by secondary */
164 maps[idx].addr = mapaddr;
165 maps[idx].size = size;
170 static int vmbus_uio_map_primary(struct vmbus_channel *chan,
171 void **ring_buf, uint32_t *ring_size)
173 struct mapped_vmbus_resource *uio_res;
175 uio_res = vmbus_uio_find_resource(chan->device);
177 VMBUS_LOG(ERR, "can not find resources!");
181 if (uio_res->nb_maps < VMBUS_MAX_RESOURCE) {
182 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
187 *ring_size = uio_res->maps[HV_TXRX_RING_MAP].size / 2;
188 *ring_buf = uio_res->maps[HV_TXRX_RING_MAP].addr;
192 static int vmbus_uio_map_subchan(const struct rte_vmbus_device *dev,
193 const struct vmbus_channel *chan,
194 void **ring_buf, uint32_t *ring_size)
196 char ring_path[PATH_MAX];
201 snprintf(ring_path, sizeof(ring_path),
202 "%s/%s/channels/%u/ring",
203 SYSFS_VMBUS_DEVICES, dev->device.name,
206 fd = open(ring_path, O_RDWR);
208 VMBUS_LOG(ERR, "Cannot open %s: %s",
209 ring_path, strerror(errno));
213 if (fstat(fd, &sb) < 0) {
214 VMBUS_LOG(ERR, "Cannot state %s: %s",
215 ring_path, strerror(errno));
219 file_size = sb.st_size;
221 if (file_size == 0 || (file_size & (PAGE_SIZE - 1))) {
222 VMBUS_LOG(ERR, "incorrect size %s: %zu",
223 ring_path, file_size);
229 *ring_size = file_size / 2;
230 *ring_buf = vmbus_map_resource(vmbus_map_addr, fd,
234 if (ring_buf == MAP_FAILED)
237 vmbus_map_addr = RTE_PTR_ADD(ring_buf, file_size);
241 int vmbus_uio_map_rings(struct vmbus_channel *chan)
243 const struct rte_vmbus_device *dev = chan->device;
248 /* Primary channel */
249 if (chan->subchannel_id == 0)
250 ret = vmbus_uio_map_primary(chan, &ring_buf, &ring_size);
252 ret = vmbus_uio_map_subchan(dev, chan, &ring_buf, &ring_size);
257 vmbus_br_setup(&chan->txbr, ring_buf, ring_size);
258 vmbus_br_setup(&chan->rxbr, (char *)ring_buf + ring_size, ring_size);
262 static int vmbus_uio_sysfs_read(const char *dir, const char *name,
263 unsigned long *val, unsigned long max_range)
269 snprintf(path, sizeof(path), "%s/%s", dir, name);
270 f = fopen(path, "r");
272 VMBUS_LOG(ERR, "can't open %s:%s",
273 path, strerror(errno));
277 if (fscanf(f, "%lu", val) != 1)
279 else if (*val > max_range)
288 static bool vmbus_uio_ring_present(const struct rte_vmbus_device *dev,
291 char ring_path[PATH_MAX];
293 /* Check if kernel has subchannel sysfs files */
294 snprintf(ring_path, sizeof(ring_path),
295 "%s/%s/channels/%u/ring",
296 SYSFS_VMBUS_DEVICES, dev->device.name, relid);
298 return access(ring_path, R_OK|W_OK) == 0;
301 bool vmbus_uio_subchannels_supported(const struct rte_vmbus_device *dev,
302 const struct vmbus_channel *chan)
304 return vmbus_uio_ring_present(dev, chan->relid);
307 static bool vmbus_isnew_subchannel(struct vmbus_channel *primary,
310 const struct vmbus_channel *c;
312 STAILQ_FOREACH(c, &primary->subchannel_list, next) {
319 int vmbus_uio_get_subchan(struct vmbus_channel *primary,
320 struct vmbus_channel **subchan)
322 const struct rte_vmbus_device *dev = primary->device;
323 char chan_path[PATH_MAX], subchan_path[PATH_MAX];
327 snprintf(chan_path, sizeof(chan_path),
329 SYSFS_VMBUS_DEVICES, dev->device.name);
331 chan_dir = opendir(chan_path);
333 VMBUS_LOG(ERR, "cannot open %s: %s",
334 chan_path, strerror(errno));
338 while ((ent = readdir(chan_dir))) {
339 unsigned long relid, subid, monid;
343 if (ent->d_name[0] == '.')
347 relid = strtoul(ent->d_name, &endp, 0);
348 if (*endp || errno != 0 || relid > UINT16_MAX) {
349 VMBUS_LOG(NOTICE, "not a valid channel relid: %s",
354 snprintf(subchan_path, sizeof(subchan_path), "%s/%lu",
356 err = vmbus_uio_sysfs_read(subchan_path, "subchannel_id",
359 VMBUS_LOG(NOTICE, "invalid subchannel id %lu",
366 continue; /* skip primary channel */
368 if (!vmbus_isnew_subchannel(primary, relid))
371 if (!vmbus_uio_ring_present(dev, relid))
372 continue; /* Ring may not be ready yet */
374 err = vmbus_uio_sysfs_read(subchan_path, "monitor_id",
377 VMBUS_LOG(NOTICE, "invalid monitor id %lu",
382 err = vmbus_chan_create(dev, relid, subid, monid, subchan);
384 VMBUS_LOG(NOTICE, "subchannel setup failed");
391 return (ent == NULL) ? -ENOENT : 0;