blob: 9c6ff8b813d966e86f3aa0e56116fcdb6ecee203 [file] [log] [blame]
Gary R Hook4b394a22016-07-26 19:10:21 -05001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
4 * Copyright (C) 2016 Advanced Micro Devices, Inc.
5 *
6 * Author: Gary R Hook <gary.hook@amd.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/kthread.h>
17#include <linux/dma-mapping.h>
18#include <linux/interrupt.h>
19#include <linux/compiler.h>
20#include <linux/ccp.h>
21
22#include "ccp-dev.h"
23
Gary R Hook103600a2016-10-18 17:33:37 -050024/* Allocate the requested number of contiguous LSB slots
25 * from the LSB bitmap. Look in the private range for this
26 * queue first; failing that, check the public area.
27 * If no space is available, wait around.
28 * Return: first slot number
29 */
Gary R Hook4b394a22016-07-26 19:10:21 -050030static u32 ccp_lsb_alloc(struct ccp_cmd_queue *cmd_q, unsigned int count)
31{
32 struct ccp_device *ccp;
33 int start;
34
35 /* First look at the map for the queue */
36 if (cmd_q->lsb >= 0) {
37 start = (u32)bitmap_find_next_zero_area(cmd_q->lsbmap,
38 LSB_SIZE,
39 0, count, 0);
40 if (start < LSB_SIZE) {
41 bitmap_set(cmd_q->lsbmap, start, count);
42 return start + cmd_q->lsb * LSB_SIZE;
43 }
44 }
45
46 /* No joy; try to get an entry from the shared blocks */
47 ccp = cmd_q->ccp;
48 for (;;) {
49 mutex_lock(&ccp->sb_mutex);
50
51 start = (u32)bitmap_find_next_zero_area(ccp->lsbmap,
52 MAX_LSB_CNT * LSB_SIZE,
53 0,
54 count, 0);
55 if (start <= MAX_LSB_CNT * LSB_SIZE) {
56 bitmap_set(ccp->lsbmap, start, count);
57
58 mutex_unlock(&ccp->sb_mutex);
Gary R Hook103600a2016-10-18 17:33:37 -050059 return start;
Gary R Hook4b394a22016-07-26 19:10:21 -050060 }
61
62 ccp->sb_avail = 0;
63
64 mutex_unlock(&ccp->sb_mutex);
65
66 /* Wait for KSB entries to become available */
67 if (wait_event_interruptible(ccp->sb_queue, ccp->sb_avail))
68 return 0;
69 }
70}
71
Gary R Hook103600a2016-10-18 17:33:37 -050072/* Free a number of LSB slots from the bitmap, starting at
73 * the indicated starting slot number.
74 */
Gary R Hook4b394a22016-07-26 19:10:21 -050075static void ccp_lsb_free(struct ccp_cmd_queue *cmd_q, unsigned int start,
76 unsigned int count)
77{
Gary R Hook4b394a22016-07-26 19:10:21 -050078 if (!start)
79 return;
80
Gary R Hook103600a2016-10-18 17:33:37 -050081 if (cmd_q->lsb == start) {
Gary R Hook4b394a22016-07-26 19:10:21 -050082 /* An entry from the private LSB */
Gary R Hook103600a2016-10-18 17:33:37 -050083 bitmap_clear(cmd_q->lsbmap, start, count);
Gary R Hook4b394a22016-07-26 19:10:21 -050084 } else {
85 /* From the shared LSBs */
86 struct ccp_device *ccp = cmd_q->ccp;
87
88 mutex_lock(&ccp->sb_mutex);
89 bitmap_clear(ccp->lsbmap, start, count);
90 ccp->sb_avail = 1;
91 mutex_unlock(&ccp->sb_mutex);
92 wake_up_interruptible_all(&ccp->sb_queue);
93 }
94}
95
96/* CCP version 5: Union to define the function field (cmd_reg1/dword0) */
97union ccp_function {
98 struct {
99 u16 size:7;
100 u16 encrypt:1;
101 u16 mode:5;
102 u16 type:2;
103 } aes;
104 struct {
105 u16 size:7;
106 u16 encrypt:1;
107 u16 rsvd:5;
108 u16 type:2;
109 } aes_xts;
110 struct {
111 u16 rsvd1:10;
112 u16 type:4;
113 u16 rsvd2:1;
114 } sha;
115 struct {
116 u16 mode:3;
117 u16 size:12;
118 } rsa;
119 struct {
120 u16 byteswap:2;
121 u16 bitwise:3;
122 u16 reflect:2;
123 u16 rsvd:8;
124 } pt;
125 struct {
126 u16 rsvd:13;
127 } zlib;
128 struct {
129 u16 size:10;
130 u16 type:2;
131 u16 mode:3;
132 } ecc;
133 u16 raw;
134};
135
136#define CCP_AES_SIZE(p) ((p)->aes.size)
137#define CCP_AES_ENCRYPT(p) ((p)->aes.encrypt)
138#define CCP_AES_MODE(p) ((p)->aes.mode)
139#define CCP_AES_TYPE(p) ((p)->aes.type)
140#define CCP_XTS_SIZE(p) ((p)->aes_xts.size)
141#define CCP_XTS_ENCRYPT(p) ((p)->aes_xts.encrypt)
142#define CCP_SHA_TYPE(p) ((p)->sha.type)
143#define CCP_RSA_SIZE(p) ((p)->rsa.size)
144#define CCP_PT_BYTESWAP(p) ((p)->pt.byteswap)
145#define CCP_PT_BITWISE(p) ((p)->pt.bitwise)
146#define CCP_ECC_MODE(p) ((p)->ecc.mode)
147#define CCP_ECC_AFFINE(p) ((p)->ecc.one)
148
149/* Word 0 */
150#define CCP5_CMD_DW0(p) ((p)->dw0)
151#define CCP5_CMD_SOC(p) (CCP5_CMD_DW0(p).soc)
152#define CCP5_CMD_IOC(p) (CCP5_CMD_DW0(p).ioc)
153#define CCP5_CMD_INIT(p) (CCP5_CMD_DW0(p).init)
154#define CCP5_CMD_EOM(p) (CCP5_CMD_DW0(p).eom)
155#define CCP5_CMD_FUNCTION(p) (CCP5_CMD_DW0(p).function)
156#define CCP5_CMD_ENGINE(p) (CCP5_CMD_DW0(p).engine)
157#define CCP5_CMD_PROT(p) (CCP5_CMD_DW0(p).prot)
158
159/* Word 1 */
160#define CCP5_CMD_DW1(p) ((p)->length)
161#define CCP5_CMD_LEN(p) (CCP5_CMD_DW1(p))
162
163/* Word 2 */
164#define CCP5_CMD_DW2(p) ((p)->src_lo)
165#define CCP5_CMD_SRC_LO(p) (CCP5_CMD_DW2(p))
166
167/* Word 3 */
168#define CCP5_CMD_DW3(p) ((p)->dw3)
169#define CCP5_CMD_SRC_MEM(p) ((p)->dw3.src_mem)
170#define CCP5_CMD_SRC_HI(p) ((p)->dw3.src_hi)
171#define CCP5_CMD_LSB_ID(p) ((p)->dw3.lsb_cxt_id)
172#define CCP5_CMD_FIX_SRC(p) ((p)->dw3.fixed)
173
174/* Words 4/5 */
175#define CCP5_CMD_DW4(p) ((p)->dw4)
176#define CCP5_CMD_DST_LO(p) (CCP5_CMD_DW4(p).dst_lo)
177#define CCP5_CMD_DW5(p) ((p)->dw5.fields.dst_hi)
178#define CCP5_CMD_DST_HI(p) (CCP5_CMD_DW5(p))
179#define CCP5_CMD_DST_MEM(p) ((p)->dw5.fields.dst_mem)
180#define CCP5_CMD_FIX_DST(p) ((p)->dw5.fields.fixed)
181#define CCP5_CMD_SHA_LO(p) ((p)->dw4.sha_len_lo)
182#define CCP5_CMD_SHA_HI(p) ((p)->dw5.sha_len_hi)
183
184/* Word 6/7 */
185#define CCP5_CMD_DW6(p) ((p)->key_lo)
186#define CCP5_CMD_KEY_LO(p) (CCP5_CMD_DW6(p))
187#define CCP5_CMD_DW7(p) ((p)->dw7)
188#define CCP5_CMD_KEY_HI(p) ((p)->dw7.key_hi)
189#define CCP5_CMD_KEY_MEM(p) ((p)->dw7.key_mem)
190
191static inline u32 low_address(unsigned long addr)
192{
193 return (u64)addr & 0x0ffffffff;
194}
195
196static inline u32 high_address(unsigned long addr)
197{
198 return ((u64)addr >> 32) & 0x00000ffff;
199}
200
201static unsigned int ccp5_get_free_slots(struct ccp_cmd_queue *cmd_q)
202{
203 unsigned int head_idx, n;
204 u32 head_lo, queue_start;
205
206 queue_start = low_address(cmd_q->qdma_tail);
207 head_lo = ioread32(cmd_q->reg_head_lo);
208 head_idx = (head_lo - queue_start) / sizeof(struct ccp5_desc);
209
210 n = head_idx + COMMANDS_PER_QUEUE - cmd_q->qidx - 1;
211
212 return n % COMMANDS_PER_QUEUE; /* Always one unused spot */
213}
214
215static int ccp5_do_cmd(struct ccp5_desc *desc,
216 struct ccp_cmd_queue *cmd_q)
217{
218 u32 *mP;
219 __le32 *dP;
220 u32 tail;
221 int i;
222 int ret = 0;
223
224 if (CCP5_CMD_SOC(desc)) {
225 CCP5_CMD_IOC(desc) = 1;
226 CCP5_CMD_SOC(desc) = 0;
227 }
228 mutex_lock(&cmd_q->q_mutex);
229
230 mP = (u32 *) &cmd_q->qbase[cmd_q->qidx];
231 dP = (__le32 *) desc;
232 for (i = 0; i < 8; i++)
233 mP[i] = cpu_to_le32(dP[i]); /* handle endianness */
234
235 cmd_q->qidx = (cmd_q->qidx + 1) % COMMANDS_PER_QUEUE;
236
237 /* The data used by this command must be flushed to memory */
238 wmb();
239
240 /* Write the new tail address back to the queue register */
241 tail = low_address(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE);
242 iowrite32(tail, cmd_q->reg_tail_lo);
243
244 /* Turn the queue back on using our cached control register */
245 iowrite32(cmd_q->qcontrol | CMD5_Q_RUN, cmd_q->reg_control);
246 mutex_unlock(&cmd_q->q_mutex);
247
248 if (CCP5_CMD_IOC(desc)) {
249 /* Wait for the job to complete */
250 ret = wait_event_interruptible(cmd_q->int_queue,
251 cmd_q->int_rcvd);
252 if (ret || cmd_q->cmd_error) {
Gary R Hook81422ba2016-09-28 11:53:56 -0500253 if (cmd_q->cmd_error)
254 ccp_log_error(cmd_q->ccp,
255 cmd_q->cmd_error);
Gary R Hook4b394a22016-07-26 19:10:21 -0500256 /* A version 5 device doesn't use Job IDs... */
257 if (!ret)
258 ret = -EIO;
259 }
260 cmd_q->int_rcvd = 0;
261 }
262
263 return 0;
264}
265
266static int ccp5_perform_aes(struct ccp_op *op)
267{
268 struct ccp5_desc desc;
269 union ccp_function function;
270 u32 key_addr = op->sb_key * LSB_ITEM_SIZE;
271
272 /* Zero out all the fields of the command desc */
273 memset(&desc, 0, Q_DESC_SIZE);
274
275 CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_AES;
276
277 CCP5_CMD_SOC(&desc) = op->soc;
278 CCP5_CMD_IOC(&desc) = 1;
279 CCP5_CMD_INIT(&desc) = op->init;
280 CCP5_CMD_EOM(&desc) = op->eom;
281 CCP5_CMD_PROT(&desc) = 0;
282
283 function.raw = 0;
284 CCP_AES_ENCRYPT(&function) = op->u.aes.action;
285 CCP_AES_MODE(&function) = op->u.aes.mode;
286 CCP_AES_TYPE(&function) = op->u.aes.type;
Gary R Hookf7cc02b32017-02-08 13:07:06 -0600287 CCP_AES_SIZE(&function) = op->u.aes.size;
Gary R Hook4b394a22016-07-26 19:10:21 -0500288
289 CCP5_CMD_FUNCTION(&desc) = function.raw;
290
291 CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
292
293 CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
294 CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
295 CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
296
297 CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
298 CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
299 CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
300
301 CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr);
302 CCP5_CMD_KEY_HI(&desc) = 0;
303 CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
304 CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
305
306 return ccp5_do_cmd(&desc, op->cmd_q);
307}
308
309static int ccp5_perform_xts_aes(struct ccp_op *op)
310{
311 struct ccp5_desc desc;
312 union ccp_function function;
313 u32 key_addr = op->sb_key * LSB_ITEM_SIZE;
314
315 /* Zero out all the fields of the command desc */
316 memset(&desc, 0, Q_DESC_SIZE);
317
318 CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_XTS_AES_128;
319
320 CCP5_CMD_SOC(&desc) = op->soc;
321 CCP5_CMD_IOC(&desc) = 1;
322 CCP5_CMD_INIT(&desc) = op->init;
323 CCP5_CMD_EOM(&desc) = op->eom;
324 CCP5_CMD_PROT(&desc) = 0;
325
326 function.raw = 0;
327 CCP_XTS_ENCRYPT(&function) = op->u.xts.action;
328 CCP_XTS_SIZE(&function) = op->u.xts.unit_size;
329 CCP5_CMD_FUNCTION(&desc) = function.raw;
330
331 CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
332
333 CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
334 CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
335 CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
336
337 CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
338 CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
339 CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
340
341 CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr);
342 CCP5_CMD_KEY_HI(&desc) = 0;
343 CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
344 CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
345
346 return ccp5_do_cmd(&desc, op->cmd_q);
347}
348
349static int ccp5_perform_sha(struct ccp_op *op)
350{
351 struct ccp5_desc desc;
352 union ccp_function function;
353
354 /* Zero out all the fields of the command desc */
355 memset(&desc, 0, Q_DESC_SIZE);
356
357 CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_SHA;
358
359 CCP5_CMD_SOC(&desc) = op->soc;
360 CCP5_CMD_IOC(&desc) = 1;
361 CCP5_CMD_INIT(&desc) = 1;
362 CCP5_CMD_EOM(&desc) = op->eom;
363 CCP5_CMD_PROT(&desc) = 0;
364
365 function.raw = 0;
366 CCP_SHA_TYPE(&function) = op->u.sha.type;
367 CCP5_CMD_FUNCTION(&desc) = function.raw;
368
369 CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
370
371 CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
372 CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
373 CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
374
375 CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
376
377 if (op->eom) {
378 CCP5_CMD_SHA_LO(&desc) = lower_32_bits(op->u.sha.msg_bits);
379 CCP5_CMD_SHA_HI(&desc) = upper_32_bits(op->u.sha.msg_bits);
380 } else {
381 CCP5_CMD_SHA_LO(&desc) = 0;
382 CCP5_CMD_SHA_HI(&desc) = 0;
383 }
384
385 return ccp5_do_cmd(&desc, op->cmd_q);
386}
387
388static int ccp5_perform_rsa(struct ccp_op *op)
389{
390 struct ccp5_desc desc;
391 union ccp_function function;
392
393 /* Zero out all the fields of the command desc */
394 memset(&desc, 0, Q_DESC_SIZE);
395
396 CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_RSA;
397
398 CCP5_CMD_SOC(&desc) = op->soc;
399 CCP5_CMD_IOC(&desc) = 1;
400 CCP5_CMD_INIT(&desc) = 0;
401 CCP5_CMD_EOM(&desc) = 1;
402 CCP5_CMD_PROT(&desc) = 0;
403
404 function.raw = 0;
Gary R Hooke6414b12016-11-01 14:05:05 -0500405 CCP_RSA_SIZE(&function) = op->u.rsa.mod_size >> 3;
Gary R Hook4b394a22016-07-26 19:10:21 -0500406 CCP5_CMD_FUNCTION(&desc) = function.raw;
407
408 CCP5_CMD_LEN(&desc) = op->u.rsa.input_len;
409
410 /* Source is from external memory */
411 CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
412 CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
413 CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
414
415 /* Destination is in external memory */
416 CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
417 CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
418 CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
419
Gary R Hooke6414b12016-11-01 14:05:05 -0500420 /* Exponent is in LSB memory */
421 CCP5_CMD_KEY_LO(&desc) = op->sb_key * LSB_ITEM_SIZE;
422 CCP5_CMD_KEY_HI(&desc) = 0;
423 CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
Gary R Hook4b394a22016-07-26 19:10:21 -0500424
425 return ccp5_do_cmd(&desc, op->cmd_q);
426}
427
428static int ccp5_perform_passthru(struct ccp_op *op)
429{
430 struct ccp5_desc desc;
431 union ccp_function function;
432 struct ccp_dma_info *saddr = &op->src.u.dma;
433 struct ccp_dma_info *daddr = &op->dst.u.dma;
434
435 memset(&desc, 0, Q_DESC_SIZE);
436
437 CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_PASSTHRU;
438
439 CCP5_CMD_SOC(&desc) = 0;
440 CCP5_CMD_IOC(&desc) = 1;
441 CCP5_CMD_INIT(&desc) = 0;
442 CCP5_CMD_EOM(&desc) = op->eom;
443 CCP5_CMD_PROT(&desc) = 0;
444
445 function.raw = 0;
446 CCP_PT_BYTESWAP(&function) = op->u.passthru.byte_swap;
447 CCP_PT_BITWISE(&function) = op->u.passthru.bit_mod;
448 CCP5_CMD_FUNCTION(&desc) = function.raw;
449
450 /* Length of source data is always 256 bytes */
451 if (op->src.type == CCP_MEMTYPE_SYSTEM)
452 CCP5_CMD_LEN(&desc) = saddr->length;
453 else
454 CCP5_CMD_LEN(&desc) = daddr->length;
455
456 if (op->src.type == CCP_MEMTYPE_SYSTEM) {
457 CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
458 CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
459 CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
460
461 if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
462 CCP5_CMD_LSB_ID(&desc) = op->sb_key;
463 } else {
464 u32 key_addr = op->src.u.sb * CCP_SB_BYTES;
465
466 CCP5_CMD_SRC_LO(&desc) = lower_32_bits(key_addr);
467 CCP5_CMD_SRC_HI(&desc) = 0;
468 CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SB;
469 }
470
471 if (op->dst.type == CCP_MEMTYPE_SYSTEM) {
472 CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
473 CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
474 CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
475 } else {
476 u32 key_addr = op->dst.u.sb * CCP_SB_BYTES;
477
478 CCP5_CMD_DST_LO(&desc) = lower_32_bits(key_addr);
479 CCP5_CMD_DST_HI(&desc) = 0;
480 CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SB;
481 }
482
483 return ccp5_do_cmd(&desc, op->cmd_q);
484}
485
486static int ccp5_perform_ecc(struct ccp_op *op)
487{
488 struct ccp5_desc desc;
489 union ccp_function function;
490
491 /* Zero out all the fields of the command desc */
492 memset(&desc, 0, Q_DESC_SIZE);
493
494 CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_ECC;
495
496 CCP5_CMD_SOC(&desc) = 0;
497 CCP5_CMD_IOC(&desc) = 1;
498 CCP5_CMD_INIT(&desc) = 0;
499 CCP5_CMD_EOM(&desc) = 1;
500 CCP5_CMD_PROT(&desc) = 0;
501
502 function.raw = 0;
503 function.ecc.mode = op->u.ecc.function;
504 CCP5_CMD_FUNCTION(&desc) = function.raw;
505
506 CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
507
508 CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
509 CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
510 CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
511
512 CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
513 CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
514 CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
515
516 return ccp5_do_cmd(&desc, op->cmd_q);
517}
518
519static int ccp_find_lsb_regions(struct ccp_cmd_queue *cmd_q, u64 status)
520{
521 int q_mask = 1 << cmd_q->id;
522 int queues = 0;
523 int j;
524
525 /* Build a bit mask to know which LSBs this queue has access to.
526 * Don't bother with segment 0 as it has special privileges.
527 */
528 for (j = 1; j < MAX_LSB_CNT; j++) {
529 if (status & q_mask)
530 bitmap_set(cmd_q->lsbmask, j, 1);
531 status >>= LSB_REGION_WIDTH;
532 }
533 queues = bitmap_weight(cmd_q->lsbmask, MAX_LSB_CNT);
534 dev_info(cmd_q->ccp->dev, "Queue %d can access %d LSB regions\n",
535 cmd_q->id, queues);
536
537 return queues ? 0 : -EINVAL;
538}
539
540
541static int ccp_find_and_assign_lsb_to_q(struct ccp_device *ccp,
542 int lsb_cnt, int n_lsbs,
543 unsigned long *lsb_pub)
544{
545 DECLARE_BITMAP(qlsb, MAX_LSB_CNT);
546 int bitno;
547 int qlsb_wgt;
548 int i;
549
550 /* For each queue:
551 * If the count of potential LSBs available to a queue matches the
552 * ordinal given to us in lsb_cnt:
553 * Copy the mask of possible LSBs for this queue into "qlsb";
554 * For each bit in qlsb, see if the corresponding bit in the
555 * aggregation mask is set; if so, we have a match.
556 * If we have a match, clear the bit in the aggregation to
557 * mark it as no longer available.
558 * If there is no match, clear the bit in qlsb and keep looking.
559 */
560 for (i = 0; i < ccp->cmd_q_count; i++) {
561 struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i];
562
563 qlsb_wgt = bitmap_weight(cmd_q->lsbmask, MAX_LSB_CNT);
564
565 if (qlsb_wgt == lsb_cnt) {
566 bitmap_copy(qlsb, cmd_q->lsbmask, MAX_LSB_CNT);
567
568 bitno = find_first_bit(qlsb, MAX_LSB_CNT);
569 while (bitno < MAX_LSB_CNT) {
570 if (test_bit(bitno, lsb_pub)) {
571 /* We found an available LSB
572 * that this queue can access
573 */
574 cmd_q->lsb = bitno;
575 bitmap_clear(lsb_pub, bitno, 1);
576 dev_info(ccp->dev,
577 "Queue %d gets LSB %d\n",
578 i, bitno);
579 break;
580 }
581 bitmap_clear(qlsb, bitno, 1);
582 bitno = find_first_bit(qlsb, MAX_LSB_CNT);
583 }
584 if (bitno >= MAX_LSB_CNT)
585 return -EINVAL;
586 n_lsbs--;
587 }
588 }
589 return n_lsbs;
590}
591
592/* For each queue, from the most- to least-constrained:
593 * find an LSB that can be assigned to the queue. If there are N queues that
594 * can only use M LSBs, where N > M, fail; otherwise, every queue will get a
595 * dedicated LSB. Remaining LSB regions become a shared resource.
596 * If we have fewer LSBs than queues, all LSB regions become shared resources.
597 */
598static int ccp_assign_lsbs(struct ccp_device *ccp)
599{
600 DECLARE_BITMAP(lsb_pub, MAX_LSB_CNT);
601 DECLARE_BITMAP(qlsb, MAX_LSB_CNT);
602 int n_lsbs = 0;
603 int bitno;
604 int i, lsb_cnt;
605 int rc = 0;
606
607 bitmap_zero(lsb_pub, MAX_LSB_CNT);
608
609 /* Create an aggregate bitmap to get a total count of available LSBs */
610 for (i = 0; i < ccp->cmd_q_count; i++)
611 bitmap_or(lsb_pub,
612 lsb_pub, ccp->cmd_q[i].lsbmask,
613 MAX_LSB_CNT);
614
615 n_lsbs = bitmap_weight(lsb_pub, MAX_LSB_CNT);
616
617 if (n_lsbs >= ccp->cmd_q_count) {
618 /* We have enough LSBS to give every queue a private LSB.
619 * Brute force search to start with the queues that are more
620 * constrained in LSB choice. When an LSB is privately
621 * assigned, it is removed from the public mask.
622 * This is an ugly N squared algorithm with some optimization.
623 */
624 for (lsb_cnt = 1;
625 n_lsbs && (lsb_cnt <= MAX_LSB_CNT);
626 lsb_cnt++) {
627 rc = ccp_find_and_assign_lsb_to_q(ccp, lsb_cnt, n_lsbs,
628 lsb_pub);
629 if (rc < 0)
630 return -EINVAL;
631 n_lsbs = rc;
632 }
633 }
634
635 rc = 0;
636 /* What's left of the LSBs, according to the public mask, now become
637 * shared. Any zero bits in the lsb_pub mask represent an LSB region
638 * that can't be used as a shared resource, so mark the LSB slots for
639 * them as "in use".
640 */
641 bitmap_copy(qlsb, lsb_pub, MAX_LSB_CNT);
642
643 bitno = find_first_zero_bit(qlsb, MAX_LSB_CNT);
644 while (bitno < MAX_LSB_CNT) {
645 bitmap_set(ccp->lsbmap, bitno * LSB_SIZE, LSB_SIZE);
646 bitmap_set(qlsb, bitno, 1);
647 bitno = find_first_zero_bit(qlsb, MAX_LSB_CNT);
648 }
649
650 return rc;
651}
652
653static int ccp5_init(struct ccp_device *ccp)
654{
655 struct device *dev = ccp->dev;
656 struct ccp_cmd_queue *cmd_q;
657 struct dma_pool *dma_pool;
658 char dma_pool_name[MAX_DMAPOOL_NAME_LEN];
659 unsigned int qmr, qim, i;
660 u64 status;
661 u32 status_lo, status_hi;
662 int ret;
663
664 /* Find available queues */
665 qim = 0;
666 qmr = ioread32(ccp->io_regs + Q_MASK_REG);
667 for (i = 0; i < MAX_HW_QUEUES; i++) {
668
669 if (!(qmr & (1 << i)))
670 continue;
671
672 /* Allocate a dma pool for this queue */
673 snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d",
674 ccp->name, i);
675 dma_pool = dma_pool_create(dma_pool_name, dev,
676 CCP_DMAPOOL_MAX_SIZE,
677 CCP_DMAPOOL_ALIGN, 0);
678 if (!dma_pool) {
679 dev_err(dev, "unable to allocate dma pool\n");
680 ret = -ENOMEM;
681 }
682
683 cmd_q = &ccp->cmd_q[ccp->cmd_q_count];
684 ccp->cmd_q_count++;
685
686 cmd_q->ccp = ccp;
687 cmd_q->id = i;
688 cmd_q->dma_pool = dma_pool;
689 mutex_init(&cmd_q->q_mutex);
690
691 /* Page alignment satisfies our needs for N <= 128 */
692 BUILD_BUG_ON(COMMANDS_PER_QUEUE > 128);
693 cmd_q->qsize = Q_SIZE(Q_DESC_SIZE);
694 cmd_q->qbase = dma_zalloc_coherent(dev, cmd_q->qsize,
695 &cmd_q->qbase_dma,
696 GFP_KERNEL);
697 if (!cmd_q->qbase) {
698 dev_err(dev, "unable to allocate command queue\n");
699 ret = -ENOMEM;
700 goto e_pool;
701 }
702
703 cmd_q->qidx = 0;
704 /* Preset some register values and masks that are queue
705 * number dependent
706 */
707 cmd_q->reg_control = ccp->io_regs +
708 CMD5_Q_STATUS_INCR * (i + 1);
709 cmd_q->reg_tail_lo = cmd_q->reg_control + CMD5_Q_TAIL_LO_BASE;
710 cmd_q->reg_head_lo = cmd_q->reg_control + CMD5_Q_HEAD_LO_BASE;
711 cmd_q->reg_int_enable = cmd_q->reg_control +
712 CMD5_Q_INT_ENABLE_BASE;
713 cmd_q->reg_interrupt_status = cmd_q->reg_control +
714 CMD5_Q_INTERRUPT_STATUS_BASE;
715 cmd_q->reg_status = cmd_q->reg_control + CMD5_Q_STATUS_BASE;
716 cmd_q->reg_int_status = cmd_q->reg_control +
717 CMD5_Q_INT_STATUS_BASE;
718 cmd_q->reg_dma_status = cmd_q->reg_control +
719 CMD5_Q_DMA_STATUS_BASE;
720 cmd_q->reg_dma_read_status = cmd_q->reg_control +
721 CMD5_Q_DMA_READ_STATUS_BASE;
722 cmd_q->reg_dma_write_status = cmd_q->reg_control +
723 CMD5_Q_DMA_WRITE_STATUS_BASE;
724
725 init_waitqueue_head(&cmd_q->int_queue);
726
727 dev_dbg(dev, "queue #%u available\n", i);
728 }
729 if (ccp->cmd_q_count == 0) {
730 dev_notice(dev, "no command queues available\n");
731 ret = -EIO;
732 goto e_pool;
733 }
734 dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count);
735
736 /* Turn off the queues and disable interrupts until ready */
737 for (i = 0; i < ccp->cmd_q_count; i++) {
738 cmd_q = &ccp->cmd_q[i];
739
740 cmd_q->qcontrol = 0; /* Start with nothing */
741 iowrite32(cmd_q->qcontrol, cmd_q->reg_control);
742
743 /* Disable the interrupts */
744 iowrite32(0x00, cmd_q->reg_int_enable);
745 ioread32(cmd_q->reg_int_status);
746 ioread32(cmd_q->reg_status);
747
748 /* Clear the interrupts */
749 iowrite32(ALL_INTERRUPTS, cmd_q->reg_interrupt_status);
750 }
751
752 dev_dbg(dev, "Requesting an IRQ...\n");
753 /* Request an irq */
754 ret = ccp->get_irq(ccp);
755 if (ret) {
756 dev_err(dev, "unable to allocate an IRQ\n");
757 goto e_pool;
758 }
759
Gary R Hook4b394a22016-07-26 19:10:21 -0500760 dev_dbg(dev, "Loading LSB map...\n");
761 /* Copy the private LSB mask to the public registers */
762 status_lo = ioread32(ccp->io_regs + LSB_PRIVATE_MASK_LO_OFFSET);
763 status_hi = ioread32(ccp->io_regs + LSB_PRIVATE_MASK_HI_OFFSET);
764 iowrite32(status_lo, ccp->io_regs + LSB_PUBLIC_MASK_LO_OFFSET);
765 iowrite32(status_hi, ccp->io_regs + LSB_PUBLIC_MASK_HI_OFFSET);
766 status = ((u64)status_hi<<30) | (u64)status_lo;
767
768 dev_dbg(dev, "Configuring virtual queues...\n");
769 /* Configure size of each virtual queue accessible to host */
770 for (i = 0; i < ccp->cmd_q_count; i++) {
771 u32 dma_addr_lo;
772 u32 dma_addr_hi;
773
774 cmd_q = &ccp->cmd_q[i];
775
776 cmd_q->qcontrol &= ~(CMD5_Q_SIZE << CMD5_Q_SHIFT);
777 cmd_q->qcontrol |= QUEUE_SIZE_VAL << CMD5_Q_SHIFT;
778
779 cmd_q->qdma_tail = cmd_q->qbase_dma;
780 dma_addr_lo = low_address(cmd_q->qdma_tail);
781 iowrite32((u32)dma_addr_lo, cmd_q->reg_tail_lo);
782 iowrite32((u32)dma_addr_lo, cmd_q->reg_head_lo);
783
784 dma_addr_hi = high_address(cmd_q->qdma_tail);
785 cmd_q->qcontrol |= (dma_addr_hi << 16);
786 iowrite32(cmd_q->qcontrol, cmd_q->reg_control);
787
788 /* Find the LSB regions accessible to the queue */
789 ccp_find_lsb_regions(cmd_q, status);
790 cmd_q->lsb = -1; /* Unassigned value */
791 }
792
793 dev_dbg(dev, "Assigning LSBs...\n");
794 ret = ccp_assign_lsbs(ccp);
795 if (ret) {
796 dev_err(dev, "Unable to assign LSBs (%d)\n", ret);
797 goto e_irq;
798 }
799
800 /* Optimization: pre-allocate LSB slots for each queue */
801 for (i = 0; i < ccp->cmd_q_count; i++) {
802 ccp->cmd_q[i].sb_key = ccp_lsb_alloc(&ccp->cmd_q[i], 2);
803 ccp->cmd_q[i].sb_ctx = ccp_lsb_alloc(&ccp->cmd_q[i], 2);
804 }
805
806 dev_dbg(dev, "Starting threads...\n");
807 /* Create a kthread for each queue */
808 for (i = 0; i < ccp->cmd_q_count; i++) {
809 struct task_struct *kthread;
810
811 cmd_q = &ccp->cmd_q[i];
812
813 kthread = kthread_create(ccp_cmd_queue_thread, cmd_q,
814 "%s-q%u", ccp->name, cmd_q->id);
815 if (IS_ERR(kthread)) {
816 dev_err(dev, "error creating queue thread (%ld)\n",
817 PTR_ERR(kthread));
818 ret = PTR_ERR(kthread);
819 goto e_kthread;
820 }
821
822 cmd_q->kthread = kthread;
823 wake_up_process(kthread);
824 }
825
826 dev_dbg(dev, "Enabling interrupts...\n");
827 /* Enable interrupts */
828 for (i = 0; i < ccp->cmd_q_count; i++) {
829 cmd_q = &ccp->cmd_q[i];
830 iowrite32(ALL_INTERRUPTS, cmd_q->reg_int_enable);
831 }
832
833 dev_dbg(dev, "Registering device...\n");
834 /* Put this on the unit list to make it available */
835 ccp_add_device(ccp);
836
Gary R Hook084935b2016-07-26 19:10:31 -0500837 ret = ccp_register_rng(ccp);
838 if (ret)
839 goto e_kthread;
840
Gary R Hook99d90b22016-07-26 19:10:40 -0500841 /* Register the DMA engine support */
842 ret = ccp_dmaengine_register(ccp);
843 if (ret)
Gary R Hook9ddb9dc2016-09-28 11:53:47 -0500844 goto e_hwrng;
Gary R Hook99d90b22016-07-26 19:10:40 -0500845
Gary R Hook4b394a22016-07-26 19:10:21 -0500846 return 0;
847
Gary R Hook9ddb9dc2016-09-28 11:53:47 -0500848e_hwrng:
849 ccp_unregister_rng(ccp);
850
Gary R Hook4b394a22016-07-26 19:10:21 -0500851e_kthread:
852 for (i = 0; i < ccp->cmd_q_count; i++)
853 if (ccp->cmd_q[i].kthread)
854 kthread_stop(ccp->cmd_q[i].kthread);
855
856e_irq:
857 ccp->free_irq(ccp);
858
859e_pool:
860 for (i = 0; i < ccp->cmd_q_count; i++)
861 dma_pool_destroy(ccp->cmd_q[i].dma_pool);
862
863 return ret;
864}
865
866static void ccp5_destroy(struct ccp_device *ccp)
867{
868 struct device *dev = ccp->dev;
869 struct ccp_cmd_queue *cmd_q;
870 struct ccp_cmd *cmd;
871 unsigned int i;
872
Gary R Hook99d90b22016-07-26 19:10:40 -0500873 /* Unregister the DMA engine */
874 ccp_dmaengine_unregister(ccp);
875
Gary R Hook084935b2016-07-26 19:10:31 -0500876 /* Unregister the RNG */
877 ccp_unregister_rng(ccp);
878
Gary R Hook4b394a22016-07-26 19:10:21 -0500879 /* Remove this device from the list of available units first */
880 ccp_del_device(ccp);
881
882 /* Disable and clear interrupts */
883 for (i = 0; i < ccp->cmd_q_count; i++) {
884 cmd_q = &ccp->cmd_q[i];
885
886 /* Turn off the run bit */
887 iowrite32(cmd_q->qcontrol & ~CMD5_Q_RUN, cmd_q->reg_control);
888
889 /* Disable the interrupts */
890 iowrite32(ALL_INTERRUPTS, cmd_q->reg_interrupt_status);
891
892 /* Clear the interrupt status */
893 iowrite32(0x00, cmd_q->reg_int_enable);
894 ioread32(cmd_q->reg_int_status);
895 ioread32(cmd_q->reg_status);
896 }
897
898 /* Stop the queue kthreads */
899 for (i = 0; i < ccp->cmd_q_count; i++)
900 if (ccp->cmd_q[i].kthread)
901 kthread_stop(ccp->cmd_q[i].kthread);
902
903 ccp->free_irq(ccp);
904
905 for (i = 0; i < ccp->cmd_q_count; i++) {
906 cmd_q = &ccp->cmd_q[i];
907 dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase,
908 cmd_q->qbase_dma);
909 }
910
911 /* Flush the cmd and backlog queue */
912 while (!list_empty(&ccp->cmd)) {
913 /* Invoke the callback directly with an error code */
914 cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
915 list_del(&cmd->entry);
916 cmd->callback(cmd->data, -ENODEV);
917 }
918 while (!list_empty(&ccp->backlog)) {
919 /* Invoke the callback directly with an error code */
920 cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry);
921 list_del(&cmd->entry);
922 cmd->callback(cmd->data, -ENODEV);
923 }
924}
925
926static irqreturn_t ccp5_irq_handler(int irq, void *data)
927{
928 struct device *dev = data;
929 struct ccp_device *ccp = dev_get_drvdata(dev);
930 u32 status;
931 unsigned int i;
932
933 for (i = 0; i < ccp->cmd_q_count; i++) {
934 struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i];
935
936 status = ioread32(cmd_q->reg_interrupt_status);
937
938 if (status) {
939 cmd_q->int_status = status;
940 cmd_q->q_status = ioread32(cmd_q->reg_status);
941 cmd_q->q_int_status = ioread32(cmd_q->reg_int_status);
942
943 /* On error, only save the first error value */
944 if ((status & INT_ERROR) && !cmd_q->cmd_error)
945 cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
946
947 cmd_q->int_rcvd = 1;
948
949 /* Acknowledge the interrupt and wake the kthread */
950 iowrite32(ALL_INTERRUPTS, cmd_q->reg_interrupt_status);
951 wake_up_interruptible(&cmd_q->int_queue);
952 }
953 }
954
955 return IRQ_HANDLED;
956}
957
958static void ccp5_config(struct ccp_device *ccp)
959{
960 /* Public side */
Gary R Hook500c0102017-01-27 15:28:45 -0600961 iowrite32(0x0, ccp->io_regs + CMD5_REQID_CONFIG_OFFSET);
Gary R Hook4b394a22016-07-26 19:10:21 -0500962}
963
Gary R Hooke14e7d12016-07-26 19:10:49 -0500964static void ccp5other_config(struct ccp_device *ccp)
965{
966 int i;
967 u32 rnd;
968
969 /* We own all of the queues on the NTB CCP */
970
971 iowrite32(0x00012D57, ccp->io_regs + CMD5_TRNG_CTL_OFFSET);
972 iowrite32(0x00000003, ccp->io_regs + CMD5_CONFIG_0_OFFSET);
973 for (i = 0; i < 12; i++) {
974 rnd = ioread32(ccp->io_regs + TRNG_OUT_REG);
975 iowrite32(rnd, ccp->io_regs + CMD5_AES_MASK_OFFSET);
976 }
977
978 iowrite32(0x0000001F, ccp->io_regs + CMD5_QUEUE_MASK_OFFSET);
979 iowrite32(0x00005B6D, ccp->io_regs + CMD5_QUEUE_PRIO_OFFSET);
980 iowrite32(0x00000000, ccp->io_regs + CMD5_CMD_TIMEOUT_OFFSET);
981
982 iowrite32(0x3FFFFFFF, ccp->io_regs + LSB_PRIVATE_MASK_LO_OFFSET);
983 iowrite32(0x000003FF, ccp->io_regs + LSB_PRIVATE_MASK_HI_OFFSET);
984
985 iowrite32(0x00108823, ccp->io_regs + CMD5_CLK_GATE_CTL_OFFSET);
986
987 ccp5_config(ccp);
988}
989
990/* Version 5 adds some function, but is essentially the same as v5 */
Gary R Hook4b394a22016-07-26 19:10:21 -0500991static const struct ccp_actions ccp5_actions = {
992 .aes = ccp5_perform_aes,
993 .xts_aes = ccp5_perform_xts_aes,
994 .sha = ccp5_perform_sha,
995 .rsa = ccp5_perform_rsa,
996 .passthru = ccp5_perform_passthru,
997 .ecc = ccp5_perform_ecc,
998 .sballoc = ccp_lsb_alloc,
999 .sbfree = ccp_lsb_free,
1000 .init = ccp5_init,
1001 .destroy = ccp5_destroy,
1002 .get_free_slots = ccp5_get_free_slots,
1003 .irqhandler = ccp5_irq_handler,
1004};
1005
Gary R Hook9ddb9dc2016-09-28 11:53:47 -05001006const struct ccp_vdata ccpv5a = {
Gary R Hook4b394a22016-07-26 19:10:21 -05001007 .version = CCP_VERSION(5, 0),
1008 .setup = ccp5_config,
1009 .perform = &ccp5_actions,
1010 .bar = 2,
1011 .offset = 0x0,
1012};
Gary R Hooke14e7d12016-07-26 19:10:49 -05001013
Gary R Hook9ddb9dc2016-09-28 11:53:47 -05001014const struct ccp_vdata ccpv5b = {
Gary R Hooke14e7d12016-07-26 19:10:49 -05001015 .version = CCP_VERSION(5, 0),
1016 .setup = ccp5other_config,
1017 .perform = &ccp5_actions,
1018 .bar = 2,
1019 .offset = 0x0,
1020};