xref: /dpdk/drivers/crypto/scheduler/rte_cryptodev_scheduler.c (revision 35b09d76f89e7d5a4f38a2926cf6915028ed1e56)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include <rte_reorder.h>
33 #include <rte_cryptodev.h>
34 #include <rte_cryptodev_pmd.h>
35 #include <rte_cryptodev_scheduler.h>
36 #include <rte_malloc.h>
37 
38 #include "scheduler_pmd_private.h"
39 
40 /** update the scheduler pmd's capability with attaching device's
41  *  capability.
42  *  For each device to be attached, the scheduler's capability should be
43  *  the common capability set of all slaves
44  **/
45 static uint32_t
46 sync_caps(struct rte_cryptodev_capabilities *caps,
47 		uint32_t nb_caps,
48 		const struct rte_cryptodev_capabilities *slave_caps)
49 {
50 	uint32_t sync_nb_caps = nb_caps, nb_slave_caps = 0;
51 	uint32_t i;
52 
53 	while (slave_caps[nb_slave_caps].op != RTE_CRYPTO_OP_TYPE_UNDEFINED)
54 		nb_slave_caps++;
55 
56 	if (nb_caps == 0) {
57 		rte_memcpy(caps, slave_caps, sizeof(*caps) * nb_slave_caps);
58 		return nb_slave_caps;
59 	}
60 
61 	for (i = 0; i < sync_nb_caps; i++) {
62 		struct rte_cryptodev_capabilities *cap = &caps[i];
63 		uint32_t j;
64 
65 		for (j = 0; j < nb_slave_caps; j++) {
66 			const struct rte_cryptodev_capabilities *s_cap =
67 					&slave_caps[i];
68 
69 			if (s_cap->op != cap->op || s_cap->sym.xform_type !=
70 					cap->sym.xform_type)
71 				continue;
72 
73 			if (s_cap->sym.xform_type ==
74 					RTE_CRYPTO_SYM_XFORM_AUTH) {
75 				if (s_cap->sym.auth.algo !=
76 						cap->sym.auth.algo)
77 					continue;
78 
79 				cap->sym.auth.digest_size.min =
80 					s_cap->sym.auth.digest_size.min <
81 					cap->sym.auth.digest_size.min ?
82 					s_cap->sym.auth.digest_size.min :
83 					cap->sym.auth.digest_size.min;
84 				cap->sym.auth.digest_size.max =
85 					s_cap->sym.auth.digest_size.max <
86 					cap->sym.auth.digest_size.max ?
87 					s_cap->sym.auth.digest_size.max :
88 					cap->sym.auth.digest_size.max;
89 
90 			}
91 
92 			if (s_cap->sym.xform_type ==
93 					RTE_CRYPTO_SYM_XFORM_CIPHER)
94 				if (s_cap->sym.cipher.algo !=
95 						cap->sym.cipher.algo)
96 					continue;
97 
98 			/* no common cap found */
99 			break;
100 		}
101 
102 		if (j < nb_slave_caps)
103 			continue;
104 
105 		/* remove a uncommon cap from the array */
106 		for (j = i; j < sync_nb_caps - 1; j++)
107 			rte_memcpy(&caps[j], &caps[j+1], sizeof(*cap));
108 
109 		memset(&caps[sync_nb_caps - 1], 0, sizeof(*cap));
110 		sync_nb_caps--;
111 	}
112 
113 	return sync_nb_caps;
114 }
115 
116 static int
117 update_scheduler_capability(struct scheduler_ctx *sched_ctx)
118 {
119 	struct rte_cryptodev_capabilities tmp_caps[256] = { {0} };
120 	uint32_t nb_caps = 0, i;
121 
122 	if (sched_ctx->capabilities)
123 		rte_free(sched_ctx->capabilities);
124 
125 	for (i = 0; i < sched_ctx->nb_slaves; i++) {
126 		struct rte_cryptodev_info dev_info;
127 
128 		rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
129 
130 		nb_caps = sync_caps(tmp_caps, nb_caps, dev_info.capabilities);
131 		if (nb_caps == 0)
132 			return -1;
133 	}
134 
135 	sched_ctx->capabilities = rte_zmalloc_socket(NULL,
136 			sizeof(struct rte_cryptodev_capabilities) *
137 			(nb_caps + 1), 0, SOCKET_ID_ANY);
138 	if (!sched_ctx->capabilities)
139 		return -ENOMEM;
140 
141 	rte_memcpy(sched_ctx->capabilities, tmp_caps,
142 			sizeof(struct rte_cryptodev_capabilities) * nb_caps);
143 
144 	return 0;
145 }
146 
147 static void
148 update_scheduler_feature_flag(struct rte_cryptodev *dev)
149 {
150 	struct scheduler_ctx *sched_ctx = dev->data->dev_private;
151 	uint32_t i;
152 
153 	dev->feature_flags = 0;
154 
155 	for (i = 0; i < sched_ctx->nb_slaves; i++) {
156 		struct rte_cryptodev_info dev_info;
157 
158 		rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
159 
160 		dev->feature_flags |= dev_info.feature_flags;
161 	}
162 }
163 
164 static void
165 update_max_nb_qp(struct scheduler_ctx *sched_ctx)
166 {
167 	uint32_t i;
168 	uint32_t max_nb_qp;
169 
170 	if (!sched_ctx->nb_slaves)
171 		return;
172 
173 	max_nb_qp = sched_ctx->nb_slaves ? UINT32_MAX : 0;
174 
175 	for (i = 0; i < sched_ctx->nb_slaves; i++) {
176 		struct rte_cryptodev_info dev_info;
177 
178 		rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
179 		max_nb_qp = dev_info.max_nb_queue_pairs < max_nb_qp ?
180 				dev_info.max_nb_queue_pairs : max_nb_qp;
181 	}
182 
183 	sched_ctx->max_nb_queue_pairs = max_nb_qp;
184 }
185 
186 /** Attach a device to the scheduler. */
187 int
188 rte_cryptodev_scheduler_slave_attach(uint8_t scheduler_id, uint8_t slave_id)
189 {
190 	struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
191 	struct scheduler_ctx *sched_ctx;
192 	struct scheduler_slave *slave;
193 	struct rte_cryptodev_info dev_info;
194 	uint32_t i;
195 
196 	if (!dev) {
197 		CS_LOG_ERR("Operation not supported");
198 		return -ENOTSUP;
199 	}
200 
201 	if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
202 		CS_LOG_ERR("Operation not supported");
203 		return -ENOTSUP;
204 	}
205 
206 	if (dev->data->dev_started) {
207 		CS_LOG_ERR("Illegal operation");
208 		return -EBUSY;
209 	}
210 
211 	sched_ctx = dev->data->dev_private;
212 	if (sched_ctx->nb_slaves >= MAX_SLAVES_NUM) {
213 		CS_LOG_ERR("Too many slaves attached");
214 		return -ENOMEM;
215 	}
216 
217 	for (i = 0; i < sched_ctx->nb_slaves; i++)
218 		if (sched_ctx->slaves[i].dev_id == slave_id) {
219 			CS_LOG_ERR("Slave already added");
220 			return -ENOTSUP;
221 		}
222 
223 	slave = &sched_ctx->slaves[sched_ctx->nb_slaves];
224 
225 	rte_cryptodev_info_get(slave_id, &dev_info);
226 
227 	slave->dev_id = slave_id;
228 	slave->dev_type = dev_info.dev_type;
229 	sched_ctx->nb_slaves++;
230 
231 	if (update_scheduler_capability(sched_ctx) < 0) {
232 		slave->dev_id = 0;
233 		slave->dev_type = 0;
234 		sched_ctx->nb_slaves--;
235 
236 		CS_LOG_ERR("capabilities update failed");
237 		return -ENOTSUP;
238 	}
239 
240 	update_scheduler_feature_flag(dev);
241 
242 	update_max_nb_qp(sched_ctx);
243 
244 	return 0;
245 }
246 
247 int
248 rte_cryptodev_scheduler_slave_detach(uint8_t scheduler_id, uint8_t slave_id)
249 {
250 	struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
251 	struct scheduler_ctx *sched_ctx;
252 	uint32_t i, slave_pos;
253 
254 	if (!dev) {
255 		CS_LOG_ERR("Operation not supported");
256 		return -ENOTSUP;
257 	}
258 
259 	if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
260 		CS_LOG_ERR("Operation not supported");
261 		return -ENOTSUP;
262 	}
263 
264 	if (dev->data->dev_started) {
265 		CS_LOG_ERR("Illegal operation");
266 		return -EBUSY;
267 	}
268 
269 	sched_ctx = dev->data->dev_private;
270 
271 	for (slave_pos = 0; slave_pos < sched_ctx->nb_slaves; slave_pos++)
272 		if (sched_ctx->slaves[slave_pos].dev_id == slave_id)
273 			break;
274 	if (slave_pos == sched_ctx->nb_slaves) {
275 		CS_LOG_ERR("Cannot find slave");
276 		return -ENOTSUP;
277 	}
278 
279 	if (sched_ctx->ops.slave_detach(dev, slave_id) < 0) {
280 		CS_LOG_ERR("Failed to detach slave");
281 		return -ENOTSUP;
282 	}
283 
284 	for (i = slave_pos; i < sched_ctx->nb_slaves - 1; i++) {
285 		memcpy(&sched_ctx->slaves[i], &sched_ctx->slaves[i+1],
286 				sizeof(struct scheduler_slave));
287 	}
288 	memset(&sched_ctx->slaves[sched_ctx->nb_slaves - 1], 0,
289 			sizeof(struct scheduler_slave));
290 	sched_ctx->nb_slaves--;
291 
292 	if (update_scheduler_capability(sched_ctx) < 0) {
293 		CS_LOG_ERR("capabilities update failed");
294 		return -ENOTSUP;
295 	}
296 
297 	update_scheduler_feature_flag(dev);
298 
299 	update_max_nb_qp(sched_ctx);
300 
301 	return 0;
302 }
303 
304 int
305 rte_crpytodev_scheduler_mode_set(uint8_t scheduler_id,
306 		enum rte_cryptodev_scheduler_mode mode)
307 {
308 	struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
309 	struct scheduler_ctx *sched_ctx;
310 
311 	if (!dev) {
312 		CS_LOG_ERR("Operation not supported");
313 		return -ENOTSUP;
314 	}
315 
316 	if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
317 		CS_LOG_ERR("Operation not supported");
318 		return -ENOTSUP;
319 	}
320 
321 	if (dev->data->dev_started) {
322 		CS_LOG_ERR("Illegal operation");
323 		return -EBUSY;
324 	}
325 
326 	sched_ctx = dev->data->dev_private;
327 
328 	if (mode == sched_ctx->mode)
329 		return 0;
330 
331 	switch (mode) {
332 	case CDEV_SCHED_MODE_ROUNDROBIN:
333 		if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
334 				roundrobin_scheduler) < 0) {
335 			CS_LOG_ERR("Failed to load scheduler");
336 			return -1;
337 		}
338 		break;
339 	default:
340 		CS_LOG_ERR("Not yet supported");
341 		return -ENOTSUP;
342 	}
343 
344 	return 0;
345 }
346 
347 enum rte_cryptodev_scheduler_mode
348 rte_crpytodev_scheduler_mode_get(uint8_t scheduler_id)
349 {
350 	struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
351 	struct scheduler_ctx *sched_ctx;
352 
353 	if (!dev) {
354 		CS_LOG_ERR("Operation not supported");
355 		return -ENOTSUP;
356 	}
357 
358 	if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
359 		CS_LOG_ERR("Operation not supported");
360 		return -ENOTSUP;
361 	}
362 
363 	sched_ctx = dev->data->dev_private;
364 
365 	return sched_ctx->mode;
366 }
367 
368 int
369 rte_cryptodev_scheduler_ordering_set(uint8_t scheduler_id,
370 		uint32_t enable_reorder)
371 {
372 	struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
373 	struct scheduler_ctx *sched_ctx;
374 
375 	if (!dev) {
376 		CS_LOG_ERR("Operation not supported");
377 		return -ENOTSUP;
378 	}
379 
380 	if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
381 		CS_LOG_ERR("Operation not supported");
382 		return -ENOTSUP;
383 	}
384 
385 	if (dev->data->dev_started) {
386 		CS_LOG_ERR("Illegal operation");
387 		return -EBUSY;
388 	}
389 
390 	sched_ctx = dev->data->dev_private;
391 
392 	sched_ctx->reordering_enabled = enable_reorder;
393 
394 	return 0;
395 }
396 
397 int
398 rte_cryptodev_scheduler_ordering_get(uint8_t scheduler_id)
399 {
400 	struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
401 	struct scheduler_ctx *sched_ctx;
402 
403 	if (!dev) {
404 		CS_LOG_ERR("Operation not supported");
405 		return -ENOTSUP;
406 	}
407 
408 	if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
409 		CS_LOG_ERR("Operation not supported");
410 		return -ENOTSUP;
411 	}
412 
413 	sched_ctx = dev->data->dev_private;
414 
415 	return (int)sched_ctx->reordering_enabled;
416 }
417 
418 int
419 rte_cryptodev_scheduler_load_user_scheduler(uint8_t scheduler_id,
420 		struct rte_cryptodev_scheduler *scheduler) {
421 
422 	struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
423 	struct scheduler_ctx *sched_ctx;
424 
425 	if (!dev) {
426 		CS_LOG_ERR("Operation not supported");
427 		return -ENOTSUP;
428 	}
429 
430 	if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
431 		CS_LOG_ERR("Operation not supported");
432 		return -ENOTSUP;
433 	}
434 
435 	if (dev->data->dev_started) {
436 		CS_LOG_ERR("Illegal operation");
437 		return -EBUSY;
438 	}
439 
440 	sched_ctx = dev->data->dev_private;
441 
442 	strncpy(sched_ctx->name, scheduler->name,
443 			RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
444 	strncpy(sched_ctx->description, scheduler->description,
445 			RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN);
446 
447 	/* load scheduler instance operations functions */
448 	sched_ctx->ops.config_queue_pair = scheduler->ops->config_queue_pair;
449 	sched_ctx->ops.create_private_ctx = scheduler->ops->create_private_ctx;
450 	sched_ctx->ops.scheduler_start = scheduler->ops->scheduler_start;
451 	sched_ctx->ops.scheduler_stop = scheduler->ops->scheduler_stop;
452 	sched_ctx->ops.slave_attach = scheduler->ops->slave_attach;
453 	sched_ctx->ops.slave_detach = scheduler->ops->slave_detach;
454 
455 	if (sched_ctx->private_ctx)
456 		rte_free(sched_ctx->private_ctx);
457 
458 	if (sched_ctx->ops.create_private_ctx) {
459 		int ret = (*sched_ctx->ops.create_private_ctx)(dev);
460 
461 		if (ret < 0) {
462 			CS_LOG_ERR("Unable to create scheduler private "
463 					"context");
464 			return ret;
465 		}
466 	}
467 
468 	sched_ctx->mode = scheduler->mode;
469 
470 	return 0;
471 }
472