New upstream version 18.11-rc1
[deb_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 #include <hw/desc/ipsec.h>
9
10 /* NXP CAAM JR PMD device name */
11
12 #define CAAM_JR_ALG_UNSUPPORT   (-1)
13
14 /* Minimum job descriptor consists of a oneword job descriptor HEADER and
15  * a pointer to the shared descriptor.
16  */
17 #define MIN_JOB_DESC_SIZE       (CAAM_CMD_SZ + CAAM_PTR_SZ)
18 #define CAAM_JOB_DESC_SIZE      13
19
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
23
24 #define DIR_ENC                 1
25 #define DIR_DEC                 0
26
27 #define JR_MAX_NB_MAX_DIGEST    32
28
29 #define RTE_CAAM_JR_PMD_MAX_NB_SESSIONS 2048
30
31
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
36                                         * parameter
37                                         */
38         SEC_OUT_OF_MEMORY,             /* Memory allocation failed. */
39         SEC_DESCRIPTOR_IN_FLIGHT,      /* API function indicates there are
40                                         * descriptors in flight
41                                         * for SEC to process.
42                                         */
43         SEC_LAST_DESCRIPTOR_IN_FLIGHT, /* API function indicates there is one
44                                         * last descriptor in flight
45                                         * for SEC to process that.
46                                         */
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
53                                         * sec_release().
54                                         */
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
60                                         * normal case.
61                                         */
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.
66                                          */
67         SEC_DRIVER_RELEASE_IN_PROGRESS, /* SEC driver shutdown is in progress,
68                                          * descriptors processing or polling is
69                                          * allowed.
70                                          */
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
78                                          * again.
79                                          */
80         SEC_RESET_ENGINE_FAILED,        /* Resetting of SEC Engine by SEC Kernel
81                                          * Driver Failed
82                                          */
83         SEC_ENABLE_IRQS_FAILED,         /* Enabling of IRQs in SEC Kernel Driver
84                                          * Failed
85                                          */
86         SEC_DISABLE_IRQS_FAILED,        /* Disabling of IRQs in SEC Kernel
87                                          * Driver Failed
88                                          */
89         /* END OF VALID VALUES */
90
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!
96                                          */
97 };
98
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 */
107         CAAM_JR_MAX
108 };
109
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*/
116         union {
117                 struct {
118                         uint8_t *data;  /* pointer to key data */
119                         size_t length;  /* key length in bytes */
120                 } aead_key;
121                 struct {
122                         struct {
123                                 uint8_t *data;  /* pointer to key data */
124                                 size_t length;  /* key length in bytes */
125                         } cipher_key;
126                         struct {
127                                 uint8_t *data;  /* pointer to key data */
128                                 size_t length;  /* key length in bytes */
129                         } auth_key;
130                 };
131         };
132         struct {
133                 uint16_t length;
134                 uint16_t offset;
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;
139         struct ip ip4_hdr;
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 */
144 };
145
146 /*
147  * 16-byte hardware scatter/gather table
148  */
149
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
156
157 struct sec4_sg_entry {
158         uint64_t ptr;
159         uint32_t len;
160         uint32_t bpid_offset;
161 };
162
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
168
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.
174  */
175 struct job_descriptor {
176         uint32_t desc[CAAM_JOB_DESC_SIZE];
177 };
178
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 */
185         int64_t vtop_offset;
186         uint8_t digest[JR_MAX_NB_MAX_DIGEST];
187 };
188
189 /**
190  * Checksum
191  *
192  * @param buffer calculate chksum for buffer
193  * @param len    buffer length
194  *
195  * @return checksum value in host cpu order
196  */
197 static inline uint16_t
198 calc_chksum(void *buffer, int len)
199 {
200         uint16_t *buf = (uint16_t *)buffer;
201         uint32_t sum = 0;
202         uint16_t result;
203
204         for (sum = 0; len > 1; len -= 2)
205                 sum += *buf++;
206
207         if (len == 1)
208                 sum += *(unsigned char *)buf;
209
210         sum = (sum >> 16) + (sum & 0xFFFF);
211         sum += (sum >> 16);
212         result = ~sum;
213
214         return  result;
215 }
216 struct uio_job_ring {
217         uint32_t jr_id;
218         uint32_t uio_fd;
219         void *register_base_addr;
220         int map_size;
221         int uio_minor_number;
222 };
223
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);
228
229 /* For Dma memory allocation of specified length and alignment */
230 static inline void *
231 caam_jr_dma_mem_alloc(size_t align, size_t len)
232 {
233         return rte_malloc("mem_alloc", len, align);
234 }
235
236 /* For freeing dma memory */
237 static inline void
238 caam_jr_dma_free(void *ptr)
239 {
240         rte_free(ptr);
241 }
242
243 static inline rte_iova_t
244 caam_jr_mem_vtop(void *vaddr)
245 {
246         const struct rte_memseg *ms;
247
248         ms = rte_mem_virt2memseg(vaddr, NULL);
249         if (ms)
250                 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
251         return (size_t)NULL;
252 }
253
254 static inline void *
255 caam_jr_dma_ptov(rte_iova_t paddr)
256 {
257         return rte_mem_iova2virt(paddr);
258 }
259
260 /* Virtual to physical address conversion */
261 static inline rte_iova_t caam_jr_dma_vtop(void *ptr)
262 {
263         return caam_jr_mem_vtop(ptr);
264 }
265
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
270  *  of this job ring.
271  *
272  * @param [in]  uio_fd     Job Ring UIO File descriptor
273  * @retval 0 for success
274  * @retval -1 value for error
275  */
276 uint32_t caam_jr_enable_irqs(uint32_t uio_fd);
277
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
282  *  of this job ring.
283  *
284  * @param [in]  uio_fd    UIO File descripto
285  * @retval 0 for success
286  * @retval -1 value for error
287  *
288  */
289 uint32_t caam_jr_disable_irqs(uint32_t uio_fd);
290
291 #endif