1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
3 */
4
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include <sys/mman.h>
9
10 #include <rte_memzone.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_kvargs.h>
13 #include <rte_string_fns.h>
14 #include <rte_rawdev_pmd.h>
15
16 #include "ioat_private.h"
17
18 /** Name of the device driver */
19 #define IDXD_PMD_RAWDEV_NAME rawdev_idxd
20 /* takes a work queue(WQ) as parameter */
21 #define IDXD_ARG_WQ "wq"
22
23 static const char * const valid_args[] = {
24 IDXD_ARG_WQ,
25 NULL
26 };
27
28 struct idxd_vdev_args {
29 uint8_t device_id;
30 uint8_t wq_id;
31 };
32
33 static const struct rte_rawdev_ops idxd_vdev_ops = {
34 .dev_close = idxd_rawdev_close,
35 .dev_selftest = ioat_rawdev_test,
36 .dump = idxd_dev_dump,
37 .dev_configure = idxd_dev_configure,
38 .dev_info_get = idxd_dev_info_get,
39 .xstats_get = ioat_xstats_get,
40 .xstats_get_names = ioat_xstats_get_names,
41 .xstats_reset = ioat_xstats_reset,
42 };
43
44 static void *
idxd_vdev_mmap_wq(struct idxd_vdev_args * args)45 idxd_vdev_mmap_wq(struct idxd_vdev_args *args)
46 {
47 void *addr;
48 char path[PATH_MAX];
49 int fd;
50
51 snprintf(path, sizeof(path), "/dev/dsa/wq%u.%u",
52 args->device_id, args->wq_id);
53 fd = open(path, O_RDWR);
54 if (fd < 0) {
55 IOAT_PMD_ERR("Failed to open device path");
56 return NULL;
57 }
58
59 addr = mmap(NULL, 0x1000, PROT_WRITE, MAP_SHARED, fd, 0);
60 close(fd);
61 if (addr == MAP_FAILED) {
62 IOAT_PMD_ERR("Failed to mmap device");
63 return NULL;
64 }
65
66 return addr;
67 }
68
69 static int
idxd_rawdev_parse_wq(const char * key __rte_unused,const char * value,void * extra_args)70 idxd_rawdev_parse_wq(const char *key __rte_unused, const char *value,
71 void *extra_args)
72 {
73 struct idxd_vdev_args *args = (struct idxd_vdev_args *)extra_args;
74 int dev, wq, bytes = -1;
75 int read = sscanf(value, "%d.%d%n", &dev, &wq, &bytes);
76
77 if (read != 2 || bytes != (int)strlen(value)) {
78 IOAT_PMD_ERR("Error parsing work-queue id. Must be in <dev_id>.<queue_id> format");
79 return -EINVAL;
80 }
81
82 if (dev >= UINT8_MAX || wq >= UINT8_MAX) {
83 IOAT_PMD_ERR("Device or work queue id out of range");
84 return -EINVAL;
85 }
86
87 args->device_id = dev;
88 args->wq_id = wq;
89
90 return 0;
91 }
92
93 static int
idxd_vdev_parse_params(struct rte_kvargs * kvlist,struct idxd_vdev_args * args)94 idxd_vdev_parse_params(struct rte_kvargs *kvlist, struct idxd_vdev_args *args)
95 {
96 int ret = 0;
97
98 if (rte_kvargs_count(kvlist, IDXD_ARG_WQ) == 1) {
99 if (rte_kvargs_process(kvlist, IDXD_ARG_WQ,
100 &idxd_rawdev_parse_wq, args) < 0) {
101 IOAT_PMD_ERR("Error parsing %s", IDXD_ARG_WQ);
102 ret = -EINVAL;
103 }
104 } else {
105 IOAT_PMD_ERR("%s is a mandatory arg", IDXD_ARG_WQ);
106 ret = -EINVAL;
107 }
108
109 rte_kvargs_free(kvlist);
110 return ret;
111 }
112
113 static int
idxd_vdev_get_max_batches(struct idxd_vdev_args * args)114 idxd_vdev_get_max_batches(struct idxd_vdev_args *args)
115 {
116 char sysfs_path[PATH_MAX];
117 FILE *f;
118 int ret;
119
120 snprintf(sysfs_path, sizeof(sysfs_path),
121 "/sys/bus/dsa/devices/wq%u.%u/size",
122 args->device_id, args->wq_id);
123 f = fopen(sysfs_path, "r");
124 if (f == NULL)
125 return -1;
126
127 if (fscanf(f, "%d", &ret) != 1)
128 ret = -1;
129
130 fclose(f);
131 return ret;
132 }
133
134 static int
idxd_rawdev_probe_vdev(struct rte_vdev_device * vdev)135 idxd_rawdev_probe_vdev(struct rte_vdev_device *vdev)
136 {
137 struct rte_kvargs *kvlist;
138 struct idxd_rawdev idxd = {{0}}; /* double {} to avoid error on BSD12 */
139 struct idxd_vdev_args vdev_args;
140 const char *name;
141 int ret = 0;
142
143 name = rte_vdev_device_name(vdev);
144 if (name == NULL)
145 return -EINVAL;
146
147 IOAT_PMD_INFO("Initializing pmd_idxd for %s", name);
148
149 kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
150 if (kvlist == NULL) {
151 IOAT_PMD_ERR("Invalid kvargs key");
152 return -EINVAL;
153 }
154
155 ret = idxd_vdev_parse_params(kvlist, &vdev_args);
156 if (ret) {
157 IOAT_PMD_ERR("Failed to parse kvargs");
158 return -EINVAL;
159 }
160
161 idxd.qid = vdev_args.wq_id;
162 idxd.u.vdev.dsa_id = vdev_args.device_id;
163 idxd.max_batches = idxd_vdev_get_max_batches(&vdev_args);
164
165 idxd.public.portal = idxd_vdev_mmap_wq(&vdev_args);
166 if (idxd.public.portal == NULL) {
167 IOAT_PMD_ERR("WQ mmap failed");
168 return -ENOENT;
169 }
170
171 ret = idxd_rawdev_create(name, &vdev->device, &idxd, &idxd_vdev_ops);
172 if (ret) {
173 IOAT_PMD_ERR("Failed to create rawdev %s", name);
174 return ret;
175 }
176
177 return 0;
178 }
179
180 static int
idxd_rawdev_remove_vdev(struct rte_vdev_device * vdev)181 idxd_rawdev_remove_vdev(struct rte_vdev_device *vdev)
182 {
183 struct idxd_rawdev *idxd;
184 const char *name;
185 struct rte_rawdev *rdev;
186 int ret = 0;
187
188 name = rte_vdev_device_name(vdev);
189 if (name == NULL)
190 return -EINVAL;
191
192 IOAT_PMD_INFO("Remove DSA vdev %p", name);
193
194 rdev = rte_rawdev_pmd_get_named_dev(name);
195 if (!rdev) {
196 IOAT_PMD_ERR("Invalid device name (%s)", name);
197 return -EINVAL;
198 }
199
200 idxd = rdev->dev_private;
201
202 /* free context and memory */
203 if (rdev->dev_private != NULL) {
204 IOAT_PMD_DEBUG("Freeing device driver memory");
205 rdev->dev_private = NULL;
206
207 if (munmap(idxd->public.portal, 0x1000) < 0) {
208 IOAT_PMD_ERR("Error unmapping portal");
209 ret = -errno;
210 }
211
212 rte_free(idxd->public.batch_ring);
213 rte_free(idxd->public.hdl_ring);
214
215 rte_memzone_free(idxd->mz);
216 }
217
218 if (rte_rawdev_pmd_release(rdev))
219 IOAT_PMD_ERR("Device cleanup failed");
220
221 return ret;
222 }
223
224 struct rte_vdev_driver idxd_rawdev_drv_vdev = {
225 .probe = idxd_rawdev_probe_vdev,
226 .remove = idxd_rawdev_remove_vdev,
227 };
228
229 RTE_PMD_REGISTER_VDEV(IDXD_PMD_RAWDEV_NAME, idxd_rawdev_drv_vdev);
230 RTE_PMD_REGISTER_PARAM_STRING(IDXD_PMD_RAWDEV_NAME,
231 "wq=<string>");
232