xref: /openbmc/linux/drivers/crypto/ccp/ccp-dev-v3.c (revision 58ea8abf490415c390e0cc671e875510c9b66318)
1ea0375afSGary R Hook /*
2ea0375afSGary R Hook  * AMD Cryptographic Coprocessor (CCP) driver
3ea0375afSGary R Hook  *
4ea0375afSGary R Hook  * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
5ea0375afSGary R Hook  *
6ea0375afSGary R Hook  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7ea0375afSGary R Hook  *
8ea0375afSGary R Hook  * This program is free software; you can redistribute it and/or modify
9ea0375afSGary R Hook  * it under the terms of the GNU General Public License version 2 as
10ea0375afSGary R Hook  * published by the Free Software Foundation.
11ea0375afSGary R Hook  */
12ea0375afSGary R Hook 
13ea0375afSGary R Hook #include <linux/module.h>
14ea0375afSGary R Hook #include <linux/kernel.h>
15ea0375afSGary R Hook #include <linux/pci.h>
16ea0375afSGary R Hook #include <linux/kthread.h>
17ea0375afSGary R Hook #include <linux/interrupt.h>
18ea0375afSGary R Hook #include <linux/ccp.h>
19ea0375afSGary R Hook 
20ea0375afSGary R Hook #include "ccp-dev.h"
21ea0375afSGary R Hook 
22ea0375afSGary R Hook static int ccp_do_cmd(struct ccp_op *op, u32 *cr, unsigned int cr_count)
23ea0375afSGary R Hook {
24ea0375afSGary R Hook 	struct ccp_cmd_queue *cmd_q = op->cmd_q;
25ea0375afSGary R Hook 	struct ccp_device *ccp = cmd_q->ccp;
26ea0375afSGary R Hook 	void __iomem *cr_addr;
27ea0375afSGary R Hook 	u32 cr0, cmd;
28ea0375afSGary R Hook 	unsigned int i;
29ea0375afSGary R Hook 	int ret = 0;
30ea0375afSGary R Hook 
31ea0375afSGary R Hook 	/* We could read a status register to see how many free slots
32ea0375afSGary R Hook 	 * are actually available, but reading that register resets it
33ea0375afSGary R Hook 	 * and you could lose some error information.
34ea0375afSGary R Hook 	 */
35ea0375afSGary R Hook 	cmd_q->free_slots--;
36ea0375afSGary R Hook 
37ea0375afSGary R Hook 	cr0 = (cmd_q->id << REQ0_CMD_Q_SHIFT)
38ea0375afSGary R Hook 	      | (op->jobid << REQ0_JOBID_SHIFT)
39ea0375afSGary R Hook 	      | REQ0_WAIT_FOR_WRITE;
40ea0375afSGary R Hook 
41ea0375afSGary R Hook 	if (op->soc)
42ea0375afSGary R Hook 		cr0 |= REQ0_STOP_ON_COMPLETE
43ea0375afSGary R Hook 		       | REQ0_INT_ON_COMPLETE;
44ea0375afSGary R Hook 
45ea0375afSGary R Hook 	if (op->ioc || !cmd_q->free_slots)
46ea0375afSGary R Hook 		cr0 |= REQ0_INT_ON_COMPLETE;
47ea0375afSGary R Hook 
48ea0375afSGary R Hook 	/* Start at CMD_REQ1 */
49ea0375afSGary R Hook 	cr_addr = ccp->io_regs + CMD_REQ0 + CMD_REQ_INCR;
50ea0375afSGary R Hook 
51ea0375afSGary R Hook 	mutex_lock(&ccp->req_mutex);
52ea0375afSGary R Hook 
53ea0375afSGary R Hook 	/* Write CMD_REQ1 through CMD_REQx first */
54ea0375afSGary R Hook 	for (i = 0; i < cr_count; i++, cr_addr += CMD_REQ_INCR)
55ea0375afSGary R Hook 		iowrite32(*(cr + i), cr_addr);
56ea0375afSGary R Hook 
57ea0375afSGary R Hook 	/* Tell the CCP to start */
58ea0375afSGary R Hook 	wmb();
59ea0375afSGary R Hook 	iowrite32(cr0, ccp->io_regs + CMD_REQ0);
60ea0375afSGary R Hook 
61ea0375afSGary R Hook 	mutex_unlock(&ccp->req_mutex);
62ea0375afSGary R Hook 
63ea0375afSGary R Hook 	if (cr0 & REQ0_INT_ON_COMPLETE) {
64ea0375afSGary R Hook 		/* Wait for the job to complete */
65ea0375afSGary R Hook 		ret = wait_event_interruptible(cmd_q->int_queue,
66ea0375afSGary R Hook 					       cmd_q->int_rcvd);
67ea0375afSGary R Hook 		if (ret || cmd_q->cmd_error) {
68ea0375afSGary R Hook 			/* On error delete all related jobs from the queue */
69ea0375afSGary R Hook 			cmd = (cmd_q->id << DEL_Q_ID_SHIFT)
70ea0375afSGary R Hook 			      | op->jobid;
71ea0375afSGary R Hook 
72ea0375afSGary R Hook 			iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB);
73ea0375afSGary R Hook 
74ea0375afSGary R Hook 			if (!ret)
75ea0375afSGary R Hook 				ret = -EIO;
76ea0375afSGary R Hook 		} else if (op->soc) {
77ea0375afSGary R Hook 			/* Delete just head job from the queue on SoC */
78ea0375afSGary R Hook 			cmd = DEL_Q_ACTIVE
79ea0375afSGary R Hook 			      | (cmd_q->id << DEL_Q_ID_SHIFT)
80ea0375afSGary R Hook 			      | op->jobid;
81ea0375afSGary R Hook 
82ea0375afSGary R Hook 			iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB);
83ea0375afSGary R Hook 		}
84ea0375afSGary R Hook 
85ea0375afSGary R Hook 		cmd_q->free_slots = CMD_Q_DEPTH(cmd_q->q_status);
86ea0375afSGary R Hook 
87ea0375afSGary R Hook 		cmd_q->int_rcvd = 0;
88ea0375afSGary R Hook 	}
89ea0375afSGary R Hook 
90ea0375afSGary R Hook 	return ret;
91ea0375afSGary R Hook }
92ea0375afSGary R Hook 
93ea0375afSGary R Hook static int ccp_perform_aes(struct ccp_op *op)
94ea0375afSGary R Hook {
95ea0375afSGary R Hook 	u32 cr[6];
96ea0375afSGary R Hook 
97ea0375afSGary R Hook 	/* Fill out the register contents for REQ1 through REQ6 */
98ea0375afSGary R Hook 	cr[0] = (CCP_ENGINE_AES << REQ1_ENGINE_SHIFT)
99ea0375afSGary R Hook 		| (op->u.aes.type << REQ1_AES_TYPE_SHIFT)
100ea0375afSGary R Hook 		| (op->u.aes.mode << REQ1_AES_MODE_SHIFT)
101ea0375afSGary R Hook 		| (op->u.aes.action << REQ1_AES_ACTION_SHIFT)
102ea0375afSGary R Hook 		| (op->ksb_key << REQ1_KEY_KSB_SHIFT);
103ea0375afSGary R Hook 	cr[1] = op->src.u.dma.length - 1;
104ea0375afSGary R Hook 	cr[2] = ccp_addr_lo(&op->src.u.dma);
105ea0375afSGary R Hook 	cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
106ea0375afSGary R Hook 		| (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
107ea0375afSGary R Hook 		| ccp_addr_hi(&op->src.u.dma);
108ea0375afSGary R Hook 	cr[4] = ccp_addr_lo(&op->dst.u.dma);
109ea0375afSGary R Hook 	cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
110ea0375afSGary R Hook 		| ccp_addr_hi(&op->dst.u.dma);
111ea0375afSGary R Hook 
112ea0375afSGary R Hook 	if (op->u.aes.mode == CCP_AES_MODE_CFB)
113ea0375afSGary R Hook 		cr[0] |= ((0x7f) << REQ1_AES_CFB_SIZE_SHIFT);
114ea0375afSGary R Hook 
115ea0375afSGary R Hook 	if (op->eom)
116ea0375afSGary R Hook 		cr[0] |= REQ1_EOM;
117ea0375afSGary R Hook 
118ea0375afSGary R Hook 	if (op->init)
119ea0375afSGary R Hook 		cr[0] |= REQ1_INIT;
120ea0375afSGary R Hook 
121ea0375afSGary R Hook 	return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
122ea0375afSGary R Hook }
123ea0375afSGary R Hook 
124ea0375afSGary R Hook static int ccp_perform_xts_aes(struct ccp_op *op)
125ea0375afSGary R Hook {
126ea0375afSGary R Hook 	u32 cr[6];
127ea0375afSGary R Hook 
128ea0375afSGary R Hook 	/* Fill out the register contents for REQ1 through REQ6 */
129ea0375afSGary R Hook 	cr[0] = (CCP_ENGINE_XTS_AES_128 << REQ1_ENGINE_SHIFT)
130ea0375afSGary R Hook 		| (op->u.xts.action << REQ1_AES_ACTION_SHIFT)
131ea0375afSGary R Hook 		| (op->u.xts.unit_size << REQ1_XTS_AES_SIZE_SHIFT)
132ea0375afSGary R Hook 		| (op->ksb_key << REQ1_KEY_KSB_SHIFT);
133ea0375afSGary R Hook 	cr[1] = op->src.u.dma.length - 1;
134ea0375afSGary R Hook 	cr[2] = ccp_addr_lo(&op->src.u.dma);
135ea0375afSGary R Hook 	cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
136ea0375afSGary R Hook 		| (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
137ea0375afSGary R Hook 		| ccp_addr_hi(&op->src.u.dma);
138ea0375afSGary R Hook 	cr[4] = ccp_addr_lo(&op->dst.u.dma);
139ea0375afSGary R Hook 	cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
140ea0375afSGary R Hook 		| ccp_addr_hi(&op->dst.u.dma);
141ea0375afSGary R Hook 
142ea0375afSGary R Hook 	if (op->eom)
143ea0375afSGary R Hook 		cr[0] |= REQ1_EOM;
144ea0375afSGary R Hook 
145ea0375afSGary R Hook 	if (op->init)
146ea0375afSGary R Hook 		cr[0] |= REQ1_INIT;
147ea0375afSGary R Hook 
148ea0375afSGary R Hook 	return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
149ea0375afSGary R Hook }
150ea0375afSGary R Hook 
151ea0375afSGary R Hook static int ccp_perform_sha(struct ccp_op *op)
152ea0375afSGary R Hook {
153ea0375afSGary R Hook 	u32 cr[6];
154ea0375afSGary R Hook 
155ea0375afSGary R Hook 	/* Fill out the register contents for REQ1 through REQ6 */
156ea0375afSGary R Hook 	cr[0] = (CCP_ENGINE_SHA << REQ1_ENGINE_SHIFT)
157ea0375afSGary R Hook 		| (op->u.sha.type << REQ1_SHA_TYPE_SHIFT)
158ea0375afSGary R Hook 		| REQ1_INIT;
159ea0375afSGary R Hook 	cr[1] = op->src.u.dma.length - 1;
160ea0375afSGary R Hook 	cr[2] = ccp_addr_lo(&op->src.u.dma);
161ea0375afSGary R Hook 	cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
162ea0375afSGary R Hook 		| (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
163ea0375afSGary R Hook 		| ccp_addr_hi(&op->src.u.dma);
164ea0375afSGary R Hook 
165ea0375afSGary R Hook 	if (op->eom) {
166ea0375afSGary R Hook 		cr[0] |= REQ1_EOM;
167ea0375afSGary R Hook 		cr[4] = lower_32_bits(op->u.sha.msg_bits);
168ea0375afSGary R Hook 		cr[5] = upper_32_bits(op->u.sha.msg_bits);
169ea0375afSGary R Hook 	} else {
170ea0375afSGary R Hook 		cr[4] = 0;
171ea0375afSGary R Hook 		cr[5] = 0;
172ea0375afSGary R Hook 	}
173ea0375afSGary R Hook 
174ea0375afSGary R Hook 	return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
175ea0375afSGary R Hook }
176ea0375afSGary R Hook 
177ea0375afSGary R Hook static int ccp_perform_rsa(struct ccp_op *op)
178ea0375afSGary R Hook {
179ea0375afSGary R Hook 	u32 cr[6];
180ea0375afSGary R Hook 
181ea0375afSGary R Hook 	/* Fill out the register contents for REQ1 through REQ6 */
182ea0375afSGary R Hook 	cr[0] = (CCP_ENGINE_RSA << REQ1_ENGINE_SHIFT)
183ea0375afSGary R Hook 		| (op->u.rsa.mod_size << REQ1_RSA_MOD_SIZE_SHIFT)
184ea0375afSGary R Hook 		| (op->ksb_key << REQ1_KEY_KSB_SHIFT)
185ea0375afSGary R Hook 		| REQ1_EOM;
186ea0375afSGary R Hook 	cr[1] = op->u.rsa.input_len - 1;
187ea0375afSGary R Hook 	cr[2] = ccp_addr_lo(&op->src.u.dma);
188ea0375afSGary R Hook 	cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
189ea0375afSGary R Hook 		| (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
190ea0375afSGary R Hook 		| ccp_addr_hi(&op->src.u.dma);
191ea0375afSGary R Hook 	cr[4] = ccp_addr_lo(&op->dst.u.dma);
192ea0375afSGary R Hook 	cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
193ea0375afSGary R Hook 		| ccp_addr_hi(&op->dst.u.dma);
194ea0375afSGary R Hook 
195ea0375afSGary R Hook 	return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
196ea0375afSGary R Hook }
197ea0375afSGary R Hook 
198ea0375afSGary R Hook static int ccp_perform_passthru(struct ccp_op *op)
199ea0375afSGary R Hook {
200ea0375afSGary R Hook 	u32 cr[6];
201ea0375afSGary R Hook 
202ea0375afSGary R Hook 	/* Fill out the register contents for REQ1 through REQ6 */
203ea0375afSGary R Hook 	cr[0] = (CCP_ENGINE_PASSTHRU << REQ1_ENGINE_SHIFT)
204ea0375afSGary R Hook 		| (op->u.passthru.bit_mod << REQ1_PT_BW_SHIFT)
205ea0375afSGary R Hook 		| (op->u.passthru.byte_swap << REQ1_PT_BS_SHIFT);
206ea0375afSGary R Hook 
207ea0375afSGary R Hook 	if (op->src.type == CCP_MEMTYPE_SYSTEM)
208ea0375afSGary R Hook 		cr[1] = op->src.u.dma.length - 1;
209ea0375afSGary R Hook 	else
210ea0375afSGary R Hook 		cr[1] = op->dst.u.dma.length - 1;
211ea0375afSGary R Hook 
212ea0375afSGary R Hook 	if (op->src.type == CCP_MEMTYPE_SYSTEM) {
213ea0375afSGary R Hook 		cr[2] = ccp_addr_lo(&op->src.u.dma);
214ea0375afSGary R Hook 		cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
215ea0375afSGary R Hook 			| ccp_addr_hi(&op->src.u.dma);
216ea0375afSGary R Hook 
217ea0375afSGary R Hook 		if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
218ea0375afSGary R Hook 			cr[3] |= (op->ksb_key << REQ4_KSB_SHIFT);
219ea0375afSGary R Hook 	} else {
220ea0375afSGary R Hook 		cr[2] = op->src.u.ksb * CCP_KSB_BYTES;
221ea0375afSGary R Hook 		cr[3] = (CCP_MEMTYPE_KSB << REQ4_MEMTYPE_SHIFT);
222ea0375afSGary R Hook 	}
223ea0375afSGary R Hook 
224ea0375afSGary R Hook 	if (op->dst.type == CCP_MEMTYPE_SYSTEM) {
225ea0375afSGary R Hook 		cr[4] = ccp_addr_lo(&op->dst.u.dma);
226ea0375afSGary R Hook 		cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
227ea0375afSGary R Hook 			| ccp_addr_hi(&op->dst.u.dma);
228ea0375afSGary R Hook 	} else {
229ea0375afSGary R Hook 		cr[4] = op->dst.u.ksb * CCP_KSB_BYTES;
230ea0375afSGary R Hook 		cr[5] = (CCP_MEMTYPE_KSB << REQ6_MEMTYPE_SHIFT);
231ea0375afSGary R Hook 	}
232ea0375afSGary R Hook 
233ea0375afSGary R Hook 	if (op->eom)
234ea0375afSGary R Hook 		cr[0] |= REQ1_EOM;
235ea0375afSGary R Hook 
236ea0375afSGary R Hook 	return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
237ea0375afSGary R Hook }
238ea0375afSGary R Hook 
239ea0375afSGary R Hook static int ccp_perform_ecc(struct ccp_op *op)
240ea0375afSGary R Hook {
241ea0375afSGary R Hook 	u32 cr[6];
242ea0375afSGary R Hook 
243ea0375afSGary R Hook 	/* Fill out the register contents for REQ1 through REQ6 */
244ea0375afSGary R Hook 	cr[0] = REQ1_ECC_AFFINE_CONVERT
245ea0375afSGary R Hook 		| (CCP_ENGINE_ECC << REQ1_ENGINE_SHIFT)
246ea0375afSGary R Hook 		| (op->u.ecc.function << REQ1_ECC_FUNCTION_SHIFT)
247ea0375afSGary R Hook 		| REQ1_EOM;
248ea0375afSGary R Hook 	cr[1] = op->src.u.dma.length - 1;
249ea0375afSGary R Hook 	cr[2] = ccp_addr_lo(&op->src.u.dma);
250ea0375afSGary R Hook 	cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
251ea0375afSGary R Hook 		| ccp_addr_hi(&op->src.u.dma);
252ea0375afSGary R Hook 	cr[4] = ccp_addr_lo(&op->dst.u.dma);
253ea0375afSGary R Hook 	cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
254ea0375afSGary R Hook 		| ccp_addr_hi(&op->dst.u.dma);
255ea0375afSGary R Hook 
256ea0375afSGary R Hook 	return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
257ea0375afSGary R Hook }
258ea0375afSGary R Hook 
259ea0375afSGary R Hook static int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
260ea0375afSGary R Hook {
261ea0375afSGary R Hook 	struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng);
262ea0375afSGary R Hook 	u32 trng_value;
263ea0375afSGary R Hook 	int len = min_t(int, sizeof(trng_value), max);
264ea0375afSGary R Hook 
265ea0375afSGary R Hook 	/*
266ea0375afSGary R Hook 	 * Locking is provided by the caller so we can update device
267ea0375afSGary R Hook 	 * hwrng-related fields safely
268ea0375afSGary R Hook 	 */
269ea0375afSGary R Hook 	trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG);
270ea0375afSGary R Hook 	if (!trng_value) {
271ea0375afSGary R Hook 		/* Zero is returned if not data is available or if a
272ea0375afSGary R Hook 		 * bad-entropy error is present. Assume an error if
273ea0375afSGary R Hook 		 * we exceed TRNG_RETRIES reads of zero.
274ea0375afSGary R Hook 		 */
275ea0375afSGary R Hook 		if (ccp->hwrng_retries++ > TRNG_RETRIES)
276ea0375afSGary R Hook 			return -EIO;
277ea0375afSGary R Hook 
278ea0375afSGary R Hook 		return 0;
279ea0375afSGary R Hook 	}
280ea0375afSGary R Hook 
281ea0375afSGary R Hook 	/* Reset the counter and save the rng value */
282ea0375afSGary R Hook 	ccp->hwrng_retries = 0;
283ea0375afSGary R Hook 	memcpy(data, &trng_value, len);
284ea0375afSGary R Hook 
285ea0375afSGary R Hook 	return len;
286ea0375afSGary R Hook }
287ea0375afSGary R Hook 
288ea0375afSGary R Hook static int ccp_init(struct ccp_device *ccp)
289ea0375afSGary R Hook {
290ea0375afSGary R Hook 	struct device *dev = ccp->dev;
291ea0375afSGary R Hook 	struct ccp_cmd_queue *cmd_q;
292ea0375afSGary R Hook 	struct dma_pool *dma_pool;
293ea0375afSGary R Hook 	char dma_pool_name[MAX_DMAPOOL_NAME_LEN];
294ea0375afSGary R Hook 	unsigned int qmr, qim, i;
295ea0375afSGary R Hook 	int ret;
296ea0375afSGary R Hook 
297ea0375afSGary R Hook 	/* Find available queues */
298ea0375afSGary R Hook 	qim = 0;
299ea0375afSGary R Hook 	qmr = ioread32(ccp->io_regs + Q_MASK_REG);
300ea0375afSGary R Hook 	for (i = 0; i < MAX_HW_QUEUES; i++) {
301ea0375afSGary R Hook 		if (!(qmr & (1 << i)))
302ea0375afSGary R Hook 			continue;
303ea0375afSGary R Hook 
304ea0375afSGary R Hook 		/* Allocate a dma pool for this queue */
305ea0375afSGary R Hook 		snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d",
306ea0375afSGary R Hook 			 ccp->name, i);
307ea0375afSGary R Hook 		dma_pool = dma_pool_create(dma_pool_name, dev,
308ea0375afSGary R Hook 					   CCP_DMAPOOL_MAX_SIZE,
309ea0375afSGary R Hook 					   CCP_DMAPOOL_ALIGN, 0);
310ea0375afSGary R Hook 		if (!dma_pool) {
311ea0375afSGary R Hook 			dev_err(dev, "unable to allocate dma pool\n");
312ea0375afSGary R Hook 			ret = -ENOMEM;
313ea0375afSGary R Hook 			goto e_pool;
314ea0375afSGary R Hook 		}
315ea0375afSGary R Hook 
316ea0375afSGary R Hook 		cmd_q = &ccp->cmd_q[ccp->cmd_q_count];
317ea0375afSGary R Hook 		ccp->cmd_q_count++;
318ea0375afSGary R Hook 
319ea0375afSGary R Hook 		cmd_q->ccp = ccp;
320ea0375afSGary R Hook 		cmd_q->id = i;
321ea0375afSGary R Hook 		cmd_q->dma_pool = dma_pool;
322ea0375afSGary R Hook 
323ea0375afSGary R Hook 		/* Reserve 2 KSB regions for the queue */
324ea0375afSGary R Hook 		cmd_q->ksb_key = KSB_START + ccp->ksb_start++;
325ea0375afSGary R Hook 		cmd_q->ksb_ctx = KSB_START + ccp->ksb_start++;
326ea0375afSGary R Hook 		ccp->ksb_count -= 2;
327ea0375afSGary R Hook 
328ea0375afSGary R Hook 		/* Preset some register values and masks that are queue
329ea0375afSGary R Hook 		 * number dependent
330ea0375afSGary R Hook 		 */
331ea0375afSGary R Hook 		cmd_q->reg_status = ccp->io_regs + CMD_Q_STATUS_BASE +
332ea0375afSGary R Hook 				    (CMD_Q_STATUS_INCR * i);
333ea0375afSGary R Hook 		cmd_q->reg_int_status = ccp->io_regs + CMD_Q_INT_STATUS_BASE +
334ea0375afSGary R Hook 					(CMD_Q_STATUS_INCR * i);
335ea0375afSGary R Hook 		cmd_q->int_ok = 1 << (i * 2);
336ea0375afSGary R Hook 		cmd_q->int_err = 1 << ((i * 2) + 1);
337ea0375afSGary R Hook 
338ea0375afSGary R Hook 		cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
339ea0375afSGary R Hook 
340ea0375afSGary R Hook 		init_waitqueue_head(&cmd_q->int_queue);
341ea0375afSGary R Hook 
342ea0375afSGary R Hook 		/* Build queue interrupt mask (two interrupts per queue) */
343ea0375afSGary R Hook 		qim |= cmd_q->int_ok | cmd_q->int_err;
344ea0375afSGary R Hook 
345ea0375afSGary R Hook #ifdef CONFIG_ARM64
346ea0375afSGary R Hook 		/* For arm64 set the recommended queue cache settings */
347ea0375afSGary R Hook 		iowrite32(ccp->axcache, ccp->io_regs + CMD_Q_CACHE_BASE +
348ea0375afSGary R Hook 			  (CMD_Q_CACHE_INC * i));
349ea0375afSGary R Hook #endif
350ea0375afSGary R Hook 
351ea0375afSGary R Hook 		dev_dbg(dev, "queue #%u available\n", i);
352ea0375afSGary R Hook 	}
353ea0375afSGary R Hook 	if (ccp->cmd_q_count == 0) {
354ea0375afSGary R Hook 		dev_notice(dev, "no command queues available\n");
355ea0375afSGary R Hook 		ret = -EIO;
356ea0375afSGary R Hook 		goto e_pool;
357ea0375afSGary R Hook 	}
358ea0375afSGary R Hook 	dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count);
359ea0375afSGary R Hook 
360ea0375afSGary R Hook 	/* Disable and clear interrupts until ready */
361ea0375afSGary R Hook 	iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG);
362ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++) {
363ea0375afSGary R Hook 		cmd_q = &ccp->cmd_q[i];
364ea0375afSGary R Hook 
365ea0375afSGary R Hook 		ioread32(cmd_q->reg_int_status);
366ea0375afSGary R Hook 		ioread32(cmd_q->reg_status);
367ea0375afSGary R Hook 	}
368ea0375afSGary R Hook 	iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG);
369ea0375afSGary R Hook 
370ea0375afSGary R Hook 	/* Request an irq */
371ea0375afSGary R Hook 	ret = ccp->get_irq(ccp);
372ea0375afSGary R Hook 	if (ret) {
373ea0375afSGary R Hook 		dev_err(dev, "unable to allocate an IRQ\n");
374ea0375afSGary R Hook 		goto e_pool;
375ea0375afSGary R Hook 	}
376ea0375afSGary R Hook 
377ea0375afSGary R Hook 	/* Initialize the queues used to wait for KSB space and suspend */
378ea0375afSGary R Hook 	init_waitqueue_head(&ccp->ksb_queue);
379ea0375afSGary R Hook 	init_waitqueue_head(&ccp->suspend_queue);
380ea0375afSGary R Hook 
381ea0375afSGary R Hook 	/* Create a kthread for each queue */
382ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++) {
383ea0375afSGary R Hook 		struct task_struct *kthread;
384ea0375afSGary R Hook 
385ea0375afSGary R Hook 		cmd_q = &ccp->cmd_q[i];
386ea0375afSGary R Hook 
387ea0375afSGary R Hook 		kthread = kthread_create(ccp_cmd_queue_thread, cmd_q,
388ea0375afSGary R Hook 					 "%s-q%u", ccp->name, cmd_q->id);
389ea0375afSGary R Hook 		if (IS_ERR(kthread)) {
390ea0375afSGary R Hook 			dev_err(dev, "error creating queue thread (%ld)\n",
391ea0375afSGary R Hook 				PTR_ERR(kthread));
392ea0375afSGary R Hook 			ret = PTR_ERR(kthread);
393ea0375afSGary R Hook 			goto e_kthread;
394ea0375afSGary R Hook 		}
395ea0375afSGary R Hook 
396ea0375afSGary R Hook 		cmd_q->kthread = kthread;
397ea0375afSGary R Hook 		wake_up_process(kthread);
398ea0375afSGary R Hook 	}
399ea0375afSGary R Hook 
400ea0375afSGary R Hook 	/* Register the RNG */
401ea0375afSGary R Hook 	ccp->hwrng.name = ccp->rngname;
402ea0375afSGary R Hook 	ccp->hwrng.read = ccp_trng_read;
403ea0375afSGary R Hook 	ret = hwrng_register(&ccp->hwrng);
404ea0375afSGary R Hook 	if (ret) {
405ea0375afSGary R Hook 		dev_err(dev, "error registering hwrng (%d)\n", ret);
406ea0375afSGary R Hook 		goto e_kthread;
407ea0375afSGary R Hook 	}
408ea0375afSGary R Hook 
409*58ea8abfSGary R Hook 	/* Register the DMA engine support */
410*58ea8abfSGary R Hook 	ret = ccp_dmaengine_register(ccp);
411*58ea8abfSGary R Hook 	if (ret)
412*58ea8abfSGary R Hook 		goto e_hwrng;
413*58ea8abfSGary R Hook 
414ea0375afSGary R Hook 	ccp_add_device(ccp);
415ea0375afSGary R Hook 
416ea0375afSGary R Hook 	/* Enable interrupts */
417ea0375afSGary R Hook 	iowrite32(qim, ccp->io_regs + IRQ_MASK_REG);
418ea0375afSGary R Hook 
419ea0375afSGary R Hook 	return 0;
420ea0375afSGary R Hook 
421*58ea8abfSGary R Hook e_hwrng:
422*58ea8abfSGary R Hook 	hwrng_unregister(&ccp->hwrng);
423*58ea8abfSGary R Hook 
424ea0375afSGary R Hook e_kthread:
425ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++)
426ea0375afSGary R Hook 		if (ccp->cmd_q[i].kthread)
427ea0375afSGary R Hook 			kthread_stop(ccp->cmd_q[i].kthread);
428ea0375afSGary R Hook 
429ea0375afSGary R Hook 	ccp->free_irq(ccp);
430ea0375afSGary R Hook 
431ea0375afSGary R Hook e_pool:
432ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++)
433ea0375afSGary R Hook 		dma_pool_destroy(ccp->cmd_q[i].dma_pool);
434ea0375afSGary R Hook 
435ea0375afSGary R Hook 	return ret;
436ea0375afSGary R Hook }
437ea0375afSGary R Hook 
438ea0375afSGary R Hook static void ccp_destroy(struct ccp_device *ccp)
439ea0375afSGary R Hook {
440ea0375afSGary R Hook 	struct ccp_cmd_queue *cmd_q;
441ea0375afSGary R Hook 	struct ccp_cmd *cmd;
442ea0375afSGary R Hook 	unsigned int qim, i;
443ea0375afSGary R Hook 
444ea0375afSGary R Hook 	/* Remove this device from the list of available units first */
445ea0375afSGary R Hook 	ccp_del_device(ccp);
446ea0375afSGary R Hook 
447*58ea8abfSGary R Hook 	/* Unregister the DMA engine */
448*58ea8abfSGary R Hook 	ccp_dmaengine_unregister(ccp);
449*58ea8abfSGary R Hook 
450ea0375afSGary R Hook 	/* Unregister the RNG */
451ea0375afSGary R Hook 	hwrng_unregister(&ccp->hwrng);
452ea0375afSGary R Hook 
453ea0375afSGary R Hook 	/* Stop the queue kthreads */
454ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++)
455ea0375afSGary R Hook 		if (ccp->cmd_q[i].kthread)
456ea0375afSGary R Hook 			kthread_stop(ccp->cmd_q[i].kthread);
457ea0375afSGary R Hook 
458ea0375afSGary R Hook 	/* Build queue interrupt mask (two interrupt masks per queue) */
459ea0375afSGary R Hook 	qim = 0;
460ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++) {
461ea0375afSGary R Hook 		cmd_q = &ccp->cmd_q[i];
462ea0375afSGary R Hook 		qim |= cmd_q->int_ok | cmd_q->int_err;
463ea0375afSGary R Hook 	}
464ea0375afSGary R Hook 
465ea0375afSGary R Hook 	/* Disable and clear interrupts */
466ea0375afSGary R Hook 	iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG);
467ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++) {
468ea0375afSGary R Hook 		cmd_q = &ccp->cmd_q[i];
469ea0375afSGary R Hook 
470ea0375afSGary R Hook 		ioread32(cmd_q->reg_int_status);
471ea0375afSGary R Hook 		ioread32(cmd_q->reg_status);
472ea0375afSGary R Hook 	}
473ea0375afSGary R Hook 	iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG);
474ea0375afSGary R Hook 
475ea0375afSGary R Hook 	ccp->free_irq(ccp);
476ea0375afSGary R Hook 
477ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++)
478ea0375afSGary R Hook 		dma_pool_destroy(ccp->cmd_q[i].dma_pool);
479ea0375afSGary R Hook 
480ea0375afSGary R Hook 	/* Flush the cmd and backlog queue */
481ea0375afSGary R Hook 	while (!list_empty(&ccp->cmd)) {
482ea0375afSGary R Hook 		/* Invoke the callback directly with an error code */
483ea0375afSGary R Hook 		cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
484ea0375afSGary R Hook 		list_del(&cmd->entry);
485ea0375afSGary R Hook 		cmd->callback(cmd->data, -ENODEV);
486ea0375afSGary R Hook 	}
487ea0375afSGary R Hook 	while (!list_empty(&ccp->backlog)) {
488ea0375afSGary R Hook 		/* Invoke the callback directly with an error code */
489ea0375afSGary R Hook 		cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry);
490ea0375afSGary R Hook 		list_del(&cmd->entry);
491ea0375afSGary R Hook 		cmd->callback(cmd->data, -ENODEV);
492ea0375afSGary R Hook 	}
493ea0375afSGary R Hook }
494ea0375afSGary R Hook 
495ea0375afSGary R Hook static irqreturn_t ccp_irq_handler(int irq, void *data)
496ea0375afSGary R Hook {
497ea0375afSGary R Hook 	struct device *dev = data;
498ea0375afSGary R Hook 	struct ccp_device *ccp = dev_get_drvdata(dev);
499ea0375afSGary R Hook 	struct ccp_cmd_queue *cmd_q;
500ea0375afSGary R Hook 	u32 q_int, status;
501ea0375afSGary R Hook 	unsigned int i;
502ea0375afSGary R Hook 
503ea0375afSGary R Hook 	status = ioread32(ccp->io_regs + IRQ_STATUS_REG);
504ea0375afSGary R Hook 
505ea0375afSGary R Hook 	for (i = 0; i < ccp->cmd_q_count; i++) {
506ea0375afSGary R Hook 		cmd_q = &ccp->cmd_q[i];
507ea0375afSGary R Hook 
508ea0375afSGary R Hook 		q_int = status & (cmd_q->int_ok | cmd_q->int_err);
509ea0375afSGary R Hook 		if (q_int) {
510ea0375afSGary R Hook 			cmd_q->int_status = status;
511ea0375afSGary R Hook 			cmd_q->q_status = ioread32(cmd_q->reg_status);
512ea0375afSGary R Hook 			cmd_q->q_int_status = ioread32(cmd_q->reg_int_status);
513ea0375afSGary R Hook 
514ea0375afSGary R Hook 			/* On error, only save the first error value */
515ea0375afSGary R Hook 			if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error)
516ea0375afSGary R Hook 				cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
517ea0375afSGary R Hook 
518ea0375afSGary R Hook 			cmd_q->int_rcvd = 1;
519ea0375afSGary R Hook 
520ea0375afSGary R Hook 			/* Acknowledge the interrupt and wake the kthread */
521ea0375afSGary R Hook 			iowrite32(q_int, ccp->io_regs + IRQ_STATUS_REG);
522ea0375afSGary R Hook 			wake_up_interruptible(&cmd_q->int_queue);
523ea0375afSGary R Hook 		}
524ea0375afSGary R Hook 	}
525ea0375afSGary R Hook 
526ea0375afSGary R Hook 	return IRQ_HANDLED;
527ea0375afSGary R Hook }
528ea0375afSGary R Hook 
529ea0375afSGary R Hook static struct ccp_actions ccp3_actions = {
530ea0375afSGary R Hook 	.perform_aes = ccp_perform_aes,
531ea0375afSGary R Hook 	.perform_xts_aes = ccp_perform_xts_aes,
532ea0375afSGary R Hook 	.perform_sha = ccp_perform_sha,
533ea0375afSGary R Hook 	.perform_rsa = ccp_perform_rsa,
534ea0375afSGary R Hook 	.perform_passthru = ccp_perform_passthru,
535ea0375afSGary R Hook 	.perform_ecc = ccp_perform_ecc,
536ea0375afSGary R Hook 	.init = ccp_init,
537ea0375afSGary R Hook 	.destroy = ccp_destroy,
538ea0375afSGary R Hook 	.irqhandler = ccp_irq_handler,
539ea0375afSGary R Hook };
540ea0375afSGary R Hook 
541ea0375afSGary R Hook struct ccp_vdata ccpv3 = {
542ea0375afSGary R Hook 	.version = CCP_VERSION(3, 0),
543ea0375afSGary R Hook 	.perform = &ccp3_actions,
544ea0375afSGary R Hook };
545