crypto/caam_jr: add routines to configure HW
[dpdk.git] / drivers / crypto / caam_jr / caam_jr_pvt.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017-2018 NXP
3  */
4
5 #ifndef CAAM_JR_PVT_H
6 #define CAAM_JR_PVT_H
7
8 /* NXP CAAM JR PMD device name */
9
10 #define CAAM_JR_ALG_UNSUPPORT   (-1)
11
12 /* Minimum job descriptor consists of a oneword job descriptor HEADER and
13  * a pointer to the shared descriptor.
14  */
15 #define MIN_JOB_DESC_SIZE       (CAAM_CMD_SZ + CAAM_PTR_SZ)
16 #define CAAM_JOB_DESC_SIZE      13
17
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
21
22 #define DIR_ENC                 1
23 #define DIR_DEC                 0
24
25 #define JR_MAX_NB_MAX_DIGEST    32
26
27 #define RTE_CAAM_JR_PMD_MAX_NB_SESSIONS 2048
28
29
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
34                                         * parameter
35                                         */
36         SEC_OUT_OF_MEMORY,             /* Memory allocation failed. */
37         SEC_DESCRIPTOR_IN_FLIGHT,      /* API function indicates there are
38                                         * descriptors in flight
39                                         * for SEC to process.
40                                         */
41         SEC_LAST_DESCRIPTOR_IN_FLIGHT, /* API function indicates there is one
42                                         * last descriptor in flight
43                                         * for SEC to process that.
44                                         */
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
51                                         * sec_release().
52                                         */
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
58                                         * normal case.
59                                         */
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.
64                                          */
65         SEC_DRIVER_RELEASE_IN_PROGRESS, /* SEC driver shutdown is in progress,
66                                          * descriptors processing or polling is
67                                          * allowed.
68                                          */
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
76                                          * again.
77                                          */
78         SEC_RESET_ENGINE_FAILED,        /* Resetting of SEC Engine by SEC Kernel
79                                          * Driver Failed
80                                          */
81         SEC_ENABLE_IRQS_FAILED,         /* Enabling of IRQs in SEC Kernel Driver
82                                          * Failed
83                                          */
84         SEC_DISABLE_IRQS_FAILED,        /* Disabling of IRQs in SEC Kernel
85                                          * Driver Failed
86                                          */
87         /* END OF VALID VALUES */
88
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!
94                                          */
95 };
96
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 */
105         CAAM_JR_MAX
106 };
107
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*/
113         union {
114                 struct {
115                         uint8_t *data;  /* pointer to key data */
116                         size_t length;  /* key length in bytes */
117                 } aead_key;
118                 struct {
119                         struct {
120                                 uint8_t *data;  /* pointer to key data */
121                                 size_t length;  /* key length in bytes */
122                         } cipher_key;
123                         struct {
124                                 uint8_t *data;  /* pointer to key data */
125                                 size_t length;  /* key length in bytes */
126                         } auth_key;
127                 };
128         };
129         struct {
130                 uint16_t length;
131                 uint16_t offset;
132         } iv;   /* Initialisation vector parameters */
133         uint16_t auth_only_len; /* Length of data for Auth only */
134         uint32_t digest_length;
135         struct ip ip4_hdr;
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 */
139 };
140
141 /*
142  * 16-byte hardware scatter/gather table
143  */
144
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
151
152 struct sec4_sg_entry {
153         uint64_t ptr;
154         uint32_t len;
155         uint32_t bpid_offset;
156 };
157
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
163
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.
169  */
170 struct job_descriptor {
171         uint32_t desc[CAAM_JOB_DESC_SIZE];
172 };
173
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 */
180         int64_t vtop_offset;
181         uint8_t digest[JR_MAX_NB_MAX_DIGEST];
182 };
183
184 /**
185  * Checksum
186  *
187  * @param buffer calculate chksum for buffer
188  * @param len    buffer length
189  *
190  * @return checksum value in host cpu order
191  */
192 static inline uint16_t
193 calc_chksum(void *buffer, int len)
194 {
195         uint16_t *buf = (uint16_t *)buffer;
196         uint32_t sum = 0;
197         uint16_t result;
198
199         for (sum = 0; len > 1; len -= 2)
200                 sum += *buf++;
201
202         if (len == 1)
203                 sum += *(unsigned char *)buf;
204
205         sum = (sum >> 16) + (sum & 0xFFFF);
206         sum += (sum >> 16);
207         result = ~sum;
208
209         return  result;
210 }
211 struct uio_job_ring {
212         uint32_t jr_id;
213         uint32_t uio_fd;
214         void *register_base_addr;
215         int map_size;
216         int uio_minor_number;
217 };
218
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);
223
224 /* For Dma memory allocation of specified length and alignment */
225 static inline void *
226 caam_jr_dma_mem_alloc(size_t align, size_t len)
227 {
228         return rte_malloc("mem_alloc", len, align);
229 }
230
231 /* For freeing dma memory */
232 static inline void
233 caam_jr_dma_free(void *ptr)
234 {
235         rte_free(ptr);
236 }
237
238 static inline rte_iova_t
239 caam_jr_mem_vtop(void *vaddr)
240 {
241         const struct rte_memseg *ms;
242
243         ms = rte_mem_virt2memseg(vaddr, NULL);
244         if (ms)
245                 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
246         return (size_t)NULL;
247 }
248
249 static inline void *
250 caam_jr_dma_ptov(rte_iova_t paddr)
251 {
252         return rte_mem_iova2virt(paddr);
253 }
254
255 /* Virtual to physical address conversion */
256 static inline rte_iova_t caam_jr_dma_vtop(void *ptr)
257 {
258         return caam_jr_mem_vtop(ptr);
259 }
260
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
265  *  of this job ring.
266  *
267  * @param [in]  uio_fd     Job Ring UIO File descriptor
268  * @retval 0 for success
269  * @retval -1 value for error
270  */
271 uint32_t caam_jr_enable_irqs(uint32_t uio_fd);
272
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
277  *  of this job ring.
278  *
279  * @param [in]  uio_fd    UIO File descripto
280  * @retval 0 for success
281  * @retval -1 value for error
282  *
283  */
284 uint32_t caam_jr_disable_irqs(uint32_t uio_fd);
285
286 #endif