Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2020 Broadcom.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include <errno.h>
7 : : #include <sys/mman.h>
8 : : #include <sys/ioctl.h>
9 : :
10 : : #include <rte_vfio.h>
11 : :
12 : : #include "bcmfs_device.h"
13 : : #include "bcmfs_logs.h"
14 : : #include "bcmfs_vfio.h"
15 : :
16 : : #ifdef VFIO_PRESENT
17 : : static int
18 : 0 : vfio_map_dev_obj(const char *path, const char *dev_obj,
19 : : uint32_t *size, void **addr, int *dev_fd)
20 : : {
21 : : int32_t ret;
22 : : struct vfio_group_status status = { .argsz = sizeof(status) };
23 : :
24 : 0 : struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
25 : 0 : struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
26 : :
27 : 0 : ret = rte_vfio_setup_device(path, dev_obj, dev_fd, &d_info);
28 [ # # ]: 0 : if (ret) {
29 : 0 : BCMFS_LOG(ERR, "VFIO Setting for device failed");
30 : 0 : return ret;
31 : : }
32 : :
33 : : /* getting device region info*/
34 : 0 : ret = ioctl(*dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info);
35 [ # # ]: 0 : if (ret < 0) {
36 : 0 : BCMFS_LOG(ERR, "Error in VFIO getting REGION_INFO");
37 : 0 : goto map_failed;
38 : : }
39 : :
40 : 0 : *addr = mmap(NULL, reg_info.size,
41 : : PROT_WRITE | PROT_READ, MAP_SHARED,
42 : 0 : *dev_fd, reg_info.offset);
43 [ # # ]: 0 : if (*addr == MAP_FAILED) {
44 : 0 : BCMFS_LOG(ERR, "Error mapping region (errno = %d)", errno);
45 : 0 : ret = errno;
46 : 0 : goto map_failed;
47 : : }
48 : 0 : *size = reg_info.size;
49 : :
50 : 0 : return 0;
51 : :
52 : 0 : map_failed:
53 : 0 : rte_vfio_release_device(path, dev_obj, *dev_fd);
54 : :
55 : 0 : return ret;
56 : : }
57 : :
58 : : int
59 : 0 : bcmfs_attach_vfio(struct bcmfs_device *dev)
60 : : {
61 : : int ret;
62 : : int vfio_dev_fd;
63 : 0 : void *v_addr = NULL;
64 : 0 : uint32_t size = 0;
65 : :
66 : 0 : ret = vfio_map_dev_obj(dev->dirname, dev->name,
67 : : &size, &v_addr, &vfio_dev_fd);
68 [ # # ]: 0 : if (ret)
69 : : return -1;
70 : :
71 : 0 : dev->mmap_size = size;
72 : 0 : dev->mmap_addr = v_addr;
73 : 0 : dev->vfio_dev_fd = vfio_dev_fd;
74 : :
75 : 0 : return 0;
76 : : }
77 : :
78 : : void
79 : 0 : bcmfs_release_vfio(struct bcmfs_device *dev)
80 : : {
81 : : int ret;
82 : :
83 [ # # ]: 0 : if (dev == NULL)
84 : : return;
85 : :
86 : : /* unmap the addr */
87 : 0 : munmap(dev->mmap_addr, dev->mmap_size);
88 : : /* release the device */
89 : 0 : ret = rte_vfio_release_device(dev->dirname, dev->name,
90 : : dev->vfio_dev_fd);
91 [ # # ]: 0 : if (ret < 0) {
92 : 0 : BCMFS_LOG(ERR, "cannot release device");
93 : 0 : return;
94 : : }
95 : : }
96 : : #else
97 : : int
98 : : bcmfs_attach_vfio(struct bcmfs_device *dev __rte_unused)
99 : : {
100 : : return -1;
101 : : }
102 : :
103 : : void
104 : : bcmfs_release_vfio(struct bcmfs_device *dev __rte_unused)
105 : : {
106 : : }
107 : : #endif
|