crypto/ccp: support session related ops
[dpdk.git] / drivers / crypto / ccp / ccp_dev.h
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3  */
4
5 #ifndef _CCP_DEV_H_
6 #define _CCP_DEV_H_
7
8 #include <limits.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 #include <rte_bus_pci.h>
14 #include <rte_atomic.h>
15 #include <rte_byteorder.h>
16 #include <rte_io.h>
17 #include <rte_pci.h>
18 #include <rte_spinlock.h>
19 #include <rte_crypto_sym.h>
20 #include <rte_cryptodev.h>
21
22 /**< CCP sspecific */
23 #define MAX_HW_QUEUES                   5
24
25 /**< CCP Register Mappings */
26 #define Q_MASK_REG                      0x000
27 #define TRNG_OUT_REG                    0x00c
28
29 /* CCP Version 5 Specifics */
30 #define CMD_QUEUE_MASK_OFFSET           0x00
31 #define CMD_QUEUE_PRIO_OFFSET           0x04
32 #define CMD_REQID_CONFIG_OFFSET         0x08
33 #define CMD_CMD_TIMEOUT_OFFSET          0x10
34 #define LSB_PUBLIC_MASK_LO_OFFSET       0x18
35 #define LSB_PUBLIC_MASK_HI_OFFSET       0x1C
36 #define LSB_PRIVATE_MASK_LO_OFFSET      0x20
37 #define LSB_PRIVATE_MASK_HI_OFFSET      0x24
38
39 #define CMD_Q_CONTROL_BASE              0x0000
40 #define CMD_Q_TAIL_LO_BASE              0x0004
41 #define CMD_Q_HEAD_LO_BASE              0x0008
42 #define CMD_Q_INT_ENABLE_BASE           0x000C
43 #define CMD_Q_INTERRUPT_STATUS_BASE     0x0010
44
45 #define CMD_Q_STATUS_BASE               0x0100
46 #define CMD_Q_INT_STATUS_BASE           0x0104
47
48 #define CMD_CONFIG_0_OFFSET             0x6000
49 #define CMD_TRNG_CTL_OFFSET             0x6008
50 #define CMD_AES_MASK_OFFSET             0x6010
51 #define CMD_CLK_GATE_CTL_OFFSET         0x603C
52
53 /* Address offset between two virtual queue registers */
54 #define CMD_Q_STATUS_INCR               0x1000
55
56 /* Bit masks */
57 #define CMD_Q_RUN                       0x1
58 #define CMD_Q_SIZE                      0x1F
59 #define CMD_Q_SHIFT                     3
60 #define COMMANDS_PER_QUEUE              2048
61
62 #define QUEUE_SIZE_VAL                  ((ffs(COMMANDS_PER_QUEUE) - 2) & \
63                                          CMD_Q_SIZE)
64 #define Q_DESC_SIZE                     sizeof(struct ccp_desc)
65 #define Q_SIZE(n)                       (COMMANDS_PER_QUEUE*(n))
66
67 #define INT_COMPLETION                  0x1
68 #define INT_ERROR                       0x2
69 #define INT_QUEUE_STOPPED               0x4
70 #define ALL_INTERRUPTS                  (INT_COMPLETION| \
71                                          INT_ERROR| \
72                                          INT_QUEUE_STOPPED)
73
74 #define LSB_REGION_WIDTH                5
75 #define MAX_LSB_CNT                     8
76
77 #define LSB_SIZE                        16
78 #define LSB_ITEM_SIZE                   32
79 #define SLSB_MAP_SIZE                   (MAX_LSB_CNT * LSB_SIZE)
80
81 /* General CCP Defines */
82
83 #define CCP_SB_BYTES                    32
84
85 /* bitmap */
86 enum {
87         BITS_PER_WORD = sizeof(unsigned long) * CHAR_BIT
88 };
89
90 #define WORD_OFFSET(b) ((b) / BITS_PER_WORD)
91 #define BIT_OFFSET(b)  ((b) % BITS_PER_WORD)
92
93 #define CCP_DIV_ROUND_UP(n, d)  (((n) + (d) - 1) / (d))
94 #define CCP_BITMAP_SIZE(nr) \
95         CCP_DIV_ROUND_UP(nr, CHAR_BIT * sizeof(unsigned long))
96
97 #define CCP_BITMAP_FIRST_WORD_MASK(start) \
98         (~0UL << ((start) & (BITS_PER_WORD - 1)))
99 #define CCP_BITMAP_LAST_WORD_MASK(nbits) \
100         (~0UL >> (-(nbits) & (BITS_PER_WORD - 1)))
101
102 #define __ccp_round_mask(x, y) ((typeof(x))((y)-1))
103 #define ccp_round_down(x, y) ((x) & ~__ccp_round_mask(x, y))
104
105 /** CCP registers Write/Read */
106
107 static inline void ccp_pci_reg_write(void *base, int offset,
108                                      uint32_t value)
109 {
110         volatile void *reg_addr = ((uint8_t *)base + offset);
111
112         rte_write32((rte_cpu_to_le_32(value)), reg_addr);
113 }
114
115 static inline uint32_t ccp_pci_reg_read(void *base, int offset)
116 {
117         volatile void *reg_addr = ((uint8_t *)base + offset);
118
119         return rte_le_to_cpu_32(rte_read32(reg_addr));
120 }
121
122 #define CCP_READ_REG(hw_addr, reg_offset) \
123         ccp_pci_reg_read(hw_addr, reg_offset)
124
125 #define CCP_WRITE_REG(hw_addr, reg_offset, value) \
126         ccp_pci_reg_write(hw_addr, reg_offset, value)
127
128 TAILQ_HEAD(ccp_list, ccp_device);
129
130 extern struct ccp_list ccp_list;
131
132 /**
133  * CCP device version
134  */
135 enum ccp_device_version {
136         CCP_VERSION_5A = 0,
137         CCP_VERSION_5B,
138 };
139
140 /**
141  * A structure describing a CCP command queue.
142  */
143 struct ccp_queue {
144         struct ccp_device *dev;
145         char memz_name[RTE_MEMZONE_NAMESIZE];
146
147         rte_atomic64_t free_slots;
148         /**< available free slots updated from enq/deq calls */
149
150         /* Queue identifier */
151         uint64_t id;    /**< queue id */
152         uint64_t qidx;  /**< queue index */
153         uint64_t qsize; /**< queue size */
154
155         /* Queue address */
156         struct ccp_desc *qbase_desc;
157         void *qbase_addr;
158         phys_addr_t qbase_phys_addr;
159         /**< queue-page registers addr */
160         void *reg_base;
161
162         uint32_t qcontrol;
163         /**< queue ctrl reg */
164
165         int lsb;
166         /**< lsb region assigned to queue */
167         unsigned long lsbmask;
168         /**< lsb regions queue can access */
169         unsigned long lsbmap[CCP_BITMAP_SIZE(LSB_SIZE)];
170         /**< all lsb resources which queue is using */
171         uint32_t sb_key;
172         /**< lsb assigned for queue */
173         uint32_t sb_iv;
174         /**< lsb assigned for iv */
175         uint32_t sb_sha;
176         /**< lsb assigned for sha ctx */
177         uint32_t sb_hmac;
178         /**< lsb assigned for hmac ctx */
179 } ____cacheline_aligned;
180
181 /**
182  * A structure describing a CCP device.
183  */
184 struct ccp_device {
185         TAILQ_ENTRY(ccp_device) next;
186         int id;
187         /**< ccp dev id on platform */
188         struct ccp_queue cmd_q[MAX_HW_QUEUES];
189         /**< ccp queue */
190         int cmd_q_count;
191         /**< no. of ccp Queues */
192         struct rte_pci_device pci;
193         /**< ccp pci identifier */
194         unsigned long lsbmap[CCP_BITMAP_SIZE(SLSB_MAP_SIZE)];
195         /**< shared lsb mask of ccp */
196         rte_spinlock_t lsb_lock;
197         /**< protection for shared lsb region allocation */
198         int qidx;
199         /**< current queue index */
200 } __rte_cache_aligned;
201
202 /**< CCP H/W engine related */
203 /**
204  * ccp_engine - CCP operation identifiers
205  *
206  * @CCP_ENGINE_AES: AES operation
207  * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
208  * @CCP_ENGINE_3DES: DES/3DES operation
209  * @CCP_ENGINE_SHA: SHA operation
210  * @CCP_ENGINE_RSA: RSA operation
211  * @CCP_ENGINE_PASSTHRU: pass-through operation
212  * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
213  * @CCP_ENGINE_ECC: ECC operation
214  */
215 enum ccp_engine {
216         CCP_ENGINE_AES = 0,
217         CCP_ENGINE_XTS_AES_128,
218         CCP_ENGINE_3DES,
219         CCP_ENGINE_SHA,
220         CCP_ENGINE_RSA,
221         CCP_ENGINE_PASSTHRU,
222         CCP_ENGINE_ZLIB_DECOMPRESS,
223         CCP_ENGINE_ECC,
224         CCP_ENGINE__LAST,
225 };
226
227 /* Passthru engine */
228 /**
229  * ccp_passthru_bitwise - type of bitwise passthru operation
230  *
231  * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
232  * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
233  * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
234  * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
235  * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
236  */
237 enum ccp_passthru_bitwise {
238         CCP_PASSTHRU_BITWISE_NOOP = 0,
239         CCP_PASSTHRU_BITWISE_AND,
240         CCP_PASSTHRU_BITWISE_OR,
241         CCP_PASSTHRU_BITWISE_XOR,
242         CCP_PASSTHRU_BITWISE_MASK,
243         CCP_PASSTHRU_BITWISE__LAST,
244 };
245
246 /**
247  * ccp_passthru_byteswap - type of byteswap passthru operation
248  *
249  * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
250  * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
251  * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
252  */
253 enum ccp_passthru_byteswap {
254         CCP_PASSTHRU_BYTESWAP_NOOP = 0,
255         CCP_PASSTHRU_BYTESWAP_32BIT,
256         CCP_PASSTHRU_BYTESWAP_256BIT,
257         CCP_PASSTHRU_BYTESWAP__LAST,
258 };
259
260 /**
261  * CCP passthru
262  */
263 struct ccp_passthru {
264         phys_addr_t src_addr;
265         phys_addr_t dest_addr;
266         enum ccp_passthru_bitwise bit_mod;
267         enum ccp_passthru_byteswap byte_swap;
268         int len;
269         int dir;
270 };
271
272 /* CCP version 5: Union to define the function field (cmd_reg1/dword0) */
273 union ccp_function {
274         struct {
275                 uint16_t size:7;
276                 uint16_t encrypt:1;
277                 uint16_t mode:5;
278                 uint16_t type:2;
279         } aes;
280         struct {
281                 uint16_t size:7;
282                 uint16_t encrypt:1;
283                 uint16_t mode:5;
284                 uint16_t type:2;
285         } des;
286         struct {
287                 uint16_t size:7;
288                 uint16_t encrypt:1;
289                 uint16_t rsvd:5;
290                 uint16_t type:2;
291         } aes_xts;
292         struct {
293                 uint16_t rsvd1:10;
294                 uint16_t type:4;
295                 uint16_t rsvd2:1;
296         } sha;
297         struct {
298                 uint16_t mode:3;
299                 uint16_t size:12;
300         } rsa;
301         struct {
302                 uint16_t byteswap:2;
303                 uint16_t bitwise:3;
304                 uint16_t reflect:2;
305                 uint16_t rsvd:8;
306         } pt;
307         struct  {
308                 uint16_t rsvd:13;
309         } zlib;
310         struct {
311                 uint16_t size:10;
312                 uint16_t type:2;
313                 uint16_t mode:3;
314         } ecc;
315         uint16_t raw;
316 };
317
318
319 /**
320  * descriptor for version 5 CPP commands
321  * 8 32-bit words:
322  * word 0: function; engine; control bits
323  * word 1: length of source data
324  * word 2: low 32 bits of source pointer
325  * word 3: upper 16 bits of source pointer; source memory type
326  * word 4: low 32 bits of destination pointer
327  * word 5: upper 16 bits of destination pointer; destination memory
328  * type
329  * word 6: low 32 bits of key pointer
330  * word 7: upper 16 bits of key pointer; key memory type
331  */
332 struct dword0 {
333         uint32_t soc:1;
334         uint32_t ioc:1;
335         uint32_t rsvd1:1;
336         uint32_t init:1;
337         uint32_t eom:1;
338         uint32_t function:15;
339         uint32_t engine:4;
340         uint32_t prot:1;
341         uint32_t rsvd2:7;
342 };
343
344 struct dword3 {
345         uint32_t src_hi:16;
346         uint32_t src_mem:2;
347         uint32_t lsb_cxt_id:8;
348         uint32_t rsvd1:5;
349         uint32_t fixed:1;
350 };
351
352 union dword4 {
353         uint32_t dst_lo;        /* NON-SHA */
354         uint32_t sha_len_lo;    /* SHA */
355 };
356
357 union dword5 {
358         struct {
359                 uint32_t dst_hi:16;
360                 uint32_t dst_mem:2;
361                 uint32_t rsvd1:13;
362                 uint32_t fixed:1;
363         }
364         fields;
365         uint32_t sha_len_hi;
366 };
367
368 struct dword7 {
369         uint32_t key_hi:16;
370         uint32_t key_mem:2;
371         uint32_t rsvd1:14;
372 };
373
374 struct ccp_desc {
375         struct dword0 dw0;
376         uint32_t length;
377         uint32_t src_lo;
378         struct dword3 dw3;
379         union dword4 dw4;
380         union dword5 dw5;
381         uint32_t key_lo;
382         struct dword7 dw7;
383 };
384
385 /**
386  * cmd id to follow order
387  */
388 enum ccp_cmd_order {
389         CCP_CMD_CIPHER = 0,
390         CCP_CMD_AUTH,
391         CCP_CMD_CIPHER_HASH,
392         CCP_CMD_HASH_CIPHER,
393         CCP_CMD_COMBINED,
394         CCP_CMD_NOT_SUPPORTED,
395 };
396
397 static inline uint32_t
398 low32_value(unsigned long addr)
399 {
400         return ((uint64_t)addr) & 0x0ffffffff;
401 }
402
403 static inline uint32_t
404 high32_value(unsigned long addr)
405 {
406         return ((uint64_t)addr >> 32) & 0x00000ffff;
407 }
408
409 /*
410  * Start CCP device
411  */
412 int ccp_dev_start(struct rte_cryptodev *dev);
413
414 /**
415  * Detect ccp platform and initialize all ccp devices
416  *
417  * @param ccp_id rte_pci_id list for supported CCP devices
418  * @return no. of successfully initialized CCP devices
419  */
420 int ccp_probe_devices(const struct rte_pci_id *ccp_id);
421
422 #endif /* _CCP_DEV_H_ */