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