blob: 2b5c01fade05a526d5e78241eab38ec16e790aa6 [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 Hook956ee212016-07-26 19:09:40 -0500193#define CCP_SHA_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600194
195#define CCP_RSA_MAX_WIDTH 4096
196
197#define CCP_PASSTHRU_BLOCKSIZE 256
198#define CCP_PASSTHRU_MASKSIZE 32
Gary R Hook956ee212016-07-26 19:09:40 -0500199#define CCP_PASSTHRU_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600200
201#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
202#define CCP_ECC_MAX_OPERANDS 6
203#define CCP_ECC_MAX_OUTPUTS 3
204#define CCP_ECC_SRC_BUF_SIZE 448
205#define CCP_ECC_DST_BUF_SIZE 192
206#define CCP_ECC_OPERAND_SIZE 64
207#define CCP_ECC_OUTPUT_SIZE 64
208#define CCP_ECC_RESULT_OFFSET 60
209#define CCP_ECC_RESULT_SUCCESS 0x0001
210
Gary R Hook956ee212016-07-26 19:09:40 -0500211#define CCP_SB_BYTES 32
212
Gary R Hookea0375a2016-03-01 13:49:25 -0600213struct ccp_op;
Tom Lendacky63b94502013-11-12 11:46:16 -0600214struct ccp_device;
215struct ccp_cmd;
Gary R Hook4b394a22016-07-26 19:10:21 -0500216struct ccp_fns;
Tom Lendacky63b94502013-11-12 11:46:16 -0600217
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500218struct ccp_dma_cmd {
219 struct list_head entry;
220
221 struct ccp_cmd ccp_cmd;
222};
223
224struct ccp_dma_desc {
225 struct list_head entry;
226
227 struct ccp_device *ccp;
228
229 struct list_head pending;
230 struct list_head active;
231
232 enum dma_status status;
233 struct dma_async_tx_descriptor tx_desc;
234 size_t len;
235};
236
237struct ccp_dma_chan {
238 struct ccp_device *ccp;
239
240 spinlock_t lock;
Gary R Hooke5da5c52017-01-27 17:09:04 -0600241 struct list_head created;
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500242 struct list_head pending;
243 struct list_head active;
244 struct list_head complete;
245
246 struct tasklet_struct cleanup_tasklet;
247
248 enum dma_status status;
249 struct dma_chan dma_chan;
250};
251
Tom Lendacky63b94502013-11-12 11:46:16 -0600252struct ccp_cmd_queue {
253 struct ccp_device *ccp;
254
255 /* Queue identifier */
256 u32 id;
257
258 /* Queue dma pool */
259 struct dma_pool *dma_pool;
260
Gary R Hook4b394a22016-07-26 19:10:21 -0500261 /* Queue base address (not neccessarily aligned)*/
262 struct ccp5_desc *qbase;
263
264 /* Aligned queue start address (per requirement) */
265 struct mutex q_mutex ____cacheline_aligned;
266 unsigned int qidx;
267
268 /* Version 5 has different requirements for queue memory */
269 unsigned int qsize;
270 dma_addr_t qbase_dma;
271 dma_addr_t qdma_tail;
272
Gary R Hook956ee212016-07-26 19:09:40 -0500273 /* Per-queue reserved storage block(s) */
274 u32 sb_key;
275 u32 sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600276
Gary R Hook4b394a22016-07-26 19:10:21 -0500277 /* Bitmap of LSBs that can be accessed by this queue */
278 DECLARE_BITMAP(lsbmask, MAX_LSB_CNT);
279 /* Private LSB that is assigned to this queue, or -1 if none.
280 * Bitmap for my private LSB, unused otherwise
281 */
Gary R Hook3cf79962016-10-12 08:47:03 -0500282 int lsb;
Gary R Hook4b394a22016-07-26 19:10:21 -0500283 DECLARE_BITMAP(lsbmap, PLSB_MAP_SIZE);
284
Tom Lendacky63b94502013-11-12 11:46:16 -0600285 /* Queue processing thread */
286 struct task_struct *kthread;
287 unsigned int active;
288 unsigned int suspended;
289
290 /* Number of free command slots available */
291 unsigned int free_slots;
292
293 /* Interrupt masks */
294 u32 int_ok;
295 u32 int_err;
296
297 /* Register addresses for queue */
Gary R Hook4b394a22016-07-26 19:10:21 -0500298 void __iomem *reg_control;
299 void __iomem *reg_tail_lo;
300 void __iomem *reg_head_lo;
301 void __iomem *reg_int_enable;
302 void __iomem *reg_interrupt_status;
Tom Lendacky63b94502013-11-12 11:46:16 -0600303 void __iomem *reg_status;
304 void __iomem *reg_int_status;
Gary R Hook4b394a22016-07-26 19:10:21 -0500305 void __iomem *reg_dma_status;
306 void __iomem *reg_dma_read_status;
307 void __iomem *reg_dma_write_status;
308 u32 qcontrol; /* Cached control register */
Tom Lendacky63b94502013-11-12 11:46:16 -0600309
310 /* Status values from job */
311 u32 int_status;
312 u32 q_status;
313 u32 q_int_status;
314 u32 cmd_error;
315
316 /* Interrupt wait queue */
317 wait_queue_head_t int_queue;
318 unsigned int int_rcvd;
319} ____cacheline_aligned;
320
321struct ccp_device {
Gary R Hook553d2372016-03-01 13:49:04 -0600322 struct list_head entry;
323
Gary R Hookc7019c42016-03-01 13:49:15 -0600324 struct ccp_vdata *vdata;
Gary R Hook553d2372016-03-01 13:49:04 -0600325 unsigned int ord;
326 char name[MAX_CCP_NAME_LEN];
327 char rngname[MAX_CCP_NAME_LEN];
328
Tom Lendacky63b94502013-11-12 11:46:16 -0600329 struct device *dev;
330
Gary R Hookfa242e82016-07-26 18:09:46 -0500331 /* Bus specific device information
Tom Lendacky63b94502013-11-12 11:46:16 -0600332 */
333 void *dev_specific;
334 int (*get_irq)(struct ccp_device *ccp);
335 void (*free_irq)(struct ccp_device *ccp);
Tom Lendacky3d775652014-06-05 10:17:45 -0500336 unsigned int irq;
Tom Lendacky63b94502013-11-12 11:46:16 -0600337
Gary R Hookfa242e82016-07-26 18:09:46 -0500338 /* I/O area used for device communication. The register mapping
Tom Lendacky63b94502013-11-12 11:46:16 -0600339 * starts at an offset into the mapped bar.
340 * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
341 * need to be protected while a command queue thread is accessing
342 * them.
343 */
344 struct mutex req_mutex ____cacheline_aligned;
345 void __iomem *io_map;
346 void __iomem *io_regs;
347
Gary R Hookfa242e82016-07-26 18:09:46 -0500348 /* Master lists that all cmds are queued on. Because there can be
Tom Lendacky63b94502013-11-12 11:46:16 -0600349 * more than one CCP command queue that can process a cmd a separate
350 * backlog list is neeeded so that the backlog completion call
351 * completes before the cmd is available for execution.
352 */
353 spinlock_t cmd_lock ____cacheline_aligned;
354 unsigned int cmd_count;
355 struct list_head cmd;
356 struct list_head backlog;
357
Gary R Hookfa242e82016-07-26 18:09:46 -0500358 /* The command queues. These represent the queues available on the
Tom Lendacky63b94502013-11-12 11:46:16 -0600359 * CCP that are available for processing cmds
360 */
361 struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
362 unsigned int cmd_q_count;
363
Gary R Hookfa242e82016-07-26 18:09:46 -0500364 /* Support for the CCP True RNG
Tom Lendacky63b94502013-11-12 11:46:16 -0600365 */
366 struct hwrng hwrng;
367 unsigned int hwrng_retries;
368
Gary R Hookfa242e82016-07-26 18:09:46 -0500369 /* Support for the CCP DMA capabilities
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500370 */
371 struct dma_device dma_dev;
372 struct ccp_dma_chan *ccp_dma_chan;
373 struct kmem_cache *dma_cmd_cache;
374 struct kmem_cache *dma_desc_cache;
375
Gary R Hookfa242e82016-07-26 18:09:46 -0500376 /* A counter used to generate job-ids for cmds submitted to the CCP
Tom Lendacky63b94502013-11-12 11:46:16 -0600377 */
378 atomic_t current_id ____cacheline_aligned;
379
Gary R Hook58a690b2016-07-26 19:09:50 -0500380 /* The v3 CCP uses key storage blocks (SB) to maintain context for
381 * certain operations. To prevent multiple cmds from using the same
382 * SB range a command queue reserves an SB range for the duration of
383 * the cmd. Each queue, will however, reserve 2 SB blocks for
384 * operations that only require single SB entries (eg. AES context/iv
385 * and key) in order to avoid allocation contention. This will reserve
386 * at most 10 SB entries, leaving 40 SB entries available for dynamic
387 * allocation.
388 *
389 * The v5 CCP Local Storage Block (LSB) is broken up into 8
390 * memrory ranges, each of which can be enabled for access by one
391 * or more queues. Device initialization takes this into account,
392 * and attempts to assign one region for exclusive use by each
393 * available queue; the rest are then aggregated as "public" use.
394 * If there are fewer regions than queues, all regions are shared
395 * amongst all queues.
Tom Lendacky63b94502013-11-12 11:46:16 -0600396 */
Gary R Hook956ee212016-07-26 19:09:40 -0500397 struct mutex sb_mutex ____cacheline_aligned;
398 DECLARE_BITMAP(sb, KSB_COUNT);
399 wait_queue_head_t sb_queue;
400 unsigned int sb_avail;
401 unsigned int sb_count;
402 u32 sb_start;
Tom Lendacky63b94502013-11-12 11:46:16 -0600403
Gary R Hook4b394a22016-07-26 19:10:21 -0500404 /* Bitmap of shared LSBs, if any */
405 DECLARE_BITMAP(lsbmap, SLSB_MAP_SIZE);
406
Tom Lendacky63b94502013-11-12 11:46:16 -0600407 /* Suspend support */
408 unsigned int suspending;
409 wait_queue_head_t suspend_queue;
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500410
411 /* DMA caching attribute support */
412 unsigned int axcache;
Tom Lendacky63b94502013-11-12 11:46:16 -0600413};
414
Gary R Hookea0375a2016-03-01 13:49:25 -0600415enum ccp_memtype {
416 CCP_MEMTYPE_SYSTEM = 0,
Gary R Hook956ee212016-07-26 19:09:40 -0500417 CCP_MEMTYPE_SB,
Gary R Hookea0375a2016-03-01 13:49:25 -0600418 CCP_MEMTYPE_LOCAL,
419 CCP_MEMTYPE__LAST,
420};
Gary R Hook4b394a22016-07-26 19:10:21 -0500421#define CCP_MEMTYPE_LSB CCP_MEMTYPE_KSB
Gary R Hookea0375a2016-03-01 13:49:25 -0600422
423struct ccp_dma_info {
424 dma_addr_t address;
425 unsigned int offset;
426 unsigned int length;
427 enum dma_data_direction dir;
428};
429
430struct ccp_dm_workarea {
431 struct device *dev;
432 struct dma_pool *dma_pool;
433 unsigned int length;
434
435 u8 *address;
436 struct ccp_dma_info dma;
437};
438
439struct ccp_sg_workarea {
440 struct scatterlist *sg;
441 int nents;
442
443 struct scatterlist *dma_sg;
444 struct device *dma_dev;
445 unsigned int dma_count;
446 enum dma_data_direction dma_dir;
447
448 unsigned int sg_used;
449
450 u64 bytes_left;
451};
452
453struct ccp_data {
454 struct ccp_sg_workarea sg_wa;
455 struct ccp_dm_workarea dm_wa;
456};
457
458struct ccp_mem {
459 enum ccp_memtype type;
460 union {
461 struct ccp_dma_info dma;
Gary R Hook956ee212016-07-26 19:09:40 -0500462 u32 sb;
Gary R Hookea0375a2016-03-01 13:49:25 -0600463 } u;
464};
465
466struct ccp_aes_op {
467 enum ccp_aes_type type;
468 enum ccp_aes_mode mode;
469 enum ccp_aes_action action;
Gary R Hookf7cc02b32017-02-08 13:07:06 -0600470 unsigned int size;
Gary R Hookea0375a2016-03-01 13:49:25 -0600471};
472
473struct ccp_xts_aes_op {
474 enum ccp_aes_action action;
475 enum ccp_xts_aes_unit_size unit_size;
476};
477
478struct ccp_sha_op {
479 enum ccp_sha_type type;
480 u64 msg_bits;
481};
482
483struct ccp_rsa_op {
484 u32 mod_size;
485 u32 input_len;
486};
487
488struct ccp_passthru_op {
489 enum ccp_passthru_bitwise bit_mod;
490 enum ccp_passthru_byteswap byte_swap;
491};
492
493struct ccp_ecc_op {
494 enum ccp_ecc_function function;
495};
496
497struct ccp_op {
498 struct ccp_cmd_queue *cmd_q;
499
500 u32 jobid;
501 u32 ioc;
502 u32 soc;
Gary R Hook956ee212016-07-26 19:09:40 -0500503 u32 sb_key;
504 u32 sb_ctx;
Gary R Hookea0375a2016-03-01 13:49:25 -0600505 u32 init;
506 u32 eom;
507
508 struct ccp_mem src;
509 struct ccp_mem dst;
Gary R Hook4b394a22016-07-26 19:10:21 -0500510 struct ccp_mem exp;
Gary R Hookea0375a2016-03-01 13:49:25 -0600511
512 union {
513 struct ccp_aes_op aes;
514 struct ccp_xts_aes_op xts;
515 struct ccp_sha_op sha;
516 struct ccp_rsa_op rsa;
517 struct ccp_passthru_op passthru;
518 struct ccp_ecc_op ecc;
519 } u;
520};
521
522static inline u32 ccp_addr_lo(struct ccp_dma_info *info)
523{
524 return lower_32_bits(info->address + info->offset);
525}
526
527static inline u32 ccp_addr_hi(struct ccp_dma_info *info)
528{
529 return upper_32_bits(info->address + info->offset) & 0x0000ffff;
530}
531
Gary R Hook4b394a22016-07-26 19:10:21 -0500532/**
533 * descriptor for version 5 CPP commands
534 * 8 32-bit words:
535 * word 0: function; engine; control bits
536 * word 1: length of source data
537 * word 2: low 32 bits of source pointer
538 * word 3: upper 16 bits of source pointer; source memory type
539 * word 4: low 32 bits of destination pointer
540 * word 5: upper 16 bits of destination pointer; destination memory type
541 * word 6: low 32 bits of key pointer
542 * word 7: upper 16 bits of key pointer; key memory type
543 */
544struct dword0 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500545 unsigned int soc:1;
546 unsigned int ioc:1;
547 unsigned int rsvd1:1;
548 unsigned int init:1;
549 unsigned int eom:1; /* AES/SHA only */
550 unsigned int function:15;
551 unsigned int engine:4;
552 unsigned int prot:1;
553 unsigned int rsvd2:7;
Gary R Hook4b394a22016-07-26 19:10:21 -0500554};
555
556struct dword3 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500557 unsigned int src_hi:16;
558 unsigned int src_mem:2;
559 unsigned int lsb_cxt_id:8;
560 unsigned int rsvd1:5;
561 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500562};
563
564union dword4 {
565 __le32 dst_lo; /* NON-SHA */
566 __le32 sha_len_lo; /* SHA */
567};
568
569union dword5 {
570 struct {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500571 unsigned int dst_hi:16;
572 unsigned int dst_mem:2;
573 unsigned int rsvd1:13;
574 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500575 } fields;
576 __le32 sha_len_hi;
577};
578
579struct dword7 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500580 unsigned int key_hi:16;
581 unsigned int key_mem:2;
582 unsigned int rsvd1:14;
Gary R Hook4b394a22016-07-26 19:10:21 -0500583};
584
585struct ccp5_desc {
586 struct dword0 dw0;
587 __le32 length;
588 __le32 src_lo;
589 struct dword3 dw3;
590 union dword4 dw4;
591 union dword5 dw5;
592 __le32 key_lo;
593 struct dword7 dw7;
594};
595
Tom Lendacky63b94502013-11-12 11:46:16 -0600596int ccp_pci_init(void);
597void ccp_pci_exit(void);
598
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500599int ccp_platform_init(void);
600void ccp_platform_exit(void);
601
Gary R Hookea0375a2016-03-01 13:49:25 -0600602void ccp_add_device(struct ccp_device *ccp);
603void ccp_del_device(struct ccp_device *ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600604
Gary R Hook81422ba2016-09-28 11:53:56 -0500605extern void ccp_log_error(struct ccp_device *, int);
606
Gary R Hookea0375a2016-03-01 13:49:25 -0600607struct ccp_device *ccp_alloc_struct(struct device *dev);
608bool ccp_queues_suspended(struct ccp_device *ccp);
609int ccp_cmd_queue_thread(void *data);
Gary R Hook8256e682016-07-26 19:10:02 -0500610int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait);
Tom Lendacky63b94502013-11-12 11:46:16 -0600611
612int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
613
Gary R Hook084935b2016-07-26 19:10:31 -0500614int ccp_register_rng(struct ccp_device *ccp);
615void ccp_unregister_rng(struct ccp_device *ccp);
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500616int ccp_dmaengine_register(struct ccp_device *ccp);
617void ccp_dmaengine_unregister(struct ccp_device *ccp);
618
Gary R Hook58a690b2016-07-26 19:09:50 -0500619/* Structure for computation functions that are device-specific */
620struct ccp_actions {
621 int (*aes)(struct ccp_op *);
622 int (*xts_aes)(struct ccp_op *);
623 int (*sha)(struct ccp_op *);
624 int (*rsa)(struct ccp_op *);
625 int (*passthru)(struct ccp_op *);
626 int (*ecc)(struct ccp_op *);
627 u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
628 void (*sbfree)(struct ccp_cmd_queue *, unsigned int,
629 unsigned int);
Gary R Hookbb4e89b2016-07-26 19:10:13 -0500630 unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500631 int (*init)(struct ccp_device *);
632 void (*destroy)(struct ccp_device *);
633 irqreturn_t (*irqhandler)(int, void *);
634};
635
636/* Structure to hold CCP version-specific values */
637struct ccp_vdata {
Gary R Hook4b394a22016-07-26 19:10:21 -0500638 const unsigned int version;
639 void (*setup)(struct ccp_device *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500640 const struct ccp_actions *perform;
641 const unsigned int bar;
642 const unsigned int offset;
643};
644
Gary R Hook9ddb9dc2016-09-28 11:53:47 -0500645extern const struct ccp_vdata ccpv3;
646extern const struct ccp_vdata ccpv5a;
647extern const struct ccp_vdata ccpv5b;
Gary R Hook58a690b2016-07-26 19:09:50 -0500648
Tom Lendacky63b94502013-11-12 11:46:16 -0600649#endif