blob: 6810b65c1939c88abee1448f06e9d9c2b71460df [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook68cc6522017-07-17 15:00:49 -05004 * Copyright (C) 2013,2017 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
Brijesh Singh720419f2017-07-06 09:59:14 -050030#include "sp-dev.h"
31
Gary R Hook553d2372016-03-01 13:49:04 -060032#define MAX_CCP_NAME_LEN 16
Tom Lendacky63b94502013-11-12 11:46:16 -060033#define MAX_DMAPOOL_NAME_LEN 32
34
35#define MAX_HW_QUEUES 5
36#define MAX_CMD_QLEN 100
37
38#define TRNG_RETRIES 10
39
Tom Lendacky126ae9a2014-07-10 10:58:35 -050040#define CACHE_NONE 0x00
Tom Lendackyc4f4b322014-06-05 10:17:57 -050041#define CACHE_WB_NO_ALLOC 0xb7
42
Tom Lendacky63b94502013-11-12 11:46:16 -060043/****** Register Mappings ******/
44#define Q_MASK_REG 0x000
45#define TRNG_OUT_REG 0x00c
46#define IRQ_MASK_REG 0x040
47#define IRQ_STATUS_REG 0x200
48
49#define DEL_CMD_Q_JOB 0x124
50#define DEL_Q_ACTIVE 0x00000200
51#define DEL_Q_ID_SHIFT 6
52
53#define CMD_REQ0 0x180
54#define CMD_REQ_INCR 0x04
55
56#define CMD_Q_STATUS_BASE 0x210
57#define CMD_Q_INT_STATUS_BASE 0x214
58#define CMD_Q_STATUS_INCR 0x20
59
Tom Lendackyc4f4b322014-06-05 10:17:57 -050060#define CMD_Q_CACHE_BASE 0x228
Tom Lendacky63b94502013-11-12 11:46:16 -060061#define CMD_Q_CACHE_INC 0x20
62
Tom Lendacky8db88462015-02-03 13:07:05 -060063#define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f)
64#define CMD_Q_DEPTH(__qs) (((__qs) >> 12) & 0x0000000f)
Tom Lendacky63b94502013-11-12 11:46:16 -060065
Gary R Hook4b394a22016-07-26 19:10:21 -050066/* ------------------------ CCP Version 5 Specifics ------------------------ */
67#define CMD5_QUEUE_MASK_OFFSET 0x00
Gary R Hooke14e7d12016-07-26 19:10:49 -050068#define CMD5_QUEUE_PRIO_OFFSET 0x04
Gary R Hook4b394a22016-07-26 19:10:21 -050069#define CMD5_REQID_CONFIG_OFFSET 0x08
Gary R Hooke14e7d12016-07-26 19:10:49 -050070#define CMD5_CMD_TIMEOUT_OFFSET 0x10
Gary R Hook4b394a22016-07-26 19:10:21 -050071#define LSB_PUBLIC_MASK_LO_OFFSET 0x18
72#define LSB_PUBLIC_MASK_HI_OFFSET 0x1C
73#define LSB_PRIVATE_MASK_LO_OFFSET 0x20
74#define LSB_PRIVATE_MASK_HI_OFFSET 0x24
Gary R Hook3cdbe342017-05-02 17:33:40 -050075#define CMD5_PSP_CCP_VERSION 0x100
Gary R Hook4b394a22016-07-26 19:10:21 -050076
77#define CMD5_Q_CONTROL_BASE 0x0000
78#define CMD5_Q_TAIL_LO_BASE 0x0004
79#define CMD5_Q_HEAD_LO_BASE 0x0008
80#define CMD5_Q_INT_ENABLE_BASE 0x000C
81#define CMD5_Q_INTERRUPT_STATUS_BASE 0x0010
82
83#define CMD5_Q_STATUS_BASE 0x0100
84#define CMD5_Q_INT_STATUS_BASE 0x0104
85#define CMD5_Q_DMA_STATUS_BASE 0x0108
86#define CMD5_Q_DMA_READ_STATUS_BASE 0x010C
87#define CMD5_Q_DMA_WRITE_STATUS_BASE 0x0110
88#define CMD5_Q_ABORT_BASE 0x0114
89#define CMD5_Q_AX_CACHE_BASE 0x0118
90
Gary R Hooke14e7d12016-07-26 19:10:49 -050091#define CMD5_CONFIG_0_OFFSET 0x6000
92#define CMD5_TRNG_CTL_OFFSET 0x6008
93#define CMD5_AES_MASK_OFFSET 0x6010
94#define CMD5_CLK_GATE_CTL_OFFSET 0x603C
95
Gary R Hook4b394a22016-07-26 19:10:21 -050096/* Address offset between two virtual queue registers */
97#define CMD5_Q_STATUS_INCR 0x1000
98
99/* Bit masks */
100#define CMD5_Q_RUN 0x1
101#define CMD5_Q_HALT 0x2
102#define CMD5_Q_MEM_LOCATION 0x4
103#define CMD5_Q_SIZE 0x1F
104#define CMD5_Q_SHIFT 3
105#define COMMANDS_PER_QUEUE 16
106#define QUEUE_SIZE_VAL ((ffs(COMMANDS_PER_QUEUE) - 2) & \
107 CMD5_Q_SIZE)
108#define Q_PTR_MASK (2 << (QUEUE_SIZE_VAL + 5) - 1)
109#define Q_DESC_SIZE sizeof(struct ccp5_desc)
110#define Q_SIZE(n) (COMMANDS_PER_QUEUE*(n))
111
112#define INT_COMPLETION 0x1
113#define INT_ERROR 0x2
114#define INT_QUEUE_STOPPED 0x4
Gary R Hook56467cb2017-04-20 15:24:09 -0500115#define INT_EMPTY_QUEUE 0x8
116#define SUPPORTED_INTERRUPTS (INT_COMPLETION | INT_ERROR)
Gary R Hook4b394a22016-07-26 19:10:21 -0500117
118#define LSB_REGION_WIDTH 5
119#define MAX_LSB_CNT 8
120
121#define LSB_SIZE 16
122#define LSB_ITEM_SIZE 32
123#define PLSB_MAP_SIZE (LSB_SIZE)
124#define SLSB_MAP_SIZE (MAX_LSB_CNT * LSB_SIZE)
125
126#define LSB_ENTRY_NUMBER(LSB_ADDR) (LSB_ADDR / LSB_ITEM_SIZE)
127
128/* ------------------------ CCP Version 3 Specifics ------------------------ */
Tom Lendacky63b94502013-11-12 11:46:16 -0600129#define REQ0_WAIT_FOR_WRITE 0x00000004
130#define REQ0_INT_ON_COMPLETE 0x00000002
131#define REQ0_STOP_ON_COMPLETE 0x00000001
132
133#define REQ0_CMD_Q_SHIFT 9
134#define REQ0_JOBID_SHIFT 3
135
136/****** REQ1 Related Values ******/
137#define REQ1_PROTECT_SHIFT 27
138#define REQ1_ENGINE_SHIFT 23
139#define REQ1_KEY_KSB_SHIFT 2
140
141#define REQ1_EOM 0x00000002
142#define REQ1_INIT 0x00000001
143
144/* AES Related Values */
145#define REQ1_AES_TYPE_SHIFT 21
146#define REQ1_AES_MODE_SHIFT 18
147#define REQ1_AES_ACTION_SHIFT 17
148#define REQ1_AES_CFB_SIZE_SHIFT 10
149
150/* XTS-AES Related Values */
151#define REQ1_XTS_AES_SIZE_SHIFT 10
152
153/* SHA Related Values */
154#define REQ1_SHA_TYPE_SHIFT 21
155
156/* RSA Related Values */
157#define REQ1_RSA_MOD_SIZE_SHIFT 10
158
159/* Pass-Through Related Values */
160#define REQ1_PT_BW_SHIFT 12
161#define REQ1_PT_BS_SHIFT 10
162
163/* ECC Related Values */
164#define REQ1_ECC_AFFINE_CONVERT 0x00200000
165#define REQ1_ECC_FUNCTION_SHIFT 18
166
167/****** REQ4 Related Values ******/
168#define REQ4_KSB_SHIFT 18
169#define REQ4_MEMTYPE_SHIFT 16
170
171/****** REQ6 Related Values ******/
172#define REQ6_MEMTYPE_SHIFT 16
173
Tom Lendacky63b94502013-11-12 11:46:16 -0600174/****** Key Storage Block ******/
175#define KSB_START 77
176#define KSB_END 127
177#define KSB_COUNT (KSB_END - KSB_START + 1)
Gary R Hook956ee212016-07-26 19:09:40 -0500178#define CCP_SB_BITS 256
Tom Lendacky63b94502013-11-12 11:46:16 -0600179
180#define CCP_JOBID_MASK 0x0000003f
181
Gary R Hook4b394a22016-07-26 19:10:21 -0500182/* ------------------------ General CCP Defines ------------------------ */
183
Gary R Hookefc989f2017-03-23 12:53:30 -0500184#define CCP_DMA_DFLT 0x0
185#define CCP_DMA_PRIV 0x1
186#define CCP_DMA_PUB 0x2
187
Tom Lendacky63b94502013-11-12 11:46:16 -0600188#define CCP_DMAPOOL_MAX_SIZE 64
Tom Lendacky8db88462015-02-03 13:07:05 -0600189#define CCP_DMAPOOL_ALIGN BIT(5)
Tom Lendacky63b94502013-11-12 11:46:16 -0600190
191#define CCP_REVERSE_BUF_SIZE 64
192
Gary R Hook956ee212016-07-26 19:09:40 -0500193#define CCP_AES_KEY_SB_COUNT 1
194#define CCP_AES_CTX_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600195
Gary R Hook956ee212016-07-26 19:09:40 -0500196#define CCP_XTS_AES_KEY_SB_COUNT 1
Gary R Hooke6523992017-07-25 14:12:11 -0500197#define CCP5_XTS_AES_KEY_SB_COUNT 2
Gary R Hook956ee212016-07-26 19:09:40 -0500198#define CCP_XTS_AES_CTX_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600199
Gary R Hook990672d2017-03-15 13:20:52 -0500200#define CCP_DES3_KEY_SB_COUNT 1
201#define CCP_DES3_CTX_SB_COUNT 1
202
Gary R Hook956ee212016-07-26 19:09:40 -0500203#define CCP_SHA_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600204
205#define CCP_RSA_MAX_WIDTH 4096
Gary R Hooke28c1902017-07-17 15:16:42 -0500206#define CCP5_RSA_MAX_WIDTH 16384
Tom Lendacky63b94502013-11-12 11:46:16 -0600207
208#define CCP_PASSTHRU_BLOCKSIZE 256
209#define CCP_PASSTHRU_MASKSIZE 32
Gary R Hook956ee212016-07-26 19:09:40 -0500210#define CCP_PASSTHRU_SB_COUNT 1
Tom Lendacky63b94502013-11-12 11:46:16 -0600211
212#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
213#define CCP_ECC_MAX_OPERANDS 6
214#define CCP_ECC_MAX_OUTPUTS 3
215#define CCP_ECC_SRC_BUF_SIZE 448
216#define CCP_ECC_DST_BUF_SIZE 192
217#define CCP_ECC_OPERAND_SIZE 64
218#define CCP_ECC_OUTPUT_SIZE 64
219#define CCP_ECC_RESULT_OFFSET 60
220#define CCP_ECC_RESULT_SUCCESS 0x0001
221
Gary R Hook956ee212016-07-26 19:09:40 -0500222#define CCP_SB_BYTES 32
223
Gary R Hookea0375a2016-03-01 13:49:25 -0600224struct ccp_op;
Tom Lendacky63b94502013-11-12 11:46:16 -0600225struct ccp_device;
226struct ccp_cmd;
Gary R Hook4b394a22016-07-26 19:10:21 -0500227struct ccp_fns;
Tom Lendacky63b94502013-11-12 11:46:16 -0600228
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500229struct ccp_dma_cmd {
230 struct list_head entry;
231
232 struct ccp_cmd ccp_cmd;
233};
234
235struct ccp_dma_desc {
236 struct list_head entry;
237
238 struct ccp_device *ccp;
239
240 struct list_head pending;
241 struct list_head active;
242
243 enum dma_status status;
244 struct dma_async_tx_descriptor tx_desc;
245 size_t len;
246};
247
248struct ccp_dma_chan {
249 struct ccp_device *ccp;
250
251 spinlock_t lock;
Gary R Hooke5da5c52017-01-27 17:09:04 -0600252 struct list_head created;
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500253 struct list_head pending;
254 struct list_head active;
255 struct list_head complete;
256
257 struct tasklet_struct cleanup_tasklet;
258
259 enum dma_status status;
260 struct dma_chan dma_chan;
261};
262
Tom Lendacky63b94502013-11-12 11:46:16 -0600263struct ccp_cmd_queue {
264 struct ccp_device *ccp;
265
266 /* Queue identifier */
267 u32 id;
268
269 /* Queue dma pool */
270 struct dma_pool *dma_pool;
271
Gary R Hook4b394a22016-07-26 19:10:21 -0500272 /* Queue base address (not neccessarily aligned)*/
273 struct ccp5_desc *qbase;
274
275 /* Aligned queue start address (per requirement) */
276 struct mutex q_mutex ____cacheline_aligned;
277 unsigned int qidx;
278
279 /* Version 5 has different requirements for queue memory */
280 unsigned int qsize;
281 dma_addr_t qbase_dma;
282 dma_addr_t qdma_tail;
283
Gary R Hook956ee212016-07-26 19:09:40 -0500284 /* Per-queue reserved storage block(s) */
285 u32 sb_key;
286 u32 sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600287
Gary R Hook4b394a22016-07-26 19:10:21 -0500288 /* Bitmap of LSBs that can be accessed by this queue */
289 DECLARE_BITMAP(lsbmask, MAX_LSB_CNT);
290 /* Private LSB that is assigned to this queue, or -1 if none.
291 * Bitmap for my private LSB, unused otherwise
292 */
Gary R Hook3cf79962016-10-12 08:47:03 -0500293 int lsb;
Gary R Hook4b394a22016-07-26 19:10:21 -0500294 DECLARE_BITMAP(lsbmap, PLSB_MAP_SIZE);
295
Tom Lendacky63b94502013-11-12 11:46:16 -0600296 /* Queue processing thread */
297 struct task_struct *kthread;
298 unsigned int active;
299 unsigned int suspended;
300
301 /* Number of free command slots available */
302 unsigned int free_slots;
303
304 /* Interrupt masks */
305 u32 int_ok;
306 u32 int_err;
307
308 /* Register addresses for queue */
Gary R Hook4b394a22016-07-26 19:10:21 -0500309 void __iomem *reg_control;
310 void __iomem *reg_tail_lo;
311 void __iomem *reg_head_lo;
312 void __iomem *reg_int_enable;
313 void __iomem *reg_interrupt_status;
Tom Lendacky63b94502013-11-12 11:46:16 -0600314 void __iomem *reg_status;
315 void __iomem *reg_int_status;
Gary R Hook4b394a22016-07-26 19:10:21 -0500316 void __iomem *reg_dma_status;
317 void __iomem *reg_dma_read_status;
318 void __iomem *reg_dma_write_status;
319 u32 qcontrol; /* Cached control register */
Tom Lendacky63b94502013-11-12 11:46:16 -0600320
321 /* Status values from job */
322 u32 int_status;
323 u32 q_status;
324 u32 q_int_status;
325 u32 cmd_error;
326
327 /* Interrupt wait queue */
328 wait_queue_head_t int_queue;
329 unsigned int int_rcvd;
Gary R Hook3cdbe342017-05-02 17:33:40 -0500330
331 /* Per-queue Statistics */
332 unsigned long total_ops;
333 unsigned long total_aes_ops;
334 unsigned long total_xts_aes_ops;
335 unsigned long total_3des_ops;
336 unsigned long total_sha_ops;
337 unsigned long total_rsa_ops;
338 unsigned long total_pt_ops;
339 unsigned long total_ecc_ops;
Tom Lendacky63b94502013-11-12 11:46:16 -0600340} ____cacheline_aligned;
341
342struct ccp_device {
Gary R Hook553d2372016-03-01 13:49:04 -0600343 struct list_head entry;
344
Gary R Hookc7019c42016-03-01 13:49:15 -0600345 struct ccp_vdata *vdata;
Gary R Hook553d2372016-03-01 13:49:04 -0600346 unsigned int ord;
347 char name[MAX_CCP_NAME_LEN];
348 char rngname[MAX_CCP_NAME_LEN];
349
Tom Lendacky63b94502013-11-12 11:46:16 -0600350 struct device *dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500351 struct sp_device *sp;
Tom Lendacky63b94502013-11-12 11:46:16 -0600352
Gary R Hookfa242e82016-07-26 18:09:46 -0500353 /* Bus specific device information
Tom Lendacky63b94502013-11-12 11:46:16 -0600354 */
355 void *dev_specific;
Gary R Hook7b537b22017-04-21 10:50:05 -0500356 unsigned int qim;
Tom Lendacky3d775652014-06-05 10:17:45 -0500357 unsigned int irq;
Gary R Hook7b537b22017-04-21 10:50:05 -0500358 bool use_tasklet;
359 struct tasklet_struct irq_tasklet;
Tom Lendacky63b94502013-11-12 11:46:16 -0600360
Gary R Hookfa242e82016-07-26 18:09:46 -0500361 /* I/O area used for device communication. The register mapping
Tom Lendacky63b94502013-11-12 11:46:16 -0600362 * starts at an offset into the mapped bar.
363 * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
364 * need to be protected while a command queue thread is accessing
365 * them.
366 */
367 struct mutex req_mutex ____cacheline_aligned;
Tom Lendacky63b94502013-11-12 11:46:16 -0600368 void __iomem *io_regs;
369
Gary R Hookfa242e82016-07-26 18:09:46 -0500370 /* Master lists that all cmds are queued on. Because there can be
Tom Lendacky63b94502013-11-12 11:46:16 -0600371 * more than one CCP command queue that can process a cmd a separate
372 * backlog list is neeeded so that the backlog completion call
373 * completes before the cmd is available for execution.
374 */
375 spinlock_t cmd_lock ____cacheline_aligned;
376 unsigned int cmd_count;
377 struct list_head cmd;
378 struct list_head backlog;
379
Gary R Hookfa242e82016-07-26 18:09:46 -0500380 /* The command queues. These represent the queues available on the
Tom Lendacky63b94502013-11-12 11:46:16 -0600381 * CCP that are available for processing cmds
382 */
383 struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
384 unsigned int cmd_q_count;
385
Gary R Hookfa242e82016-07-26 18:09:46 -0500386 /* Support for the CCP True RNG
Tom Lendacky63b94502013-11-12 11:46:16 -0600387 */
388 struct hwrng hwrng;
389 unsigned int hwrng_retries;
390
Gary R Hookfa242e82016-07-26 18:09:46 -0500391 /* Support for the CCP DMA capabilities
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500392 */
393 struct dma_device dma_dev;
394 struct ccp_dma_chan *ccp_dma_chan;
395 struct kmem_cache *dma_cmd_cache;
396 struct kmem_cache *dma_desc_cache;
397
Gary R Hookfa242e82016-07-26 18:09:46 -0500398 /* A counter used to generate job-ids for cmds submitted to the CCP
Tom Lendacky63b94502013-11-12 11:46:16 -0600399 */
400 atomic_t current_id ____cacheline_aligned;
401
Gary R Hook58a690b2016-07-26 19:09:50 -0500402 /* The v3 CCP uses key storage blocks (SB) to maintain context for
403 * certain operations. To prevent multiple cmds from using the same
404 * SB range a command queue reserves an SB range for the duration of
405 * the cmd. Each queue, will however, reserve 2 SB blocks for
406 * operations that only require single SB entries (eg. AES context/iv
407 * and key) in order to avoid allocation contention. This will reserve
408 * at most 10 SB entries, leaving 40 SB entries available for dynamic
409 * allocation.
410 *
411 * The v5 CCP Local Storage Block (LSB) is broken up into 8
412 * memrory ranges, each of which can be enabled for access by one
413 * or more queues. Device initialization takes this into account,
414 * and attempts to assign one region for exclusive use by each
415 * available queue; the rest are then aggregated as "public" use.
416 * If there are fewer regions than queues, all regions are shared
417 * amongst all queues.
Tom Lendacky63b94502013-11-12 11:46:16 -0600418 */
Gary R Hook956ee212016-07-26 19:09:40 -0500419 struct mutex sb_mutex ____cacheline_aligned;
420 DECLARE_BITMAP(sb, KSB_COUNT);
421 wait_queue_head_t sb_queue;
422 unsigned int sb_avail;
423 unsigned int sb_count;
424 u32 sb_start;
Tom Lendacky63b94502013-11-12 11:46:16 -0600425
Gary R Hook4b394a22016-07-26 19:10:21 -0500426 /* Bitmap of shared LSBs, if any */
427 DECLARE_BITMAP(lsbmap, SLSB_MAP_SIZE);
428
Tom Lendacky63b94502013-11-12 11:46:16 -0600429 /* Suspend support */
430 unsigned int suspending;
431 wait_queue_head_t suspend_queue;
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500432
433 /* DMA caching attribute support */
434 unsigned int axcache;
Gary R Hook3cdbe342017-05-02 17:33:40 -0500435
436 /* Device Statistics */
437 unsigned long total_interrupts;
438
439 /* DebugFS info */
440 struct dentry *debugfs_instance;
Tom Lendacky63b94502013-11-12 11:46:16 -0600441};
442
Gary R Hookea0375a2016-03-01 13:49:25 -0600443enum ccp_memtype {
444 CCP_MEMTYPE_SYSTEM = 0,
Gary R Hook956ee212016-07-26 19:09:40 -0500445 CCP_MEMTYPE_SB,
Gary R Hookea0375a2016-03-01 13:49:25 -0600446 CCP_MEMTYPE_LOCAL,
447 CCP_MEMTYPE__LAST,
448};
Gary R Hook4b394a22016-07-26 19:10:21 -0500449#define CCP_MEMTYPE_LSB CCP_MEMTYPE_KSB
Gary R Hookea0375a2016-03-01 13:49:25 -0600450
Gary R Hook2d158392017-03-28 10:57:26 -0500451
Gary R Hookea0375a2016-03-01 13:49:25 -0600452struct ccp_dma_info {
453 dma_addr_t address;
454 unsigned int offset;
455 unsigned int length;
456 enum dma_data_direction dir;
Gary R Hook2d158392017-03-28 10:57:26 -0500457} __packed __aligned(4);
Gary R Hookea0375a2016-03-01 13:49:25 -0600458
459struct ccp_dm_workarea {
460 struct device *dev;
461 struct dma_pool *dma_pool;
Gary R Hookea0375a2016-03-01 13:49:25 -0600462
463 u8 *address;
464 struct ccp_dma_info dma;
Gary R Hook2d158392017-03-28 10:57:26 -0500465 unsigned int length;
Gary R Hookea0375a2016-03-01 13:49:25 -0600466};
467
468struct ccp_sg_workarea {
469 struct scatterlist *sg;
470 int nents;
Gary R Hook2d158392017-03-28 10:57:26 -0500471 unsigned int sg_used;
Gary R Hookea0375a2016-03-01 13:49:25 -0600472
473 struct scatterlist *dma_sg;
474 struct device *dma_dev;
475 unsigned int dma_count;
476 enum dma_data_direction dma_dir;
477
Gary R Hookea0375a2016-03-01 13:49:25 -0600478 u64 bytes_left;
479};
480
481struct ccp_data {
482 struct ccp_sg_workarea sg_wa;
483 struct ccp_dm_workarea dm_wa;
484};
485
486struct ccp_mem {
487 enum ccp_memtype type;
488 union {
489 struct ccp_dma_info dma;
Gary R Hook956ee212016-07-26 19:09:40 -0500490 u32 sb;
Gary R Hookea0375a2016-03-01 13:49:25 -0600491 } u;
492};
493
494struct ccp_aes_op {
495 enum ccp_aes_type type;
496 enum ccp_aes_mode mode;
497 enum ccp_aes_action action;
Gary R Hookf7cc02b32017-02-08 13:07:06 -0600498 unsigned int size;
Gary R Hookea0375a2016-03-01 13:49:25 -0600499};
500
501struct ccp_xts_aes_op {
Gary R Hooke6523992017-07-25 14:12:11 -0500502 enum ccp_aes_type type;
Gary R Hookea0375a2016-03-01 13:49:25 -0600503 enum ccp_aes_action action;
504 enum ccp_xts_aes_unit_size unit_size;
505};
506
Gary R Hook990672d2017-03-15 13:20:52 -0500507struct ccp_des3_op {
508 enum ccp_des3_type type;
509 enum ccp_des3_mode mode;
510 enum ccp_des3_action action;
511};
512
Gary R Hookea0375a2016-03-01 13:49:25 -0600513struct ccp_sha_op {
514 enum ccp_sha_type type;
515 u64 msg_bits;
516};
517
518struct ccp_rsa_op {
519 u32 mod_size;
520 u32 input_len;
521};
522
523struct ccp_passthru_op {
524 enum ccp_passthru_bitwise bit_mod;
525 enum ccp_passthru_byteswap byte_swap;
526};
527
528struct ccp_ecc_op {
529 enum ccp_ecc_function function;
530};
531
532struct ccp_op {
533 struct ccp_cmd_queue *cmd_q;
534
535 u32 jobid;
536 u32 ioc;
537 u32 soc;
Gary R Hook956ee212016-07-26 19:09:40 -0500538 u32 sb_key;
539 u32 sb_ctx;
Gary R Hookea0375a2016-03-01 13:49:25 -0600540 u32 init;
541 u32 eom;
542
543 struct ccp_mem src;
544 struct ccp_mem dst;
Gary R Hook4b394a22016-07-26 19:10:21 -0500545 struct ccp_mem exp;
Gary R Hookea0375a2016-03-01 13:49:25 -0600546
547 union {
548 struct ccp_aes_op aes;
549 struct ccp_xts_aes_op xts;
Gary R Hook990672d2017-03-15 13:20:52 -0500550 struct ccp_des3_op des3;
Gary R Hookea0375a2016-03-01 13:49:25 -0600551 struct ccp_sha_op sha;
552 struct ccp_rsa_op rsa;
553 struct ccp_passthru_op passthru;
554 struct ccp_ecc_op ecc;
555 } u;
556};
557
558static inline u32 ccp_addr_lo(struct ccp_dma_info *info)
559{
560 return lower_32_bits(info->address + info->offset);
561}
562
563static inline u32 ccp_addr_hi(struct ccp_dma_info *info)
564{
565 return upper_32_bits(info->address + info->offset) & 0x0000ffff;
566}
567
Gary R Hook4b394a22016-07-26 19:10:21 -0500568/**
569 * descriptor for version 5 CPP commands
570 * 8 32-bit words:
571 * word 0: function; engine; control bits
572 * word 1: length of source data
573 * word 2: low 32 bits of source pointer
574 * word 3: upper 16 bits of source pointer; source memory type
575 * word 4: low 32 bits of destination pointer
576 * word 5: upper 16 bits of destination pointer; destination memory type
577 * word 6: low 32 bits of key pointer
578 * word 7: upper 16 bits of key pointer; key memory type
579 */
580struct dword0 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500581 unsigned int soc:1;
582 unsigned int ioc:1;
583 unsigned int rsvd1:1;
584 unsigned int init:1;
585 unsigned int eom:1; /* AES/SHA only */
586 unsigned int function:15;
587 unsigned int engine:4;
588 unsigned int prot:1;
589 unsigned int rsvd2:7;
Gary R Hook4b394a22016-07-26 19:10:21 -0500590};
591
592struct dword3 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500593 unsigned int src_hi:16;
594 unsigned int src_mem:2;
595 unsigned int lsb_cxt_id:8;
596 unsigned int rsvd1:5;
597 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500598};
599
600union dword4 {
601 __le32 dst_lo; /* NON-SHA */
602 __le32 sha_len_lo; /* SHA */
603};
604
605union dword5 {
606 struct {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500607 unsigned int dst_hi:16;
608 unsigned int dst_mem:2;
609 unsigned int rsvd1:13;
610 unsigned int fixed:1;
Gary R Hook4b394a22016-07-26 19:10:21 -0500611 } fields;
612 __le32 sha_len_hi;
613};
614
615struct dword7 {
Gary R Hookfdd2cf92016-10-18 17:28:35 -0500616 unsigned int key_hi:16;
617 unsigned int key_mem:2;
618 unsigned int rsvd1:14;
Gary R Hook4b394a22016-07-26 19:10:21 -0500619};
620
621struct ccp5_desc {
622 struct dword0 dw0;
623 __le32 length;
624 __le32 src_lo;
625 struct dword3 dw3;
626 union dword4 dw4;
627 union dword5 dw5;
628 __le32 key_lo;
629 struct dword7 dw7;
630};
631
Gary R Hookea0375a2016-03-01 13:49:25 -0600632void ccp_add_device(struct ccp_device *ccp);
633void ccp_del_device(struct ccp_device *ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600634
Gary R Hook81422ba2016-09-28 11:53:56 -0500635extern void ccp_log_error(struct ccp_device *, int);
636
Brijesh Singh720419f2017-07-06 09:59:14 -0500637struct ccp_device *ccp_alloc_struct(struct sp_device *sp);
Gary R Hookea0375a2016-03-01 13:49:25 -0600638bool ccp_queues_suspended(struct ccp_device *ccp);
639int ccp_cmd_queue_thread(void *data);
Gary R Hook8256e682016-07-26 19:10:02 -0500640int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait);
Tom Lendacky63b94502013-11-12 11:46:16 -0600641
642int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
643
Gary R Hook084935b2016-07-26 19:10:31 -0500644int ccp_register_rng(struct ccp_device *ccp);
645void ccp_unregister_rng(struct ccp_device *ccp);
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500646int ccp_dmaengine_register(struct ccp_device *ccp);
647void ccp_dmaengine_unregister(struct ccp_device *ccp);
648
Gary R Hook3cdbe342017-05-02 17:33:40 -0500649void ccp5_debugfs_setup(struct ccp_device *ccp);
650void ccp5_debugfs_destroy(void);
651
Gary R Hook58a690b2016-07-26 19:09:50 -0500652/* Structure for computation functions that are device-specific */
653struct ccp_actions {
654 int (*aes)(struct ccp_op *);
655 int (*xts_aes)(struct ccp_op *);
Gary R Hook990672d2017-03-15 13:20:52 -0500656 int (*des3)(struct ccp_op *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500657 int (*sha)(struct ccp_op *);
658 int (*rsa)(struct ccp_op *);
659 int (*passthru)(struct ccp_op *);
660 int (*ecc)(struct ccp_op *);
661 u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
Gary R Hook990672d2017-03-15 13:20:52 -0500662 void (*sbfree)(struct ccp_cmd_queue *, unsigned int, unsigned int);
Gary R Hookbb4e89b2016-07-26 19:10:13 -0500663 unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
Gary R Hook58a690b2016-07-26 19:09:50 -0500664 int (*init)(struct ccp_device *);
665 void (*destroy)(struct ccp_device *);
666 irqreturn_t (*irqhandler)(int, void *);
667};
668
Brijesh Singh970e8302017-07-06 09:59:13 -0500669extern const struct ccp_vdata ccpv3_platform;
Gary R Hook9ddb9dc2016-09-28 11:53:47 -0500670extern const struct ccp_vdata ccpv3;
671extern const struct ccp_vdata ccpv5a;
672extern const struct ccp_vdata ccpv5b;
Gary R Hook58a690b2016-07-26 19:09:50 -0500673
Tom Lendacky63b94502013-11-12 11:46:16 -0600674#endif