blob: 754e9c2f6ee3adcece6e444cbf77b1814a35cf82 [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
112#define ALL_INTERRUPTS (INT_COMPLETION| \
113 INT_ERROR| \
114 INT_QUEUE_STOPPED)
115
116#define LSB_REGION_WIDTH 5
117#define MAX_LSB_CNT 8
118
119#define LSB_SIZE 16
120#define LSB_ITEM_SIZE 32
121#define PLSB_MAP_SIZE (LSB_SIZE)
122#define SLSB_MAP_SIZE (MAX_LSB_CNT * LSB_SIZE)
123
124#define LSB_ENTRY_NUMBER(LSB_ADDR) (LSB_ADDR / LSB_ITEM_SIZE)
125
126/* ------------------------ CCP Version 3 Specifics ------------------------ */
Tom Lendacky63b94502013-11-12 11:46:16 -0600127#define REQ0_WAIT_FOR_WRITE 0x00000004
128#define REQ0_INT_ON_COMPLETE 0x00000002
129#define REQ0_STOP_ON_COMPLETE 0x00000001
130
131#define REQ0_CMD_Q_SHIFT 9
132#define REQ0_JOBID_SHIFT 3
133
134/****** REQ1 Related Values ******/
135#define REQ1_PROTECT_SHIFT 27
136#define REQ1_ENGINE_SHIFT 23
137#define REQ1_KEY_KSB_SHIFT 2
138
139#define REQ1_EOM 0x00000002
140#define REQ1_INIT 0x00000001
141
142/* AES Related Values */
143#define REQ1_AES_TYPE_SHIFT 21
144#define REQ1_AES_MODE_SHIFT 18
145#define REQ1_AES_ACTION_SHIFT 17
146#define REQ1_AES_CFB_SIZE_SHIFT 10
147
148/* XTS-AES Related Values */
149#define REQ1_XTS_AES_SIZE_SHIFT 10
150
151/* SHA Related Values */
152#define REQ1_SHA_TYPE_SHIFT 21
153
154/* RSA Related Values */
155#define REQ1_RSA_MOD_SIZE_SHIFT 10
156
157/* Pass-Through Related Values */
158#define REQ1_PT_BW_SHIFT 12
159#define REQ1_PT_BS_SHIFT 10
160
161/* ECC Related Values */
162#define REQ1_ECC_AFFINE_CONVERT 0x00200000
163#define REQ1_ECC_FUNCTION_SHIFT 18
164
165/****** REQ4 Related Values ******/
166#define REQ4_KSB_SHIFT 18
167#define REQ4_MEMTYPE_SHIFT 16
168
169/****** REQ6 Related Values ******/
170#define REQ6_MEMTYPE_SHIFT 16
171
Tom Lendacky63b94502013-11-12 11:46:16 -0600172/****** Key Storage Block ******/
173#define KSB_START 77
174#define KSB_END 127
175#define KSB_COUNT (KSB_END - KSB_START + 1)
Gary R Hook956ee212016-07-26 19:09:40 -0500176#define CCP_SB_BITS 256
Tom Lendacky63b94502013-11-12 11:46:16 -0600177
178#define CCP_JOBID_MASK 0x0000003f
179
Gary R Hook4b394a22016-07-26 19:10:21 -0500180/* ------------------------ General CCP Defines ------------------------ */
181
Tom Lendacky63b94502013-11-12 11:46:16 -0600182#define CCP_DMAPOOL_MAX_SIZE 64
Tom Lendacky8db88462015-02-03 13:07:05 -0600183#define CCP_DMAPOOL_ALIGN BIT(5)
Tom Lendacky63b94502013-11-12 11:46:16 -0600184
185#define CCP_REVERSE_BUF_SIZE 64
186
Gary R Hook956ee212016-07-26 19:09:40 -0500187#define CCP_AES_KEY_SB_COUNT 1
188#define CCP_AES_CTX_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600189
Gary R Hook956ee212016-07-26 19:09:40 -0500190#define CCP_XTS_AES_KEY_SB_COUNT 1
191#define CCP_XTS_AES_CTX_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600192
Gary R Hook990672d2017-03-15 13:20:52 -0500193#define CCP_DES3_KEY_SB_COUNT 1
194#define CCP_DES3_CTX_SB_COUNT 1
195
Gary R Hook956ee212016-07-26 19:09:40 -0500196#define CCP_SHA_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600197
198#define CCP_RSA_MAX_WIDTH 4096
199
200#define CCP_PASSTHRU_BLOCKSIZE 256
201#define CCP_PASSTHRU_MASKSIZE 32
Gary R Hook956ee212016-07-26 19:09:40 -0500202#define CCP_PASSTHRU_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600203
204#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
205#define CCP_ECC_MAX_OPERANDS 6
206#define CCP_ECC_MAX_OUTPUTS 3
207#define CCP_ECC_SRC_BUF_SIZE 448
208#define CCP_ECC_DST_BUF_SIZE 192
209#define CCP_ECC_OPERAND_SIZE 64
210#define CCP_ECC_OUTPUT_SIZE 64
211#define CCP_ECC_RESULT_OFFSET 60
212#define CCP_ECC_RESULT_SUCCESS 0x0001
213
Gary R Hook956ee212016-07-26 19:09:40 -0500214#define CCP_SB_BYTES 32
215
Gary R Hookea0375a2016-03-01 13:49:25 -0600216struct ccp_op;
Tom Lendacky63b94502013-11-12 11:46:16 -0600217struct ccp_device;
218struct ccp_cmd;
Gary R Hook4b394a22016-07-26 19:10:21 -0500219struct ccp_fns;
Tom Lendacky63b94502013-11-12 11:46:16 -0600220
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500221struct ccp_dma_cmd {
222 struct list_head entry;
223
224 struct ccp_cmd ccp_cmd;
225};
226
227struct ccp_dma_desc {
228 struct list_head entry;
229
230 struct ccp_device *ccp;
231
232 struct list_head pending;
233 struct list_head active;
234
235 enum dma_status status;
236 struct dma_async_tx_descriptor tx_desc;
237 size_t len;
238};
239
240struct ccp_dma_chan {
241 struct ccp_device *ccp;
242
243 spinlock_t lock;
Gary R Hooke5da5c52017-01-27 17:09:04 -0600244 struct list_head created;
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500245 struct list_head pending;
246 struct list_head active;
247 struct list_head complete;
248
249 struct tasklet_struct cleanup_tasklet;
250
251 enum dma_status status;
252 struct dma_chan dma_chan;
253};
254
Tom Lendacky63b94502013-11-12 11:46:16 -0600255struct ccp_cmd_queue {
256 struct ccp_device *ccp;
257
258 /* Queue identifier */
259 u32 id;
260
261 /* Queue dma pool */
262 struct dma_pool *dma_pool;
263
Gary R Hook4b394a22016-07-26 19:10:21 -0500264 /* Queue base address (not neccessarily aligned)*/
265 struct ccp5_desc *qbase;
266
267 /* Aligned queue start address (per requirement) */
268 struct mutex q_mutex ____cacheline_aligned;
269 unsigned int qidx;
270
271 /* Version 5 has different requirements for queue memory */
272 unsigned int qsize;
273 dma_addr_t qbase_dma;
274 dma_addr_t qdma_tail;
275
Gary R Hook956ee212016-07-26 19:09:40 -0500276 /* Per-queue reserved storage block(s) */
277 u32 sb_key;
278 u32 sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600279
Gary R Hook4b394a22016-07-26 19:10:21 -0500280 /* Bitmap of LSBs that can be accessed by this queue */
281 DECLARE_BITMAP(lsbmask, MAX_LSB_CNT);
282 /* Private LSB that is assigned to this queue, or -1 if none.
283 * Bitmap for my private LSB, unused otherwise
284 */
Gary R Hook3cf79962016-10-12 08:47:03 -0500285 int lsb;
Gary R Hook4b394a22016-07-26 19:10:21 -0500286 DECLARE_BITMAP(lsbmap, PLSB_MAP_SIZE);
287
Tom Lendacky63b94502013-11-12 11:46:16 -0600288 /* Queue processing thread */
289 struct task_struct *kthread;
290 unsigned int active;
291 unsigned int suspended;
292
293 /* Number of free command slots available */
294 unsigned int free_slots;
295
296 /* Interrupt masks */
297 u32 int_ok;
298 u32 int_err;
299
300 /* Register addresses for queue */
Gary R Hook4b394a22016-07-26 19:10:21 -0500301 void __iomem *reg_control;
302 void __iomem *reg_tail_lo;
303 void __iomem *reg_head_lo;
304 void __iomem *reg_int_enable;
305 void __iomem *reg_interrupt_status;
Tom Lendacky63b94502013-11-12 11:46:16 -0600306 void __iomem *reg_status;
307 void __iomem *reg_int_status;
Gary R Hook4b394a22016-07-26 19:10:21 -0500308 void __iomem *reg_dma_status;
309 void __iomem *reg_dma_read_status;
310 void __iomem *reg_dma_write_status;
311 u32 qcontrol; /* Cached control register */
Tom Lendacky63b94502013-11-12 11:46:16 -0600312
313 /* Status values from job */
314 u32 int_status;
315 u32 q_status;
316 u32 q_int_status;
317 u32 cmd_error;
318
319 /* Interrupt wait queue */
320 wait_queue_head_t int_queue;
321 unsigned int int_rcvd;
322} ____cacheline_aligned;
323
324struct ccp_device {
Gary R Hook553d2372016-03-01 13:49:04 -0600325 struct list_head entry;
326
Gary R Hookc7019c42016-03-01 13:49:15 -0600327 struct ccp_vdata *vdata;
Gary R Hook553d2372016-03-01 13:49:04 -0600328 unsigned int ord;
329 char name[MAX_CCP_NAME_LEN];
330 char rngname[MAX_CCP_NAME_LEN];
331
Tom Lendacky63b94502013-11-12 11:46:16 -0600332 struct device *dev;
333
Gary R Hookfa242e82016-07-26 18:09:46 -0500334 /* Bus specific device information
Tom Lendacky63b94502013-11-12 11:46:16 -0600335 */
336 void *dev_specific;
337 int (*get_irq)(struct ccp_device *ccp);
338 void (*free_irq)(struct ccp_device *ccp);
Tom Lendacky3d775652014-06-05 10:17:45 -0500339 unsigned int irq;
Tom Lendacky63b94502013-11-12 11:46:16 -0600340
Gary R Hookfa242e82016-07-26 18:09:46 -0500341 /* I/O area used for device communication. The register mapping
Tom Lendacky63b94502013-11-12 11:46:16 -0600342 * starts at an offset into the mapped bar.
343 * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
344 * need to be protected while a command queue thread is accessing
345 * them.
346 */
347 struct mutex req_mutex ____cacheline_aligned;
348 void __iomem *io_map;
349 void __iomem *io_regs;
350
Gary R Hookfa242e82016-07-26 18:09:46 -0500351 /* Master lists that all cmds are queued on. Because there can be
Tom Lendacky63b94502013-11-12 11:46:16 -0600352 * more than one CCP command queue that can process a cmd a separate
353 * backlog list is neeeded so that the backlog completion call
354 * completes before the cmd is available for execution.
355 */
356 spinlock_t cmd_lock ____cacheline_aligned;
357 unsigned int cmd_count;
358 struct list_head cmd;
359 struct list_head backlog;
360
Gary R Hookfa242e82016-07-26 18:09:46 -0500361 /* The command queues. These represent the queues available on the
Tom Lendacky63b94502013-11-12 11:46:16 -0600362 * CCP that are available for processing cmds
363 */
364 struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
365 unsigned int cmd_q_count;
366
Gary R Hookfa242e82016-07-26 18:09:46 -0500367 /* Support for the CCP True RNG
Tom Lendacky63b94502013-11-12 11:46:16 -0600368 */
369 struct hwrng hwrng;
370 unsigned int hwrng_retries;
371
Gary R Hookfa242e82016-07-26 18:09:46 -0500372 /* Support for the CCP DMA capabilities
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500373 */
374 struct dma_device dma_dev;
375 struct ccp_dma_chan *ccp_dma_chan;
376 struct kmem_cache *dma_cmd_cache;
377 struct kmem_cache *dma_desc_cache;
378
Gary R Hookfa242e82016-07-26 18:09:46 -0500379 /* A counter used to generate job-ids for cmds submitted to the CCP
Tom Lendacky63b94502013-11-12 11:46:16 -0600380 */
381 atomic_t current_id ____cacheline_aligned;
382
Gary R Hook58a690b2016-07-26 19:09:50 -0500383 /* The v3 CCP uses key storage blocks (SB) to maintain context for
384 * certain operations. To prevent multiple cmds from using the same
385 * SB range a command queue reserves an SB range for the duration of
386 * the cmd. Each queue, will however, reserve 2 SB blocks for
387 * operations that only require single SB entries (eg. AES context/iv
388 * and key) in order to avoid allocation contention. This will reserve
389 * at most 10 SB entries, leaving 40 SB entries available for dynamic
390 * allocation.
391 *
392 * The v5 CCP Local Storage Block (LSB) is broken up into 8
393 * memrory ranges, each of which can be enabled for access by one
394 * or more queues. Device initialization takes this into account,
395 * and attempts to assign one region for exclusive use by each
396 * available queue; the rest are then aggregated as "public" use.
397 * If there are fewer regions than queues, all regions are shared
398 * amongst all queues.
Tom Lendacky63b94502013-11-12 11:46:16 -0600399 */
Gary R Hook956ee212016-07-26 19:09:40 -0500400 struct mutex sb_mutex ____cacheline_aligned;
401 DECLARE_BITMAP(sb, KSB_COUNT);
402 wait_queue_head_t sb_queue;
403 unsigned int sb_avail;
404 unsigned int sb_count;
405 u32 sb_start;
Tom Lendacky63b94502013-11-12 11:46:16 -0600406
Gary R Hook4b394a22016-07-26 19:10:21 -0500407 /* Bitmap of shared LSBs, if any */
408 DECLARE_BITMAP(lsbmap, SLSB_MAP_SIZE);
409
Tom Lendacky63b94502013-11-12 11:46:16 -0600410 /* Suspend support */
411 unsigned int suspending;
412 wait_queue_head_t suspend_queue;
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500413
414 /* DMA caching attribute support */
415 unsigned int axcache;
Tom Lendacky63b94502013-11-12 11:46:16 -0600416};
417
Gary R Hookea0375a2016-03-01 13:49:25 -0600418enum ccp_memtype {
419 CCP_MEMTYPE_SYSTEM = 0,
Gary R Hook956ee212016-07-26 19:09:40 -0500420 CCP_MEMTYPE_SB,
Gary R Hookea0375a2016-03-01 13:49:25 -0600421 CCP_MEMTYPE_LOCAL,
422 CCP_MEMTYPE__LAST,
423};
Gary R Hook4b394a22016-07-26 19:10:21 -0500424#define CCP_MEMTYPE_LSB CCP_MEMTYPE_KSB
Gary R Hookea0375a2016-03-01 13:49:25 -0600425
426struct ccp_dma_info {
427 dma_addr_t address;
428 unsigned int offset;
429 unsigned int length;
430 enum dma_data_direction dir;
431};
432
433struct ccp_dm_workarea {
434 struct device *dev;
435 struct dma_pool *dma_pool;
436 unsigned int length;
437
438 u8 *address;
439 struct ccp_dma_info dma;
440};
441
442struct ccp_sg_workarea {
443 struct scatterlist *sg;
444 int nents;
445
446 struct scatterlist *dma_sg;
447 struct device *dma_dev;
448 unsigned int dma_count;
449 enum dma_data_direction dma_dir;
450
451 unsigned int sg_used;
452
453 u64 bytes_left;
454};
455
456struct ccp_data {
457 struct ccp_sg_workarea sg_wa;
458 struct ccp_dm_workarea dm_wa;
459};
460
461struct ccp_mem {
462 enum ccp_memtype type;
463 union {
464 struct ccp_dma_info dma;
Gary R Hook956ee212016-07-26 19:09:40 -0500465 u32 sb;
Gary R Hookea0375a2016-03-01 13:49:25 -0600466 } u;
467};
468
469struct ccp_aes_op {
470 enum ccp_aes_type type;
471 enum ccp_aes_mode mode;
472 enum ccp_aes_action action;
Gary R Hookf7cc02b32017-02-08 13:07:06 -0600473 unsigned int size;
Gary R Hookea0375a2016-03-01 13:49:25 -0600474};
475
476struct ccp_xts_aes_op {
477 enum ccp_aes_action action;
478 enum ccp_xts_aes_unit_size unit_size;
479};
480
Gary R Hook990672d2017-03-15 13:20:52 -0500481struct ccp_des3_op {
482 enum ccp_des3_type type;
483 enum ccp_des3_mode mode;
484 enum ccp_des3_action action;
485};
486
Gary R Hookea0375a2016-03-01 13:49:25 -0600487struct ccp_sha_op {
488 enum ccp_sha_type type;
489 u64 msg_bits;
490};
491
492struct ccp_rsa_op {
493 u32 mod_size;
494 u32 input_len;
495};
496
497struct ccp_passthru_op {
498 enum ccp_passthru_bitwise bit_mod;
499 enum ccp_passthru_byteswap byte_swap;
500};
501
502struct ccp_ecc_op {
503 enum ccp_ecc_function function;
504};
505
506struct ccp_op {
507 struct ccp_cmd_queue *cmd_q;
508
509 u32 jobid;
510 u32 ioc;
511 u32 soc;
Gary R Hook956ee212016-07-26 19:09:40 -0500512 u32 sb_key;
513 u32 sb_ctx;
Gary R Hookea0375a2016-03-01 13:49:25 -0600514 u32 init;
515 u32 eom;
516
517 struct ccp_mem src;
518 struct ccp_mem dst;
Gary R Hook4b394a22016-07-26 19:10:21 -0500519 struct ccp_mem exp;
Gary R Hookea0375a2016-03-01 13:49:25 -0600520
521 union {
522 struct ccp_aes_op aes;
523 struct ccp_xts_aes_op xts;
Gary R Hook990672d2017-03-15 13:20:52 -0500524 struct ccp_des3_op des3;
Gary R Hookea0375a2016-03-01 13:49:25 -0600525 struct ccp_sha_op sha;
526 struct ccp_rsa_op rsa;
527 struct ccp_passthru_op passthru;
528 struct ccp_ecc_op ecc;
529 } u;
530};
531
532static inline u32 ccp_addr_lo(struct ccp_dma_info *info)
533{
534 return lower_32_bits(info->address + info->offset);
535}
536
537static inline u32 ccp_addr_hi(struct ccp_dma_info *info)
538{
539 return upper_32_bits(info->address + info->offset) & 0x0000ffff;
540}
541
Gary R Hook4b394a22016-07-26 19:10:21 -0500542/**
543 * descriptor for version 5 CPP commands
544 * 8 32-bit words:
545 * word 0: function; engine; control bits
546 * word 1: length of source data
547 * word 2: low 32 bits of source pointer
548 * word 3: upper 16 bits of source pointer; source memory type
549 * word 4: low 32 bits of destination pointer
550 * word 5: upper 16 bits of destination pointer; destination memory type
551 * word 6: low 32 bits of key pointer
552 * word 7: upper 16 bits of key pointer; key memory type
553 */
554struct dword0 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500555 unsigned int soc:1;
556 unsigned int ioc:1;
557 unsigned int rsvd1:1;
558 unsigned int init:1;
559 unsigned int eom:1; /* AES/SHA only */
560 unsigned int function:15;
561 unsigned int engine:4;
562 unsigned int prot:1;
563 unsigned int rsvd2:7;
Gary R Hook4b394a22016-07-26 19:10:21 -0500564};
565
566struct dword3 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500567 unsigned int src_hi:16;
568 unsigned int src_mem:2;
569 unsigned int lsb_cxt_id:8;
570 unsigned int rsvd1:5;
571 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500572};
573
574union dword4 {
575 __le32 dst_lo; /* NON-SHA */
576 __le32 sha_len_lo; /* SHA */
577};
578
579union dword5 {
580 struct {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500581 unsigned int dst_hi:16;
582 unsigned int dst_mem:2;
583 unsigned int rsvd1:13;
584 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500585 } fields;
586 __le32 sha_len_hi;
587};
588
589struct dword7 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500590 unsigned int key_hi:16;
591 unsigned int key_mem:2;
592 unsigned int rsvd1:14;
Gary R Hook4b394a22016-07-26 19:10:21 -0500593};
594
595struct ccp5_desc {
596 struct dword0 dw0;
597 __le32 length;
598 __le32 src_lo;
599 struct dword3 dw3;
600 union dword4 dw4;
601 union dword5 dw5;
602 __le32 key_lo;
603 struct dword7 dw7;
604};
605
Tom Lendacky63b94502013-11-12 11:46:16 -0600606int ccp_pci_init(void);
607void ccp_pci_exit(void);
608
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500609int ccp_platform_init(void);
610void ccp_platform_exit(void);
611
Gary R Hookea0375a2016-03-01 13:49:25 -0600612void ccp_add_device(struct ccp_device *ccp);
613void ccp_del_device(struct ccp_device *ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600614
Gary R Hook81422ba2016-09-28 11:53:56 -0500615extern void ccp_log_error(struct ccp_device *, int);
616
Gary R Hookea0375a2016-03-01 13:49:25 -0600617struct ccp_device *ccp_alloc_struct(struct device *dev);
618bool ccp_queues_suspended(struct ccp_device *ccp);
619int ccp_cmd_queue_thread(void *data);
Gary R Hook8256e682016-07-26 19:10:02 -0500620int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait);
Tom Lendacky63b94502013-11-12 11:46:16 -0600621
622int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
623
Gary R Hook084935b2016-07-26 19:10:31 -0500624int ccp_register_rng(struct ccp_device *ccp);
625void ccp_unregister_rng(struct ccp_device *ccp);
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500626int ccp_dmaengine_register(struct ccp_device *ccp);
627void ccp_dmaengine_unregister(struct ccp_device *ccp);
628
Gary R Hook58a690b2016-07-26 19:09:50 -0500629/* Structure for computation functions that are device-specific */
630struct ccp_actions {
631 int (*aes)(struct ccp_op *);
632 int (*xts_aes)(struct ccp_op *);
Gary R Hook990672d2017-03-15 13:20:52 -0500633 int (*des3)(struct ccp_op *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500634 int (*sha)(struct ccp_op *);
635 int (*rsa)(struct ccp_op *);
636 int (*passthru)(struct ccp_op *);
637 int (*ecc)(struct ccp_op *);
638 u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
Gary R Hook990672d2017-03-15 13:20:52 -0500639 void (*sbfree)(struct ccp_cmd_queue *, unsigned int, unsigned int);
Gary R Hookbb4e89b2016-07-26 19:10:13 -0500640 unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500641 int (*init)(struct ccp_device *);
642 void (*destroy)(struct ccp_device *);
643 irqreturn_t (*irqhandler)(int, void *);
644};
645
646/* Structure to hold CCP version-specific values */
647struct ccp_vdata {
Gary R Hook4b394a22016-07-26 19:10:21 -0500648 const unsigned int version;
649 void (*setup)(struct ccp_device *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500650 const struct ccp_actions *perform;
651 const unsigned int bar;
652 const unsigned int offset;
653};
654
Gary R Hook9ddb9dc2016-09-28 11:53:47 -0500655extern const struct ccp_vdata ccpv3;
656extern const struct ccp_vdata ccpv5a;
657extern const struct ccp_vdata ccpv5b;
Gary R Hook58a690b2016-07-26 19:09:50 -0500658
Tom Lendacky63b94502013-11-12 11:46:16 -0600659#endif