1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017-2018 NXP
8 #include <hw/desc/ipsec.h>
10 /* NXP CAAM JR PMD device name */
12 #define CAAM_JR_ALG_UNSUPPORT (-1)
14 /* Minimum job descriptor consists of a oneword job descriptor HEADER and
15 * a pointer to the shared descriptor.
17 #define MIN_JOB_DESC_SIZE (CAAM_CMD_SZ + CAAM_PTR_SZ)
18 #define CAAM_JOB_DESC_SIZE 13
20 /* CTX_POOL_NUM_BUFS is set as per the ipsec-secgw application */
21 #define CTX_POOL_NUM_BUFS 32000
22 #define CTX_POOL_CACHE_SIZE 512
27 #define JR_MAX_NB_MAX_DIGEST 32
29 #define RTE_CAAM_JR_PMD_MAX_NB_SESSIONS 2048
32 /* Return codes for SEC user space driver APIs */
33 enum sec_return_code_e {
34 SEC_SUCCESS = 0, /* Operation executed successfully.*/
35 SEC_INVALID_INPUT_PARAM, /* API received an invalid input
38 SEC_OUT_OF_MEMORY, /* Memory allocation failed. */
39 SEC_DESCRIPTOR_IN_FLIGHT, /* API function indicates there are
40 * descriptors in flight
43 SEC_LAST_DESCRIPTOR_IN_FLIGHT, /* API function indicates there is one
44 * last descriptor in flight
45 * for SEC to process that.
47 SEC_PROCESSING_ERROR, /* Indicates a SEC processing error
48 * occurred on a Job Ring which requires
49 * a SEC user space driver shutdown. Can
50 * be returned from sec_poll_job_ring().
51 * Then the only other API that can be
52 * called after this error is
55 SEC_DESC_PROCESSING_ERROR, /* Indicates a SEC descriptor processing
56 * error occurred on a Job Ring. Can be
57 * returned from sec_poll_job_ring().
58 * The driver was able to reset job ring
59 * and job ring can be used like in a
62 SEC_JR_IS_FULL, /* Job Ring is full. There is no more
63 * room in the JR for new descriptors.
64 * This can happen if the descriptor RX
65 * rate is higher than SEC's capacity.
67 SEC_DRIVER_RELEASE_IN_PROGRESS, /* SEC driver shutdown is in progress,
68 * descriptors processing or polling is
71 SEC_DRIVER_ALREADY_INITIALIZED, /* SEC driver is already initialized.*/
72 SEC_DRIVER_NOT_INITIALIZED, /* SEC driver is NOT initialized. */
73 SEC_JOB_RING_RESET_IN_PROGRESS, /* Job ring is resetting due to a
74 * per-descriptor SEC processing error
75 * ::SEC_desc_PROCESSING_ERROR. Reset is
76 * finished when sec_poll_job_ring()
77 * return. Then the job ring can be used
80 SEC_RESET_ENGINE_FAILED, /* Resetting of SEC Engine by SEC Kernel
83 SEC_ENABLE_IRQS_FAILED, /* Enabling of IRQs in SEC Kernel Driver
86 SEC_DISABLE_IRQS_FAILED, /* Disabling of IRQs in SEC Kernel
89 /* END OF VALID VALUES */
91 SEC_RETURN_CODE_MAX_VALUE, /* Invalid value for return code. It is
92 * used to mark the end of the return
93 * code values. @note ALL new return
94 * code values MUST be added before
95 * ::SEC_RETURN_CODE_MAX_VALUE!
99 enum caam_jr_op_type {
100 CAAM_JR_NONE, /* No Cipher operations*/
101 CAAM_JR_CIPHER,/* CIPHER operations */
102 CAAM_JR_AUTH, /* Authentication Operations */
103 CAAM_JR_AEAD, /* Authenticated Encryption with associated data */
104 CAAM_JR_IPSEC, /* IPSEC protocol operations*/
105 CAAM_JR_PDCP, /* PDCP protocol operations*/
106 CAAM_JR_PKC, /* Public Key Cryptographic Operations */
110 struct caam_jr_session {
111 uint8_t dir; /* Operation Direction */
112 enum rte_crypto_cipher_algorithm cipher_alg; /* Cipher Algorithm*/
113 enum rte_crypto_auth_algorithm auth_alg; /* Authentication Algorithm*/
114 enum rte_crypto_aead_algorithm aead_alg; /* AEAD Algorithm*/
115 enum rte_security_session_protocol proto_alg; /* Security Algorithm*/
118 uint8_t *data; /* pointer to key data */
119 size_t length; /* key length in bytes */
123 uint8_t *data; /* pointer to key data */
124 size_t length; /* key length in bytes */
127 uint8_t *data; /* pointer to key data */
128 size_t length; /* key length in bytes */
135 } iv; /* Initialisation vector parameters */
136 uint16_t auth_only_len; /* Length of data for Auth only */
137 uint32_t digest_length;
138 struct ipsec_encap_pdb encap_pdb;
140 struct ipsec_decap_pdb decap_pdb;
141 struct caam_jr_qp *qp;
142 struct sec_cdb *cdb; /* cmd block associated with qp */
143 struct rte_mempool *ctx_pool; /* session mempool for caam_jr_op_ctx */
147 * 16-byte hardware scatter/gather table
150 #define SEC4_SG_LEN_EXT 0x80000000 /* Entry points to table */
151 #define SEC4_SG_LEN_FIN 0x40000000 /* Last ent in table */
152 #define SEC4_SG_BPID_MASK 0x000000ff
153 #define SEC4_SG_BPID_SHIFT 16
154 #define SEC4_SG_LEN_MASK 0x3fffffff /* Excludes EXT and FINAL */
155 #define SEC4_SG_OFFSET_MASK 0x00001fff
157 struct sec4_sg_entry {
160 uint32_t bpid_offset;
163 #define MAX_SG_ENTRIES 16
164 #define SG_CACHELINE_0 0
165 #define SG_CACHELINE_1 4
166 #define SG_CACHELINE_2 8
167 #define SG_CACHELINE_3 12
169 /* Structure encompassing a job descriptor which is to be processed
170 * by SEC. User should also initialise this structure with the callback
171 * function pointer which will be called by driver after recieving proccessed
172 * descriptor from SEC. User data is also passed in this data structure which
173 * will be sent as an argument to the user callback function.
175 struct job_descriptor {
176 uint32_t desc[CAAM_JOB_DESC_SIZE];
179 struct caam_jr_op_ctx {
180 struct job_descriptor jobdes;
181 /* sg[0] output, sg[1] input, others are possible sub frames */
182 struct sec4_sg_entry sg[MAX_SG_ENTRIES];
183 struct rte_crypto_op *op;
184 struct rte_mempool *ctx_pool; /* mempool pointer for caam_jr_op_ctx */
186 uint8_t digest[JR_MAX_NB_MAX_DIGEST];
192 * @param buffer calculate chksum for buffer
193 * @param len buffer length
195 * @return checksum value in host cpu order
197 static inline uint16_t
198 calc_chksum(void *buffer, int len)
200 uint16_t *buf = (uint16_t *)buffer;
204 for (sum = 0; len > 1; len -= 2)
208 sum += *(unsigned char *)buf;
210 sum = (sum >> 16) + (sum & 0xFFFF);
216 struct uio_job_ring {
219 void *register_base_addr;
221 int uio_minor_number;
224 int sec_cleanup(void);
225 int sec_configure(void);
226 struct uio_job_ring *config_job_ring(void);
227 void free_job_ring(uint32_t uio_fd);
229 /* For Dma memory allocation of specified length and alignment */
231 caam_jr_dma_mem_alloc(size_t align, size_t len)
233 return rte_malloc("mem_alloc", len, align);
236 /* For freeing dma memory */
238 caam_jr_dma_free(void *ptr)
243 static inline rte_iova_t
244 caam_jr_mem_vtop(void *vaddr)
246 const struct rte_memseg *ms;
248 ms = rte_mem_virt2memseg(vaddr, NULL);
250 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
255 caam_jr_dma_ptov(rte_iova_t paddr)
257 return rte_mem_iova2virt(paddr);
260 /* Virtual to physical address conversion */
261 static inline rte_iova_t caam_jr_dma_vtop(void *ptr)
263 return caam_jr_mem_vtop(ptr);
266 /** @brief Request to SEC kernel driver to enable interrupts for
267 * descriptor finished processing
268 * Use UIO to communicate with SEC kernel driver: write command
269 * value that indicates an IRQ enable action into UIO file descriptor
272 * @param [in] uio_fd Job Ring UIO File descriptor
273 * @retval 0 for success
274 * @retval -1 value for error
276 uint32_t caam_jr_enable_irqs(uint32_t uio_fd);
278 /** @brief Request to SEC kernel driver to disable interrupts for descriptor
279 * finished processing
280 * Use UIO to communicate with SEC kernel driver: write command
281 * value that indicates an IRQ disable action into UIO file descriptor
284 * @param [in] uio_fd UIO File descripto
285 * @retval 0 for success
286 * @retval -1 value for error
289 uint32_t caam_jr_disable_irqs(uint32_t uio_fd);