1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017-2018 NXP
8 /* NXP CAAM JR PMD device name */
10 #define CAAM_JR_ALG_UNSUPPORT (-1)
12 /* Minimum job descriptor consists of a oneword job descriptor HEADER and
13 * a pointer to the shared descriptor.
15 #define MIN_JOB_DESC_SIZE (CAAM_CMD_SZ + CAAM_PTR_SZ)
16 #define CAAM_JOB_DESC_SIZE 13
18 /* CTX_POOL_NUM_BUFS is set as per the ipsec-secgw application */
19 #define CTX_POOL_NUM_BUFS 32000
20 #define CTX_POOL_CACHE_SIZE 512
25 #define JR_MAX_NB_MAX_DIGEST 32
27 #define RTE_CAAM_JR_PMD_MAX_NB_SESSIONS 2048
30 /* Return codes for SEC user space driver APIs */
31 enum sec_return_code_e {
32 SEC_SUCCESS = 0, /* Operation executed successfully.*/
33 SEC_INVALID_INPUT_PARAM, /* API received an invalid input
36 SEC_OUT_OF_MEMORY, /* Memory allocation failed. */
37 SEC_DESCRIPTOR_IN_FLIGHT, /* API function indicates there are
38 * descriptors in flight
41 SEC_LAST_DESCRIPTOR_IN_FLIGHT, /* API function indicates there is one
42 * last descriptor in flight
43 * for SEC to process that.
45 SEC_PROCESSING_ERROR, /* Indicates a SEC processing error
46 * occurred on a Job Ring which requires
47 * a SEC user space driver shutdown. Can
48 * be returned from sec_poll_job_ring().
49 * Then the only other API that can be
50 * called after this error is
53 SEC_DESC_PROCESSING_ERROR, /* Indicates a SEC descriptor processing
54 * error occurred on a Job Ring. Can be
55 * returned from sec_poll_job_ring().
56 * The driver was able to reset job ring
57 * and job ring can be used like in a
60 SEC_JR_IS_FULL, /* Job Ring is full. There is no more
61 * room in the JR for new descriptors.
62 * This can happen if the descriptor RX
63 * rate is higher than SEC's capacity.
65 SEC_DRIVER_RELEASE_IN_PROGRESS, /* SEC driver shutdown is in progress,
66 * descriptors processing or polling is
69 SEC_DRIVER_ALREADY_INITIALIZED, /* SEC driver is already initialized.*/
70 SEC_DRIVER_NOT_INITIALIZED, /* SEC driver is NOT initialized. */
71 SEC_JOB_RING_RESET_IN_PROGRESS, /* Job ring is resetting due to a
72 * per-descriptor SEC processing error
73 * ::SEC_desc_PROCESSING_ERROR. Reset is
74 * finished when sec_poll_job_ring()
75 * return. Then the job ring can be used
78 SEC_RESET_ENGINE_FAILED, /* Resetting of SEC Engine by SEC Kernel
81 SEC_ENABLE_IRQS_FAILED, /* Enabling of IRQs in SEC Kernel Driver
84 SEC_DISABLE_IRQS_FAILED, /* Disabling of IRQs in SEC Kernel
87 /* END OF VALID VALUES */
89 SEC_RETURN_CODE_MAX_VALUE, /* Invalid value for return code. It is
90 * used to mark the end of the return
91 * code values. @note ALL new return
92 * code values MUST be added before
93 * ::SEC_RETURN_CODE_MAX_VALUE!
97 enum caam_jr_op_type {
98 CAAM_JR_NONE, /* No Cipher operations*/
99 CAAM_JR_CIPHER,/* CIPHER operations */
100 CAAM_JR_AUTH, /* Authentication Operations */
101 CAAM_JR_AEAD, /* Authenticated Encryption with associated data */
102 CAAM_JR_IPSEC, /* IPSEC protocol operations*/
103 CAAM_JR_PDCP, /* PDCP protocol operations*/
104 CAAM_JR_PKC, /* Public Key Cryptographic Operations */
108 struct caam_jr_session {
109 uint8_t dir; /* Operation Direction */
110 enum rte_crypto_cipher_algorithm cipher_alg; /* Cipher Algorithm*/
111 enum rte_crypto_auth_algorithm auth_alg; /* Authentication Algorithm*/
112 enum rte_crypto_aead_algorithm aead_alg; /* AEAD Algorithm*/
115 uint8_t *data; /* pointer to key data */
116 size_t length; /* key length in bytes */
120 uint8_t *data; /* pointer to key data */
121 size_t length; /* key length in bytes */
124 uint8_t *data; /* pointer to key data */
125 size_t length; /* key length in bytes */
132 } iv; /* Initialisation vector parameters */
133 uint16_t auth_only_len; /* Length of data for Auth only */
134 uint32_t digest_length;
136 struct caam_jr_qp *qp;
137 struct sec_cdb *cdb; /* cmd block associated with qp */
138 struct rte_mempool *ctx_pool; /* session mempool for caam_jr_op_ctx */
142 * 16-byte hardware scatter/gather table
145 #define SEC4_SG_LEN_EXT 0x80000000 /* Entry points to table */
146 #define SEC4_SG_LEN_FIN 0x40000000 /* Last ent in table */
147 #define SEC4_SG_BPID_MASK 0x000000ff
148 #define SEC4_SG_BPID_SHIFT 16
149 #define SEC4_SG_LEN_MASK 0x3fffffff /* Excludes EXT and FINAL */
150 #define SEC4_SG_OFFSET_MASK 0x00001fff
152 struct sec4_sg_entry {
155 uint32_t bpid_offset;
158 #define MAX_SG_ENTRIES 16
159 #define SG_CACHELINE_0 0
160 #define SG_CACHELINE_1 4
161 #define SG_CACHELINE_2 8
162 #define SG_CACHELINE_3 12
164 /* Structure encompassing a job descriptor which is to be processed
165 * by SEC. User should also initialise this structure with the callback
166 * function pointer which will be called by driver after recieving proccessed
167 * descriptor from SEC. User data is also passed in this data structure which
168 * will be sent as an argument to the user callback function.
170 struct job_descriptor {
171 uint32_t desc[CAAM_JOB_DESC_SIZE];
174 struct caam_jr_op_ctx {
175 struct job_descriptor jobdes;
176 /* sg[0] output, sg[1] input, others are possible sub frames */
177 struct sec4_sg_entry sg[MAX_SG_ENTRIES];
178 struct rte_crypto_op *op;
179 struct rte_mempool *ctx_pool; /* mempool pointer for caam_jr_op_ctx */
181 uint8_t digest[JR_MAX_NB_MAX_DIGEST];
187 * @param buffer calculate chksum for buffer
188 * @param len buffer length
190 * @return checksum value in host cpu order
192 static inline uint16_t
193 calc_chksum(void *buffer, int len)
195 uint16_t *buf = (uint16_t *)buffer;
199 for (sum = 0; len > 1; len -= 2)
203 sum += *(unsigned char *)buf;
205 sum = (sum >> 16) + (sum & 0xFFFF);
211 struct uio_job_ring {
214 void *register_base_addr;
216 int uio_minor_number;
219 int sec_cleanup(void);
220 int sec_configure(void);
221 struct uio_job_ring *config_job_ring(void);
222 void free_job_ring(uint32_t uio_fd);
224 /* For Dma memory allocation of specified length and alignment */
226 caam_jr_dma_mem_alloc(size_t align, size_t len)
228 return rte_malloc("mem_alloc", len, align);
231 /* For freeing dma memory */
233 caam_jr_dma_free(void *ptr)
238 static inline rte_iova_t
239 caam_jr_mem_vtop(void *vaddr)
241 const struct rte_memseg *ms;
243 ms = rte_mem_virt2memseg(vaddr, NULL);
245 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
250 caam_jr_dma_ptov(rte_iova_t paddr)
252 return rte_mem_iova2virt(paddr);
255 /* Virtual to physical address conversion */
256 static inline rte_iova_t caam_jr_dma_vtop(void *ptr)
258 return caam_jr_mem_vtop(ptr);
261 /** @brief Request to SEC kernel driver to enable interrupts for
262 * descriptor finished processing
263 * Use UIO to communicate with SEC kernel driver: write command
264 * value that indicates an IRQ enable action into UIO file descriptor
267 * @param [in] uio_fd Job Ring UIO File descriptor
268 * @retval 0 for success
269 * @retval -1 value for error
271 uint32_t caam_jr_enable_irqs(uint32_t uio_fd);
273 /** @brief Request to SEC kernel driver to disable interrupts for descriptor
274 * finished processing
275 * Use UIO to communicate with SEC kernel driver: write command
276 * value that indicates an IRQ disable action into UIO file descriptor
279 * @param [in] uio_fd UIO File descripto
280 * @retval 0 for success
281 * @retval -1 value for error
284 uint32_t caam_jr_disable_irqs(uint32_t uio_fd);