1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018, Microsoft Corporation.
17 #include <rte_memory.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 ((rte_intr_fd_get(dev->intr_handle) < 0) ||
34 write(rte_intr_fd_get(dev->intr_handle), &onoff,
36 VMBUS_LOG(ERR, "cannot write to %d:%s",
37 rte_intr_fd_get(dev->intr_handle),
42 int vmbus_uio_irq_read(struct rte_vmbus_device *dev)
47 if (rte_intr_fd_get(dev->intr_handle) < 0)
50 cc = read(rte_intr_fd_get(dev->intr_handle), &count,
52 if (cc < (int)sizeof(count)) {
54 VMBUS_LOG(ERR, "IRQ read failed %s",
58 VMBUS_LOG(ERR, "can't read IRQ count");
66 vmbus_uio_free_resource(struct rte_vmbus_device *dev,
67 struct mapped_vmbus_resource *uio_res)
71 if (rte_intr_dev_fd_get(dev->intr_handle) >= 0) {
72 close(rte_intr_dev_fd_get(dev->intr_handle));
73 rte_intr_dev_fd_set(dev->intr_handle, -1);
76 if (rte_intr_fd_get(dev->intr_handle) >= 0) {
77 close(rte_intr_fd_get(dev->intr_handle));
78 rte_intr_fd_set(dev->intr_handle, -1);
79 rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UNKNOWN);
84 vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
85 struct mapped_vmbus_resource **uio_res)
87 char devname[PATH_MAX]; /* contains the /dev/uioX */
90 /* save fd if in primary process */
91 snprintf(devname, sizeof(devname), "/dev/uio%u", dev->uio_num);
92 fd = open(devname, O_RDWR);
94 VMBUS_LOG(ERR, "Cannot open %s: %s",
95 devname, strerror(errno));
99 if (rte_intr_fd_set(dev->intr_handle, fd))
102 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO_INTX))
105 /* allocate the mapping details for secondary processes*/
106 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
107 if (*uio_res == NULL) {
108 VMBUS_LOG(ERR, "cannot store uio mmap details");
112 strlcpy((*uio_res)->path, devname, PATH_MAX);
113 rte_uuid_copy((*uio_res)->id, dev->device_id);
118 vmbus_uio_free_resource(dev, *uio_res);
123 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
125 size_t sz = msl->memseg_arr.len * msl->page_sz;
126 void *end_va = RTE_PTR_ADD(msl->base_va, sz);
129 if (*max_va < end_va)
135 * TODO: this should be part of memseg api.
136 * code is duplicated from PCI.
139 vmbus_find_max_end_va(void)
143 rte_memseg_list_walk(find_max_end_va, &va);
148 vmbus_uio_map_resource_by_index(struct rte_vmbus_device *dev, int idx,
149 struct mapped_vmbus_resource *uio_res,
152 size_t size = dev->resource[idx].len;
153 struct vmbus_map *maps = uio_res->maps;
158 /* devname for mmap */
159 fd = open(uio_res->path, O_RDWR);
161 VMBUS_LOG(ERR, "Cannot open %s: %s",
162 uio_res->path, strerror(errno));
166 /* try mapping somewhere close to the end of hugepages */
167 if (vmbus_map_addr == NULL)
168 vmbus_map_addr = vmbus_find_max_end_va();
170 /* offset is special in uio it indicates which resource */
171 offset = idx * rte_mem_page_size();
173 mapaddr = vmbus_map_resource(vmbus_map_addr, fd, offset, size, flags);
176 if (mapaddr == MAP_FAILED)
179 dev->resource[idx].addr = mapaddr;
180 vmbus_map_addr = RTE_PTR_ADD(mapaddr, size);
182 /* Record result of successful mapping for use by secondary */
183 maps[idx].addr = mapaddr;
184 maps[idx].size = size;
189 static int vmbus_uio_map_primary(struct vmbus_channel *chan,
190 void **ring_buf, uint32_t *ring_size)
192 struct mapped_vmbus_resource *uio_res;
194 uio_res = vmbus_uio_find_resource(chan->device);
196 VMBUS_LOG(ERR, "can not find resources!");
200 if (uio_res->nb_maps < VMBUS_MAX_RESOURCE) {
201 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
206 *ring_size = uio_res->maps[HV_TXRX_RING_MAP].size / 2;
207 *ring_buf = uio_res->maps[HV_TXRX_RING_MAP].addr;
211 static int vmbus_uio_map_subchan(const struct rte_vmbus_device *dev,
212 const struct vmbus_channel *chan,
213 void **ring_buf, uint32_t *ring_size)
215 char ring_path[PATH_MAX];
220 struct mapped_vmbus_resource *uio_res;
223 uio_res = vmbus_uio_find_resource(dev);
225 VMBUS_LOG(ERR, "can not find resources for mapping subchan");
229 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
230 if (uio_res->nb_subchannels >= UIO_MAX_SUBCHANNEL) {
232 "exceeding max subchannels UIO_MAX_SUBCHANNEL(%d)",
234 VMBUS_LOG(ERR, "Change UIO_MAX_SUBCHANNEL and recompile");
238 for (channel_idx = 0; channel_idx < uio_res->nb_subchannels;
240 if (uio_res->subchannel_maps[channel_idx].relid ==
243 if (channel_idx == uio_res->nb_subchannels) {
245 "couldn't find sub channel %d from shared mapping in primary",
249 vmbus_map_addr = uio_res->subchannel_maps[channel_idx].addr;
252 snprintf(ring_path, sizeof(ring_path),
253 "%s/%s/channels/%u/ring",
254 SYSFS_VMBUS_DEVICES, dev->device.name,
257 fd = open(ring_path, O_RDWR);
259 VMBUS_LOG(ERR, "Cannot open %s: %s",
260 ring_path, strerror(errno));
264 if (fstat(fd, &sb) < 0) {
265 VMBUS_LOG(ERR, "Cannot state %s: %s",
266 ring_path, strerror(errno));
270 file_size = sb.st_size;
272 if (file_size == 0 || (file_size & (rte_mem_page_size() - 1))) {
273 VMBUS_LOG(ERR, "incorrect size %s: %zu",
274 ring_path, file_size);
280 mapaddr = vmbus_map_resource(vmbus_map_addr, fd,
284 if (mapaddr == MAP_FAILED)
287 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
289 /* Add this mapping to uio_res for use by secondary */
290 uio_res->subchannel_maps[uio_res->nb_subchannels].relid =
292 uio_res->subchannel_maps[uio_res->nb_subchannels].addr =
294 uio_res->subchannel_maps[uio_res->nb_subchannels].size =
296 uio_res->nb_subchannels++;
298 vmbus_map_addr = RTE_PTR_ADD(mapaddr, file_size);
300 if (mapaddr != vmbus_map_addr) {
301 VMBUS_LOG(ERR, "failed to map channel %d to addr %p",
302 chan->relid, mapaddr);
303 vmbus_unmap_resource(mapaddr, file_size);
308 *ring_size = file_size / 2;
314 int vmbus_uio_map_rings(struct vmbus_channel *chan)
316 const struct rte_vmbus_device *dev = chan->device;
321 /* Primary channel */
322 if (chan->subchannel_id == 0)
323 ret = vmbus_uio_map_primary(chan, &ring_buf, &ring_size);
325 ret = vmbus_uio_map_subchan(dev, chan, &ring_buf, &ring_size);
330 vmbus_br_setup(&chan->txbr, ring_buf, ring_size);
331 vmbus_br_setup(&chan->rxbr, (char *)ring_buf + ring_size, ring_size);
335 static int vmbus_uio_sysfs_read(const char *dir, const char *name,
336 unsigned long *val, unsigned long max_range)
342 snprintf(path, sizeof(path), "%s/%s", dir, name);
343 f = fopen(path, "r");
345 VMBUS_LOG(ERR, "can't open %s:%s",
346 path, strerror(errno));
350 if (fscanf(f, "%lu", val) != 1)
352 else if (*val > max_range)
361 static bool vmbus_uio_ring_present(const struct rte_vmbus_device *dev,
364 char ring_path[PATH_MAX];
366 /* Check if kernel has subchannel sysfs files */
367 snprintf(ring_path, sizeof(ring_path),
368 "%s/%s/channels/%u/ring",
369 SYSFS_VMBUS_DEVICES, dev->device.name, relid);
371 return access(ring_path, R_OK|W_OK) == 0;
374 bool vmbus_uio_subchannels_supported(const struct rte_vmbus_device *dev,
375 const struct vmbus_channel *chan)
377 return vmbus_uio_ring_present(dev, chan->relid);
380 static bool vmbus_isnew_subchannel(struct vmbus_channel *primary,
383 const struct vmbus_channel *c;
385 STAILQ_FOREACH(c, &primary->subchannel_list, next) {
392 int vmbus_uio_get_subchan(struct vmbus_channel *primary,
393 struct vmbus_channel **subchan)
395 const struct rte_vmbus_device *dev = primary->device;
396 char chan_path[PATH_MAX], subchan_path[PATH_MAX];
401 snprintf(chan_path, sizeof(chan_path),
403 SYSFS_VMBUS_DEVICES, dev->device.name);
405 chan_dir = opendir(chan_path);
407 VMBUS_LOG(ERR, "cannot open %s: %s",
408 chan_path, strerror(errno));
412 while ((ent = readdir(chan_dir))) {
413 unsigned long relid, subid, monid;
416 if (ent->d_name[0] == '.')
420 relid = strtoul(ent->d_name, &endp, 0);
421 if (*endp || errno != 0 || relid > UINT16_MAX) {
422 VMBUS_LOG(NOTICE, "not a valid channel relid: %s",
427 if (!vmbus_isnew_subchannel(primary, relid)) {
428 VMBUS_LOG(DEBUG, "skip already found channel: %lu",
433 if (!vmbus_uio_ring_present(dev, relid)) {
434 VMBUS_LOG(DEBUG, "ring mmap not found (yet) for: %lu",
439 snprintf(subchan_path, sizeof(subchan_path), "%s/%lu",
441 err = vmbus_uio_sysfs_read(subchan_path, "subchannel_id",
444 VMBUS_LOG(NOTICE, "no subchannel_id in %s:%s",
445 subchan_path, strerror(-err));
450 continue; /* skip primary channel */
452 err = vmbus_uio_sysfs_read(subchan_path, "monitor_id",
455 VMBUS_LOG(NOTICE, "no monitor_id in %s:%s",
456 subchan_path, strerror(-err));
460 err = vmbus_chan_create(dev, relid, subid, monid, subchan);
462 VMBUS_LOG(ERR, "subchannel setup failed");
469 return (ent == NULL) ? -ENOENT : 0;