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 (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)
44 cc = read(dev->intr_handle.fd, &count, sizeof(count));
45 if (cc < (int)sizeof(count)) {
47 VMBUS_LOG(ERR, "IRQ read failed %s",
51 VMBUS_LOG(ERR, "can't read IRQ count");
59 vmbus_uio_free_resource(struct rte_vmbus_device *dev,
60 struct mapped_vmbus_resource *uio_res)
64 if (dev->intr_handle.uio_cfg_fd >= 0) {
65 close(dev->intr_handle.uio_cfg_fd);
66 dev->intr_handle.uio_cfg_fd = -1;
69 if (dev->intr_handle.fd >= 0) {
70 close(dev->intr_handle.fd);
71 dev->intr_handle.fd = -1;
72 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
77 vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
78 struct mapped_vmbus_resource **uio_res)
80 char devname[PATH_MAX]; /* contains the /dev/uioX */
82 /* save fd if in primary process */
83 snprintf(devname, sizeof(devname), "/dev/uio%u", dev->uio_num);
84 dev->intr_handle.fd = open(devname, O_RDWR);
85 if (dev->intr_handle.fd < 0) {
86 VMBUS_LOG(ERR, "Cannot open %s: %s",
87 devname, strerror(errno));
90 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
92 /* allocate the mapping details for secondary processes*/
93 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
94 if (*uio_res == NULL) {
95 VMBUS_LOG(ERR, "cannot store uio mmap details");
99 strlcpy((*uio_res)->path, devname, PATH_MAX);
100 rte_uuid_copy((*uio_res)->id, dev->device_id);
105 vmbus_uio_free_resource(dev, *uio_res);
110 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
112 size_t sz = msl->memseg_arr.len * msl->page_sz;
113 void *end_va = RTE_PTR_ADD(msl->base_va, sz);
116 if (*max_va < end_va)
122 * TODO: this should be part of memseg api.
123 * code is duplicated from PCI.
126 vmbus_find_max_end_va(void)
130 rte_memseg_list_walk(find_max_end_va, &va);
135 vmbus_uio_map_resource_by_index(struct rte_vmbus_device *dev, int idx,
136 struct mapped_vmbus_resource *uio_res,
139 size_t size = dev->resource[idx].len;
140 struct vmbus_map *maps = uio_res->maps;
145 /* devname for mmap */
146 fd = open(uio_res->path, O_RDWR);
148 VMBUS_LOG(ERR, "Cannot open %s: %s",
149 uio_res->path, strerror(errno));
153 /* try mapping somewhere close to the end of hugepages */
154 if (vmbus_map_addr == NULL)
155 vmbus_map_addr = vmbus_find_max_end_va();
157 /* offset is special in uio it indicates which resource */
158 offset = idx * rte_mem_page_size();
160 mapaddr = vmbus_map_resource(vmbus_map_addr, fd, offset, size, flags);
163 if (mapaddr == MAP_FAILED)
166 dev->resource[idx].addr = mapaddr;
167 vmbus_map_addr = RTE_PTR_ADD(mapaddr, size);
169 /* Record result of successful mapping for use by secondary */
170 maps[idx].addr = mapaddr;
171 maps[idx].size = size;
176 static int vmbus_uio_map_primary(struct vmbus_channel *chan,
177 void **ring_buf, uint32_t *ring_size)
179 struct mapped_vmbus_resource *uio_res;
181 uio_res = vmbus_uio_find_resource(chan->device);
183 VMBUS_LOG(ERR, "can not find resources!");
187 if (uio_res->nb_maps < VMBUS_MAX_RESOURCE) {
188 VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
193 *ring_size = uio_res->maps[HV_TXRX_RING_MAP].size / 2;
194 *ring_buf = uio_res->maps[HV_TXRX_RING_MAP].addr;
198 static int vmbus_uio_map_subchan(const struct rte_vmbus_device *dev,
199 const struct vmbus_channel *chan,
200 void **ring_buf, uint32_t *ring_size)
202 char ring_path[PATH_MAX];
207 struct mapped_vmbus_resource *uio_res;
210 uio_res = vmbus_uio_find_resource(dev);
212 VMBUS_LOG(ERR, "can not find resources for mapping subchan");
216 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
217 if (uio_res->nb_subchannels >= UIO_MAX_SUBCHANNEL) {
219 "exceeding max subchannels UIO_MAX_SUBCHANNEL(%d)",
221 VMBUS_LOG(ERR, "Change UIO_MAX_SUBCHANNEL and recompile");
225 for (channel_idx = 0; channel_idx < uio_res->nb_subchannels;
227 if (uio_res->subchannel_maps[channel_idx].relid ==
230 if (channel_idx == uio_res->nb_subchannels) {
232 "couldn't find sub channel %d from shared mapping in primary",
236 vmbus_map_addr = uio_res->subchannel_maps[channel_idx].addr;
239 snprintf(ring_path, sizeof(ring_path),
240 "%s/%s/channels/%u/ring",
241 SYSFS_VMBUS_DEVICES, dev->device.name,
244 fd = open(ring_path, O_RDWR);
246 VMBUS_LOG(ERR, "Cannot open %s: %s",
247 ring_path, strerror(errno));
251 if (fstat(fd, &sb) < 0) {
252 VMBUS_LOG(ERR, "Cannot state %s: %s",
253 ring_path, strerror(errno));
257 file_size = sb.st_size;
259 if (file_size == 0 || (file_size & (rte_mem_page_size() - 1))) {
260 VMBUS_LOG(ERR, "incorrect size %s: %zu",
261 ring_path, file_size);
267 mapaddr = vmbus_map_resource(vmbus_map_addr, fd,
271 if (mapaddr == MAP_FAILED)
274 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
276 /* Add this mapping to uio_res for use by secondary */
277 uio_res->subchannel_maps[uio_res->nb_subchannels].relid =
279 uio_res->subchannel_maps[uio_res->nb_subchannels].addr =
281 uio_res->subchannel_maps[uio_res->nb_subchannels].size =
283 uio_res->nb_subchannels++;
285 vmbus_map_addr = RTE_PTR_ADD(mapaddr, file_size);
287 if (mapaddr != vmbus_map_addr) {
288 VMBUS_LOG(ERR, "failed to map channel %d to addr %p",
289 chan->relid, mapaddr);
290 vmbus_unmap_resource(mapaddr, file_size);
295 *ring_size = file_size / 2;
301 int vmbus_uio_map_rings(struct vmbus_channel *chan)
303 const struct rte_vmbus_device *dev = chan->device;
308 /* Primary channel */
309 if (chan->subchannel_id == 0)
310 ret = vmbus_uio_map_primary(chan, &ring_buf, &ring_size);
312 ret = vmbus_uio_map_subchan(dev, chan, &ring_buf, &ring_size);
317 vmbus_br_setup(&chan->txbr, ring_buf, ring_size);
318 vmbus_br_setup(&chan->rxbr, (char *)ring_buf + ring_size, ring_size);
322 static int vmbus_uio_sysfs_read(const char *dir, const char *name,
323 unsigned long *val, unsigned long max_range)
329 snprintf(path, sizeof(path), "%s/%s", dir, name);
330 f = fopen(path, "r");
332 VMBUS_LOG(ERR, "can't open %s:%s",
333 path, strerror(errno));
337 if (fscanf(f, "%lu", val) != 1)
339 else if (*val > max_range)
348 static bool vmbus_uio_ring_present(const struct rte_vmbus_device *dev,
351 char ring_path[PATH_MAX];
353 /* Check if kernel has subchannel sysfs files */
354 snprintf(ring_path, sizeof(ring_path),
355 "%s/%s/channels/%u/ring",
356 SYSFS_VMBUS_DEVICES, dev->device.name, relid);
358 return access(ring_path, R_OK|W_OK) == 0;
361 bool vmbus_uio_subchannels_supported(const struct rte_vmbus_device *dev,
362 const struct vmbus_channel *chan)
364 return vmbus_uio_ring_present(dev, chan->relid);
367 static bool vmbus_isnew_subchannel(struct vmbus_channel *primary,
370 const struct vmbus_channel *c;
372 STAILQ_FOREACH(c, &primary->subchannel_list, next) {
379 int vmbus_uio_get_subchan(struct vmbus_channel *primary,
380 struct vmbus_channel **subchan)
382 const struct rte_vmbus_device *dev = primary->device;
383 char chan_path[PATH_MAX], subchan_path[PATH_MAX];
388 snprintf(chan_path, sizeof(chan_path),
390 SYSFS_VMBUS_DEVICES, dev->device.name);
392 chan_dir = opendir(chan_path);
394 VMBUS_LOG(ERR, "cannot open %s: %s",
395 chan_path, strerror(errno));
399 while ((ent = readdir(chan_dir))) {
400 unsigned long relid, subid, monid;
403 if (ent->d_name[0] == '.')
407 relid = strtoul(ent->d_name, &endp, 0);
408 if (*endp || errno != 0 || relid > UINT16_MAX) {
409 VMBUS_LOG(NOTICE, "not a valid channel relid: %s",
414 if (!vmbus_isnew_subchannel(primary, relid)) {
415 VMBUS_LOG(DEBUG, "skip already found channel: %lu",
420 if (!vmbus_uio_ring_present(dev, relid)) {
421 VMBUS_LOG(DEBUG, "ring mmap not found (yet) for: %lu",
426 snprintf(subchan_path, sizeof(subchan_path), "%s/%lu",
428 err = vmbus_uio_sysfs_read(subchan_path, "subchannel_id",
431 VMBUS_LOG(NOTICE, "no subchannel_id in %s:%s",
432 subchan_path, strerror(-err));
437 continue; /* skip primary channel */
439 err = vmbus_uio_sysfs_read(subchan_path, "monitor_id",
442 VMBUS_LOG(NOTICE, "no monitor_id in %s:%s",
443 subchan_path, strerror(-err));
447 err = vmbus_chan_create(dev, relid, subid, monid, subchan);
449 VMBUS_LOG(ERR, "subchannel setup failed");
456 return (ent == NULL) ? -ENOENT : 0;