blob: 0cb09d0feeaf0bc748b87eebbb522bdf3e3d66b5 [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook553d2372016-03-01 13:49:04 -06004 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06005 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hookfba88552016-07-26 19:09:20 -05007 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky63b94502013-11-12 11:46:16 -06008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#ifndef __CCP_DEV_H__
15#define __CCP_DEV_H__
16
17#include <linux/device.h>
18#include <linux/pci.h>
19#include <linux/spinlock.h>
20#include <linux/mutex.h>
21#include <linux/list.h>
22#include <linux/wait.h>
23#include <linux/dmapool.h>
24#include <linux/hw_random.h>
Tom Lendacky8db88462015-02-03 13:07:05 -060025#include <linux/bitops.h>
Gary R Hook58ea8ab2016-04-18 09:21:44 -050026#include <linux/interrupt.h>
27#include <linux/irqreturn.h>
28#include <linux/dmaengine.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060029
Gary R Hook553d2372016-03-01 13:49:04 -060030#define MAX_CCP_NAME_LEN 16
Tom Lendacky63b94502013-11-12 11:46:16 -060031#define MAX_DMAPOOL_NAME_LEN 32
32
33#define MAX_HW_QUEUES 5
34#define MAX_CMD_QLEN 100
35
36#define TRNG_RETRIES 10
37
Tom Lendacky126ae9a2014-07-10 10:58:35 -050038#define CACHE_NONE 0x00
Tom Lendackyc4f4b322014-06-05 10:17:57 -050039#define CACHE_WB_NO_ALLOC 0xb7
40
Tom Lendacky63b94502013-11-12 11:46:16 -060041/****** Register Mappings ******/
42#define Q_MASK_REG 0x000
43#define TRNG_OUT_REG 0x00c
44#define IRQ_MASK_REG 0x040
45#define IRQ_STATUS_REG 0x200
46
47#define DEL_CMD_Q_JOB 0x124
48#define DEL_Q_ACTIVE 0x00000200
49#define DEL_Q_ID_SHIFT 6
50
51#define CMD_REQ0 0x180
52#define CMD_REQ_INCR 0x04
53
54#define CMD_Q_STATUS_BASE 0x210
55#define CMD_Q_INT_STATUS_BASE 0x214
56#define CMD_Q_STATUS_INCR 0x20
57
Tom Lendackyc4f4b322014-06-05 10:17:57 -050058#define CMD_Q_CACHE_BASE 0x228
Tom Lendacky63b94502013-11-12 11:46:16 -060059#define CMD_Q_CACHE_INC 0x20
60
Tom Lendacky8db88462015-02-03 13:07:05 -060061#define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f)
62#define CMD_Q_DEPTH(__qs) (((__qs) >> 12) & 0x0000000f)
Tom Lendacky63b94502013-11-12 11:46:16 -060063
Gary R Hook4b394a22016-07-26 19:10:21 -050064/* ------------------------ CCP Version 5 Specifics ------------------------ */
65#define CMD5_QUEUE_MASK_OFFSET 0x00
Gary R Hooke14e7d12016-07-26 19:10:49 -050066#define CMD5_QUEUE_PRIO_OFFSET 0x04
Gary R Hook4b394a22016-07-26 19:10:21 -050067#define CMD5_REQID_CONFIG_OFFSET 0x08
Gary R Hooke14e7d12016-07-26 19:10:49 -050068#define CMD5_CMD_TIMEOUT_OFFSET 0x10
Gary R Hook4b394a22016-07-26 19:10:21 -050069#define LSB_PUBLIC_MASK_LO_OFFSET 0x18
70#define LSB_PUBLIC_MASK_HI_OFFSET 0x1C
71#define LSB_PRIVATE_MASK_LO_OFFSET 0x20
72#define LSB_PRIVATE_MASK_HI_OFFSET 0x24
73
74#define CMD5_Q_CONTROL_BASE 0x0000
75#define CMD5_Q_TAIL_LO_BASE 0x0004
76#define CMD5_Q_HEAD_LO_BASE 0x0008
77#define CMD5_Q_INT_ENABLE_BASE 0x000C
78#define CMD5_Q_INTERRUPT_STATUS_BASE 0x0010
79
80#define CMD5_Q_STATUS_BASE 0x0100
81#define CMD5_Q_INT_STATUS_BASE 0x0104
82#define CMD5_Q_DMA_STATUS_BASE 0x0108
83#define CMD5_Q_DMA_READ_STATUS_BASE 0x010C
84#define CMD5_Q_DMA_WRITE_STATUS_BASE 0x0110
85#define CMD5_Q_ABORT_BASE 0x0114
86#define CMD5_Q_AX_CACHE_BASE 0x0118
87
Gary R Hooke14e7d12016-07-26 19:10:49 -050088#define CMD5_CONFIG_0_OFFSET 0x6000
89#define CMD5_TRNG_CTL_OFFSET 0x6008
90#define CMD5_AES_MASK_OFFSET 0x6010
91#define CMD5_CLK_GATE_CTL_OFFSET 0x603C
92
Gary R Hook4b394a22016-07-26 19:10:21 -050093/* Address offset between two virtual queue registers */
94#define CMD5_Q_STATUS_INCR 0x1000
95
96/* Bit masks */
97#define CMD5_Q_RUN 0x1
98#define CMD5_Q_HALT 0x2
99#define CMD5_Q_MEM_LOCATION 0x4
100#define CMD5_Q_SIZE 0x1F
101#define CMD5_Q_SHIFT 3
102#define COMMANDS_PER_QUEUE 16
103#define QUEUE_SIZE_VAL ((ffs(COMMANDS_PER_QUEUE) - 2) & \
104 CMD5_Q_SIZE)
105#define Q_PTR_MASK (2 << (QUEUE_SIZE_VAL + 5) - 1)
106#define Q_DESC_SIZE sizeof(struct ccp5_desc)
107#define Q_SIZE(n) (COMMANDS_PER_QUEUE*(n))
108
109#define INT_COMPLETION 0x1
110#define INT_ERROR 0x2
111#define INT_QUEUE_STOPPED 0x4
Gary R Hook56467cb2017-04-20 15:24:09 -0500112#define INT_EMPTY_QUEUE 0x8
113#define SUPPORTED_INTERRUPTS (INT_COMPLETION | INT_ERROR)
Gary R Hook4b394a22016-07-26 19:10:21 -0500114
115#define LSB_REGION_WIDTH 5
116#define MAX_LSB_CNT 8
117
118#define LSB_SIZE 16
119#define LSB_ITEM_SIZE 32
120#define PLSB_MAP_SIZE (LSB_SIZE)
121#define SLSB_MAP_SIZE (MAX_LSB_CNT * LSB_SIZE)
122
123#define LSB_ENTRY_NUMBER(LSB_ADDR) (LSB_ADDR / LSB_ITEM_SIZE)
124
125/* ------------------------ CCP Version 3 Specifics ------------------------ */
Tom Lendacky63b94502013-11-12 11:46:16 -0600126#define REQ0_WAIT_FOR_WRITE 0x00000004
127#define REQ0_INT_ON_COMPLETE 0x00000002
128#define REQ0_STOP_ON_COMPLETE 0x00000001
129
130#define REQ0_CMD_Q_SHIFT 9
131#define REQ0_JOBID_SHIFT 3
132
133/****** REQ1 Related Values ******/
134#define REQ1_PROTECT_SHIFT 27
135#define REQ1_ENGINE_SHIFT 23
136#define REQ1_KEY_KSB_SHIFT 2
137
138#define REQ1_EOM 0x00000002
139#define REQ1_INIT 0x00000001
140
141/* AES Related Values */
142#define REQ1_AES_TYPE_SHIFT 21
143#define REQ1_AES_MODE_SHIFT 18
144#define REQ1_AES_ACTION_SHIFT 17
145#define REQ1_AES_CFB_SIZE_SHIFT 10
146
147/* XTS-AES Related Values */
148#define REQ1_XTS_AES_SIZE_SHIFT 10
149
150/* SHA Related Values */
151#define REQ1_SHA_TYPE_SHIFT 21
152
153/* RSA Related Values */
154#define REQ1_RSA_MOD_SIZE_SHIFT 10
155
156/* Pass-Through Related Values */
157#define REQ1_PT_BW_SHIFT 12
158#define REQ1_PT_BS_SHIFT 10
159
160/* ECC Related Values */
161#define REQ1_ECC_AFFINE_CONVERT 0x00200000
162#define REQ1_ECC_FUNCTION_SHIFT 18
163
164/****** REQ4 Related Values ******/
165#define REQ4_KSB_SHIFT 18
166#define REQ4_MEMTYPE_SHIFT 16
167
168/****** REQ6 Related Values ******/
169#define REQ6_MEMTYPE_SHIFT 16
170
Tom Lendacky63b94502013-11-12 11:46:16 -0600171/****** Key Storage Block ******/
172#define KSB_START 77
173#define KSB_END 127
174#define KSB_COUNT (KSB_END - KSB_START + 1)
Gary R Hook956ee212016-07-26 19:09:40 -0500175#define CCP_SB_BITS 256
Tom Lendacky63b94502013-11-12 11:46:16 -0600176
177#define CCP_JOBID_MASK 0x0000003f
178
Gary R Hook4b394a22016-07-26 19:10:21 -0500179/* ------------------------ General CCP Defines ------------------------ */
180
Gary R Hookefc989f2017-03-23 12:53:30 -0500181#define CCP_DMA_DFLT 0x0
182#define CCP_DMA_PRIV 0x1
183#define CCP_DMA_PUB 0x2
184
Tom Lendacky63b94502013-11-12 11:46:16 -0600185#define CCP_DMAPOOL_MAX_SIZE 64
Tom Lendacky8db88462015-02-03 13:07:05 -0600186#define CCP_DMAPOOL_ALIGN BIT(5)
Tom Lendacky63b94502013-11-12 11:46:16 -0600187
188#define CCP_REVERSE_BUF_SIZE 64
189
Gary R Hook956ee212016-07-26 19:09:40 -0500190#define CCP_AES_KEY_SB_COUNT 1
191#define CCP_AES_CTX_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600192
Gary R Hook956ee212016-07-26 19:09:40 -0500193#define CCP_XTS_AES_KEY_SB_COUNT 1
194#define CCP_XTS_AES_CTX_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600195
Gary R Hook990672d2017-03-15 13:20:52 -0500196#define CCP_DES3_KEY_SB_COUNT 1
197#define CCP_DES3_CTX_SB_COUNT 1
198
Gary R Hook956ee212016-07-26 19:09:40 -0500199#define CCP_SHA_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600200
201#define CCP_RSA_MAX_WIDTH 4096
202
203#define CCP_PASSTHRU_BLOCKSIZE 256
204#define CCP_PASSTHRU_MASKSIZE 32
Gary R Hook956ee212016-07-26 19:09:40 -0500205#define CCP_PASSTHRU_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600206
207#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
208#define CCP_ECC_MAX_OPERANDS 6
209#define CCP_ECC_MAX_OUTPUTS 3
210#define CCP_ECC_SRC_BUF_SIZE 448
211#define CCP_ECC_DST_BUF_SIZE 192
212#define CCP_ECC_OPERAND_SIZE 64
213#define CCP_ECC_OUTPUT_SIZE 64
214#define CCP_ECC_RESULT_OFFSET 60
215#define CCP_ECC_RESULT_SUCCESS 0x0001
216
Gary R Hook956ee212016-07-26 19:09:40 -0500217#define CCP_SB_BYTES 32
218
Gary R Hookea0375a2016-03-01 13:49:25 -0600219struct ccp_op;
Tom Lendacky63b94502013-11-12 11:46:16 -0600220struct ccp_device;
221struct ccp_cmd;
Gary R Hook4b394a22016-07-26 19:10:21 -0500222struct ccp_fns;
Tom Lendacky63b94502013-11-12 11:46:16 -0600223
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500224struct ccp_dma_cmd {
225 struct list_head entry;
226
227 struct ccp_cmd ccp_cmd;
228};
229
230struct ccp_dma_desc {
231 struct list_head entry;
232
233 struct ccp_device *ccp;
234
235 struct list_head pending;
236 struct list_head active;
237
238 enum dma_status status;
239 struct dma_async_tx_descriptor tx_desc;
240 size_t len;
241};
242
243struct ccp_dma_chan {
244 struct ccp_device *ccp;
245
246 spinlock_t lock;
Gary R Hooke5da5c52017-01-27 17:09:04 -0600247 struct list_head created;
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500248 struct list_head pending;
249 struct list_head active;
250 struct list_head complete;
251
252 struct tasklet_struct cleanup_tasklet;
253
254 enum dma_status status;
255 struct dma_chan dma_chan;
256};
257
Tom Lendacky63b94502013-11-12 11:46:16 -0600258struct ccp_cmd_queue {
259 struct ccp_device *ccp;
260
261 /* Queue identifier */
262 u32 id;
263
264 /* Queue dma pool */
265 struct dma_pool *dma_pool;
266
Gary R Hook4b394a22016-07-26 19:10:21 -0500267 /* Queue base address (not neccessarily aligned)*/
268 struct ccp5_desc *qbase;
269
270 /* Aligned queue start address (per requirement) */
271 struct mutex q_mutex ____cacheline_aligned;
272 unsigned int qidx;
273
274 /* Version 5 has different requirements for queue memory */
275 unsigned int qsize;
276 dma_addr_t qbase_dma;
277 dma_addr_t qdma_tail;
278
Gary R Hook956ee212016-07-26 19:09:40 -0500279 /* Per-queue reserved storage block(s) */
280 u32 sb_key;
281 u32 sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600282
Gary R Hook4b394a22016-07-26 19:10:21 -0500283 /* Bitmap of LSBs that can be accessed by this queue */
284 DECLARE_BITMAP(lsbmask, MAX_LSB_CNT);
285 /* Private LSB that is assigned to this queue, or -1 if none.
286 * Bitmap for my private LSB, unused otherwise
287 */
Gary R Hook3cf79962016-10-12 08:47:03 -0500288 int lsb;
Gary R Hook4b394a22016-07-26 19:10:21 -0500289 DECLARE_BITMAP(lsbmap, PLSB_MAP_SIZE);
290
Tom Lendacky63b94502013-11-12 11:46:16 -0600291 /* Queue processing thread */
292 struct task_struct *kthread;
293 unsigned int active;
294 unsigned int suspended;
295
296 /* Number of free command slots available */
297 unsigned int free_slots;
298
299 /* Interrupt masks */
300 u32 int_ok;
301 u32 int_err;
302
303 /* Register addresses for queue */
Gary R Hook4b394a22016-07-26 19:10:21 -0500304 void __iomem *reg_control;
305 void __iomem *reg_tail_lo;
306 void __iomem *reg_head_lo;
307 void __iomem *reg_int_enable;
308 void __iomem *reg_interrupt_status;
Tom Lendacky63b94502013-11-12 11:46:16 -0600309 void __iomem *reg_status;
310 void __iomem *reg_int_status;
Gary R Hook4b394a22016-07-26 19:10:21 -0500311 void __iomem *reg_dma_status;
312 void __iomem *reg_dma_read_status;
313 void __iomem *reg_dma_write_status;
314 u32 qcontrol; /* Cached control register */
Tom Lendacky63b94502013-11-12 11:46:16 -0600315
316 /* Status values from job */
317 u32 int_status;
318 u32 q_status;
319 u32 q_int_status;
320 u32 cmd_error;
321
322 /* Interrupt wait queue */
323 wait_queue_head_t int_queue;
324 unsigned int int_rcvd;
325} ____cacheline_aligned;
326
327struct ccp_device {
Gary R Hook553d2372016-03-01 13:49:04 -0600328 struct list_head entry;
329
Gary R Hookc7019c42016-03-01 13:49:15 -0600330 struct ccp_vdata *vdata;
Gary R Hook553d2372016-03-01 13:49:04 -0600331 unsigned int ord;
332 char name[MAX_CCP_NAME_LEN];
333 char rngname[MAX_CCP_NAME_LEN];
334
Tom Lendacky63b94502013-11-12 11:46:16 -0600335 struct device *dev;
336
Gary R Hookfa242e82016-07-26 18:09:46 -0500337 /* Bus specific device information
Tom Lendacky63b94502013-11-12 11:46:16 -0600338 */
339 void *dev_specific;
340 int (*get_irq)(struct ccp_device *ccp);
341 void (*free_irq)(struct ccp_device *ccp);
Gary R Hook7b537b22017-04-21 10:50:05 -0500342 unsigned int qim;
Tom Lendacky3d775652014-06-05 10:17:45 -0500343 unsigned int irq;
Gary R Hook7b537b22017-04-21 10:50:05 -0500344 bool use_tasklet;
345 struct tasklet_struct irq_tasklet;
Tom Lendacky63b94502013-11-12 11:46:16 -0600346
Gary R Hookfa242e82016-07-26 18:09:46 -0500347 /* I/O area used for device communication. The register mapping
Tom Lendacky63b94502013-11-12 11:46:16 -0600348 * starts at an offset into the mapped bar.
349 * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
350 * need to be protected while a command queue thread is accessing
351 * them.
352 */
353 struct mutex req_mutex ____cacheline_aligned;
354 void __iomem *io_map;
355 void __iomem *io_regs;
356
Gary R Hookfa242e82016-07-26 18:09:46 -0500357 /* Master lists that all cmds are queued on. Because there can be
Tom Lendacky63b94502013-11-12 11:46:16 -0600358 * more than one CCP command queue that can process a cmd a separate
359 * backlog list is neeeded so that the backlog completion call
360 * completes before the cmd is available for execution.
361 */
362 spinlock_t cmd_lock ____cacheline_aligned;
363 unsigned int cmd_count;
364 struct list_head cmd;
365 struct list_head backlog;
366
Gary R Hookfa242e82016-07-26 18:09:46 -0500367 /* The command queues. These represent the queues available on the
Tom Lendacky63b94502013-11-12 11:46:16 -0600368 * CCP that are available for processing cmds
369 */
370 struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
371 unsigned int cmd_q_count;
372
Gary R Hookfa242e82016-07-26 18:09:46 -0500373 /* Support for the CCP True RNG
Tom Lendacky63b94502013-11-12 11:46:16 -0600374 */
375 struct hwrng hwrng;
376 unsigned int hwrng_retries;
377
Gary R Hookfa242e82016-07-26 18:09:46 -0500378 /* Support for the CCP DMA capabilities
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500379 */
380 struct dma_device dma_dev;
381 struct ccp_dma_chan *ccp_dma_chan;
382 struct kmem_cache *dma_cmd_cache;
383 struct kmem_cache *dma_desc_cache;
384
Gary R Hookfa242e82016-07-26 18:09:46 -0500385 /* A counter used to generate job-ids for cmds submitted to the CCP
Tom Lendacky63b94502013-11-12 11:46:16 -0600386 */
387 atomic_t current_id ____cacheline_aligned;
388
Gary R Hook58a690b2016-07-26 19:09:50 -0500389 /* The v3 CCP uses key storage blocks (SB) to maintain context for
390 * certain operations. To prevent multiple cmds from using the same
391 * SB range a command queue reserves an SB range for the duration of
392 * the cmd. Each queue, will however, reserve 2 SB blocks for
393 * operations that only require single SB entries (eg. AES context/iv
394 * and key) in order to avoid allocation contention. This will reserve
395 * at most 10 SB entries, leaving 40 SB entries available for dynamic
396 * allocation.
397 *
398 * The v5 CCP Local Storage Block (LSB) is broken up into 8
399 * memrory ranges, each of which can be enabled for access by one
400 * or more queues. Device initialization takes this into account,
401 * and attempts to assign one region for exclusive use by each
402 * available queue; the rest are then aggregated as "public" use.
403 * If there are fewer regions than queues, all regions are shared
404 * amongst all queues.
Tom Lendacky63b94502013-11-12 11:46:16 -0600405 */
Gary R Hook956ee212016-07-26 19:09:40 -0500406 struct mutex sb_mutex ____cacheline_aligned;
407 DECLARE_BITMAP(sb, KSB_COUNT);
408 wait_queue_head_t sb_queue;
409 unsigned int sb_avail;
410 unsigned int sb_count;
411 u32 sb_start;
Tom Lendacky63b94502013-11-12 11:46:16 -0600412
Gary R Hook4b394a22016-07-26 19:10:21 -0500413 /* Bitmap of shared LSBs, if any */
414 DECLARE_BITMAP(lsbmap, SLSB_MAP_SIZE);
415
Tom Lendacky63b94502013-11-12 11:46:16 -0600416 /* Suspend support */
417 unsigned int suspending;
418 wait_queue_head_t suspend_queue;
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500419
420 /* DMA caching attribute support */
421 unsigned int axcache;
Tom Lendacky63b94502013-11-12 11:46:16 -0600422};
423
Gary R Hookea0375a2016-03-01 13:49:25 -0600424enum ccp_memtype {
425 CCP_MEMTYPE_SYSTEM = 0,
Gary R Hook956ee212016-07-26 19:09:40 -0500426 CCP_MEMTYPE_SB,
Gary R Hookea0375a2016-03-01 13:49:25 -0600427 CCP_MEMTYPE_LOCAL,
428 CCP_MEMTYPE__LAST,
429};
Gary R Hook4b394a22016-07-26 19:10:21 -0500430#define CCP_MEMTYPE_LSB CCP_MEMTYPE_KSB
Gary R Hookea0375a2016-03-01 13:49:25 -0600431
Gary R Hook2d158392017-03-28 10:57:26 -0500432
Gary R Hookea0375a2016-03-01 13:49:25 -0600433struct ccp_dma_info {
434 dma_addr_t address;
435 unsigned int offset;
436 unsigned int length;
437 enum dma_data_direction dir;
Gary R Hook2d158392017-03-28 10:57:26 -0500438} __packed __aligned(4);
Gary R Hookea0375a2016-03-01 13:49:25 -0600439
440struct ccp_dm_workarea {
441 struct device *dev;
442 struct dma_pool *dma_pool;
Gary R Hookea0375a2016-03-01 13:49:25 -0600443
444 u8 *address;
445 struct ccp_dma_info dma;
Gary R Hook2d158392017-03-28 10:57:26 -0500446 unsigned int length;
Gary R Hookea0375a2016-03-01 13:49:25 -0600447};
448
449struct ccp_sg_workarea {
450 struct scatterlist *sg;
451 int nents;
Gary R Hook2d158392017-03-28 10:57:26 -0500452 unsigned int sg_used;
Gary R Hookea0375a2016-03-01 13:49:25 -0600453
454 struct scatterlist *dma_sg;
455 struct device *dma_dev;
456 unsigned int dma_count;
457 enum dma_data_direction dma_dir;
458
Gary R Hookea0375a2016-03-01 13:49:25 -0600459 u64 bytes_left;
460};
461
462struct ccp_data {
463 struct ccp_sg_workarea sg_wa;
464 struct ccp_dm_workarea dm_wa;
465};
466
467struct ccp_mem {
468 enum ccp_memtype type;
469 union {
470 struct ccp_dma_info dma;
Gary R Hook956ee212016-07-26 19:09:40 -0500471 u32 sb;
Gary R Hookea0375a2016-03-01 13:49:25 -0600472 } u;
473};
474
475struct ccp_aes_op {
476 enum ccp_aes_type type;
477 enum ccp_aes_mode mode;
478 enum ccp_aes_action action;
Gary R Hookf7cc02b32017-02-08 13:07:06 -0600479 unsigned int size;
Gary R Hookea0375a2016-03-01 13:49:25 -0600480};
481
482struct ccp_xts_aes_op {
483 enum ccp_aes_action action;
484 enum ccp_xts_aes_unit_size unit_size;
485};
486
Gary R Hook990672d2017-03-15 13:20:52 -0500487struct ccp_des3_op {
488 enum ccp_des3_type type;
489 enum ccp_des3_mode mode;
490 enum ccp_des3_action action;
491};
492
Gary R Hookea0375a2016-03-01 13:49:25 -0600493struct ccp_sha_op {
494 enum ccp_sha_type type;
495 u64 msg_bits;
496};
497
498struct ccp_rsa_op {
499 u32 mod_size;
500 u32 input_len;
501};
502
503struct ccp_passthru_op {
504 enum ccp_passthru_bitwise bit_mod;
505 enum ccp_passthru_byteswap byte_swap;
506};
507
508struct ccp_ecc_op {
509 enum ccp_ecc_function function;
510};
511
512struct ccp_op {
513 struct ccp_cmd_queue *cmd_q;
514
515 u32 jobid;
516 u32 ioc;
517 u32 soc;
Gary R Hook956ee212016-07-26 19:09:40 -0500518 u32 sb_key;
519 u32 sb_ctx;
Gary R Hookea0375a2016-03-01 13:49:25 -0600520 u32 init;
521 u32 eom;
522
523 struct ccp_mem src;
524 struct ccp_mem dst;
Gary R Hook4b394a22016-07-26 19:10:21 -0500525 struct ccp_mem exp;
Gary R Hookea0375a2016-03-01 13:49:25 -0600526
527 union {
528 struct ccp_aes_op aes;
529 struct ccp_xts_aes_op xts;
Gary R Hook990672d2017-03-15 13:20:52 -0500530 struct ccp_des3_op des3;
Gary R Hookea0375a2016-03-01 13:49:25 -0600531 struct ccp_sha_op sha;
532 struct ccp_rsa_op rsa;
533 struct ccp_passthru_op passthru;
534 struct ccp_ecc_op ecc;
535 } u;
536};
537
538static inline u32 ccp_addr_lo(struct ccp_dma_info *info)
539{
540 return lower_32_bits(info->address + info->offset);
541}
542
543static inline u32 ccp_addr_hi(struct ccp_dma_info *info)
544{
545 return upper_32_bits(info->address + info->offset) & 0x0000ffff;
546}
547
Gary R Hook4b394a22016-07-26 19:10:21 -0500548/**
549 * descriptor for version 5 CPP commands
550 * 8 32-bit words:
551 * word 0: function; engine; control bits
552 * word 1: length of source data
553 * word 2: low 32 bits of source pointer
554 * word 3: upper 16 bits of source pointer; source memory type
555 * word 4: low 32 bits of destination pointer
556 * word 5: upper 16 bits of destination pointer; destination memory type
557 * word 6: low 32 bits of key pointer
558 * word 7: upper 16 bits of key pointer; key memory type
559 */
560struct dword0 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500561 unsigned int soc:1;
562 unsigned int ioc:1;
563 unsigned int rsvd1:1;
564 unsigned int init:1;
565 unsigned int eom:1; /* AES/SHA only */
566 unsigned int function:15;
567 unsigned int engine:4;
568 unsigned int prot:1;
569 unsigned int rsvd2:7;
Gary R Hook4b394a22016-07-26 19:10:21 -0500570};
571
572struct dword3 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500573 unsigned int src_hi:16;
574 unsigned int src_mem:2;
575 unsigned int lsb_cxt_id:8;
576 unsigned int rsvd1:5;
577 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500578};
579
580union dword4 {
581 __le32 dst_lo; /* NON-SHA */
582 __le32 sha_len_lo; /* SHA */
583};
584
585union dword5 {
586 struct {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500587 unsigned int dst_hi:16;
588 unsigned int dst_mem:2;
589 unsigned int rsvd1:13;
590 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500591 } fields;
592 __le32 sha_len_hi;
593};
594
595struct dword7 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500596 unsigned int key_hi:16;
597 unsigned int key_mem:2;
598 unsigned int rsvd1:14;
Gary R Hook4b394a22016-07-26 19:10:21 -0500599};
600
601struct ccp5_desc {
602 struct dword0 dw0;
603 __le32 length;
604 __le32 src_lo;
605 struct dword3 dw3;
606 union dword4 dw4;
607 union dword5 dw5;
608 __le32 key_lo;
609 struct dword7 dw7;
610};
611
Tom Lendacky63b94502013-11-12 11:46:16 -0600612int ccp_pci_init(void);
613void ccp_pci_exit(void);
614
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500615int ccp_platform_init(void);
616void ccp_platform_exit(void);
617
Gary R Hookea0375a2016-03-01 13:49:25 -0600618void ccp_add_device(struct ccp_device *ccp);
619void ccp_del_device(struct ccp_device *ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600620
Gary R Hook81422ba2016-09-28 11:53:56 -0500621extern void ccp_log_error(struct ccp_device *, int);
622
Gary R Hookea0375a2016-03-01 13:49:25 -0600623struct ccp_device *ccp_alloc_struct(struct device *dev);
624bool ccp_queues_suspended(struct ccp_device *ccp);
625int ccp_cmd_queue_thread(void *data);
Gary R Hook8256e682016-07-26 19:10:02 -0500626int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait);
Tom Lendacky63b94502013-11-12 11:46:16 -0600627
628int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
629
Gary R Hook084935b2016-07-26 19:10:31 -0500630int ccp_register_rng(struct ccp_device *ccp);
631void ccp_unregister_rng(struct ccp_device *ccp);
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500632int ccp_dmaengine_register(struct ccp_device *ccp);
633void ccp_dmaengine_unregister(struct ccp_device *ccp);
634
Gary R Hook58a690b2016-07-26 19:09:50 -0500635/* Structure for computation functions that are device-specific */
636struct ccp_actions {
637 int (*aes)(struct ccp_op *);
638 int (*xts_aes)(struct ccp_op *);
Gary R Hook990672d2017-03-15 13:20:52 -0500639 int (*des3)(struct ccp_op *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500640 int (*sha)(struct ccp_op *);
641 int (*rsa)(struct ccp_op *);
642 int (*passthru)(struct ccp_op *);
643 int (*ecc)(struct ccp_op *);
644 u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
Gary R Hook990672d2017-03-15 13:20:52 -0500645 void (*sbfree)(struct ccp_cmd_queue *, unsigned int, unsigned int);
Gary R Hookbb4e89b2016-07-26 19:10:13 -0500646 unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500647 int (*init)(struct ccp_device *);
648 void (*destroy)(struct ccp_device *);
649 irqreturn_t (*irqhandler)(int, void *);
650};
651
652/* Structure to hold CCP version-specific values */
653struct ccp_vdata {
Gary R Hook4b394a22016-07-26 19:10:21 -0500654 const unsigned int version;
Gary R Hookefc989f2017-03-23 12:53:30 -0500655 const unsigned int dma_chan_attr;
Gary R Hook4b394a22016-07-26 19:10:21 -0500656 void (*setup)(struct ccp_device *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500657 const struct ccp_actions *perform;
658 const unsigned int bar;
659 const unsigned int offset;
660};
661
Gary R Hook9ddb9dc2016-09-28 11:53:47 -0500662extern const struct ccp_vdata ccpv3;
663extern const struct ccp_vdata ccpv5a;
664extern const struct ccp_vdata ccpv5b;
Gary R Hook58a690b2016-07-26 19:09:50 -0500665
Tom Lendacky63b94502013-11-12 11:46:16 -0600666#endif