1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2017-2018 NXP
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <rte_common.h>
#include <rte_malloc.h>
#include <rte_crypto.h>
#include <rte_security.h>
#include <caam_jr_config.h>
#include <caam_jr_hw_specific.h>
#include <caam_jr_pvt.h>
#include <caam_jr_log.h>
/* RTA header files */
#include <hw/desc/common.h>
#include <hw/desc/algo.h>
#include <hw/desc/ipsec.h>
/* Prefix path to sysfs directory where UIO device attributes are exported.
* Path for UIO device X is /sys/class/uio/uioX
*/
#define SEC_UIO_DEVICE_SYS_ATTR_PATH "/sys/class/uio"
/* Subfolder in sysfs where mapping attributes are exported
* for each UIO device. Path for mapping Y for device X is:
* /sys/class/uio/uioX/maps/mapY
*/
#define SEC_UIO_DEVICE_SYS_MAP_ATTR "maps/map"
/* Name of UIO device file prefix. Each UIO device will have a device file
* /dev/uioX, where X is the minor device number.
*/
#define SEC_UIO_DEVICE_FILE_NAME "/dev/uio"
/*
* Name of UIO device. Each user space SEC job ring will have a corresponding
* UIO device with the name sec-channelX, where X is the job ring id.
* Maximum length is #SEC_UIO_MAX_DEVICE_NAME_LENGTH.
*
* @note Must be kept in synch with SEC kernel driver
* define #SEC_UIO_DEVICE_NAME !
*/
#define SEC_UIO_DEVICE_NAME "fsl-jr"
/* Maximum length for the name of an UIO device file.
* Device file name format is: /dev/uioX.
*/
#define SEC_UIO_MAX_DEVICE_FILE_NAME_LENGTH 30
/* Maximum length for the name of an attribute file for an UIO device.
* Attribute files are exported in sysfs and have the name formatted as:
* /sys/class/uio/uioX/<attribute_file_name>
*/
#define SEC_UIO_MAX_ATTR_FILE_NAME 100
/* Command that is used by SEC user space driver and SEC kernel driver
* to signal a request from the former to the later to disable job DONE
* and error IRQs on a certain job ring.
* The configuration is done at SEC Controller's level.
* @note Need to be kept in synch with #SEC_UIO_DISABLE_IRQ_CMD from
* linux/drivers/crypto/talitos.c !
*/
#define SEC_UIO_DISABLE_IRQ_CMD 0
/* Command that is used by SEC user space driver and SEC kernel driver
* to signal a request from the former to the later to enable job DONE
* and error IRQs on a certain job ring.
* The configuration is done at SEC Controller's level.
* @note Need to be kept in synch with #SEC_UIO_ENABLE_IRQ_CMD from
* linux/drivers/crypto/talitos.c !
*/
#define SEC_UIO_ENABLE_IRQ_CMD 1
/** Command that is used by SEC user space driver and SEC kernel driver
* to signal a request from the former to the later to do a SEC engine reset.
* @note Need to be kept in synch with #SEC_UIO_RESET_SEC_ENGINE_CMD from
* linux/drivers/crypto/talitos.c !
*/
#define SEC_UIO_RESET_SEC_ENGINE_CMD 3
/* The id for the mapping used to export SEC's registers to
* user space through UIO devices.
*/
#define SEC_UIO_MAP_ID 0
static struct uio_job_ring g_uio_job_ring[MAX_SEC_JOB_RINGS];
static int g_uio_jr_num;
/** @brief Checks if a file name contains a certain substring.
* If so, it extracts the number following the substring.
* This function assumes a filename format of: [text][number].
* @param [in] filename File name
* @param [in] match String to match in file name
* @param [out] number The number extracted from filename
*
* @retval true if file name matches the criteria
* @retval false if file name does not match the criteria
*/
static bool
file_name_match_extract(const char filename[], const char match[], int *number)
{
char *substr = NULL;
substr = strstr(filename, match);
if (substr == NULL)
return false;
/* substring <match> was found in <filename>
* read number following <match> substring in <filename>
*/
if (sscanf(filename + strlen(match), "%d", number) <= 0)
return false;
return true;
}
/** @brief Reads first line from a file.
* Composes file name as: root/subdir/filename
*
* @param [in] root Root path
* @param [in] subdir Subdirectory name
* @param [in] filename File name
* @param [out] line The first line read from file.
*
* @retval 0 for succes
* @retval other value for error
*/
static int
file_read_first_line(const char root[], const char subdir[],
const char filename[], char *line)
{
char absolute_file_name[SEC_UIO_MAX_ATTR_FILE_NAME];
int fd = 0, ret = 0;
/*compose the file name: root/subdir/filename */
memset(absolute_file_name, 0, sizeof(absolute_file_name));
snprintf(absolute_file_name, SEC_UIO_MAX_ATTR_FILE_NAME,
"%s/%s/%s", root, subdir, filename);
fd = open(absolute_file_name, O_RDONLY);
SEC_ASSERT(fd > 0, fd, "Error opening file %s",
absolute_file_name);
/* read UIO device name from first line in file */
ret = read(fd, line, SEC_UIO_MAX_DEVICE_FILE_NAME_LENGTH);
close(fd);
/* NULL-ify string */
line[SEC_UIO_MAX_DEVICE_FILE_NAME_LENGTH - 1] = '\0';
if (ret <= 0) {
CAAM_JR_ERR("Error reading from file %s", absolute_file_name);
return ret;
}
return 0;
}
/** @brief Uses UIO control to send commands to SEC kernel driver.
* The mechanism is to write a command word into the file descriptor
* that the user-space driver obtained for each user-space SEC job ring.
* Both user-space driver and kernel driver must have the same understanding
* about the command codes.
*
* @param [in] UIO FD The UIO file descriptor
* @param [in] uio_command Command word
*
* @retval Result of write operation on the job ring's UIO file descriptor.
* Should be sizeof(int) for success operations.
* Other values can be returned and used, if desired to add special
* meaning to return values, but this has to be programmed in SEC
* kernel driver as well. No special return values are used.
*/
static int
sec_uio_send_command(uint32_t uio_fd, int32_t uio_command)
{
int ret;
/* Use UIO file descriptor we have for this job ring.
* Writing a command code to this file descriptor will make the
* SEC kernel driver execute the desired command.
*/
ret = write(uio_fd, &uio_command, sizeof(int));
return ret;
}
/** @brief Request to SEC kernel driver to enable interrupts for
* descriptor finished processing
* Use UIO to communicate with SEC kernel driver: write command
* value that indicates an IRQ enable action into UIO file descriptor
* of this job ring.
*
* @param [in] uio_fd Job Ring UIO File descriptor
* @retval 0 for success
* @retval -1 value for error
*/
uint32_t
caam_jr_enable_irqs(uint32_t uio_fd)
{
int ret;
/* Use UIO file descriptor we have for this job ring.
* Writing a command code to this file descriptor will make the
* SEC kernel driver enable DONE and Error IRQs for this job ring,
* at Controller level.
*/
ret = sec_uio_send_command(uio_fd, SEC_UIO_ENABLE_IRQ_CMD);
SEC_ASSERT(ret == sizeof(int), -1,
"Failed to request SEC engine to enable job done and "
"error IRQs through UIO control. UIO FD %d. Reset SEC driver!",
uio_fd);
CAAM_JR_DEBUG("Enabled IRQs on jr with uio_fd %d", uio_fd);
return 0;
}
/** @brief Request to SEC kernel driver to disable interrupts for descriptor
* finished processing
* Use UIO to communicate with SEC kernel driver: write command
* value that indicates an IRQ disable action into UIO file descriptor
* of this job ring.
*
* @param [in] uio_fd UIO File descripto
* @retval 0 for success
* @retval -1 value for error
*
*/
uint32_t
caam_jr_disable_irqs(uint32_t uio_fd)
{
int ret;
/* Use UIO file descriptor we have for this job ring.
* Writing a command code to this file descriptor will make the
* SEC kernel driver disable IRQs for this job ring,
* at Controller level.
*/
ret = sec_uio_send_command(uio_fd, SEC_UIO_DISABLE_IRQ_CMD);
SEC_ASSERT(ret == sizeof(int), -1,
"Failed to request SEC engine to disable job done and "
"IRQs through UIO control. UIO_FD %d Reset SEC driver!",
uio_fd);
CAAM_JR_DEBUG("Disabled IRQs on jr with uio_fd %d", uio_fd);
return 0;
}
/** @brief Maps register range assigned for a job ring.
*
* @param [in] uio_device_fd UIO device file descriptor
* @param [in] uio_device_id UIO device id
* @param [in] uio_map_id UIO allows maximum 5 different mapping for
each device. Maps start with id 0.
* @param [out] map_size Map size.
* @retval NULL if failed to map registers
* @retval Virtual address for mapped register address range
*/
static void *
uio_map_registers(int uio_device_fd, int uio_device_id,
int uio_map_id, int *map_size)
{
void *mapped_address = NULL;
unsigned int uio_map_size = 0;
char uio_sys_root[SEC_UIO_MAX_ATTR_FILE_NAME];
char uio_sys_map_subdir[SEC_UIO_MAX_ATTR_FILE_NAME];
char uio_map_size_str[32];
int ret = 0;
/* compose the file name: root/subdir/filename */
memset(uio_sys_root, 0, sizeof(uio_sys_root));
memset(uio_sys_map_subdir, 0, sizeof(uio_sys_map_subdir));
memset(uio_map_size_str, 0, sizeof(uio_map_size_str));
/* Compose string: /sys/class/uio/uioX */
sprintf(uio_sys_root, "%s/%s%d", SEC_UIO_DEVICE_SYS_ATTR_PATH,
"uio", uio_device_id);
/* Compose string: maps/mapY */
sprintf(uio_sys_map_subdir, "%s%d", SEC_UIO_DEVICE_SYS_MAP_ATTR,
uio_map_id);
/* Read first (and only) line from file
* /sys/class/uio/uioX/maps/mapY/size
*/
ret = file_read_first_line(uio_sys_root, uio_sys_map_subdir,
"size", uio_map_size_str);
SEC_ASSERT(ret == 0, NULL, "file_read_first_line() failed");
/* Read mapping size, expressed in hexa(base 16) */
uio_map_size = strtol(uio_map_size_str, NULL, 16);
/* Map the region in user space */
mapped_address = mmap(0, /*dynamically choose virtual address */
uio_map_size, PROT_READ | PROT_WRITE,
MAP_SHARED, uio_device_fd, 0);
/* offset = 0 because UIO device has only one mapping
* for the entire SEC register memory
*/
if (mapped_address == MAP_FAILED) {
CAAM_JR_ERR(
"Failed to map registers! errno = %d job ring fd = %d,"
"uio device id = %d, uio map id = %d", errno,
uio_device_fd, uio_device_id, uio_map_id);
return NULL;
}
/*
* Save the map size to use it later on for munmap-ing.
*/
*map_size = uio_map_size;
CAAM_JR_INFO("UIO dev[%d] mapped region [id =%d] size 0x%x at %p",
uio_device_id, uio_map_id, uio_map_size, mapped_address);
return mapped_address;
}
void
free_job_ring(uint32_t uio_fd)
{
struct uio_job_ring *job_ring = NULL;
int i;
if (!uio_fd)
return;
for (i = 0; i < MAX_SEC_JOB_RINGS; i++) {
if (g_uio_job_ring[i].uio_fd == uio_fd) {
job_ring = &g_uio_job_ring[i];
break;
}
}
if (job_ring == NULL) {
CAAM_JR_ERR("JR not available for fd = %x\n", uio_fd);
return;
}
/* Open device file */
CAAM_JR_INFO("Closed device file for job ring %d , fd = %d",
job_ring->jr_id, job_ring->uio_fd);
close(job_ring->uio_fd);
g_uio_jr_num--;
job_ring->uio_fd = 0;
if (job_ring->register_base_addr == NULL)
return;
/* Unmap the PCI memory resource of device */
if (munmap(job_ring->register_base_addr, job_ring->map_size)) {
CAAM_JR_INFO("cannot munmap(%p, 0x%lx): %s",
job_ring->register_base_addr,
(unsigned long)job_ring->map_size, strerror(errno));
} else
CAAM_JR_DEBUG(" JR UIO memory unmapped at %p",
job_ring->register_base_addr);
job_ring->register_base_addr = NULL;
}
struct
uio_job_ring *config_job_ring(void)
{
char uio_device_file_name[32];
struct uio_job_ring *job_ring = NULL;
int i;
for (i = 0; i < MAX_SEC_JOB_RINGS; i++) {
if (g_uio_job_ring[i].uio_fd == 0) {
job_ring = &g_uio_job_ring[i];
g_uio_jr_num++;
break;
}
}
if (job_ring == NULL) {
CAAM_JR_ERR("No free job ring\n");
return NULL;
}
/* Find UIO device created by SEC kernel driver for this job ring. */
memset(uio_device_file_name, 0, sizeof(uio_device_file_name));
sprintf(uio_device_file_name, "%s%d", SEC_UIO_DEVICE_FILE_NAME,
job_ring->uio_minor_number);
/* Open device file */
job_ring->uio_fd = open(uio_device_file_name, O_RDWR);
SEC_ASSERT(job_ring->uio_fd > 0, NULL,
"Failed to open UIO device file for job ring %d",
job_ring->jr_id);
CAAM_JR_INFO("Open device(%s) file for job ring=%d , uio_fd = %d",
uio_device_file_name, job_ring->jr_id, job_ring->uio_fd);
ASSERT(job_ring->register_base_addr == NULL);
job_ring->register_base_addr = uio_map_registers(
job_ring->uio_fd, job_ring->uio_minor_number,
SEC_UIO_MAP_ID, &job_ring->map_size);
SEC_ASSERT(job_ring->register_base_addr != NULL, NULL,
"Failed to map SEC registers");
return job_ring;
}
int
sec_configure(void)
{
char uio_name[32];
int config_jr_no = 0, jr_id = -1;
int uio_minor_number = -1;
int ret;
DIR *d = NULL;
struct dirent *dir;
d = opendir(SEC_UIO_DEVICE_SYS_ATTR_PATH);
if (d == NULL) {
printf("\nError opening directory '%s': %s\n",
SEC_UIO_DEVICE_SYS_ATTR_PATH, strerror(errno));
return -1;
}
/* Iterate through all subdirs */
while ((dir = readdir(d)) != NULL) {
if (!strncmp(dir->d_name, ".", 1) ||
!strncmp(dir->d_name, "..", 2))
continue;
if (file_name_match_extract
(dir->d_name, "uio", &uio_minor_number)) {
/*
* Open file uioX/name and read first line which contains
* the name for the device. Based on the name check if this
* UIO device is UIO device for job ring with id jr_id.
*/
memset(uio_name, 0, sizeof(uio_name));
ret = file_read_first_line(SEC_UIO_DEVICE_SYS_ATTR_PATH,
dir->d_name, "name", uio_name);
CAAM_JR_INFO("sec device uio name: %s", uio_name);
SEC_ASSERT(ret == 0, -1, "file_read_first_line failed");
if (file_name_match_extract(uio_name,
SEC_UIO_DEVICE_NAME,
&jr_id)) {
g_uio_job_ring[config_jr_no].jr_id = jr_id;
g_uio_job_ring[config_jr_no].uio_minor_number =
uio_minor_number;
CAAM_JR_INFO("Detected logical JRID:%d", jr_id);
config_jr_no++;
/* todo find the actual ring id
* OF_FULLNAME=/soc/crypto@1700000/jr@20000
*/
}
}
}
closedir(d);
if (config_jr_no == 0) {
CAAM_JR_ERR("! No SEC Job Rings assigned for userspace usage!");
return 0;
}
CAAM_JR_INFO("Total JR detected =%d", config_jr_no);
return config_jr_no;
}
int
sec_cleanup(void)
{
int i;
struct uio_job_ring *job_ring;
for (i = 0; i < g_uio_jr_num; i++) {
job_ring = &g_uio_job_ring[i];
/* munmap SEC's register memory */
if (job_ring->register_base_addr) {
munmap(job_ring->register_base_addr,
job_ring->map_size);
job_ring->register_base_addr = NULL;
}
/* I need to close the fd after shutdown UIO commands need to be
* sent using the fd
*/
if (job_ring->uio_fd != 0) {
CAAM_JR_INFO(
"Closed device file for job ring %d , fd = %d",
job_ring->jr_id, job_ring->uio_fd);
close(job_ring->uio_fd);
}
}
return 0;
}
|