Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: ccp - Let a v5 CCP provide the same function as v3

Enable equivalent function on a v5 CCP. Add support for a
version 5 CCP which enables AES/XTS/SHA services. Also,
more work on the data structures to virtualize
functionality.

Signed-off-by: Gary R Hook <gary.hook@amd.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Gary R Hook and committed by
Herbert Xu
4b394a23 bb4e89b3

+1344 -124
+1
drivers/crypto/ccp/Makefile
··· 2 2 ccp-objs := ccp-dev.o \ 3 3 ccp-ops.o \ 4 4 ccp-dev-v3.o \ 5 + ccp-dev-v5.o \ 5 6 ccp-platform.o \ 6 7 ccp-dmaengine.o 7 8 ccp-$(CONFIG_PCI) += ccp-pci.o
+17 -1
drivers/crypto/ccp/ccp-crypto-sha.c
··· 4 4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc. 5 5 * 6 6 * Author: Tom Lendacky <thomas.lendacky@amd.com> 7 + * Author: Gary R Hook <gary.hook@amd.com> 7 8 * 8 9 * This program is free software; you can redistribute it and/or modify 9 10 * it under the terms of the GNU General Public License version 2 as ··· 135 134 rctx->cmd.engine = CCP_ENGINE_SHA; 136 135 rctx->cmd.u.sha.type = rctx->type; 137 136 rctx->cmd.u.sha.ctx = &rctx->ctx_sg; 138 - rctx->cmd.u.sha.ctx_len = sizeof(rctx->ctx); 137 + 138 + switch (rctx->type) { 139 + case CCP_SHA_TYPE_1: 140 + rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE; 141 + break; 142 + case CCP_SHA_TYPE_224: 143 + rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE; 144 + break; 145 + case CCP_SHA_TYPE_256: 146 + rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE; 147 + break; 148 + default: 149 + /* Should never get here */ 150 + break; 151 + } 152 + 139 153 rctx->cmd.u.sha.src = sg; 140 154 rctx->cmd.u.sha.src_len = rctx->hash_cnt; 141 155 rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
+16 -12
drivers/crypto/ccp/ccp-dev-v3.c
··· 405 405 init_waitqueue_head(&ccp->sb_queue); 406 406 init_waitqueue_head(&ccp->suspend_queue); 407 407 408 + dev_dbg(dev, "Starting threads...\n"); 408 409 /* Create a kthread for each queue */ 409 410 for (i = 0; i < ccp->cmd_q_count; i++) { 410 411 struct task_struct *kthread; ··· 425 424 wake_up_process(kthread); 426 425 } 427 426 427 + dev_dbg(dev, "Enabling interrupts...\n"); 428 + /* Enable interrupts */ 429 + iowrite32(qim, ccp->io_regs + IRQ_MASK_REG); 430 + 431 + dev_dbg(dev, "Registering device...\n"); 432 + ccp_add_device(ccp); 433 + 428 434 /* Register the RNG */ 429 435 ccp->hwrng.name = ccp->rngname; 430 436 ccp->hwrng.read = ccp_trng_read; ··· 445 437 ret = ccp_dmaengine_register(ccp); 446 438 if (ret) 447 439 goto e_hwrng; 448 - 449 - ccp_add_device(ccp); 450 - 451 - /* Enable interrupts */ 452 - iowrite32(qim, ccp->io_regs + IRQ_MASK_REG); 453 440 454 441 return 0; 455 442 ··· 471 468 struct ccp_cmd *cmd; 472 469 unsigned int qim, i; 473 470 474 - /* Remove this device from the list of available units first */ 471 + /* Unregister the DMA engine */ 472 + ccp_dmaengine_unregister(ccp); 473 + 474 + /* Unregister the RNG */ 475 + hwrng_unregister(&ccp->hwrng); 476 + 477 + /* Remove this device from the list of available units */ 475 478 ccp_del_device(ccp); 476 479 477 480 /* Build queue interrupt mask (two interrupt masks per queue) */ ··· 496 487 ioread32(cmd_q->reg_status); 497 488 } 498 489 iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG); 499 - 500 - /* Unregister the DMA engine */ 501 - ccp_dmaengine_unregister(ccp); 502 - 503 - /* Unregister the RNG */ 504 - hwrng_unregister(&ccp->hwrng); 505 490 506 491 /* Stop the queue kthreads */ 507 492 for (i = 0; i < ccp->cmd_q_count; i++) ··· 573 570 574 571 struct ccp_vdata ccpv3 = { 575 572 .version = CCP_VERSION(3, 0), 573 + .setup = NULL, 576 574 .perform = &ccp3_actions, 577 575 .bar = 2, 578 576 .offset = 0x20000,
+961
drivers/crypto/ccp/ccp-dev-v5.c
··· 1 + /* 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 + 24 + static u32 ccp_lsb_alloc(struct ccp_cmd_queue *cmd_q, unsigned int count) 25 + { 26 + struct ccp_device *ccp; 27 + int start; 28 + 29 + /* First look at the map for the queue */ 30 + if (cmd_q->lsb >= 0) { 31 + start = (u32)bitmap_find_next_zero_area(cmd_q->lsbmap, 32 + LSB_SIZE, 33 + 0, count, 0); 34 + if (start < LSB_SIZE) { 35 + bitmap_set(cmd_q->lsbmap, start, count); 36 + return start + cmd_q->lsb * LSB_SIZE; 37 + } 38 + } 39 + 40 + /* No joy; try to get an entry from the shared blocks */ 41 + ccp = cmd_q->ccp; 42 + for (;;) { 43 + mutex_lock(&ccp->sb_mutex); 44 + 45 + start = (u32)bitmap_find_next_zero_area(ccp->lsbmap, 46 + MAX_LSB_CNT * LSB_SIZE, 47 + 0, 48 + count, 0); 49 + if (start <= MAX_LSB_CNT * LSB_SIZE) { 50 + bitmap_set(ccp->lsbmap, start, count); 51 + 52 + mutex_unlock(&ccp->sb_mutex); 53 + return start * LSB_ITEM_SIZE; 54 + } 55 + 56 + ccp->sb_avail = 0; 57 + 58 + mutex_unlock(&ccp->sb_mutex); 59 + 60 + /* Wait for KSB entries to become available */ 61 + if (wait_event_interruptible(ccp->sb_queue, ccp->sb_avail)) 62 + return 0; 63 + } 64 + } 65 + 66 + static void ccp_lsb_free(struct ccp_cmd_queue *cmd_q, unsigned int start, 67 + unsigned int count) 68 + { 69 + int lsbno = start / LSB_SIZE; 70 + 71 + if (!start) 72 + return; 73 + 74 + if (cmd_q->lsb == lsbno) { 75 + /* An entry from the private LSB */ 76 + bitmap_clear(cmd_q->lsbmap, start % LSB_SIZE, count); 77 + } else { 78 + /* From the shared LSBs */ 79 + struct ccp_device *ccp = cmd_q->ccp; 80 + 81 + mutex_lock(&ccp->sb_mutex); 82 + bitmap_clear(ccp->lsbmap, start, count); 83 + ccp->sb_avail = 1; 84 + mutex_unlock(&ccp->sb_mutex); 85 + wake_up_interruptible_all(&ccp->sb_queue); 86 + } 87 + } 88 + 89 + /* CCP version 5: Union to define the function field (cmd_reg1/dword0) */ 90 + union ccp_function { 91 + struct { 92 + u16 size:7; 93 + u16 encrypt:1; 94 + u16 mode:5; 95 + u16 type:2; 96 + } aes; 97 + struct { 98 + u16 size:7; 99 + u16 encrypt:1; 100 + u16 rsvd:5; 101 + u16 type:2; 102 + } aes_xts; 103 + struct { 104 + u16 rsvd1:10; 105 + u16 type:4; 106 + u16 rsvd2:1; 107 + } sha; 108 + struct { 109 + u16 mode:3; 110 + u16 size:12; 111 + } rsa; 112 + struct { 113 + u16 byteswap:2; 114 + u16 bitwise:3; 115 + u16 reflect:2; 116 + u16 rsvd:8; 117 + } pt; 118 + struct { 119 + u16 rsvd:13; 120 + } zlib; 121 + struct { 122 + u16 size:10; 123 + u16 type:2; 124 + u16 mode:3; 125 + } ecc; 126 + u16 raw; 127 + }; 128 + 129 + #define CCP_AES_SIZE(p) ((p)->aes.size) 130 + #define CCP_AES_ENCRYPT(p) ((p)->aes.encrypt) 131 + #define CCP_AES_MODE(p) ((p)->aes.mode) 132 + #define CCP_AES_TYPE(p) ((p)->aes.type) 133 + #define CCP_XTS_SIZE(p) ((p)->aes_xts.size) 134 + #define CCP_XTS_ENCRYPT(p) ((p)->aes_xts.encrypt) 135 + #define CCP_SHA_TYPE(p) ((p)->sha.type) 136 + #define CCP_RSA_SIZE(p) ((p)->rsa.size) 137 + #define CCP_PT_BYTESWAP(p) ((p)->pt.byteswap) 138 + #define CCP_PT_BITWISE(p) ((p)->pt.bitwise) 139 + #define CCP_ECC_MODE(p) ((p)->ecc.mode) 140 + #define CCP_ECC_AFFINE(p) ((p)->ecc.one) 141 + 142 + /* Word 0 */ 143 + #define CCP5_CMD_DW0(p) ((p)->dw0) 144 + #define CCP5_CMD_SOC(p) (CCP5_CMD_DW0(p).soc) 145 + #define CCP5_CMD_IOC(p) (CCP5_CMD_DW0(p).ioc) 146 + #define CCP5_CMD_INIT(p) (CCP5_CMD_DW0(p).init) 147 + #define CCP5_CMD_EOM(p) (CCP5_CMD_DW0(p).eom) 148 + #define CCP5_CMD_FUNCTION(p) (CCP5_CMD_DW0(p).function) 149 + #define CCP5_CMD_ENGINE(p) (CCP5_CMD_DW0(p).engine) 150 + #define CCP5_CMD_PROT(p) (CCP5_CMD_DW0(p).prot) 151 + 152 + /* Word 1 */ 153 + #define CCP5_CMD_DW1(p) ((p)->length) 154 + #define CCP5_CMD_LEN(p) (CCP5_CMD_DW1(p)) 155 + 156 + /* Word 2 */ 157 + #define CCP5_CMD_DW2(p) ((p)->src_lo) 158 + #define CCP5_CMD_SRC_LO(p) (CCP5_CMD_DW2(p)) 159 + 160 + /* Word 3 */ 161 + #define CCP5_CMD_DW3(p) ((p)->dw3) 162 + #define CCP5_CMD_SRC_MEM(p) ((p)->dw3.src_mem) 163 + #define CCP5_CMD_SRC_HI(p) ((p)->dw3.src_hi) 164 + #define CCP5_CMD_LSB_ID(p) ((p)->dw3.lsb_cxt_id) 165 + #define CCP5_CMD_FIX_SRC(p) ((p)->dw3.fixed) 166 + 167 + /* Words 4/5 */ 168 + #define CCP5_CMD_DW4(p) ((p)->dw4) 169 + #define CCP5_CMD_DST_LO(p) (CCP5_CMD_DW4(p).dst_lo) 170 + #define CCP5_CMD_DW5(p) ((p)->dw5.fields.dst_hi) 171 + #define CCP5_CMD_DST_HI(p) (CCP5_CMD_DW5(p)) 172 + #define CCP5_CMD_DST_MEM(p) ((p)->dw5.fields.dst_mem) 173 + #define CCP5_CMD_FIX_DST(p) ((p)->dw5.fields.fixed) 174 + #define CCP5_CMD_SHA_LO(p) ((p)->dw4.sha_len_lo) 175 + #define CCP5_CMD_SHA_HI(p) ((p)->dw5.sha_len_hi) 176 + 177 + /* Word 6/7 */ 178 + #define CCP5_CMD_DW6(p) ((p)->key_lo) 179 + #define CCP5_CMD_KEY_LO(p) (CCP5_CMD_DW6(p)) 180 + #define CCP5_CMD_DW7(p) ((p)->dw7) 181 + #define CCP5_CMD_KEY_HI(p) ((p)->dw7.key_hi) 182 + #define CCP5_CMD_KEY_MEM(p) ((p)->dw7.key_mem) 183 + 184 + static inline u32 low_address(unsigned long addr) 185 + { 186 + return (u64)addr & 0x0ffffffff; 187 + } 188 + 189 + static inline u32 high_address(unsigned long addr) 190 + { 191 + return ((u64)addr >> 32) & 0x00000ffff; 192 + } 193 + 194 + static unsigned int ccp5_get_free_slots(struct ccp_cmd_queue *cmd_q) 195 + { 196 + unsigned int head_idx, n; 197 + u32 head_lo, queue_start; 198 + 199 + queue_start = low_address(cmd_q->qdma_tail); 200 + head_lo = ioread32(cmd_q->reg_head_lo); 201 + head_idx = (head_lo - queue_start) / sizeof(struct ccp5_desc); 202 + 203 + n = head_idx + COMMANDS_PER_QUEUE - cmd_q->qidx - 1; 204 + 205 + return n % COMMANDS_PER_QUEUE; /* Always one unused spot */ 206 + } 207 + 208 + static int ccp5_do_cmd(struct ccp5_desc *desc, 209 + struct ccp_cmd_queue *cmd_q) 210 + { 211 + u32 *mP; 212 + __le32 *dP; 213 + u32 tail; 214 + int i; 215 + int ret = 0; 216 + 217 + if (CCP5_CMD_SOC(desc)) { 218 + CCP5_CMD_IOC(desc) = 1; 219 + CCP5_CMD_SOC(desc) = 0; 220 + } 221 + mutex_lock(&cmd_q->q_mutex); 222 + 223 + mP = (u32 *) &cmd_q->qbase[cmd_q->qidx]; 224 + dP = (__le32 *) desc; 225 + for (i = 0; i < 8; i++) 226 + mP[i] = cpu_to_le32(dP[i]); /* handle endianness */ 227 + 228 + cmd_q->qidx = (cmd_q->qidx + 1) % COMMANDS_PER_QUEUE; 229 + 230 + /* The data used by this command must be flushed to memory */ 231 + wmb(); 232 + 233 + /* Write the new tail address back to the queue register */ 234 + tail = low_address(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE); 235 + iowrite32(tail, cmd_q->reg_tail_lo); 236 + 237 + /* Turn the queue back on using our cached control register */ 238 + iowrite32(cmd_q->qcontrol | CMD5_Q_RUN, cmd_q->reg_control); 239 + mutex_unlock(&cmd_q->q_mutex); 240 + 241 + if (CCP5_CMD_IOC(desc)) { 242 + /* Wait for the job to complete */ 243 + ret = wait_event_interruptible(cmd_q->int_queue, 244 + cmd_q->int_rcvd); 245 + if (ret || cmd_q->cmd_error) { 246 + /* A version 5 device doesn't use Job IDs... */ 247 + if (!ret) 248 + ret = -EIO; 249 + } 250 + cmd_q->int_rcvd = 0; 251 + } 252 + 253 + return 0; 254 + } 255 + 256 + static int ccp5_perform_aes(struct ccp_op *op) 257 + { 258 + struct ccp5_desc desc; 259 + union ccp_function function; 260 + u32 key_addr = op->sb_key * LSB_ITEM_SIZE; 261 + 262 + /* Zero out all the fields of the command desc */ 263 + memset(&desc, 0, Q_DESC_SIZE); 264 + 265 + CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_AES; 266 + 267 + CCP5_CMD_SOC(&desc) = op->soc; 268 + CCP5_CMD_IOC(&desc) = 1; 269 + CCP5_CMD_INIT(&desc) = op->init; 270 + CCP5_CMD_EOM(&desc) = op->eom; 271 + CCP5_CMD_PROT(&desc) = 0; 272 + 273 + function.raw = 0; 274 + CCP_AES_ENCRYPT(&function) = op->u.aes.action; 275 + CCP_AES_MODE(&function) = op->u.aes.mode; 276 + CCP_AES_TYPE(&function) = op->u.aes.type; 277 + if (op->u.aes.mode == CCP_AES_MODE_CFB) 278 + CCP_AES_SIZE(&function) = 0x7f; 279 + 280 + CCP5_CMD_FUNCTION(&desc) = function.raw; 281 + 282 + CCP5_CMD_LEN(&desc) = op->src.u.dma.length; 283 + 284 + CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma); 285 + CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma); 286 + CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 287 + 288 + CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma); 289 + CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma); 290 + CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 291 + 292 + CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr); 293 + CCP5_CMD_KEY_HI(&desc) = 0; 294 + CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB; 295 + CCP5_CMD_LSB_ID(&desc) = op->sb_ctx; 296 + 297 + return ccp5_do_cmd(&desc, op->cmd_q); 298 + } 299 + 300 + static int ccp5_perform_xts_aes(struct ccp_op *op) 301 + { 302 + struct ccp5_desc desc; 303 + union ccp_function function; 304 + u32 key_addr = op->sb_key * LSB_ITEM_SIZE; 305 + 306 + /* Zero out all the fields of the command desc */ 307 + memset(&desc, 0, Q_DESC_SIZE); 308 + 309 + CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_XTS_AES_128; 310 + 311 + CCP5_CMD_SOC(&desc) = op->soc; 312 + CCP5_CMD_IOC(&desc) = 1; 313 + CCP5_CMD_INIT(&desc) = op->init; 314 + CCP5_CMD_EOM(&desc) = op->eom; 315 + CCP5_CMD_PROT(&desc) = 0; 316 + 317 + function.raw = 0; 318 + CCP_XTS_ENCRYPT(&function) = op->u.xts.action; 319 + CCP_XTS_SIZE(&function) = op->u.xts.unit_size; 320 + CCP5_CMD_FUNCTION(&desc) = function.raw; 321 + 322 + CCP5_CMD_LEN(&desc) = op->src.u.dma.length; 323 + 324 + CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma); 325 + CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma); 326 + CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 327 + 328 + CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma); 329 + CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma); 330 + CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 331 + 332 + CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr); 333 + CCP5_CMD_KEY_HI(&desc) = 0; 334 + CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB; 335 + CCP5_CMD_LSB_ID(&desc) = op->sb_ctx; 336 + 337 + return ccp5_do_cmd(&desc, op->cmd_q); 338 + } 339 + 340 + static int ccp5_perform_sha(struct ccp_op *op) 341 + { 342 + struct ccp5_desc desc; 343 + union ccp_function function; 344 + 345 + /* Zero out all the fields of the command desc */ 346 + memset(&desc, 0, Q_DESC_SIZE); 347 + 348 + CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_SHA; 349 + 350 + CCP5_CMD_SOC(&desc) = op->soc; 351 + CCP5_CMD_IOC(&desc) = 1; 352 + CCP5_CMD_INIT(&desc) = 1; 353 + CCP5_CMD_EOM(&desc) = op->eom; 354 + CCP5_CMD_PROT(&desc) = 0; 355 + 356 + function.raw = 0; 357 + CCP_SHA_TYPE(&function) = op->u.sha.type; 358 + CCP5_CMD_FUNCTION(&desc) = function.raw; 359 + 360 + CCP5_CMD_LEN(&desc) = op->src.u.dma.length; 361 + 362 + CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma); 363 + CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma); 364 + CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 365 + 366 + CCP5_CMD_LSB_ID(&desc) = op->sb_ctx; 367 + 368 + if (op->eom) { 369 + CCP5_CMD_SHA_LO(&desc) = lower_32_bits(op->u.sha.msg_bits); 370 + CCP5_CMD_SHA_HI(&desc) = upper_32_bits(op->u.sha.msg_bits); 371 + } else { 372 + CCP5_CMD_SHA_LO(&desc) = 0; 373 + CCP5_CMD_SHA_HI(&desc) = 0; 374 + } 375 + 376 + return ccp5_do_cmd(&desc, op->cmd_q); 377 + } 378 + 379 + static int ccp5_perform_rsa(struct ccp_op *op) 380 + { 381 + struct ccp5_desc desc; 382 + union ccp_function function; 383 + 384 + /* Zero out all the fields of the command desc */ 385 + memset(&desc, 0, Q_DESC_SIZE); 386 + 387 + CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_RSA; 388 + 389 + CCP5_CMD_SOC(&desc) = op->soc; 390 + CCP5_CMD_IOC(&desc) = 1; 391 + CCP5_CMD_INIT(&desc) = 0; 392 + CCP5_CMD_EOM(&desc) = 1; 393 + CCP5_CMD_PROT(&desc) = 0; 394 + 395 + function.raw = 0; 396 + CCP_RSA_SIZE(&function) = op->u.rsa.mod_size; 397 + CCP5_CMD_FUNCTION(&desc) = function.raw; 398 + 399 + CCP5_CMD_LEN(&desc) = op->u.rsa.input_len; 400 + 401 + /* Source is from external memory */ 402 + CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma); 403 + CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma); 404 + CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 405 + 406 + /* Destination is in external memory */ 407 + CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma); 408 + CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma); 409 + CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 410 + 411 + /* Key (Exponent) is in external memory */ 412 + CCP5_CMD_KEY_LO(&desc) = ccp_addr_lo(&op->exp.u.dma); 413 + CCP5_CMD_KEY_HI(&desc) = ccp_addr_hi(&op->exp.u.dma); 414 + CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 415 + 416 + return ccp5_do_cmd(&desc, op->cmd_q); 417 + } 418 + 419 + static int ccp5_perform_passthru(struct ccp_op *op) 420 + { 421 + struct ccp5_desc desc; 422 + union ccp_function function; 423 + struct ccp_dma_info *saddr = &op->src.u.dma; 424 + struct ccp_dma_info *daddr = &op->dst.u.dma; 425 + 426 + memset(&desc, 0, Q_DESC_SIZE); 427 + 428 + CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_PASSTHRU; 429 + 430 + CCP5_CMD_SOC(&desc) = 0; 431 + CCP5_CMD_IOC(&desc) = 1; 432 + CCP5_CMD_INIT(&desc) = 0; 433 + CCP5_CMD_EOM(&desc) = op->eom; 434 + CCP5_CMD_PROT(&desc) = 0; 435 + 436 + function.raw = 0; 437 + CCP_PT_BYTESWAP(&function) = op->u.passthru.byte_swap; 438 + CCP_PT_BITWISE(&function) = op->u.passthru.bit_mod; 439 + CCP5_CMD_FUNCTION(&desc) = function.raw; 440 + 441 + /* Length of source data is always 256 bytes */ 442 + if (op->src.type == CCP_MEMTYPE_SYSTEM) 443 + CCP5_CMD_LEN(&desc) = saddr->length; 444 + else 445 + CCP5_CMD_LEN(&desc) = daddr->length; 446 + 447 + if (op->src.type == CCP_MEMTYPE_SYSTEM) { 448 + CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma); 449 + CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma); 450 + CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 451 + 452 + if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP) 453 + CCP5_CMD_LSB_ID(&desc) = op->sb_key; 454 + } else { 455 + u32 key_addr = op->src.u.sb * CCP_SB_BYTES; 456 + 457 + CCP5_CMD_SRC_LO(&desc) = lower_32_bits(key_addr); 458 + CCP5_CMD_SRC_HI(&desc) = 0; 459 + CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SB; 460 + } 461 + 462 + if (op->dst.type == CCP_MEMTYPE_SYSTEM) { 463 + CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma); 464 + CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma); 465 + CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 466 + } else { 467 + u32 key_addr = op->dst.u.sb * CCP_SB_BYTES; 468 + 469 + CCP5_CMD_DST_LO(&desc) = lower_32_bits(key_addr); 470 + CCP5_CMD_DST_HI(&desc) = 0; 471 + CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SB; 472 + } 473 + 474 + return ccp5_do_cmd(&desc, op->cmd_q); 475 + } 476 + 477 + static int ccp5_perform_ecc(struct ccp_op *op) 478 + { 479 + struct ccp5_desc desc; 480 + union ccp_function function; 481 + 482 + /* Zero out all the fields of the command desc */ 483 + memset(&desc, 0, Q_DESC_SIZE); 484 + 485 + CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_ECC; 486 + 487 + CCP5_CMD_SOC(&desc) = 0; 488 + CCP5_CMD_IOC(&desc) = 1; 489 + CCP5_CMD_INIT(&desc) = 0; 490 + CCP5_CMD_EOM(&desc) = 1; 491 + CCP5_CMD_PROT(&desc) = 0; 492 + 493 + function.raw = 0; 494 + function.ecc.mode = op->u.ecc.function; 495 + CCP5_CMD_FUNCTION(&desc) = function.raw; 496 + 497 + CCP5_CMD_LEN(&desc) = op->src.u.dma.length; 498 + 499 + CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma); 500 + CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma); 501 + CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 502 + 503 + CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma); 504 + CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma); 505 + CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM; 506 + 507 + return ccp5_do_cmd(&desc, op->cmd_q); 508 + } 509 + 510 + static int ccp_find_lsb_regions(struct ccp_cmd_queue *cmd_q, u64 status) 511 + { 512 + int q_mask = 1 << cmd_q->id; 513 + int queues = 0; 514 + int j; 515 + 516 + /* Build a bit mask to know which LSBs this queue has access to. 517 + * Don't bother with segment 0 as it has special privileges. 518 + */ 519 + for (j = 1; j < MAX_LSB_CNT; j++) { 520 + if (status & q_mask) 521 + bitmap_set(cmd_q->lsbmask, j, 1); 522 + status >>= LSB_REGION_WIDTH; 523 + } 524 + queues = bitmap_weight(cmd_q->lsbmask, MAX_LSB_CNT); 525 + dev_info(cmd_q->ccp->dev, "Queue %d can access %d LSB regions\n", 526 + cmd_q->id, queues); 527 + 528 + return queues ? 0 : -EINVAL; 529 + } 530 + 531 + 532 + static int ccp_find_and_assign_lsb_to_q(struct ccp_device *ccp, 533 + int lsb_cnt, int n_lsbs, 534 + unsigned long *lsb_pub) 535 + { 536 + DECLARE_BITMAP(qlsb, MAX_LSB_CNT); 537 + int bitno; 538 + int qlsb_wgt; 539 + int i; 540 + 541 + /* For each queue: 542 + * If the count of potential LSBs available to a queue matches the 543 + * ordinal given to us in lsb_cnt: 544 + * Copy the mask of possible LSBs for this queue into "qlsb"; 545 + * For each bit in qlsb, see if the corresponding bit in the 546 + * aggregation mask is set; if so, we have a match. 547 + * If we have a match, clear the bit in the aggregation to 548 + * mark it as no longer available. 549 + * If there is no match, clear the bit in qlsb and keep looking. 550 + */ 551 + for (i = 0; i < ccp->cmd_q_count; i++) { 552 + struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i]; 553 + 554 + qlsb_wgt = bitmap_weight(cmd_q->lsbmask, MAX_LSB_CNT); 555 + 556 + if (qlsb_wgt == lsb_cnt) { 557 + bitmap_copy(qlsb, cmd_q->lsbmask, MAX_LSB_CNT); 558 + 559 + bitno = find_first_bit(qlsb, MAX_LSB_CNT); 560 + while (bitno < MAX_LSB_CNT) { 561 + if (test_bit(bitno, lsb_pub)) { 562 + /* We found an available LSB 563 + * that this queue can access 564 + */ 565 + cmd_q->lsb = bitno; 566 + bitmap_clear(lsb_pub, bitno, 1); 567 + dev_info(ccp->dev, 568 + "Queue %d gets LSB %d\n", 569 + i, bitno); 570 + break; 571 + } 572 + bitmap_clear(qlsb, bitno, 1); 573 + bitno = find_first_bit(qlsb, MAX_LSB_CNT); 574 + } 575 + if (bitno >= MAX_LSB_CNT) 576 + return -EINVAL; 577 + n_lsbs--; 578 + } 579 + } 580 + return n_lsbs; 581 + } 582 + 583 + /* For each queue, from the most- to least-constrained: 584 + * find an LSB that can be assigned to the queue. If there are N queues that 585 + * can only use M LSBs, where N > M, fail; otherwise, every queue will get a 586 + * dedicated LSB. Remaining LSB regions become a shared resource. 587 + * If we have fewer LSBs than queues, all LSB regions become shared resources. 588 + */ 589 + static int ccp_assign_lsbs(struct ccp_device *ccp) 590 + { 591 + DECLARE_BITMAP(lsb_pub, MAX_LSB_CNT); 592 + DECLARE_BITMAP(qlsb, MAX_LSB_CNT); 593 + int n_lsbs = 0; 594 + int bitno; 595 + int i, lsb_cnt; 596 + int rc = 0; 597 + 598 + bitmap_zero(lsb_pub, MAX_LSB_CNT); 599 + 600 + /* Create an aggregate bitmap to get a total count of available LSBs */ 601 + for (i = 0; i < ccp->cmd_q_count; i++) 602 + bitmap_or(lsb_pub, 603 + lsb_pub, ccp->cmd_q[i].lsbmask, 604 + MAX_LSB_CNT); 605 + 606 + n_lsbs = bitmap_weight(lsb_pub, MAX_LSB_CNT); 607 + 608 + if (n_lsbs >= ccp->cmd_q_count) { 609 + /* We have enough LSBS to give every queue a private LSB. 610 + * Brute force search to start with the queues that are more 611 + * constrained in LSB choice. When an LSB is privately 612 + * assigned, it is removed from the public mask. 613 + * This is an ugly N squared algorithm with some optimization. 614 + */ 615 + for (lsb_cnt = 1; 616 + n_lsbs && (lsb_cnt <= MAX_LSB_CNT); 617 + lsb_cnt++) { 618 + rc = ccp_find_and_assign_lsb_to_q(ccp, lsb_cnt, n_lsbs, 619 + lsb_pub); 620 + if (rc < 0) 621 + return -EINVAL; 622 + n_lsbs = rc; 623 + } 624 + } 625 + 626 + rc = 0; 627 + /* What's left of the LSBs, according to the public mask, now become 628 + * shared. Any zero bits in the lsb_pub mask represent an LSB region 629 + * that can't be used as a shared resource, so mark the LSB slots for 630 + * them as "in use". 631 + */ 632 + bitmap_copy(qlsb, lsb_pub, MAX_LSB_CNT); 633 + 634 + bitno = find_first_zero_bit(qlsb, MAX_LSB_CNT); 635 + while (bitno < MAX_LSB_CNT) { 636 + bitmap_set(ccp->lsbmap, bitno * LSB_SIZE, LSB_SIZE); 637 + bitmap_set(qlsb, bitno, 1); 638 + bitno = find_first_zero_bit(qlsb, MAX_LSB_CNT); 639 + } 640 + 641 + return rc; 642 + } 643 + 644 + static int ccp5_init(struct ccp_device *ccp) 645 + { 646 + struct device *dev = ccp->dev; 647 + struct ccp_cmd_queue *cmd_q; 648 + struct dma_pool *dma_pool; 649 + char dma_pool_name[MAX_DMAPOOL_NAME_LEN]; 650 + unsigned int qmr, qim, i; 651 + u64 status; 652 + u32 status_lo, status_hi; 653 + int ret; 654 + 655 + /* Find available queues */ 656 + qim = 0; 657 + qmr = ioread32(ccp->io_regs + Q_MASK_REG); 658 + for (i = 0; i < MAX_HW_QUEUES; i++) { 659 + 660 + if (!(qmr & (1 << i))) 661 + continue; 662 + 663 + /* Allocate a dma pool for this queue */ 664 + snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d", 665 + ccp->name, i); 666 + dma_pool = dma_pool_create(dma_pool_name, dev, 667 + CCP_DMAPOOL_MAX_SIZE, 668 + CCP_DMAPOOL_ALIGN, 0); 669 + if (!dma_pool) { 670 + dev_err(dev, "unable to allocate dma pool\n"); 671 + ret = -ENOMEM; 672 + } 673 + 674 + cmd_q = &ccp->cmd_q[ccp->cmd_q_count]; 675 + ccp->cmd_q_count++; 676 + 677 + cmd_q->ccp = ccp; 678 + cmd_q->id = i; 679 + cmd_q->dma_pool = dma_pool; 680 + mutex_init(&cmd_q->q_mutex); 681 + 682 + /* Page alignment satisfies our needs for N <= 128 */ 683 + BUILD_BUG_ON(COMMANDS_PER_QUEUE > 128); 684 + cmd_q->qsize = Q_SIZE(Q_DESC_SIZE); 685 + cmd_q->qbase = dma_zalloc_coherent(dev, cmd_q->qsize, 686 + &cmd_q->qbase_dma, 687 + GFP_KERNEL); 688 + if (!cmd_q->qbase) { 689 + dev_err(dev, "unable to allocate command queue\n"); 690 + ret = -ENOMEM; 691 + goto e_pool; 692 + } 693 + 694 + cmd_q->qidx = 0; 695 + /* Preset some register values and masks that are queue 696 + * number dependent 697 + */ 698 + cmd_q->reg_control = ccp->io_regs + 699 + CMD5_Q_STATUS_INCR * (i + 1); 700 + cmd_q->reg_tail_lo = cmd_q->reg_control + CMD5_Q_TAIL_LO_BASE; 701 + cmd_q->reg_head_lo = cmd_q->reg_control + CMD5_Q_HEAD_LO_BASE; 702 + cmd_q->reg_int_enable = cmd_q->reg_control + 703 + CMD5_Q_INT_ENABLE_BASE; 704 + cmd_q->reg_interrupt_status = cmd_q->reg_control + 705 + CMD5_Q_INTERRUPT_STATUS_BASE; 706 + cmd_q->reg_status = cmd_q->reg_control + CMD5_Q_STATUS_BASE; 707 + cmd_q->reg_int_status = cmd_q->reg_control + 708 + CMD5_Q_INT_STATUS_BASE; 709 + cmd_q->reg_dma_status = cmd_q->reg_control + 710 + CMD5_Q_DMA_STATUS_BASE; 711 + cmd_q->reg_dma_read_status = cmd_q->reg_control + 712 + CMD5_Q_DMA_READ_STATUS_BASE; 713 + cmd_q->reg_dma_write_status = cmd_q->reg_control + 714 + CMD5_Q_DMA_WRITE_STATUS_BASE; 715 + 716 + init_waitqueue_head(&cmd_q->int_queue); 717 + 718 + dev_dbg(dev, "queue #%u available\n", i); 719 + } 720 + if (ccp->cmd_q_count == 0) { 721 + dev_notice(dev, "no command queues available\n"); 722 + ret = -EIO; 723 + goto e_pool; 724 + } 725 + dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count); 726 + 727 + /* Turn off the queues and disable interrupts until ready */ 728 + for (i = 0; i < ccp->cmd_q_count; i++) { 729 + cmd_q = &ccp->cmd_q[i]; 730 + 731 + cmd_q->qcontrol = 0; /* Start with nothing */ 732 + iowrite32(cmd_q->qcontrol, cmd_q->reg_control); 733 + 734 + /* Disable the interrupts */ 735 + iowrite32(0x00, cmd_q->reg_int_enable); 736 + ioread32(cmd_q->reg_int_status); 737 + ioread32(cmd_q->reg_status); 738 + 739 + /* Clear the interrupts */ 740 + iowrite32(ALL_INTERRUPTS, cmd_q->reg_interrupt_status); 741 + } 742 + 743 + dev_dbg(dev, "Requesting an IRQ...\n"); 744 + /* Request an irq */ 745 + ret = ccp->get_irq(ccp); 746 + if (ret) { 747 + dev_err(dev, "unable to allocate an IRQ\n"); 748 + goto e_pool; 749 + } 750 + 751 + /* Initialize the queue used to suspend */ 752 + init_waitqueue_head(&ccp->suspend_queue); 753 + 754 + dev_dbg(dev, "Loading LSB map...\n"); 755 + /* Copy the private LSB mask to the public registers */ 756 + status_lo = ioread32(ccp->io_regs + LSB_PRIVATE_MASK_LO_OFFSET); 757 + status_hi = ioread32(ccp->io_regs + LSB_PRIVATE_MASK_HI_OFFSET); 758 + iowrite32(status_lo, ccp->io_regs + LSB_PUBLIC_MASK_LO_OFFSET); 759 + iowrite32(status_hi, ccp->io_regs + LSB_PUBLIC_MASK_HI_OFFSET); 760 + status = ((u64)status_hi<<30) | (u64)status_lo; 761 + 762 + dev_dbg(dev, "Configuring virtual queues...\n"); 763 + /* Configure size of each virtual queue accessible to host */ 764 + for (i = 0; i < ccp->cmd_q_count; i++) { 765 + u32 dma_addr_lo; 766 + u32 dma_addr_hi; 767 + 768 + cmd_q = &ccp->cmd_q[i]; 769 + 770 + cmd_q->qcontrol &= ~(CMD5_Q_SIZE << CMD5_Q_SHIFT); 771 + cmd_q->qcontrol |= QUEUE_SIZE_VAL << CMD5_Q_SHIFT; 772 + 773 + cmd_q->qdma_tail = cmd_q->qbase_dma; 774 + dma_addr_lo = low_address(cmd_q->qdma_tail); 775 + iowrite32((u32)dma_addr_lo, cmd_q->reg_tail_lo); 776 + iowrite32((u32)dma_addr_lo, cmd_q->reg_head_lo); 777 + 778 + dma_addr_hi = high_address(cmd_q->qdma_tail); 779 + cmd_q->qcontrol |= (dma_addr_hi << 16); 780 + iowrite32(cmd_q->qcontrol, cmd_q->reg_control); 781 + 782 + /* Find the LSB regions accessible to the queue */ 783 + ccp_find_lsb_regions(cmd_q, status); 784 + cmd_q->lsb = -1; /* Unassigned value */ 785 + } 786 + 787 + dev_dbg(dev, "Assigning LSBs...\n"); 788 + ret = ccp_assign_lsbs(ccp); 789 + if (ret) { 790 + dev_err(dev, "Unable to assign LSBs (%d)\n", ret); 791 + goto e_irq; 792 + } 793 + 794 + /* Optimization: pre-allocate LSB slots for each queue */ 795 + for (i = 0; i < ccp->cmd_q_count; i++) { 796 + ccp->cmd_q[i].sb_key = ccp_lsb_alloc(&ccp->cmd_q[i], 2); 797 + ccp->cmd_q[i].sb_ctx = ccp_lsb_alloc(&ccp->cmd_q[i], 2); 798 + } 799 + 800 + dev_dbg(dev, "Starting threads...\n"); 801 + /* Create a kthread for each queue */ 802 + for (i = 0; i < ccp->cmd_q_count; i++) { 803 + struct task_struct *kthread; 804 + 805 + cmd_q = &ccp->cmd_q[i]; 806 + 807 + kthread = kthread_create(ccp_cmd_queue_thread, cmd_q, 808 + "%s-q%u", ccp->name, cmd_q->id); 809 + if (IS_ERR(kthread)) { 810 + dev_err(dev, "error creating queue thread (%ld)\n", 811 + PTR_ERR(kthread)); 812 + ret = PTR_ERR(kthread); 813 + goto e_kthread; 814 + } 815 + 816 + cmd_q->kthread = kthread; 817 + wake_up_process(kthread); 818 + } 819 + 820 + dev_dbg(dev, "Enabling interrupts...\n"); 821 + /* Enable interrupts */ 822 + for (i = 0; i < ccp->cmd_q_count; i++) { 823 + cmd_q = &ccp->cmd_q[i]; 824 + iowrite32(ALL_INTERRUPTS, cmd_q->reg_int_enable); 825 + } 826 + 827 + dev_dbg(dev, "Registering device...\n"); 828 + /* Put this on the unit list to make it available */ 829 + ccp_add_device(ccp); 830 + 831 + return 0; 832 + 833 + e_kthread: 834 + for (i = 0; i < ccp->cmd_q_count; i++) 835 + if (ccp->cmd_q[i].kthread) 836 + kthread_stop(ccp->cmd_q[i].kthread); 837 + 838 + e_irq: 839 + ccp->free_irq(ccp); 840 + 841 + e_pool: 842 + for (i = 0; i < ccp->cmd_q_count; i++) 843 + dma_pool_destroy(ccp->cmd_q[i].dma_pool); 844 + 845 + return ret; 846 + } 847 + 848 + static void ccp5_destroy(struct ccp_device *ccp) 849 + { 850 + struct device *dev = ccp->dev; 851 + struct ccp_cmd_queue *cmd_q; 852 + struct ccp_cmd *cmd; 853 + unsigned int i; 854 + 855 + /* Remove this device from the list of available units first */ 856 + ccp_del_device(ccp); 857 + 858 + /* Disable and clear interrupts */ 859 + for (i = 0; i < ccp->cmd_q_count; i++) { 860 + cmd_q = &ccp->cmd_q[i]; 861 + 862 + /* Turn off the run bit */ 863 + iowrite32(cmd_q->qcontrol & ~CMD5_Q_RUN, cmd_q->reg_control); 864 + 865 + /* Disable the interrupts */ 866 + iowrite32(ALL_INTERRUPTS, cmd_q->reg_interrupt_status); 867 + 868 + /* Clear the interrupt status */ 869 + iowrite32(0x00, cmd_q->reg_int_enable); 870 + ioread32(cmd_q->reg_int_status); 871 + ioread32(cmd_q->reg_status); 872 + } 873 + 874 + /* Stop the queue kthreads */ 875 + for (i = 0; i < ccp->cmd_q_count; i++) 876 + if (ccp->cmd_q[i].kthread) 877 + kthread_stop(ccp->cmd_q[i].kthread); 878 + 879 + ccp->free_irq(ccp); 880 + 881 + for (i = 0; i < ccp->cmd_q_count; i++) { 882 + cmd_q = &ccp->cmd_q[i]; 883 + dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase, 884 + cmd_q->qbase_dma); 885 + } 886 + 887 + /* Flush the cmd and backlog queue */ 888 + while (!list_empty(&ccp->cmd)) { 889 + /* Invoke the callback directly with an error code */ 890 + cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry); 891 + list_del(&cmd->entry); 892 + cmd->callback(cmd->data, -ENODEV); 893 + } 894 + while (!list_empty(&ccp->backlog)) { 895 + /* Invoke the callback directly with an error code */ 896 + cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry); 897 + list_del(&cmd->entry); 898 + cmd->callback(cmd->data, -ENODEV); 899 + } 900 + } 901 + 902 + static irqreturn_t ccp5_irq_handler(int irq, void *data) 903 + { 904 + struct device *dev = data; 905 + struct ccp_device *ccp = dev_get_drvdata(dev); 906 + u32 status; 907 + unsigned int i; 908 + 909 + for (i = 0; i < ccp->cmd_q_count; i++) { 910 + struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i]; 911 + 912 + status = ioread32(cmd_q->reg_interrupt_status); 913 + 914 + if (status) { 915 + cmd_q->int_status = status; 916 + cmd_q->q_status = ioread32(cmd_q->reg_status); 917 + cmd_q->q_int_status = ioread32(cmd_q->reg_int_status); 918 + 919 + /* On error, only save the first error value */ 920 + if ((status & INT_ERROR) && !cmd_q->cmd_error) 921 + cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status); 922 + 923 + cmd_q->int_rcvd = 1; 924 + 925 + /* Acknowledge the interrupt and wake the kthread */ 926 + iowrite32(ALL_INTERRUPTS, cmd_q->reg_interrupt_status); 927 + wake_up_interruptible(&cmd_q->int_queue); 928 + } 929 + } 930 + 931 + return IRQ_HANDLED; 932 + } 933 + 934 + static void ccp5_config(struct ccp_device *ccp) 935 + { 936 + /* Public side */ 937 + iowrite32(0x00001249, ccp->io_regs + CMD5_REQID_CONFIG_OFFSET); 938 + } 939 + 940 + static const struct ccp_actions ccp5_actions = { 941 + .aes = ccp5_perform_aes, 942 + .xts_aes = ccp5_perform_xts_aes, 943 + .sha = ccp5_perform_sha, 944 + .rsa = ccp5_perform_rsa, 945 + .passthru = ccp5_perform_passthru, 946 + .ecc = ccp5_perform_ecc, 947 + .sballoc = ccp_lsb_alloc, 948 + .sbfree = ccp_lsb_free, 949 + .init = ccp5_init, 950 + .destroy = ccp5_destroy, 951 + .get_free_slots = ccp5_get_free_slots, 952 + .irqhandler = ccp5_irq_handler, 953 + }; 954 + 955 + struct ccp_vdata ccpv5 = { 956 + .version = CCP_VERSION(5, 0), 957 + .setup = ccp5_config, 958 + .perform = &ccp5_actions, 959 + .bar = 2, 960 + .offset = 0x0, 961 + };
+161 -3
drivers/crypto/ccp/ccp-dev.h
··· 61 61 #define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f) 62 62 #define CMD_Q_DEPTH(__qs) (((__qs) >> 12) & 0x0000000f) 63 63 64 - /****** REQ0 Related Values ******/ 64 + /* ------------------------ CCP Version 5 Specifics ------------------------ */ 65 + #define CMD5_QUEUE_MASK_OFFSET 0x00 66 + #define CMD5_REQID_CONFIG_OFFSET 0x08 67 + #define LSB_PUBLIC_MASK_LO_OFFSET 0x18 68 + #define LSB_PUBLIC_MASK_HI_OFFSET 0x1C 69 + #define LSB_PRIVATE_MASK_LO_OFFSET 0x20 70 + #define LSB_PRIVATE_MASK_HI_OFFSET 0x24 71 + 72 + #define CMD5_Q_CONTROL_BASE 0x0000 73 + #define CMD5_Q_TAIL_LO_BASE 0x0004 74 + #define CMD5_Q_HEAD_LO_BASE 0x0008 75 + #define CMD5_Q_INT_ENABLE_BASE 0x000C 76 + #define CMD5_Q_INTERRUPT_STATUS_BASE 0x0010 77 + 78 + #define CMD5_Q_STATUS_BASE 0x0100 79 + #define CMD5_Q_INT_STATUS_BASE 0x0104 80 + #define CMD5_Q_DMA_STATUS_BASE 0x0108 81 + #define CMD5_Q_DMA_READ_STATUS_BASE 0x010C 82 + #define CMD5_Q_DMA_WRITE_STATUS_BASE 0x0110 83 + #define CMD5_Q_ABORT_BASE 0x0114 84 + #define CMD5_Q_AX_CACHE_BASE 0x0118 85 + 86 + /* Address offset between two virtual queue registers */ 87 + #define CMD5_Q_STATUS_INCR 0x1000 88 + 89 + /* Bit masks */ 90 + #define CMD5_Q_RUN 0x1 91 + #define CMD5_Q_HALT 0x2 92 + #define CMD5_Q_MEM_LOCATION 0x4 93 + #define CMD5_Q_SIZE 0x1F 94 + #define CMD5_Q_SHIFT 3 95 + #define COMMANDS_PER_QUEUE 16 96 + #define QUEUE_SIZE_VAL ((ffs(COMMANDS_PER_QUEUE) - 2) & \ 97 + CMD5_Q_SIZE) 98 + #define Q_PTR_MASK (2 << (QUEUE_SIZE_VAL + 5) - 1) 99 + #define Q_DESC_SIZE sizeof(struct ccp5_desc) 100 + #define Q_SIZE(n) (COMMANDS_PER_QUEUE*(n)) 101 + 102 + #define INT_COMPLETION 0x1 103 + #define INT_ERROR 0x2 104 + #define INT_QUEUE_STOPPED 0x4 105 + #define ALL_INTERRUPTS (INT_COMPLETION| \ 106 + INT_ERROR| \ 107 + INT_QUEUE_STOPPED) 108 + 109 + #define LSB_REGION_WIDTH 5 110 + #define MAX_LSB_CNT 8 111 + 112 + #define LSB_SIZE 16 113 + #define LSB_ITEM_SIZE 32 114 + #define PLSB_MAP_SIZE (LSB_SIZE) 115 + #define SLSB_MAP_SIZE (MAX_LSB_CNT * LSB_SIZE) 116 + 117 + #define LSB_ENTRY_NUMBER(LSB_ADDR) (LSB_ADDR / LSB_ITEM_SIZE) 118 + 119 + /* ------------------------ CCP Version 3 Specifics ------------------------ */ 65 120 #define REQ0_WAIT_FOR_WRITE 0x00000004 66 121 #define REQ0_INT_ON_COMPLETE 0x00000002 67 122 #define REQ0_STOP_ON_COMPLETE 0x00000001 ··· 170 115 171 116 #define CCP_JOBID_MASK 0x0000003f 172 117 118 + /* ------------------------ General CCP Defines ------------------------ */ 119 + 173 120 #define CCP_DMAPOOL_MAX_SIZE 64 174 121 #define CCP_DMAPOOL_ALIGN BIT(5) 175 122 ··· 206 149 struct ccp_op; 207 150 struct ccp_device; 208 151 struct ccp_cmd; 152 + struct ccp_fns; 209 153 210 154 struct ccp_dma_cmd { 211 155 struct list_head entry; ··· 250 192 /* Queue dma pool */ 251 193 struct dma_pool *dma_pool; 252 194 195 + /* Queue base address (not neccessarily aligned)*/ 196 + struct ccp5_desc *qbase; 197 + 198 + /* Aligned queue start address (per requirement) */ 199 + struct mutex q_mutex ____cacheline_aligned; 200 + unsigned int qidx; 201 + 202 + /* Version 5 has different requirements for queue memory */ 203 + unsigned int qsize; 204 + dma_addr_t qbase_dma; 205 + dma_addr_t qdma_tail; 206 + 253 207 /* Per-queue reserved storage block(s) */ 254 208 u32 sb_key; 255 209 u32 sb_ctx; 210 + 211 + /* Bitmap of LSBs that can be accessed by this queue */ 212 + DECLARE_BITMAP(lsbmask, MAX_LSB_CNT); 213 + /* Private LSB that is assigned to this queue, or -1 if none. 214 + * Bitmap for my private LSB, unused otherwise 215 + */ 216 + unsigned int lsb; 217 + DECLARE_BITMAP(lsbmap, PLSB_MAP_SIZE); 256 218 257 219 /* Queue processing thread */ 258 220 struct task_struct *kthread; ··· 287 209 u32 int_err; 288 210 289 211 /* Register addresses for queue */ 212 + void __iomem *reg_control; 213 + void __iomem *reg_tail_lo; 214 + void __iomem *reg_head_lo; 215 + void __iomem *reg_int_enable; 216 + void __iomem *reg_interrupt_status; 290 217 void __iomem *reg_status; 291 218 void __iomem *reg_int_status; 219 + void __iomem *reg_dma_status; 220 + void __iomem *reg_dma_read_status; 221 + void __iomem *reg_dma_write_status; 222 + u32 qcontrol; /* Cached control register */ 292 223 293 224 /* Status values from job */ 294 225 u32 int_status; ··· 393 306 unsigned int sb_count; 394 307 u32 sb_start; 395 308 309 + /* Bitmap of shared LSBs, if any */ 310 + DECLARE_BITMAP(lsbmap, SLSB_MAP_SIZE); 311 + 396 312 /* Suspend support */ 397 313 unsigned int suspending; 398 314 wait_queue_head_t suspend_queue; ··· 410 320 CCP_MEMTYPE_LOCAL, 411 321 CCP_MEMTYPE__LAST, 412 322 }; 323 + #define CCP_MEMTYPE_LSB CCP_MEMTYPE_KSB 413 324 414 325 struct ccp_dma_info { 415 326 dma_addr_t address; ··· 498 407 499 408 struct ccp_mem src; 500 409 struct ccp_mem dst; 410 + struct ccp_mem exp; 501 411 502 412 union { 503 413 struct ccp_aes_op aes; ··· 508 416 struct ccp_passthru_op passthru; 509 417 struct ccp_ecc_op ecc; 510 418 } u; 419 + struct ccp_mem key; 511 420 }; 512 421 513 422 static inline u32 ccp_addr_lo(struct ccp_dma_info *info) ··· 520 427 { 521 428 return upper_32_bits(info->address + info->offset) & 0x0000ffff; 522 429 } 430 + 431 + /** 432 + * descriptor for version 5 CPP commands 433 + * 8 32-bit words: 434 + * word 0: function; engine; control bits 435 + * word 1: length of source data 436 + * word 2: low 32 bits of source pointer 437 + * word 3: upper 16 bits of source pointer; source memory type 438 + * word 4: low 32 bits of destination pointer 439 + * word 5: upper 16 bits of destination pointer; destination memory type 440 + * word 6: low 32 bits of key pointer 441 + * word 7: upper 16 bits of key pointer; key memory type 442 + */ 443 + struct dword0 { 444 + __le32 soc:1; 445 + __le32 ioc:1; 446 + __le32 rsvd1:1; 447 + __le32 init:1; 448 + __le32 eom:1; /* AES/SHA only */ 449 + __le32 function:15; 450 + __le32 engine:4; 451 + __le32 prot:1; 452 + __le32 rsvd2:7; 453 + }; 454 + 455 + struct dword3 { 456 + __le32 src_hi:16; 457 + __le32 src_mem:2; 458 + __le32 lsb_cxt_id:8; 459 + __le32 rsvd1:5; 460 + __le32 fixed:1; 461 + }; 462 + 463 + union dword4 { 464 + __le32 dst_lo; /* NON-SHA */ 465 + __le32 sha_len_lo; /* SHA */ 466 + }; 467 + 468 + union dword5 { 469 + struct { 470 + __le32 dst_hi:16; 471 + __le32 dst_mem:2; 472 + __le32 rsvd1:13; 473 + __le32 fixed:1; 474 + } fields; 475 + __le32 sha_len_hi; 476 + }; 477 + 478 + struct dword7 { 479 + __le32 key_hi:16; 480 + __le32 key_mem:2; 481 + __le32 rsvd1:14; 482 + }; 483 + 484 + struct ccp5_desc { 485 + struct dword0 dw0; 486 + __le32 length; 487 + __le32 src_lo; 488 + struct dword3 dw3; 489 + union dword4 dw4; 490 + union dword5 dw5; 491 + __le32 key_lo; 492 + struct dword7 dw7; 493 + }; 523 494 524 495 int ccp_pci_init(void); 525 496 void ccp_pci_exit(void); ··· 623 466 624 467 /* Structure to hold CCP version-specific values */ 625 468 struct ccp_vdata { 626 - unsigned int version; 627 - int (*init)(struct ccp_device *); 469 + const unsigned int version; 470 + void (*setup)(struct ccp_device *); 628 471 const struct ccp_actions *perform; 629 472 const unsigned int bar; 630 473 const unsigned int offset; 631 474 }; 632 475 633 476 extern struct ccp_vdata ccpv3; 477 + extern struct ccp_vdata ccpv5; 634 478 635 479 #endif
+183 -104
drivers/crypto/ccp/ccp-ops.c
··· 21 21 #include "ccp-dev.h" 22 22 23 23 /* SHA initial context values */ 24 - static const __be32 ccp_sha1_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = { 24 + static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = { 25 25 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1), 26 26 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3), 27 - cpu_to_be32(SHA1_H4), 0, 0, 0, 27 + cpu_to_be32(SHA1_H4), 28 28 }; 29 29 30 - static const __be32 ccp_sha224_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = { 30 + static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = { 31 31 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1), 32 32 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3), 33 33 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5), 34 34 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7), 35 35 }; 36 36 37 - static const __be32 ccp_sha256_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = { 37 + static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = { 38 38 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1), 39 39 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3), 40 40 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5), 41 41 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7), 42 42 }; 43 + 44 + #define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \ 45 + ccp_gen_jobid(ccp) : 0) 43 46 44 47 static u32 ccp_gen_jobid(struct ccp_device *ccp) 45 48 { ··· 490 487 ret = -EIO; 491 488 memset(&op, 0, sizeof(op)); 492 489 op.cmd_q = cmd_q; 493 - op.jobid = ccp_gen_jobid(cmd_q->ccp); 490 + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); 494 491 op.sb_key = cmd_q->sb_key; 495 492 op.sb_ctx = cmd_q->sb_ctx; 496 493 op.init = 1; ··· 643 640 ret = -EIO; 644 641 memset(&op, 0, sizeof(op)); 645 642 op.cmd_q = cmd_q; 646 - op.jobid = ccp_gen_jobid(cmd_q->ccp); 643 + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); 647 644 op.sb_key = cmd_q->sb_key; 648 645 op.sb_ctx = cmd_q->sb_ctx; 649 646 op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1; ··· 682 679 goto e_key; 683 680 684 681 if (aes->mode != CCP_AES_MODE_ECB) { 685 - /* Load the AES context - conver to LE */ 682 + /* Load the AES context - convert to LE */ 686 683 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE; 687 684 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len); 688 685 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx, ··· 820 817 ret = -EIO; 821 818 memset(&op, 0, sizeof(op)); 822 819 op.cmd_q = cmd_q; 823 - op.jobid = ccp_gen_jobid(cmd_q->ccp); 820 + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); 824 821 op.sb_key = cmd_q->sb_key; 825 822 op.sb_ctx = cmd_q->sb_ctx; 826 823 op.init = 1; ··· 939 936 struct ccp_dm_workarea ctx; 940 937 struct ccp_data src; 941 938 struct ccp_op op; 939 + unsigned int ioffset, ooffset; 940 + unsigned int digest_size; 941 + int sb_count; 942 + const void *init; 943 + u64 block_size; 944 + int ctx_size; 942 945 int ret; 943 946 944 - if (sha->ctx_len != CCP_SHA_CTXSIZE) 947 + switch (sha->type) { 948 + case CCP_SHA_TYPE_1: 949 + if (sha->ctx_len < SHA1_DIGEST_SIZE) 950 + return -EINVAL; 951 + block_size = SHA1_BLOCK_SIZE; 952 + break; 953 + case CCP_SHA_TYPE_224: 954 + if (sha->ctx_len < SHA224_DIGEST_SIZE) 955 + return -EINVAL; 956 + block_size = SHA224_BLOCK_SIZE; 957 + break; 958 + case CCP_SHA_TYPE_256: 959 + if (sha->ctx_len < SHA256_DIGEST_SIZE) 960 + return -EINVAL; 961 + block_size = SHA256_BLOCK_SIZE; 962 + break; 963 + default: 945 964 return -EINVAL; 965 + } 946 966 947 967 if (!sha->ctx) 948 968 return -EINVAL; 949 969 950 - if (!sha->final && (sha->src_len & (CCP_SHA_BLOCKSIZE - 1))) 970 + if (!sha->final && (sha->src_len & (block_size - 1))) 951 971 return -EINVAL; 952 972 953 - if (!sha->src_len) { 954 - const u8 *sha_zero; 973 + /* The version 3 device can't handle zero-length input */ 974 + if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) { 955 975 956 - /* Not final, just return */ 957 - if (!sha->final) 976 + if (!sha->src_len) { 977 + unsigned int digest_len; 978 + const u8 *sha_zero; 979 + 980 + /* Not final, just return */ 981 + if (!sha->final) 982 + return 0; 983 + 984 + /* CCP can't do a zero length sha operation so the 985 + * caller must buffer the data. 986 + */ 987 + if (sha->msg_bits) 988 + return -EINVAL; 989 + 990 + /* The CCP cannot perform zero-length sha operations 991 + * so the caller is required to buffer data for the 992 + * final operation. However, a sha operation for a 993 + * message with a total length of zero is valid so 994 + * known values are required to supply the result. 995 + */ 996 + switch (sha->type) { 997 + case CCP_SHA_TYPE_1: 998 + sha_zero = sha1_zero_message_hash; 999 + digest_len = SHA1_DIGEST_SIZE; 1000 + break; 1001 + case CCP_SHA_TYPE_224: 1002 + sha_zero = sha224_zero_message_hash; 1003 + digest_len = SHA224_DIGEST_SIZE; 1004 + break; 1005 + case CCP_SHA_TYPE_256: 1006 + sha_zero = sha256_zero_message_hash; 1007 + digest_len = SHA256_DIGEST_SIZE; 1008 + break; 1009 + default: 1010 + return -EINVAL; 1011 + } 1012 + 1013 + scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0, 1014 + digest_len, 1); 1015 + 958 1016 return 0; 959 - 960 - /* CCP can't do a zero length sha operation so the caller 961 - * must buffer the data. 962 - */ 963 - if (sha->msg_bits) 964 - return -EINVAL; 965 - 966 - /* The CCP cannot perform zero-length sha operations so the 967 - * caller is required to buffer data for the final operation. 968 - * However, a sha operation for a message with a total length 969 - * of zero is valid so known values are required to supply 970 - * the result. 971 - */ 972 - switch (sha->type) { 973 - case CCP_SHA_TYPE_1: 974 - sha_zero = sha1_zero_message_hash; 975 - break; 976 - case CCP_SHA_TYPE_224: 977 - sha_zero = sha224_zero_message_hash; 978 - break; 979 - case CCP_SHA_TYPE_256: 980 - sha_zero = sha256_zero_message_hash; 981 - break; 982 - default: 983 - return -EINVAL; 984 1017 } 985 - 986 - scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0, 987 - sha->ctx_len, 1); 988 - 989 - return 0; 990 1018 } 991 1019 992 - if (!sha->src) 993 - return -EINVAL; 1020 + /* Set variables used throughout */ 1021 + switch (sha->type) { 1022 + case CCP_SHA_TYPE_1: 1023 + digest_size = SHA1_DIGEST_SIZE; 1024 + init = (void *) ccp_sha1_init; 1025 + ctx_size = SHA1_DIGEST_SIZE; 1026 + sb_count = 1; 1027 + if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0)) 1028 + ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE; 1029 + else 1030 + ooffset = ioffset = 0; 1031 + break; 1032 + case CCP_SHA_TYPE_224: 1033 + digest_size = SHA224_DIGEST_SIZE; 1034 + init = (void *) ccp_sha224_init; 1035 + ctx_size = SHA256_DIGEST_SIZE; 1036 + sb_count = 1; 1037 + ioffset = 0; 1038 + if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0)) 1039 + ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE; 1040 + else 1041 + ooffset = 0; 1042 + break; 1043 + case CCP_SHA_TYPE_256: 1044 + digest_size = SHA256_DIGEST_SIZE; 1045 + init = (void *) ccp_sha256_init; 1046 + ctx_size = SHA256_DIGEST_SIZE; 1047 + sb_count = 1; 1048 + ooffset = ioffset = 0; 1049 + break; 1050 + default: 1051 + ret = -EINVAL; 1052 + goto e_data; 1053 + } 994 1054 995 - BUILD_BUG_ON(CCP_SHA_SB_COUNT != 1); 1055 + /* For zero-length plaintext the src pointer is ignored; 1056 + * otherwise both parts must be valid 1057 + */ 1058 + if (sha->src_len && !sha->src) 1059 + return -EINVAL; 996 1060 997 1061 memset(&op, 0, sizeof(op)); 998 1062 op.cmd_q = cmd_q; 999 - op.jobid = ccp_gen_jobid(cmd_q->ccp); 1000 - op.sb_ctx = cmd_q->sb_ctx; 1063 + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); 1064 + op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */ 1001 1065 op.u.sha.type = sha->type; 1002 1066 op.u.sha.msg_bits = sha->msg_bits; 1003 1067 1004 - /* The SHA context fits in a single (32-byte) SB entry and 1005 - * must be in little endian format. Use the 256-bit byte swap 1006 - * passthru option to convert from big endian to little endian. 1007 - */ 1008 - ret = ccp_init_dm_workarea(&ctx, cmd_q, 1009 - CCP_SHA_SB_COUNT * CCP_SB_BYTES, 1068 + ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES, 1010 1069 DMA_BIDIRECTIONAL); 1011 1070 if (ret) 1012 1071 return ret; 1013 - 1014 1072 if (sha->first) { 1015 - const __be32 *init; 1016 - 1017 1073 switch (sha->type) { 1018 1074 case CCP_SHA_TYPE_1: 1019 - init = ccp_sha1_init; 1020 - break; 1021 1075 case CCP_SHA_TYPE_224: 1022 - init = ccp_sha224_init; 1023 - break; 1024 1076 case CCP_SHA_TYPE_256: 1025 - init = ccp_sha256_init; 1077 + memcpy(ctx.address + ioffset, init, ctx_size); 1026 1078 break; 1027 1079 default: 1028 1080 ret = -EINVAL; 1029 1081 goto e_ctx; 1030 1082 } 1031 - memcpy(ctx.address, init, CCP_SHA_CTXSIZE); 1032 1083 } else { 1033 - ccp_set_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len); 1084 + /* Restore the context */ 1085 + ccp_set_dm_area(&ctx, 0, sha->ctx, 0, 1086 + sb_count * CCP_SB_BYTES); 1034 1087 } 1035 1088 1036 1089 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx, ··· 1096 1037 goto e_ctx; 1097 1038 } 1098 1039 1099 - /* Send data to the CCP SHA engine */ 1100 - ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len, 1101 - CCP_SHA_BLOCKSIZE, DMA_TO_DEVICE); 1102 - if (ret) 1103 - goto e_ctx; 1040 + if (sha->src) { 1041 + /* Send data to the CCP SHA engine; block_size is set above */ 1042 + ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len, 1043 + block_size, DMA_TO_DEVICE); 1044 + if (ret) 1045 + goto e_ctx; 1104 1046 1105 - while (src.sg_wa.bytes_left) { 1106 - ccp_prepare_data(&src, NULL, &op, CCP_SHA_BLOCKSIZE, false); 1107 - if (sha->final && !src.sg_wa.bytes_left) 1108 - op.eom = 1; 1047 + while (src.sg_wa.bytes_left) { 1048 + ccp_prepare_data(&src, NULL, &op, block_size, false); 1049 + if (sha->final && !src.sg_wa.bytes_left) 1050 + op.eom = 1; 1109 1051 1052 + ret = cmd_q->ccp->vdata->perform->sha(&op); 1053 + if (ret) { 1054 + cmd->engine_error = cmd_q->cmd_error; 1055 + goto e_data; 1056 + } 1057 + 1058 + ccp_process_data(&src, NULL, &op); 1059 + } 1060 + } else { 1061 + op.eom = 1; 1110 1062 ret = cmd_q->ccp->vdata->perform->sha(&op); 1111 1063 if (ret) { 1112 1064 cmd->engine_error = cmd_q->cmd_error; 1113 1065 goto e_data; 1114 1066 } 1115 - 1116 - ccp_process_data(&src, NULL, &op); 1117 1067 } 1118 1068 1119 1069 /* Retrieve the SHA context - convert from LE to BE using ··· 1135 1067 goto e_data; 1136 1068 } 1137 1069 1138 - ccp_get_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len); 1070 + if (sha->final) { 1071 + /* Finishing up, so get the digest */ 1072 + switch (sha->type) { 1073 + case CCP_SHA_TYPE_1: 1074 + case CCP_SHA_TYPE_224: 1075 + case CCP_SHA_TYPE_256: 1076 + ccp_get_dm_area(&ctx, ooffset, 1077 + sha->ctx, 0, 1078 + digest_size); 1079 + break; 1080 + default: 1081 + ret = -EINVAL; 1082 + goto e_ctx; 1083 + } 1084 + } else { 1085 + /* Stash the context */ 1086 + ccp_get_dm_area(&ctx, 0, sha->ctx, 0, 1087 + sb_count * CCP_SB_BYTES); 1088 + } 1139 1089 1140 1090 if (sha->final && sha->opad) { 1141 1091 /* HMAC operation, recursively perform final SHA */ 1142 1092 struct ccp_cmd hmac_cmd; 1143 1093 struct scatterlist sg; 1144 - u64 block_size, digest_size; 1145 1094 u8 *hmac_buf; 1146 - 1147 - switch (sha->type) { 1148 - case CCP_SHA_TYPE_1: 1149 - block_size = SHA1_BLOCK_SIZE; 1150 - digest_size = SHA1_DIGEST_SIZE; 1151 - break; 1152 - case CCP_SHA_TYPE_224: 1153 - block_size = SHA224_BLOCK_SIZE; 1154 - digest_size = SHA224_DIGEST_SIZE; 1155 - break; 1156 - case CCP_SHA_TYPE_256: 1157 - block_size = SHA256_BLOCK_SIZE; 1158 - digest_size = SHA256_DIGEST_SIZE; 1159 - break; 1160 - default: 1161 - ret = -EINVAL; 1162 - goto e_data; 1163 - } 1164 1095 1165 1096 if (sha->opad_len != block_size) { 1166 1097 ret = -EINVAL; ··· 1174 1107 sg_init_one(&sg, hmac_buf, block_size + digest_size); 1175 1108 1176 1109 scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0); 1177 - memcpy(hmac_buf + block_size, ctx.address, digest_size); 1110 + switch (sha->type) { 1111 + case CCP_SHA_TYPE_1: 1112 + case CCP_SHA_TYPE_224: 1113 + case CCP_SHA_TYPE_256: 1114 + memcpy(hmac_buf + block_size, 1115 + ctx.address + ooffset, 1116 + digest_size); 1117 + break; 1118 + default: 1119 + ret = -EINVAL; 1120 + goto e_ctx; 1121 + } 1178 1122 1179 1123 memset(&hmac_cmd, 0, sizeof(hmac_cmd)); 1180 1124 hmac_cmd.engine = CCP_ENGINE_SHA; ··· 1208 1130 } 1209 1131 1210 1132 e_data: 1211 - ccp_free_data(&src, cmd_q); 1133 + if (sha->src) 1134 + ccp_free_data(&src, cmd_q); 1212 1135 1213 1136 e_ctx: 1214 1137 ccp_dm_free(&ctx); ··· 1340 1261 struct ccp_op op; 1341 1262 bool in_place = false; 1342 1263 unsigned int i; 1343 - int ret; 1264 + int ret = 0; 1344 1265 1345 1266 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1))) 1346 1267 return -EINVAL; ··· 1359 1280 1360 1281 memset(&op, 0, sizeof(op)); 1361 1282 op.cmd_q = cmd_q; 1362 - op.jobid = ccp_gen_jobid(cmd_q->ccp); 1283 + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); 1363 1284 1364 1285 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) { 1365 1286 /* Load the mask */ ··· 1548 1469 1549 1470 memset(&op, 0, sizeof(op)); 1550 1471 op.cmd_q = cmd_q; 1551 - op.jobid = ccp_gen_jobid(cmd_q->ccp); 1472 + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); 1552 1473 1553 1474 /* Concatenate the modulus and the operands. Both the modulus and 1554 1475 * the operands must be in little endian format. Since the input ··· 1673 1594 1674 1595 memset(&op, 0, sizeof(op)); 1675 1596 op.cmd_q = cmd_q; 1676 - op.jobid = ccp_gen_jobid(cmd_q->ccp); 1597 + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); 1677 1598 1678 1599 /* Concatenate the modulus and the operands. Both the modulus and 1679 1600 * the operands must be in little endian format. Since the input ··· 1711 1632 goto e_src; 1712 1633 src.address += CCP_ECC_OPERAND_SIZE; 1713 1634 1714 - /* Set the first point Z coordianate to 1 */ 1635 + /* Set the first point Z coordinate to 1 */ 1715 1636 *src.address = 0x01; 1716 1637 src.address += CCP_ECC_OPERAND_SIZE; 1717 1638 ··· 1730 1651 goto e_src; 1731 1652 src.address += CCP_ECC_OPERAND_SIZE; 1732 1653 1733 - /* Set the second point Z coordianate to 1 */ 1654 + /* Set the second point Z coordinate to 1 */ 1734 1655 *src.address = 0x01; 1735 1656 src.address += CCP_ECC_OPERAND_SIZE; 1736 1657 } else {
+5 -1
drivers/crypto/ccp/ccp-pci.c
··· 141 141 free_irq(ccp_pci->msix[ccp_pci->msix_count].vector, 142 142 dev); 143 143 pci_disable_msix(pdev); 144 - } else { 144 + } else if (ccp->irq) { 145 145 free_irq(ccp->irq, dev); 146 146 pci_disable_msi(pdev); 147 147 } 148 + ccp->irq = 0; 148 149 } 149 150 150 151 static int ccp_find_mmio_area(struct ccp_device *ccp) ··· 230 229 231 230 dev_set_drvdata(dev, ccp); 232 231 232 + if (ccp->vdata->setup) 233 + ccp->vdata->setup(ccp); 233 234 ret = ccp->vdata->perform->init(ccp); 234 235 if (ret) 235 236 goto e_iomap; ··· 324 321 325 322 static const struct pci_device_id ccp_pci_table[] = { 326 323 { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&ccpv3 }, 324 + { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&ccpv5 }, 327 325 /* Last entry must be zero */ 328 326 { 0, } 329 327 };
-3
include/linux/ccp.h
··· 238 238 }; 239 239 240 240 /***** SHA engine *****/ 241 - #define CCP_SHA_BLOCKSIZE SHA256_BLOCK_SIZE 242 - #define CCP_SHA_CTXSIZE SHA256_DIGEST_SIZE 243 - 244 241 /** 245 242 * ccp_sha_type - type of SHA operation 246 243 *