2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2016 Advanced Micro Devices, Inc.
6 * Author: Gary R Hook <gary.hook@amd.com>
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.
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>
24 /* 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
30 static u32
ccp_lsb_alloc(struct ccp_cmd_queue
*cmd_q
, unsigned int count
)
32 struct ccp_device
*ccp
;
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
,
40 if (start
< LSB_SIZE
) {
41 bitmap_set(cmd_q
->lsbmap
, start
, count
);
42 return start
+ cmd_q
->lsb
* LSB_SIZE
;
46 /* No joy; try to get an entry from the shared blocks */
49 mutex_lock(&ccp
->sb_mutex
);
51 start
= (u32
)bitmap_find_next_zero_area(ccp
->lsbmap
,
52 MAX_LSB_CNT
* LSB_SIZE
,
55 if (start
<= MAX_LSB_CNT
* LSB_SIZE
) {
56 bitmap_set(ccp
->lsbmap
, start
, count
);
58 mutex_unlock(&ccp
->sb_mutex
);
64 mutex_unlock(&ccp
->sb_mutex
);
66 /* Wait for KSB entries to become available */
67 if (wait_event_interruptible(ccp
->sb_queue
, ccp
->sb_avail
))
72 /* Free a number of LSB slots from the bitmap, starting at
73 * the indicated starting slot number.
75 static void ccp_lsb_free(struct ccp_cmd_queue
*cmd_q
, unsigned int start
,
81 if (cmd_q
->lsb
== start
) {
82 /* An entry from the private LSB */
83 bitmap_clear(cmd_q
->lsbmap
, start
, count
);
85 /* From the shared LSBs */
86 struct ccp_device
*ccp
= cmd_q
->ccp
;
88 mutex_lock(&ccp
->sb_mutex
);
89 bitmap_clear(ccp
->lsbmap
, start
, count
);
91 mutex_unlock(&ccp
->sb_mutex
);
92 wake_up_interruptible_all(&ccp
->sb_queue
);
96 /* CCP version 5: Union to define the function field (cmd_reg1/dword0) */
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)
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)
160 #define CCP5_CMD_DW1(p) ((p)->length)
161 #define CCP5_CMD_LEN(p) (CCP5_CMD_DW1(p))
164 #define CCP5_CMD_DW2(p) ((p)->src_lo)
165 #define CCP5_CMD_SRC_LO(p) (CCP5_CMD_DW2(p))
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)
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)
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)
191 static inline u32
low_address(unsigned long addr
)
193 return (u64
)addr
& 0x0ffffffff;
196 static inline u32
high_address(unsigned long addr
)
198 return ((u64
)addr
>> 32) & 0x00000ffff;
201 static unsigned int ccp5_get_free_slots(struct ccp_cmd_queue
*cmd_q
)
203 unsigned int head_idx
, n
;
204 u32 head_lo
, queue_start
;
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
);
210 n
= head_idx
+ COMMANDS_PER_QUEUE
- cmd_q
->qidx
- 1;
212 return n
% COMMANDS_PER_QUEUE
; /* Always one unused spot */
215 static int ccp5_do_cmd(struct ccp5_desc
*desc
,
216 struct ccp_cmd_queue
*cmd_q
)
224 if (CCP5_CMD_SOC(desc
)) {
225 CCP5_CMD_IOC(desc
) = 1;
226 CCP5_CMD_SOC(desc
) = 0;
228 mutex_lock(&cmd_q
->q_mutex
);
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 */
235 cmd_q
->qidx
= (cmd_q
->qidx
+ 1) % COMMANDS_PER_QUEUE
;
237 /* The data used by this command must be flushed to memory */
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
);
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
);
248 if (CCP5_CMD_IOC(desc
)) {
249 /* Wait for the job to complete */
250 ret
= wait_event_interruptible(cmd_q
->int_queue
,
252 if (ret
|| cmd_q
->cmd_error
) {
253 if (cmd_q
->cmd_error
)
254 ccp_log_error(cmd_q
->ccp
,
256 /* A version 5 device doesn't use Job IDs... */
266 static int ccp5_perform_aes(struct ccp_op
*op
)
268 struct ccp5_desc desc
;
269 union ccp_function function
;
270 u32 key_addr
= op
->sb_key
* LSB_ITEM_SIZE
;
272 /* Zero out all the fields of the command desc */
273 memset(&desc
, 0, Q_DESC_SIZE
);
275 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_AES
;
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;
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
;
287 if (op
->u
.aes
.mode
== CCP_AES_MODE_CFB
)
288 CCP_AES_SIZE(&function
) = 0x7f;
290 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
292 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
294 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
295 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
296 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
298 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
299 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
300 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
302 CCP5_CMD_KEY_LO(&desc
) = lower_32_bits(key_addr
);
303 CCP5_CMD_KEY_HI(&desc
) = 0;
304 CCP5_CMD_KEY_MEM(&desc
) = CCP_MEMTYPE_SB
;
305 CCP5_CMD_LSB_ID(&desc
) = op
->sb_ctx
;
307 return ccp5_do_cmd(&desc
, op
->cmd_q
);
310 static int ccp5_perform_xts_aes(struct ccp_op
*op
)
312 struct ccp5_desc desc
;
313 union ccp_function function
;
314 u32 key_addr
= op
->sb_key
* LSB_ITEM_SIZE
;
316 /* Zero out all the fields of the command desc */
317 memset(&desc
, 0, Q_DESC_SIZE
);
319 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_XTS_AES_128
;
321 CCP5_CMD_SOC(&desc
) = op
->soc
;
322 CCP5_CMD_IOC(&desc
) = 1;
323 CCP5_CMD_INIT(&desc
) = op
->init
;
324 CCP5_CMD_EOM(&desc
) = op
->eom
;
325 CCP5_CMD_PROT(&desc
) = 0;
328 CCP_XTS_ENCRYPT(&function
) = op
->u
.xts
.action
;
329 CCP_XTS_SIZE(&function
) = op
->u
.xts
.unit_size
;
330 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
332 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
334 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
335 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
336 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
338 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
339 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
340 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
342 CCP5_CMD_KEY_LO(&desc
) = lower_32_bits(key_addr
);
343 CCP5_CMD_KEY_HI(&desc
) = 0;
344 CCP5_CMD_KEY_MEM(&desc
) = CCP_MEMTYPE_SB
;
345 CCP5_CMD_LSB_ID(&desc
) = op
->sb_ctx
;
347 return ccp5_do_cmd(&desc
, op
->cmd_q
);
350 static int ccp5_perform_sha(struct ccp_op
*op
)
352 struct ccp5_desc desc
;
353 union ccp_function function
;
355 /* Zero out all the fields of the command desc */
356 memset(&desc
, 0, Q_DESC_SIZE
);
358 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_SHA
;
360 CCP5_CMD_SOC(&desc
) = op
->soc
;
361 CCP5_CMD_IOC(&desc
) = 1;
362 CCP5_CMD_INIT(&desc
) = 1;
363 CCP5_CMD_EOM(&desc
) = op
->eom
;
364 CCP5_CMD_PROT(&desc
) = 0;
367 CCP_SHA_TYPE(&function
) = op
->u
.sha
.type
;
368 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
370 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
372 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
373 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
374 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
376 CCP5_CMD_LSB_ID(&desc
) = op
->sb_ctx
;
379 CCP5_CMD_SHA_LO(&desc
) = lower_32_bits(op
->u
.sha
.msg_bits
);
380 CCP5_CMD_SHA_HI(&desc
) = upper_32_bits(op
->u
.sha
.msg_bits
);
382 CCP5_CMD_SHA_LO(&desc
) = 0;
383 CCP5_CMD_SHA_HI(&desc
) = 0;
386 return ccp5_do_cmd(&desc
, op
->cmd_q
);
389 static int ccp5_perform_rsa(struct ccp_op
*op
)
391 struct ccp5_desc desc
;
392 union ccp_function function
;
394 /* Zero out all the fields of the command desc */
395 memset(&desc
, 0, Q_DESC_SIZE
);
397 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_RSA
;
399 CCP5_CMD_SOC(&desc
) = op
->soc
;
400 CCP5_CMD_IOC(&desc
) = 1;
401 CCP5_CMD_INIT(&desc
) = 0;
402 CCP5_CMD_EOM(&desc
) = 1;
403 CCP5_CMD_PROT(&desc
) = 0;
406 CCP_RSA_SIZE(&function
) = op
->u
.rsa
.mod_size
>> 3;
407 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
409 CCP5_CMD_LEN(&desc
) = op
->u
.rsa
.input_len
;
411 /* Source is from external memory */
412 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
413 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
414 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
416 /* Destination is in external memory */
417 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
418 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
419 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
421 /* Exponent is in LSB memory */
422 CCP5_CMD_KEY_LO(&desc
) = op
->sb_key
* LSB_ITEM_SIZE
;
423 CCP5_CMD_KEY_HI(&desc
) = 0;
424 CCP5_CMD_KEY_MEM(&desc
) = CCP_MEMTYPE_SB
;
426 return ccp5_do_cmd(&desc
, op
->cmd_q
);
429 static int ccp5_perform_passthru(struct ccp_op
*op
)
431 struct ccp5_desc desc
;
432 union ccp_function function
;
433 struct ccp_dma_info
*saddr
= &op
->src
.u
.dma
;
434 struct ccp_dma_info
*daddr
= &op
->dst
.u
.dma
;
436 memset(&desc
, 0, Q_DESC_SIZE
);
438 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_PASSTHRU
;
440 CCP5_CMD_SOC(&desc
) = 0;
441 CCP5_CMD_IOC(&desc
) = 1;
442 CCP5_CMD_INIT(&desc
) = 0;
443 CCP5_CMD_EOM(&desc
) = op
->eom
;
444 CCP5_CMD_PROT(&desc
) = 0;
447 CCP_PT_BYTESWAP(&function
) = op
->u
.passthru
.byte_swap
;
448 CCP_PT_BITWISE(&function
) = op
->u
.passthru
.bit_mod
;
449 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
451 /* Length of source data is always 256 bytes */
452 if (op
->src
.type
== CCP_MEMTYPE_SYSTEM
)
453 CCP5_CMD_LEN(&desc
) = saddr
->length
;
455 CCP5_CMD_LEN(&desc
) = daddr
->length
;
457 if (op
->src
.type
== CCP_MEMTYPE_SYSTEM
) {
458 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
459 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
460 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
462 if (op
->u
.passthru
.bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
)
463 CCP5_CMD_LSB_ID(&desc
) = op
->sb_key
;
465 u32 key_addr
= op
->src
.u
.sb
* CCP_SB_BYTES
;
467 CCP5_CMD_SRC_LO(&desc
) = lower_32_bits(key_addr
);
468 CCP5_CMD_SRC_HI(&desc
) = 0;
469 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SB
;
472 if (op
->dst
.type
== CCP_MEMTYPE_SYSTEM
) {
473 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
474 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
475 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
477 u32 key_addr
= op
->dst
.u
.sb
* CCP_SB_BYTES
;
479 CCP5_CMD_DST_LO(&desc
) = lower_32_bits(key_addr
);
480 CCP5_CMD_DST_HI(&desc
) = 0;
481 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SB
;
484 return ccp5_do_cmd(&desc
, op
->cmd_q
);
487 static int ccp5_perform_ecc(struct ccp_op
*op
)
489 struct ccp5_desc desc
;
490 union ccp_function function
;
492 /* Zero out all the fields of the command desc */
493 memset(&desc
, 0, Q_DESC_SIZE
);
495 CCP5_CMD_ENGINE(&desc
) = CCP_ENGINE_ECC
;
497 CCP5_CMD_SOC(&desc
) = 0;
498 CCP5_CMD_IOC(&desc
) = 1;
499 CCP5_CMD_INIT(&desc
) = 0;
500 CCP5_CMD_EOM(&desc
) = 1;
501 CCP5_CMD_PROT(&desc
) = 0;
504 function
.ecc
.mode
= op
->u
.ecc
.function
;
505 CCP5_CMD_FUNCTION(&desc
) = function
.raw
;
507 CCP5_CMD_LEN(&desc
) = op
->src
.u
.dma
.length
;
509 CCP5_CMD_SRC_LO(&desc
) = ccp_addr_lo(&op
->src
.u
.dma
);
510 CCP5_CMD_SRC_HI(&desc
) = ccp_addr_hi(&op
->src
.u
.dma
);
511 CCP5_CMD_SRC_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
513 CCP5_CMD_DST_LO(&desc
) = ccp_addr_lo(&op
->dst
.u
.dma
);
514 CCP5_CMD_DST_HI(&desc
) = ccp_addr_hi(&op
->dst
.u
.dma
);
515 CCP5_CMD_DST_MEM(&desc
) = CCP_MEMTYPE_SYSTEM
;
517 return ccp5_do_cmd(&desc
, op
->cmd_q
);
520 static int ccp_find_lsb_regions(struct ccp_cmd_queue
*cmd_q
, u64 status
)
522 int q_mask
= 1 << cmd_q
->id
;
526 /* Build a bit mask to know which LSBs this queue has access to.
527 * Don't bother with segment 0 as it has special privileges.
529 for (j
= 1; j
< MAX_LSB_CNT
; j
++) {
531 bitmap_set(cmd_q
->lsbmask
, j
, 1);
532 status
>>= LSB_REGION_WIDTH
;
534 queues
= bitmap_weight(cmd_q
->lsbmask
, MAX_LSB_CNT
);
535 dev_info(cmd_q
->ccp
->dev
, "Queue %d can access %d LSB regions\n",
538 return queues
? 0 : -EINVAL
;
542 static int ccp_find_and_assign_lsb_to_q(struct ccp_device
*ccp
,
543 int lsb_cnt
, int n_lsbs
,
544 unsigned long *lsb_pub
)
546 DECLARE_BITMAP(qlsb
, MAX_LSB_CNT
);
552 * If the count of potential LSBs available to a queue matches the
553 * ordinal given to us in lsb_cnt:
554 * Copy the mask of possible LSBs for this queue into "qlsb";
555 * For each bit in qlsb, see if the corresponding bit in the
556 * aggregation mask is set; if so, we have a match.
557 * If we have a match, clear the bit in the aggregation to
558 * mark it as no longer available.
559 * If there is no match, clear the bit in qlsb and keep looking.
561 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
562 struct ccp_cmd_queue
*cmd_q
= &ccp
->cmd_q
[i
];
564 qlsb_wgt
= bitmap_weight(cmd_q
->lsbmask
, MAX_LSB_CNT
);
566 if (qlsb_wgt
== lsb_cnt
) {
567 bitmap_copy(qlsb
, cmd_q
->lsbmask
, MAX_LSB_CNT
);
569 bitno
= find_first_bit(qlsb
, MAX_LSB_CNT
);
570 while (bitno
< MAX_LSB_CNT
) {
571 if (test_bit(bitno
, lsb_pub
)) {
572 /* We found an available LSB
573 * that this queue can access
576 bitmap_clear(lsb_pub
, bitno
, 1);
578 "Queue %d gets LSB %d\n",
582 bitmap_clear(qlsb
, bitno
, 1);
583 bitno
= find_first_bit(qlsb
, MAX_LSB_CNT
);
585 if (bitno
>= MAX_LSB_CNT
)
593 /* For each queue, from the most- to least-constrained:
594 * find an LSB that can be assigned to the queue. If there are N queues that
595 * can only use M LSBs, where N > M, fail; otherwise, every queue will get a
596 * dedicated LSB. Remaining LSB regions become a shared resource.
597 * If we have fewer LSBs than queues, all LSB regions become shared resources.
599 static int ccp_assign_lsbs(struct ccp_device
*ccp
)
601 DECLARE_BITMAP(lsb_pub
, MAX_LSB_CNT
);
602 DECLARE_BITMAP(qlsb
, MAX_LSB_CNT
);
608 bitmap_zero(lsb_pub
, MAX_LSB_CNT
);
610 /* Create an aggregate bitmap to get a total count of available LSBs */
611 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
613 lsb_pub
, ccp
->cmd_q
[i
].lsbmask
,
616 n_lsbs
= bitmap_weight(lsb_pub
, MAX_LSB_CNT
);
618 if (n_lsbs
>= ccp
->cmd_q_count
) {
619 /* We have enough LSBS to give every queue a private LSB.
620 * Brute force search to start with the queues that are more
621 * constrained in LSB choice. When an LSB is privately
622 * assigned, it is removed from the public mask.
623 * This is an ugly N squared algorithm with some optimization.
626 n_lsbs
&& (lsb_cnt
<= MAX_LSB_CNT
);
628 rc
= ccp_find_and_assign_lsb_to_q(ccp
, lsb_cnt
, n_lsbs
,
637 /* What's left of the LSBs, according to the public mask, now become
638 * shared. Any zero bits in the lsb_pub mask represent an LSB region
639 * that can't be used as a shared resource, so mark the LSB slots for
642 bitmap_copy(qlsb
, lsb_pub
, MAX_LSB_CNT
);
644 bitno
= find_first_zero_bit(qlsb
, MAX_LSB_CNT
);
645 while (bitno
< MAX_LSB_CNT
) {
646 bitmap_set(ccp
->lsbmap
, bitno
* LSB_SIZE
, LSB_SIZE
);
647 bitmap_set(qlsb
, bitno
, 1);
648 bitno
= find_first_zero_bit(qlsb
, MAX_LSB_CNT
);
654 static int ccp5_init(struct ccp_device
*ccp
)
656 struct device
*dev
= ccp
->dev
;
657 struct ccp_cmd_queue
*cmd_q
;
658 struct dma_pool
*dma_pool
;
659 char dma_pool_name
[MAX_DMAPOOL_NAME_LEN
];
660 unsigned int qmr
, qim
, i
;
662 u32 status_lo
, status_hi
;
665 /* Find available queues */
667 qmr
= ioread32(ccp
->io_regs
+ Q_MASK_REG
);
668 for (i
= 0; i
< MAX_HW_QUEUES
; i
++) {
670 if (!(qmr
& (1 << i
)))
673 /* Allocate a dma pool for this queue */
674 snprintf(dma_pool_name
, sizeof(dma_pool_name
), "%s_q%d",
676 dma_pool
= dma_pool_create(dma_pool_name
, dev
,
677 CCP_DMAPOOL_MAX_SIZE
,
678 CCP_DMAPOOL_ALIGN
, 0);
680 dev_err(dev
, "unable to allocate dma pool\n");
684 cmd_q
= &ccp
->cmd_q
[ccp
->cmd_q_count
];
689 cmd_q
->dma_pool
= dma_pool
;
690 mutex_init(&cmd_q
->q_mutex
);
692 /* Page alignment satisfies our needs for N <= 128 */
693 BUILD_BUG_ON(COMMANDS_PER_QUEUE
> 128);
694 cmd_q
->qsize
= Q_SIZE(Q_DESC_SIZE
);
695 cmd_q
->qbase
= dma_zalloc_coherent(dev
, cmd_q
->qsize
,
699 dev_err(dev
, "unable to allocate command queue\n");
705 /* Preset some register values and masks that are queue
708 cmd_q
->reg_control
= ccp
->io_regs
+
709 CMD5_Q_STATUS_INCR
* (i
+ 1);
710 cmd_q
->reg_tail_lo
= cmd_q
->reg_control
+ CMD5_Q_TAIL_LO_BASE
;
711 cmd_q
->reg_head_lo
= cmd_q
->reg_control
+ CMD5_Q_HEAD_LO_BASE
;
712 cmd_q
->reg_int_enable
= cmd_q
->reg_control
+
713 CMD5_Q_INT_ENABLE_BASE
;
714 cmd_q
->reg_interrupt_status
= cmd_q
->reg_control
+
715 CMD5_Q_INTERRUPT_STATUS_BASE
;
716 cmd_q
->reg_status
= cmd_q
->reg_control
+ CMD5_Q_STATUS_BASE
;
717 cmd_q
->reg_int_status
= cmd_q
->reg_control
+
718 CMD5_Q_INT_STATUS_BASE
;
719 cmd_q
->reg_dma_status
= cmd_q
->reg_control
+
720 CMD5_Q_DMA_STATUS_BASE
;
721 cmd_q
->reg_dma_read_status
= cmd_q
->reg_control
+
722 CMD5_Q_DMA_READ_STATUS_BASE
;
723 cmd_q
->reg_dma_write_status
= cmd_q
->reg_control
+
724 CMD5_Q_DMA_WRITE_STATUS_BASE
;
726 init_waitqueue_head(&cmd_q
->int_queue
);
728 dev_dbg(dev
, "queue #%u available\n", i
);
730 if (ccp
->cmd_q_count
== 0) {
731 dev_notice(dev
, "no command queues available\n");
735 dev_notice(dev
, "%u command queues available\n", ccp
->cmd_q_count
);
737 /* Turn off the queues and disable interrupts until ready */
738 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
739 cmd_q
= &ccp
->cmd_q
[i
];
741 cmd_q
->qcontrol
= 0; /* Start with nothing */
742 iowrite32(cmd_q
->qcontrol
, cmd_q
->reg_control
);
744 /* Disable the interrupts */
745 iowrite32(0x00, cmd_q
->reg_int_enable
);
746 ioread32(cmd_q
->reg_int_status
);
747 ioread32(cmd_q
->reg_status
);
749 /* Clear the interrupts */
750 iowrite32(ALL_INTERRUPTS
, cmd_q
->reg_interrupt_status
);
753 dev_dbg(dev
, "Requesting an IRQ...\n");
755 ret
= ccp
->get_irq(ccp
);
757 dev_err(dev
, "unable to allocate an IRQ\n");
761 dev_dbg(dev
, "Loading LSB map...\n");
762 /* Copy the private LSB mask to the public registers */
763 status_lo
= ioread32(ccp
->io_regs
+ LSB_PRIVATE_MASK_LO_OFFSET
);
764 status_hi
= ioread32(ccp
->io_regs
+ LSB_PRIVATE_MASK_HI_OFFSET
);
765 iowrite32(status_lo
, ccp
->io_regs
+ LSB_PUBLIC_MASK_LO_OFFSET
);
766 iowrite32(status_hi
, ccp
->io_regs
+ LSB_PUBLIC_MASK_HI_OFFSET
);
767 status
= ((u64
)status_hi
<<30) | (u64
)status_lo
;
769 dev_dbg(dev
, "Configuring virtual queues...\n");
770 /* Configure size of each virtual queue accessible to host */
771 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
775 cmd_q
= &ccp
->cmd_q
[i
];
777 cmd_q
->qcontrol
&= ~(CMD5_Q_SIZE
<< CMD5_Q_SHIFT
);
778 cmd_q
->qcontrol
|= QUEUE_SIZE_VAL
<< CMD5_Q_SHIFT
;
780 cmd_q
->qdma_tail
= cmd_q
->qbase_dma
;
781 dma_addr_lo
= low_address(cmd_q
->qdma_tail
);
782 iowrite32((u32
)dma_addr_lo
, cmd_q
->reg_tail_lo
);
783 iowrite32((u32
)dma_addr_lo
, cmd_q
->reg_head_lo
);
785 dma_addr_hi
= high_address(cmd_q
->qdma_tail
);
786 cmd_q
->qcontrol
|= (dma_addr_hi
<< 16);
787 iowrite32(cmd_q
->qcontrol
, cmd_q
->reg_control
);
789 /* Find the LSB regions accessible to the queue */
790 ccp_find_lsb_regions(cmd_q
, status
);
791 cmd_q
->lsb
= -1; /* Unassigned value */
794 dev_dbg(dev
, "Assigning LSBs...\n");
795 ret
= ccp_assign_lsbs(ccp
);
797 dev_err(dev
, "Unable to assign LSBs (%d)\n", ret
);
801 /* Optimization: pre-allocate LSB slots for each queue */
802 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
803 ccp
->cmd_q
[i
].sb_key
= ccp_lsb_alloc(&ccp
->cmd_q
[i
], 2);
804 ccp
->cmd_q
[i
].sb_ctx
= ccp_lsb_alloc(&ccp
->cmd_q
[i
], 2);
807 dev_dbg(dev
, "Starting threads...\n");
808 /* Create a kthread for each queue */
809 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
810 struct task_struct
*kthread
;
812 cmd_q
= &ccp
->cmd_q
[i
];
814 kthread
= kthread_create(ccp_cmd_queue_thread
, cmd_q
,
815 "%s-q%u", ccp
->name
, cmd_q
->id
);
816 if (IS_ERR(kthread
)) {
817 dev_err(dev
, "error creating queue thread (%ld)\n",
819 ret
= PTR_ERR(kthread
);
823 cmd_q
->kthread
= kthread
;
824 wake_up_process(kthread
);
827 dev_dbg(dev
, "Enabling interrupts...\n");
828 /* Enable interrupts */
829 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
830 cmd_q
= &ccp
->cmd_q
[i
];
831 iowrite32(ALL_INTERRUPTS
, cmd_q
->reg_int_enable
);
834 dev_dbg(dev
, "Registering device...\n");
835 /* Put this on the unit list to make it available */
838 ret
= ccp_register_rng(ccp
);
842 /* Register the DMA engine support */
843 ret
= ccp_dmaengine_register(ccp
);
850 ccp_unregister_rng(ccp
);
853 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
854 if (ccp
->cmd_q
[i
].kthread
)
855 kthread_stop(ccp
->cmd_q
[i
].kthread
);
861 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
862 dma_pool_destroy(ccp
->cmd_q
[i
].dma_pool
);
867 static void ccp5_destroy(struct ccp_device
*ccp
)
869 struct device
*dev
= ccp
->dev
;
870 struct ccp_cmd_queue
*cmd_q
;
874 /* Unregister the DMA engine */
875 ccp_dmaengine_unregister(ccp
);
877 /* Unregister the RNG */
878 ccp_unregister_rng(ccp
);
880 /* Remove this device from the list of available units first */
883 /* Disable and clear interrupts */
884 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
885 cmd_q
= &ccp
->cmd_q
[i
];
887 /* Turn off the run bit */
888 iowrite32(cmd_q
->qcontrol
& ~CMD5_Q_RUN
, cmd_q
->reg_control
);
890 /* Disable the interrupts */
891 iowrite32(ALL_INTERRUPTS
, cmd_q
->reg_interrupt_status
);
893 /* Clear the interrupt status */
894 iowrite32(0x00, cmd_q
->reg_int_enable
);
895 ioread32(cmd_q
->reg_int_status
);
896 ioread32(cmd_q
->reg_status
);
899 /* Stop the queue kthreads */
900 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
901 if (ccp
->cmd_q
[i
].kthread
)
902 kthread_stop(ccp
->cmd_q
[i
].kthread
);
906 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
907 cmd_q
= &ccp
->cmd_q
[i
];
908 dma_free_coherent(dev
, cmd_q
->qsize
, cmd_q
->qbase
,
912 /* Flush the cmd and backlog queue */
913 while (!list_empty(&ccp
->cmd
)) {
914 /* Invoke the callback directly with an error code */
915 cmd
= list_first_entry(&ccp
->cmd
, struct ccp_cmd
, entry
);
916 list_del(&cmd
->entry
);
917 cmd
->callback(cmd
->data
, -ENODEV
);
919 while (!list_empty(&ccp
->backlog
)) {
920 /* Invoke the callback directly with an error code */
921 cmd
= list_first_entry(&ccp
->backlog
, struct ccp_cmd
, entry
);
922 list_del(&cmd
->entry
);
923 cmd
->callback(cmd
->data
, -ENODEV
);
927 static irqreturn_t
ccp5_irq_handler(int irq
, void *data
)
929 struct device
*dev
= data
;
930 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
934 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
935 struct ccp_cmd_queue
*cmd_q
= &ccp
->cmd_q
[i
];
937 status
= ioread32(cmd_q
->reg_interrupt_status
);
940 cmd_q
->int_status
= status
;
941 cmd_q
->q_status
= ioread32(cmd_q
->reg_status
);
942 cmd_q
->q_int_status
= ioread32(cmd_q
->reg_int_status
);
944 /* On error, only save the first error value */
945 if ((status
& INT_ERROR
) && !cmd_q
->cmd_error
)
946 cmd_q
->cmd_error
= CMD_Q_ERROR(cmd_q
->q_status
);
950 /* Acknowledge the interrupt and wake the kthread */
951 iowrite32(ALL_INTERRUPTS
, cmd_q
->reg_interrupt_status
);
952 wake_up_interruptible(&cmd_q
->int_queue
);
959 static void ccp5_config(struct ccp_device
*ccp
)
962 iowrite32(0x00001249, ccp
->io_regs
+ CMD5_REQID_CONFIG_OFFSET
);
965 static void ccp5other_config(struct ccp_device
*ccp
)
970 /* We own all of the queues on the NTB CCP */
972 iowrite32(0x00012D57, ccp
->io_regs
+ CMD5_TRNG_CTL_OFFSET
);
973 iowrite32(0x00000003, ccp
->io_regs
+ CMD5_CONFIG_0_OFFSET
);
974 for (i
= 0; i
< 12; i
++) {
975 rnd
= ioread32(ccp
->io_regs
+ TRNG_OUT_REG
);
976 iowrite32(rnd
, ccp
->io_regs
+ CMD5_AES_MASK_OFFSET
);
979 iowrite32(0x0000001F, ccp
->io_regs
+ CMD5_QUEUE_MASK_OFFSET
);
980 iowrite32(0x00005B6D, ccp
->io_regs
+ CMD5_QUEUE_PRIO_OFFSET
);
981 iowrite32(0x00000000, ccp
->io_regs
+ CMD5_CMD_TIMEOUT_OFFSET
);
983 iowrite32(0x3FFFFFFF, ccp
->io_regs
+ LSB_PRIVATE_MASK_LO_OFFSET
);
984 iowrite32(0x000003FF, ccp
->io_regs
+ LSB_PRIVATE_MASK_HI_OFFSET
);
986 iowrite32(0x00108823, ccp
->io_regs
+ CMD5_CLK_GATE_CTL_OFFSET
);
991 /* Version 5 adds some function, but is essentially the same as v5 */
992 static const struct ccp_actions ccp5_actions
= {
993 .aes
= ccp5_perform_aes
,
994 .xts_aes
= ccp5_perform_xts_aes
,
995 .sha
= ccp5_perform_sha
,
996 .rsa
= ccp5_perform_rsa
,
997 .passthru
= ccp5_perform_passthru
,
998 .ecc
= ccp5_perform_ecc
,
999 .sballoc
= ccp_lsb_alloc
,
1000 .sbfree
= ccp_lsb_free
,
1002 .destroy
= ccp5_destroy
,
1003 .get_free_slots
= ccp5_get_free_slots
,
1004 .irqhandler
= ccp5_irq_handler
,
1007 const struct ccp_vdata ccpv5a
= {
1008 .version
= CCP_VERSION(5, 0),
1009 .setup
= ccp5_config
,
1010 .perform
= &ccp5_actions
,
1015 const struct ccp_vdata ccpv5b
= {
1016 .version
= CCP_VERSION(5, 0),
1017 .setup
= ccp5other_config
,
1018 .perform
= &ccp5_actions
,