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