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

crypto: ccp - Add abstraction for device-specific calls

Support for different generations of the coprocessor
requires that an abstraction layer be implemented for
interacting with the hardware. This patch splits out
version-specific functions to a separate file and populates
the version structure (acting as a driver) with function
pointers.

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

authored by

Gary R Hook and committed by
Herbert Xu
ea0375af c7019c4d

+711 -668
+1 -1
drivers/crypto/ccp/Makefile
··· 1 1 obj-$(CONFIG_CRYPTO_DEV_CCP_DD) += ccp.o 2 - ccp-objs := ccp-dev.o ccp-ops.o ccp-platform.o 2 + ccp-objs := ccp-dev.o ccp-ops.o ccp-dev-v3.o ccp-platform.o 3 3 ccp-$(CONFIG_PCI) += ccp-pci.o 4 4 5 5 obj-$(CONFIG_CRYPTO_DEV_CCP_CRYPTO) += ccp-crypto.o
+533
drivers/crypto/ccp/ccp-dev-v3.c
··· 1 + /* 2 + * AMD Cryptographic Coprocessor (CCP) driver 3 + * 4 + * Copyright (C) 2013,2016 Advanced Micro Devices, Inc. 5 + * 6 + * Author: Tom Lendacky <thomas.lendacky@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/interrupt.h> 18 + #include <linux/ccp.h> 19 + 20 + #include "ccp-dev.h" 21 + 22 + static int ccp_do_cmd(struct ccp_op *op, u32 *cr, unsigned int cr_count) 23 + { 24 + struct ccp_cmd_queue *cmd_q = op->cmd_q; 25 + struct ccp_device *ccp = cmd_q->ccp; 26 + void __iomem *cr_addr; 27 + u32 cr0, cmd; 28 + unsigned int i; 29 + int ret = 0; 30 + 31 + /* We could read a status register to see how many free slots 32 + * are actually available, but reading that register resets it 33 + * and you could lose some error information. 34 + */ 35 + cmd_q->free_slots--; 36 + 37 + cr0 = (cmd_q->id << REQ0_CMD_Q_SHIFT) 38 + | (op->jobid << REQ0_JOBID_SHIFT) 39 + | REQ0_WAIT_FOR_WRITE; 40 + 41 + if (op->soc) 42 + cr0 |= REQ0_STOP_ON_COMPLETE 43 + | REQ0_INT_ON_COMPLETE; 44 + 45 + if (op->ioc || !cmd_q->free_slots) 46 + cr0 |= REQ0_INT_ON_COMPLETE; 47 + 48 + /* Start at CMD_REQ1 */ 49 + cr_addr = ccp->io_regs + CMD_REQ0 + CMD_REQ_INCR; 50 + 51 + mutex_lock(&ccp->req_mutex); 52 + 53 + /* Write CMD_REQ1 through CMD_REQx first */ 54 + for (i = 0; i < cr_count; i++, cr_addr += CMD_REQ_INCR) 55 + iowrite32(*(cr + i), cr_addr); 56 + 57 + /* Tell the CCP to start */ 58 + wmb(); 59 + iowrite32(cr0, ccp->io_regs + CMD_REQ0); 60 + 61 + mutex_unlock(&ccp->req_mutex); 62 + 63 + if (cr0 & REQ0_INT_ON_COMPLETE) { 64 + /* Wait for the job to complete */ 65 + ret = wait_event_interruptible(cmd_q->int_queue, 66 + cmd_q->int_rcvd); 67 + if (ret || cmd_q->cmd_error) { 68 + /* On error delete all related jobs from the queue */ 69 + cmd = (cmd_q->id << DEL_Q_ID_SHIFT) 70 + | op->jobid; 71 + 72 + iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB); 73 + 74 + if (!ret) 75 + ret = -EIO; 76 + } else if (op->soc) { 77 + /* Delete just head job from the queue on SoC */ 78 + cmd = DEL_Q_ACTIVE 79 + | (cmd_q->id << DEL_Q_ID_SHIFT) 80 + | op->jobid; 81 + 82 + iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB); 83 + } 84 + 85 + cmd_q->free_slots = CMD_Q_DEPTH(cmd_q->q_status); 86 + 87 + cmd_q->int_rcvd = 0; 88 + } 89 + 90 + return ret; 91 + } 92 + 93 + static int ccp_perform_aes(struct ccp_op *op) 94 + { 95 + u32 cr[6]; 96 + 97 + /* Fill out the register contents for REQ1 through REQ6 */ 98 + cr[0] = (CCP_ENGINE_AES << REQ1_ENGINE_SHIFT) 99 + | (op->u.aes.type << REQ1_AES_TYPE_SHIFT) 100 + | (op->u.aes.mode << REQ1_AES_MODE_SHIFT) 101 + | (op->u.aes.action << REQ1_AES_ACTION_SHIFT) 102 + | (op->ksb_key << REQ1_KEY_KSB_SHIFT); 103 + cr[1] = op->src.u.dma.length - 1; 104 + cr[2] = ccp_addr_lo(&op->src.u.dma); 105 + cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 106 + | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 107 + | ccp_addr_hi(&op->src.u.dma); 108 + cr[4] = ccp_addr_lo(&op->dst.u.dma); 109 + cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 110 + | ccp_addr_hi(&op->dst.u.dma); 111 + 112 + if (op->u.aes.mode == CCP_AES_MODE_CFB) 113 + cr[0] |= ((0x7f) << REQ1_AES_CFB_SIZE_SHIFT); 114 + 115 + if (op->eom) 116 + cr[0] |= REQ1_EOM; 117 + 118 + if (op->init) 119 + cr[0] |= REQ1_INIT; 120 + 121 + return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 122 + } 123 + 124 + static int ccp_perform_xts_aes(struct ccp_op *op) 125 + { 126 + u32 cr[6]; 127 + 128 + /* Fill out the register contents for REQ1 through REQ6 */ 129 + cr[0] = (CCP_ENGINE_XTS_AES_128 << REQ1_ENGINE_SHIFT) 130 + | (op->u.xts.action << REQ1_AES_ACTION_SHIFT) 131 + | (op->u.xts.unit_size << REQ1_XTS_AES_SIZE_SHIFT) 132 + | (op->ksb_key << REQ1_KEY_KSB_SHIFT); 133 + cr[1] = op->src.u.dma.length - 1; 134 + cr[2] = ccp_addr_lo(&op->src.u.dma); 135 + cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 136 + | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 137 + | ccp_addr_hi(&op->src.u.dma); 138 + cr[4] = ccp_addr_lo(&op->dst.u.dma); 139 + cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 140 + | ccp_addr_hi(&op->dst.u.dma); 141 + 142 + if (op->eom) 143 + cr[0] |= REQ1_EOM; 144 + 145 + if (op->init) 146 + cr[0] |= REQ1_INIT; 147 + 148 + return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 149 + } 150 + 151 + static int ccp_perform_sha(struct ccp_op *op) 152 + { 153 + u32 cr[6]; 154 + 155 + /* Fill out the register contents for REQ1 through REQ6 */ 156 + cr[0] = (CCP_ENGINE_SHA << REQ1_ENGINE_SHIFT) 157 + | (op->u.sha.type << REQ1_SHA_TYPE_SHIFT) 158 + | REQ1_INIT; 159 + cr[1] = op->src.u.dma.length - 1; 160 + cr[2] = ccp_addr_lo(&op->src.u.dma); 161 + cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 162 + | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 163 + | ccp_addr_hi(&op->src.u.dma); 164 + 165 + if (op->eom) { 166 + cr[0] |= REQ1_EOM; 167 + cr[4] = lower_32_bits(op->u.sha.msg_bits); 168 + cr[5] = upper_32_bits(op->u.sha.msg_bits); 169 + } else { 170 + cr[4] = 0; 171 + cr[5] = 0; 172 + } 173 + 174 + return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 175 + } 176 + 177 + static int ccp_perform_rsa(struct ccp_op *op) 178 + { 179 + u32 cr[6]; 180 + 181 + /* Fill out the register contents for REQ1 through REQ6 */ 182 + cr[0] = (CCP_ENGINE_RSA << REQ1_ENGINE_SHIFT) 183 + | (op->u.rsa.mod_size << REQ1_RSA_MOD_SIZE_SHIFT) 184 + | (op->ksb_key << REQ1_KEY_KSB_SHIFT) 185 + | REQ1_EOM; 186 + cr[1] = op->u.rsa.input_len - 1; 187 + cr[2] = ccp_addr_lo(&op->src.u.dma); 188 + cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 189 + | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 190 + | ccp_addr_hi(&op->src.u.dma); 191 + cr[4] = ccp_addr_lo(&op->dst.u.dma); 192 + cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 193 + | ccp_addr_hi(&op->dst.u.dma); 194 + 195 + return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 196 + } 197 + 198 + static int ccp_perform_passthru(struct ccp_op *op) 199 + { 200 + u32 cr[6]; 201 + 202 + /* Fill out the register contents for REQ1 through REQ6 */ 203 + cr[0] = (CCP_ENGINE_PASSTHRU << REQ1_ENGINE_SHIFT) 204 + | (op->u.passthru.bit_mod << REQ1_PT_BW_SHIFT) 205 + | (op->u.passthru.byte_swap << REQ1_PT_BS_SHIFT); 206 + 207 + if (op->src.type == CCP_MEMTYPE_SYSTEM) 208 + cr[1] = op->src.u.dma.length - 1; 209 + else 210 + cr[1] = op->dst.u.dma.length - 1; 211 + 212 + if (op->src.type == CCP_MEMTYPE_SYSTEM) { 213 + cr[2] = ccp_addr_lo(&op->src.u.dma); 214 + cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 215 + | ccp_addr_hi(&op->src.u.dma); 216 + 217 + if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP) 218 + cr[3] |= (op->ksb_key << REQ4_KSB_SHIFT); 219 + } else { 220 + cr[2] = op->src.u.ksb * CCP_KSB_BYTES; 221 + cr[3] = (CCP_MEMTYPE_KSB << REQ4_MEMTYPE_SHIFT); 222 + } 223 + 224 + if (op->dst.type == CCP_MEMTYPE_SYSTEM) { 225 + cr[4] = ccp_addr_lo(&op->dst.u.dma); 226 + cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 227 + | ccp_addr_hi(&op->dst.u.dma); 228 + } else { 229 + cr[4] = op->dst.u.ksb * CCP_KSB_BYTES; 230 + cr[5] = (CCP_MEMTYPE_KSB << REQ6_MEMTYPE_SHIFT); 231 + } 232 + 233 + if (op->eom) 234 + cr[0] |= REQ1_EOM; 235 + 236 + return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 237 + } 238 + 239 + static int ccp_perform_ecc(struct ccp_op *op) 240 + { 241 + u32 cr[6]; 242 + 243 + /* Fill out the register contents for REQ1 through REQ6 */ 244 + cr[0] = REQ1_ECC_AFFINE_CONVERT 245 + | (CCP_ENGINE_ECC << REQ1_ENGINE_SHIFT) 246 + | (op->u.ecc.function << REQ1_ECC_FUNCTION_SHIFT) 247 + | REQ1_EOM; 248 + cr[1] = op->src.u.dma.length - 1; 249 + cr[2] = ccp_addr_lo(&op->src.u.dma); 250 + cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 251 + | ccp_addr_hi(&op->src.u.dma); 252 + cr[4] = ccp_addr_lo(&op->dst.u.dma); 253 + cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 254 + | ccp_addr_hi(&op->dst.u.dma); 255 + 256 + return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 257 + } 258 + 259 + static int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait) 260 + { 261 + struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng); 262 + u32 trng_value; 263 + int len = min_t(int, sizeof(trng_value), max); 264 + 265 + /* 266 + * Locking is provided by the caller so we can update device 267 + * hwrng-related fields safely 268 + */ 269 + trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG); 270 + if (!trng_value) { 271 + /* Zero is returned if not data is available or if a 272 + * bad-entropy error is present. Assume an error if 273 + * we exceed TRNG_RETRIES reads of zero. 274 + */ 275 + if (ccp->hwrng_retries++ > TRNG_RETRIES) 276 + return -EIO; 277 + 278 + return 0; 279 + } 280 + 281 + /* Reset the counter and save the rng value */ 282 + ccp->hwrng_retries = 0; 283 + memcpy(data, &trng_value, len); 284 + 285 + return len; 286 + } 287 + 288 + static int ccp_init(struct ccp_device *ccp) 289 + { 290 + struct device *dev = ccp->dev; 291 + struct ccp_cmd_queue *cmd_q; 292 + struct dma_pool *dma_pool; 293 + char dma_pool_name[MAX_DMAPOOL_NAME_LEN]; 294 + unsigned int qmr, qim, i; 295 + int ret; 296 + 297 + /* Find available queues */ 298 + qim = 0; 299 + qmr = ioread32(ccp->io_regs + Q_MASK_REG); 300 + for (i = 0; i < MAX_HW_QUEUES; i++) { 301 + if (!(qmr & (1 << i))) 302 + continue; 303 + 304 + /* Allocate a dma pool for this queue */ 305 + snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d", 306 + ccp->name, i); 307 + dma_pool = dma_pool_create(dma_pool_name, dev, 308 + CCP_DMAPOOL_MAX_SIZE, 309 + CCP_DMAPOOL_ALIGN, 0); 310 + if (!dma_pool) { 311 + dev_err(dev, "unable to allocate dma pool\n"); 312 + ret = -ENOMEM; 313 + goto e_pool; 314 + } 315 + 316 + cmd_q = &ccp->cmd_q[ccp->cmd_q_count]; 317 + ccp->cmd_q_count++; 318 + 319 + cmd_q->ccp = ccp; 320 + cmd_q->id = i; 321 + cmd_q->dma_pool = dma_pool; 322 + 323 + /* Reserve 2 KSB regions for the queue */ 324 + cmd_q->ksb_key = KSB_START + ccp->ksb_start++; 325 + cmd_q->ksb_ctx = KSB_START + ccp->ksb_start++; 326 + ccp->ksb_count -= 2; 327 + 328 + /* Preset some register values and masks that are queue 329 + * number dependent 330 + */ 331 + cmd_q->reg_status = ccp->io_regs + CMD_Q_STATUS_BASE + 332 + (CMD_Q_STATUS_INCR * i); 333 + cmd_q->reg_int_status = ccp->io_regs + CMD_Q_INT_STATUS_BASE + 334 + (CMD_Q_STATUS_INCR * i); 335 + cmd_q->int_ok = 1 << (i * 2); 336 + cmd_q->int_err = 1 << ((i * 2) + 1); 337 + 338 + cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status)); 339 + 340 + init_waitqueue_head(&cmd_q->int_queue); 341 + 342 + /* Build queue interrupt mask (two interrupts per queue) */ 343 + qim |= cmd_q->int_ok | cmd_q->int_err; 344 + 345 + #ifdef CONFIG_ARM64 346 + /* For arm64 set the recommended queue cache settings */ 347 + iowrite32(ccp->axcache, ccp->io_regs + CMD_Q_CACHE_BASE + 348 + (CMD_Q_CACHE_INC * i)); 349 + #endif 350 + 351 + dev_dbg(dev, "queue #%u available\n", i); 352 + } 353 + if (ccp->cmd_q_count == 0) { 354 + dev_notice(dev, "no command queues available\n"); 355 + ret = -EIO; 356 + goto e_pool; 357 + } 358 + dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count); 359 + 360 + /* Disable and clear interrupts until ready */ 361 + iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG); 362 + for (i = 0; i < ccp->cmd_q_count; i++) { 363 + cmd_q = &ccp->cmd_q[i]; 364 + 365 + ioread32(cmd_q->reg_int_status); 366 + ioread32(cmd_q->reg_status); 367 + } 368 + iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG); 369 + 370 + /* Request an irq */ 371 + ret = ccp->get_irq(ccp); 372 + if (ret) { 373 + dev_err(dev, "unable to allocate an IRQ\n"); 374 + goto e_pool; 375 + } 376 + 377 + /* Initialize the queues used to wait for KSB space and suspend */ 378 + init_waitqueue_head(&ccp->ksb_queue); 379 + init_waitqueue_head(&ccp->suspend_queue); 380 + 381 + /* Create a kthread for each queue */ 382 + for (i = 0; i < ccp->cmd_q_count; i++) { 383 + struct task_struct *kthread; 384 + 385 + cmd_q = &ccp->cmd_q[i]; 386 + 387 + kthread = kthread_create(ccp_cmd_queue_thread, cmd_q, 388 + "%s-q%u", ccp->name, cmd_q->id); 389 + if (IS_ERR(kthread)) { 390 + dev_err(dev, "error creating queue thread (%ld)\n", 391 + PTR_ERR(kthread)); 392 + ret = PTR_ERR(kthread); 393 + goto e_kthread; 394 + } 395 + 396 + cmd_q->kthread = kthread; 397 + wake_up_process(kthread); 398 + } 399 + 400 + /* Register the RNG */ 401 + ccp->hwrng.name = ccp->rngname; 402 + ccp->hwrng.read = ccp_trng_read; 403 + ret = hwrng_register(&ccp->hwrng); 404 + if (ret) { 405 + dev_err(dev, "error registering hwrng (%d)\n", ret); 406 + goto e_kthread; 407 + } 408 + 409 + ccp_add_device(ccp); 410 + 411 + /* Enable interrupts */ 412 + iowrite32(qim, ccp->io_regs + IRQ_MASK_REG); 413 + 414 + return 0; 415 + 416 + e_kthread: 417 + for (i = 0; i < ccp->cmd_q_count; i++) 418 + if (ccp->cmd_q[i].kthread) 419 + kthread_stop(ccp->cmd_q[i].kthread); 420 + 421 + ccp->free_irq(ccp); 422 + 423 + e_pool: 424 + for (i = 0; i < ccp->cmd_q_count; i++) 425 + dma_pool_destroy(ccp->cmd_q[i].dma_pool); 426 + 427 + return ret; 428 + } 429 + 430 + static void ccp_destroy(struct ccp_device *ccp) 431 + { 432 + struct ccp_cmd_queue *cmd_q; 433 + struct ccp_cmd *cmd; 434 + unsigned int qim, i; 435 + 436 + /* Remove this device from the list of available units first */ 437 + ccp_del_device(ccp); 438 + 439 + /* Unregister the RNG */ 440 + hwrng_unregister(&ccp->hwrng); 441 + 442 + /* Stop the queue kthreads */ 443 + for (i = 0; i < ccp->cmd_q_count; i++) 444 + if (ccp->cmd_q[i].kthread) 445 + kthread_stop(ccp->cmd_q[i].kthread); 446 + 447 + /* Build queue interrupt mask (two interrupt masks per queue) */ 448 + qim = 0; 449 + for (i = 0; i < ccp->cmd_q_count; i++) { 450 + cmd_q = &ccp->cmd_q[i]; 451 + qim |= cmd_q->int_ok | cmd_q->int_err; 452 + } 453 + 454 + /* Disable and clear interrupts */ 455 + iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG); 456 + for (i = 0; i < ccp->cmd_q_count; i++) { 457 + cmd_q = &ccp->cmd_q[i]; 458 + 459 + ioread32(cmd_q->reg_int_status); 460 + ioread32(cmd_q->reg_status); 461 + } 462 + iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG); 463 + 464 + ccp->free_irq(ccp); 465 + 466 + for (i = 0; i < ccp->cmd_q_count; i++) 467 + dma_pool_destroy(ccp->cmd_q[i].dma_pool); 468 + 469 + /* Flush the cmd and backlog queue */ 470 + while (!list_empty(&ccp->cmd)) { 471 + /* Invoke the callback directly with an error code */ 472 + cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry); 473 + list_del(&cmd->entry); 474 + cmd->callback(cmd->data, -ENODEV); 475 + } 476 + while (!list_empty(&ccp->backlog)) { 477 + /* Invoke the callback directly with an error code */ 478 + cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry); 479 + list_del(&cmd->entry); 480 + cmd->callback(cmd->data, -ENODEV); 481 + } 482 + } 483 + 484 + static irqreturn_t ccp_irq_handler(int irq, void *data) 485 + { 486 + struct device *dev = data; 487 + struct ccp_device *ccp = dev_get_drvdata(dev); 488 + struct ccp_cmd_queue *cmd_q; 489 + u32 q_int, status; 490 + unsigned int i; 491 + 492 + status = ioread32(ccp->io_regs + IRQ_STATUS_REG); 493 + 494 + for (i = 0; i < ccp->cmd_q_count; i++) { 495 + cmd_q = &ccp->cmd_q[i]; 496 + 497 + q_int = status & (cmd_q->int_ok | cmd_q->int_err); 498 + if (q_int) { 499 + cmd_q->int_status = status; 500 + cmd_q->q_status = ioread32(cmd_q->reg_status); 501 + cmd_q->q_int_status = ioread32(cmd_q->reg_int_status); 502 + 503 + /* On error, only save the first error value */ 504 + if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error) 505 + cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status); 506 + 507 + cmd_q->int_rcvd = 1; 508 + 509 + /* Acknowledge the interrupt and wake the kthread */ 510 + iowrite32(q_int, ccp->io_regs + IRQ_STATUS_REG); 511 + wake_up_interruptible(&cmd_q->int_queue); 512 + } 513 + } 514 + 515 + return IRQ_HANDLED; 516 + } 517 + 518 + static struct ccp_actions ccp3_actions = { 519 + .perform_aes = ccp_perform_aes, 520 + .perform_xts_aes = ccp_perform_xts_aes, 521 + .perform_sha = ccp_perform_sha, 522 + .perform_rsa = ccp_perform_rsa, 523 + .perform_passthru = ccp_perform_passthru, 524 + .perform_ecc = ccp_perform_ecc, 525 + .init = ccp_init, 526 + .destroy = ccp_destroy, 527 + .irqhandler = ccp_irq_handler, 528 + }; 529 + 530 + struct ccp_vdata ccpv3 = { 531 + .version = CCP_VERSION(3, 0), 532 + .perform = &ccp3_actions, 533 + };
+21 -285
drivers/crypto/ccp/ccp-dev.c
··· 63 63 return atomic_inc_return(&ccp_unit_ordinal); 64 64 } 65 65 66 - /* 66 + /** 67 + * ccp_add_device - add a CCP device to the list 68 + * 69 + * @ccp: ccp_device struct pointer 70 + * 67 71 * Put this CCP on the unit list, which makes it available 68 72 * for use. 73 + * 74 + * Returns zero if a CCP device is present, -ENODEV otherwise. 69 75 */ 70 - static inline void ccp_add_device(struct ccp_device *ccp) 76 + void ccp_add_device(struct ccp_device *ccp) 71 77 { 72 78 unsigned long flags; 73 79 ··· 87 81 write_unlock_irqrestore(&ccp_unit_lock, flags); 88 82 } 89 83 90 - /* Remove this unit from the list of devices. If the next device 84 + /** 85 + * ccp_del_device - remove a CCP device from the list 86 + * 87 + * @ccp: ccp_device struct pointer 88 + * 89 + * Remove this unit from the list of devices. If the next device 91 90 * up for use is this one, adjust the pointer. If this is the last 92 91 * device, NULL the pointer. 93 92 */ 94 - static inline void ccp_del_device(struct ccp_device *ccp) 93 + void ccp_del_device(struct ccp_device *ccp) 95 94 { 96 95 unsigned long flags; 97 96 ··· 337 326 complete(&tdata->completion); 338 327 } 339 328 340 - static int ccp_cmd_queue_thread(void *data) 329 + /** 330 + * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue 331 + * 332 + * @data: thread-specific data 333 + */ 334 + int ccp_cmd_queue_thread(void *data) 341 335 { 342 336 struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data; 343 337 struct ccp_cmd *cmd; ··· 378 362 return 0; 379 363 } 380 364 381 - static int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait) 382 - { 383 - struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng); 384 - u32 trng_value; 385 - int len = min_t(int, sizeof(trng_value), max); 386 - 387 - /* 388 - * Locking is provided by the caller so we can update device 389 - * hwrng-related fields safely 390 - */ 391 - trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG); 392 - if (!trng_value) { 393 - /* Zero is returned if not data is available or if a 394 - * bad-entropy error is present. Assume an error if 395 - * we exceed TRNG_RETRIES reads of zero. 396 - */ 397 - if (ccp->hwrng_retries++ > TRNG_RETRIES) 398 - return -EIO; 399 - 400 - return 0; 401 - } 402 - 403 - /* Reset the counter and save the rng value */ 404 - ccp->hwrng_retries = 0; 405 - memcpy(data, &trng_value, len); 406 - 407 - return len; 408 - } 409 - 410 365 /** 411 366 * ccp_alloc_struct - allocate and initialize the ccp_device struct 412 367 * ··· 408 421 return ccp; 409 422 } 410 423 411 - /** 412 - * ccp_init - initialize the CCP device 413 - * 414 - * @ccp: ccp_device struct 415 - */ 416 - int ccp_init(struct ccp_device *ccp) 417 - { 418 - struct device *dev = ccp->dev; 419 - struct ccp_cmd_queue *cmd_q; 420 - struct dma_pool *dma_pool; 421 - char dma_pool_name[MAX_DMAPOOL_NAME_LEN]; 422 - unsigned int qmr, qim, i; 423 - int ret; 424 - 425 - /* Find available queues */ 426 - qim = 0; 427 - qmr = ioread32(ccp->io_regs + Q_MASK_REG); 428 - for (i = 0; i < MAX_HW_QUEUES; i++) { 429 - if (!(qmr & (1 << i))) 430 - continue; 431 - 432 - /* Allocate a dma pool for this queue */ 433 - snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d", 434 - ccp->name, i); 435 - dma_pool = dma_pool_create(dma_pool_name, dev, 436 - CCP_DMAPOOL_MAX_SIZE, 437 - CCP_DMAPOOL_ALIGN, 0); 438 - if (!dma_pool) { 439 - dev_err(dev, "unable to allocate dma pool\n"); 440 - ret = -ENOMEM; 441 - goto e_pool; 442 - } 443 - 444 - cmd_q = &ccp->cmd_q[ccp->cmd_q_count]; 445 - ccp->cmd_q_count++; 446 - 447 - cmd_q->ccp = ccp; 448 - cmd_q->id = i; 449 - cmd_q->dma_pool = dma_pool; 450 - 451 - /* Reserve 2 KSB regions for the queue */ 452 - cmd_q->ksb_key = KSB_START + ccp->ksb_start++; 453 - cmd_q->ksb_ctx = KSB_START + ccp->ksb_start++; 454 - ccp->ksb_count -= 2; 455 - 456 - /* Preset some register values and masks that are queue 457 - * number dependent 458 - */ 459 - cmd_q->reg_status = ccp->io_regs + CMD_Q_STATUS_BASE + 460 - (CMD_Q_STATUS_INCR * i); 461 - cmd_q->reg_int_status = ccp->io_regs + CMD_Q_INT_STATUS_BASE + 462 - (CMD_Q_STATUS_INCR * i); 463 - cmd_q->int_ok = 1 << (i * 2); 464 - cmd_q->int_err = 1 << ((i * 2) + 1); 465 - 466 - cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status)); 467 - 468 - init_waitqueue_head(&cmd_q->int_queue); 469 - 470 - /* Build queue interrupt mask (two interrupts per queue) */ 471 - qim |= cmd_q->int_ok | cmd_q->int_err; 472 - 473 - #ifdef CONFIG_ARM64 474 - /* For arm64 set the recommended queue cache settings */ 475 - iowrite32(ccp->axcache, ccp->io_regs + CMD_Q_CACHE_BASE + 476 - (CMD_Q_CACHE_INC * i)); 477 - #endif 478 - 479 - dev_dbg(dev, "queue #%u available\n", i); 480 - } 481 - if (ccp->cmd_q_count == 0) { 482 - dev_notice(dev, "no command queues available\n"); 483 - ret = -EIO; 484 - goto e_pool; 485 - } 486 - dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count); 487 - 488 - /* Disable and clear interrupts until ready */ 489 - iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG); 490 - for (i = 0; i < ccp->cmd_q_count; i++) { 491 - cmd_q = &ccp->cmd_q[i]; 492 - 493 - ioread32(cmd_q->reg_int_status); 494 - ioread32(cmd_q->reg_status); 495 - } 496 - iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG); 497 - 498 - /* Request an irq */ 499 - ret = ccp->get_irq(ccp); 500 - if (ret) { 501 - dev_err(dev, "unable to allocate an IRQ\n"); 502 - goto e_pool; 503 - } 504 - 505 - /* Initialize the queues used to wait for KSB space and suspend */ 506 - init_waitqueue_head(&ccp->ksb_queue); 507 - init_waitqueue_head(&ccp->suspend_queue); 508 - 509 - /* Create a kthread for each queue */ 510 - for (i = 0; i < ccp->cmd_q_count; i++) { 511 - struct task_struct *kthread; 512 - 513 - cmd_q = &ccp->cmd_q[i]; 514 - 515 - kthread = kthread_create(ccp_cmd_queue_thread, cmd_q, 516 - "%s-q%u", ccp->name, cmd_q->id); 517 - if (IS_ERR(kthread)) { 518 - dev_err(dev, "error creating queue thread (%ld)\n", 519 - PTR_ERR(kthread)); 520 - ret = PTR_ERR(kthread); 521 - goto e_kthread; 522 - } 523 - 524 - cmd_q->kthread = kthread; 525 - wake_up_process(kthread); 526 - } 527 - 528 - /* Register the RNG */ 529 - ccp->hwrng.name = ccp->rngname; 530 - ccp->hwrng.read = ccp_trng_read; 531 - ret = hwrng_register(&ccp->hwrng); 532 - if (ret) { 533 - dev_err(dev, "error registering hwrng (%d)\n", ret); 534 - goto e_kthread; 535 - } 536 - 537 - /* Make the device struct available before enabling interrupts */ 538 - ccp_add_device(ccp); 539 - 540 - /* Enable interrupts */ 541 - iowrite32(qim, ccp->io_regs + IRQ_MASK_REG); 542 - 543 - return 0; 544 - 545 - e_kthread: 546 - for (i = 0; i < ccp->cmd_q_count; i++) 547 - if (ccp->cmd_q[i].kthread) 548 - kthread_stop(ccp->cmd_q[i].kthread); 549 - 550 - ccp->free_irq(ccp); 551 - 552 - e_pool: 553 - for (i = 0; i < ccp->cmd_q_count; i++) 554 - dma_pool_destroy(ccp->cmd_q[i].dma_pool); 555 - 556 - return ret; 557 - } 558 - 559 - /** 560 - * ccp_destroy - tear down the CCP device 561 - * 562 - * @ccp: ccp_device struct 563 - */ 564 - void ccp_destroy(struct ccp_device *ccp) 565 - { 566 - struct ccp_cmd_queue *cmd_q; 567 - struct ccp_cmd *cmd; 568 - unsigned int qim, i; 569 - 570 - /* Remove general access to the device struct */ 571 - ccp_del_device(ccp); 572 - 573 - /* Unregister the RNG */ 574 - hwrng_unregister(&ccp->hwrng); 575 - 576 - /* Stop the queue kthreads */ 577 - for (i = 0; i < ccp->cmd_q_count; i++) 578 - if (ccp->cmd_q[i].kthread) 579 - kthread_stop(ccp->cmd_q[i].kthread); 580 - 581 - /* Build queue interrupt mask (two interrupt masks per queue) */ 582 - qim = 0; 583 - for (i = 0; i < ccp->cmd_q_count; i++) { 584 - cmd_q = &ccp->cmd_q[i]; 585 - qim |= cmd_q->int_ok | cmd_q->int_err; 586 - } 587 - 588 - /* Disable and clear interrupts */ 589 - iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG); 590 - for (i = 0; i < ccp->cmd_q_count; i++) { 591 - cmd_q = &ccp->cmd_q[i]; 592 - 593 - ioread32(cmd_q->reg_int_status); 594 - ioread32(cmd_q->reg_status); 595 - } 596 - iowrite32(qim, ccp->io_regs + IRQ_STATUS_REG); 597 - 598 - ccp->free_irq(ccp); 599 - 600 - for (i = 0; i < ccp->cmd_q_count; i++) 601 - dma_pool_destroy(ccp->cmd_q[i].dma_pool); 602 - 603 - /* Flush the cmd and backlog queue */ 604 - while (!list_empty(&ccp->cmd)) { 605 - /* Invoke the callback directly with an error code */ 606 - cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry); 607 - list_del(&cmd->entry); 608 - cmd->callback(cmd->data, -ENODEV); 609 - } 610 - while (!list_empty(&ccp->backlog)) { 611 - /* Invoke the callback directly with an error code */ 612 - cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry); 613 - list_del(&cmd->entry); 614 - cmd->callback(cmd->data, -ENODEV); 615 - } 616 - } 617 - 618 - /** 619 - * ccp_irq_handler - handle interrupts generated by the CCP device 620 - * 621 - * @irq: the irq associated with the interrupt 622 - * @data: the data value supplied when the irq was created 623 - */ 624 - irqreturn_t ccp_irq_handler(int irq, void *data) 625 - { 626 - struct device *dev = data; 627 - struct ccp_device *ccp = dev_get_drvdata(dev); 628 - struct ccp_cmd_queue *cmd_q; 629 - u32 q_int, status; 630 - unsigned int i; 631 - 632 - status = ioread32(ccp->io_regs + IRQ_STATUS_REG); 633 - 634 - for (i = 0; i < ccp->cmd_q_count; i++) { 635 - cmd_q = &ccp->cmd_q[i]; 636 - 637 - q_int = status & (cmd_q->int_ok | cmd_q->int_err); 638 - if (q_int) { 639 - cmd_q->int_status = status; 640 - cmd_q->q_status = ioread32(cmd_q->reg_status); 641 - cmd_q->q_int_status = ioread32(cmd_q->reg_int_status); 642 - 643 - /* On error, only save the first error value */ 644 - if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error) 645 - cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status); 646 - 647 - cmd_q->int_rcvd = 1; 648 - 649 - /* Acknowledge the interrupt and wake the kthread */ 650 - iowrite32(q_int, ccp->io_regs + IRQ_STATUS_REG); 651 - wake_up_interruptible(&cmd_q->int_queue); 652 - } 653 - } 654 - 655 - return IRQ_HANDLED; 656 - } 657 - 658 424 #ifdef CONFIG_PM 659 425 bool ccp_queues_suspended(struct ccp_device *ccp) 660 426 { ··· 426 686 return ccp->cmd_q_count == suspended; 427 687 } 428 688 #endif 429 - 430 - struct ccp_vdata ccpv3 = { 431 - .version = CCP_VERSION(3, 0), 432 - }; 433 689 434 690 static int __init ccp_mod_init(void) 435 691 {
+135 -5
drivers/crypto/ccp/ccp-dev.h
··· 141 141 #define CCP_ECC_RESULT_OFFSET 60 142 142 #define CCP_ECC_RESULT_SUCCESS 0x0001 143 143 144 + struct ccp_op; 145 + 146 + /* Structure for computation functions that are device-specific */ 147 + struct ccp_actions { 148 + int (*perform_aes)(struct ccp_op *); 149 + int (*perform_xts_aes)(struct ccp_op *); 150 + int (*perform_sha)(struct ccp_op *); 151 + int (*perform_rsa)(struct ccp_op *); 152 + int (*perform_passthru)(struct ccp_op *); 153 + int (*perform_ecc)(struct ccp_op *); 154 + int (*init)(struct ccp_device *); 155 + void (*destroy)(struct ccp_device *); 156 + irqreturn_t (*irqhandler)(int, void *); 157 + }; 158 + 144 159 /* Structure to hold CCP version-specific values */ 145 160 struct ccp_vdata { 146 161 unsigned int version; 162 + struct ccp_actions *perform; 147 163 }; 148 164 149 165 extern struct ccp_vdata ccpv3; ··· 289 273 unsigned int axcache; 290 274 }; 291 275 276 + enum ccp_memtype { 277 + CCP_MEMTYPE_SYSTEM = 0, 278 + CCP_MEMTYPE_KSB, 279 + CCP_MEMTYPE_LOCAL, 280 + CCP_MEMTYPE__LAST, 281 + }; 282 + 283 + struct ccp_dma_info { 284 + dma_addr_t address; 285 + unsigned int offset; 286 + unsigned int length; 287 + enum dma_data_direction dir; 288 + }; 289 + 290 + struct ccp_dm_workarea { 291 + struct device *dev; 292 + struct dma_pool *dma_pool; 293 + unsigned int length; 294 + 295 + u8 *address; 296 + struct ccp_dma_info dma; 297 + }; 298 + 299 + struct ccp_sg_workarea { 300 + struct scatterlist *sg; 301 + int nents; 302 + 303 + struct scatterlist *dma_sg; 304 + struct device *dma_dev; 305 + unsigned int dma_count; 306 + enum dma_data_direction dma_dir; 307 + 308 + unsigned int sg_used; 309 + 310 + u64 bytes_left; 311 + }; 312 + 313 + struct ccp_data { 314 + struct ccp_sg_workarea sg_wa; 315 + struct ccp_dm_workarea dm_wa; 316 + }; 317 + 318 + struct ccp_mem { 319 + enum ccp_memtype type; 320 + union { 321 + struct ccp_dma_info dma; 322 + u32 ksb; 323 + } u; 324 + }; 325 + 326 + struct ccp_aes_op { 327 + enum ccp_aes_type type; 328 + enum ccp_aes_mode mode; 329 + enum ccp_aes_action action; 330 + }; 331 + 332 + struct ccp_xts_aes_op { 333 + enum ccp_aes_action action; 334 + enum ccp_xts_aes_unit_size unit_size; 335 + }; 336 + 337 + struct ccp_sha_op { 338 + enum ccp_sha_type type; 339 + u64 msg_bits; 340 + }; 341 + 342 + struct ccp_rsa_op { 343 + u32 mod_size; 344 + u32 input_len; 345 + }; 346 + 347 + struct ccp_passthru_op { 348 + enum ccp_passthru_bitwise bit_mod; 349 + enum ccp_passthru_byteswap byte_swap; 350 + }; 351 + 352 + struct ccp_ecc_op { 353 + enum ccp_ecc_function function; 354 + }; 355 + 356 + struct ccp_op { 357 + struct ccp_cmd_queue *cmd_q; 358 + 359 + u32 jobid; 360 + u32 ioc; 361 + u32 soc; 362 + u32 ksb_key; 363 + u32 ksb_ctx; 364 + u32 init; 365 + u32 eom; 366 + 367 + struct ccp_mem src; 368 + struct ccp_mem dst; 369 + 370 + union { 371 + struct ccp_aes_op aes; 372 + struct ccp_xts_aes_op xts; 373 + struct ccp_sha_op sha; 374 + struct ccp_rsa_op rsa; 375 + struct ccp_passthru_op passthru; 376 + struct ccp_ecc_op ecc; 377 + } u; 378 + }; 379 + 380 + static inline u32 ccp_addr_lo(struct ccp_dma_info *info) 381 + { 382 + return lower_32_bits(info->address + info->offset); 383 + } 384 + 385 + static inline u32 ccp_addr_hi(struct ccp_dma_info *info) 386 + { 387 + return upper_32_bits(info->address + info->offset) & 0x0000ffff; 388 + } 389 + 292 390 int ccp_pci_init(void); 293 391 void ccp_pci_exit(void); 294 392 295 393 int ccp_platform_init(void); 296 394 void ccp_platform_exit(void); 297 395 298 - struct ccp_device *ccp_alloc_struct(struct device *dev); 299 - int ccp_init(struct ccp_device *ccp); 300 - void ccp_destroy(struct ccp_device *ccp); 301 - bool ccp_queues_suspended(struct ccp_device *ccp); 396 + void ccp_add_device(struct ccp_device *ccp); 397 + void ccp_del_device(struct ccp_device *ccp); 302 398 303 - irqreturn_t ccp_irq_handler(int irq, void *data); 399 + struct ccp_device *ccp_alloc_struct(struct device *dev); 400 + bool ccp_queues_suspended(struct ccp_device *ccp); 401 + int ccp_cmd_queue_thread(void *data); 304 402 305 403 int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd); 306 404
+11 -370
drivers/crypto/ccp/ccp-ops.c
··· 1 1 /* 2 2 * AMD Cryptographic Coprocessor (CCP) driver 3 3 * 4 - * Copyright (C) 2013 Advanced Micro Devices, Inc. 4 + * Copyright (C) 2013,2016 Advanced Micro Devices, Inc. 5 5 * 6 6 * Author: Tom Lendacky <thomas.lendacky@amd.com> 7 7 * ··· 13 13 #include <linux/module.h> 14 14 #include <linux/kernel.h> 15 15 #include <linux/pci.h> 16 - #include <linux/pci_ids.h> 17 - #include <linux/kthread.h> 18 - #include <linux/sched.h> 19 16 #include <linux/interrupt.h> 20 - #include <linux/spinlock.h> 21 - #include <linux/mutex.h> 22 - #include <linux/delay.h> 23 - #include <linux/ccp.h> 24 - #include <linux/scatterlist.h> 25 17 #include <crypto/scatterwalk.h> 26 - #include <crypto/sha.h> 18 + #include <linux/ccp.h> 27 19 28 20 #include "ccp-dev.h" 29 - 30 - enum ccp_memtype { 31 - CCP_MEMTYPE_SYSTEM = 0, 32 - CCP_MEMTYPE_KSB, 33 - CCP_MEMTYPE_LOCAL, 34 - CCP_MEMTYPE__LAST, 35 - }; 36 - 37 - struct ccp_dma_info { 38 - dma_addr_t address; 39 - unsigned int offset; 40 - unsigned int length; 41 - enum dma_data_direction dir; 42 - }; 43 - 44 - struct ccp_dm_workarea { 45 - struct device *dev; 46 - struct dma_pool *dma_pool; 47 - unsigned int length; 48 - 49 - u8 *address; 50 - struct ccp_dma_info dma; 51 - }; 52 - 53 - struct ccp_sg_workarea { 54 - struct scatterlist *sg; 55 - int nents; 56 - 57 - struct scatterlist *dma_sg; 58 - struct device *dma_dev; 59 - unsigned int dma_count; 60 - enum dma_data_direction dma_dir; 61 - 62 - unsigned int sg_used; 63 - 64 - u64 bytes_left; 65 - }; 66 - 67 - struct ccp_data { 68 - struct ccp_sg_workarea sg_wa; 69 - struct ccp_dm_workarea dm_wa; 70 - }; 71 - 72 - struct ccp_mem { 73 - enum ccp_memtype type; 74 - union { 75 - struct ccp_dma_info dma; 76 - u32 ksb; 77 - } u; 78 - }; 79 - 80 - struct ccp_aes_op { 81 - enum ccp_aes_type type; 82 - enum ccp_aes_mode mode; 83 - enum ccp_aes_action action; 84 - }; 85 - 86 - struct ccp_xts_aes_op { 87 - enum ccp_aes_action action; 88 - enum ccp_xts_aes_unit_size unit_size; 89 - }; 90 - 91 - struct ccp_sha_op { 92 - enum ccp_sha_type type; 93 - u64 msg_bits; 94 - }; 95 - 96 - struct ccp_rsa_op { 97 - u32 mod_size; 98 - u32 input_len; 99 - }; 100 - 101 - struct ccp_passthru_op { 102 - enum ccp_passthru_bitwise bit_mod; 103 - enum ccp_passthru_byteswap byte_swap; 104 - }; 105 - 106 - struct ccp_ecc_op { 107 - enum ccp_ecc_function function; 108 - }; 109 - 110 - struct ccp_op { 111 - struct ccp_cmd_queue *cmd_q; 112 - 113 - u32 jobid; 114 - u32 ioc; 115 - u32 soc; 116 - u32 ksb_key; 117 - u32 ksb_ctx; 118 - u32 init; 119 - u32 eom; 120 - 121 - struct ccp_mem src; 122 - struct ccp_mem dst; 123 - 124 - union { 125 - struct ccp_aes_op aes; 126 - struct ccp_xts_aes_op xts; 127 - struct ccp_sha_op sha; 128 - struct ccp_rsa_op rsa; 129 - struct ccp_passthru_op passthru; 130 - struct ccp_ecc_op ecc; 131 - } u; 132 - }; 133 21 134 22 /* SHA initial context values */ 135 23 static const __be32 ccp_sha1_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = { ··· 39 151 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5), 40 152 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7), 41 153 }; 42 - 43 - static u32 ccp_addr_lo(struct ccp_dma_info *info) 44 - { 45 - return lower_32_bits(info->address + info->offset); 46 - } 47 - 48 - static u32 ccp_addr_hi(struct ccp_dma_info *info) 49 - { 50 - return upper_32_bits(info->address + info->offset) & 0x0000ffff; 51 - } 52 - 53 - static int ccp_do_cmd(struct ccp_op *op, u32 *cr, unsigned int cr_count) 54 - { 55 - struct ccp_cmd_queue *cmd_q = op->cmd_q; 56 - struct ccp_device *ccp = cmd_q->ccp; 57 - void __iomem *cr_addr; 58 - u32 cr0, cmd; 59 - unsigned int i; 60 - int ret = 0; 61 - 62 - /* We could read a status register to see how many free slots 63 - * are actually available, but reading that register resets it 64 - * and you could lose some error information. 65 - */ 66 - cmd_q->free_slots--; 67 - 68 - cr0 = (cmd_q->id << REQ0_CMD_Q_SHIFT) 69 - | (op->jobid << REQ0_JOBID_SHIFT) 70 - | REQ0_WAIT_FOR_WRITE; 71 - 72 - if (op->soc) 73 - cr0 |= REQ0_STOP_ON_COMPLETE 74 - | REQ0_INT_ON_COMPLETE; 75 - 76 - if (op->ioc || !cmd_q->free_slots) 77 - cr0 |= REQ0_INT_ON_COMPLETE; 78 - 79 - /* Start at CMD_REQ1 */ 80 - cr_addr = ccp->io_regs + CMD_REQ0 + CMD_REQ_INCR; 81 - 82 - mutex_lock(&ccp->req_mutex); 83 - 84 - /* Write CMD_REQ1 through CMD_REQx first */ 85 - for (i = 0; i < cr_count; i++, cr_addr += CMD_REQ_INCR) 86 - iowrite32(*(cr + i), cr_addr); 87 - 88 - /* Tell the CCP to start */ 89 - wmb(); 90 - iowrite32(cr0, ccp->io_regs + CMD_REQ0); 91 - 92 - mutex_unlock(&ccp->req_mutex); 93 - 94 - if (cr0 & REQ0_INT_ON_COMPLETE) { 95 - /* Wait for the job to complete */ 96 - ret = wait_event_interruptible(cmd_q->int_queue, 97 - cmd_q->int_rcvd); 98 - if (ret || cmd_q->cmd_error) { 99 - /* On error delete all related jobs from the queue */ 100 - cmd = (cmd_q->id << DEL_Q_ID_SHIFT) 101 - | op->jobid; 102 - 103 - iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB); 104 - 105 - if (!ret) 106 - ret = -EIO; 107 - } else if (op->soc) { 108 - /* Delete just head job from the queue on SoC */ 109 - cmd = DEL_Q_ACTIVE 110 - | (cmd_q->id << DEL_Q_ID_SHIFT) 111 - | op->jobid; 112 - 113 - iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB); 114 - } 115 - 116 - cmd_q->free_slots = CMD_Q_DEPTH(cmd_q->q_status); 117 - 118 - cmd_q->int_rcvd = 0; 119 - } 120 - 121 - return ret; 122 - } 123 - 124 - static int ccp_perform_aes(struct ccp_op *op) 125 - { 126 - u32 cr[6]; 127 - 128 - /* Fill out the register contents for REQ1 through REQ6 */ 129 - cr[0] = (CCP_ENGINE_AES << REQ1_ENGINE_SHIFT) 130 - | (op->u.aes.type << REQ1_AES_TYPE_SHIFT) 131 - | (op->u.aes.mode << REQ1_AES_MODE_SHIFT) 132 - | (op->u.aes.action << REQ1_AES_ACTION_SHIFT) 133 - | (op->ksb_key << REQ1_KEY_KSB_SHIFT); 134 - cr[1] = op->src.u.dma.length - 1; 135 - cr[2] = ccp_addr_lo(&op->src.u.dma); 136 - cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 137 - | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 138 - | ccp_addr_hi(&op->src.u.dma); 139 - cr[4] = ccp_addr_lo(&op->dst.u.dma); 140 - cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 141 - | ccp_addr_hi(&op->dst.u.dma); 142 - 143 - if (op->u.aes.mode == CCP_AES_MODE_CFB) 144 - cr[0] |= ((0x7f) << REQ1_AES_CFB_SIZE_SHIFT); 145 - 146 - if (op->eom) 147 - cr[0] |= REQ1_EOM; 148 - 149 - if (op->init) 150 - cr[0] |= REQ1_INIT; 151 - 152 - return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 153 - } 154 - 155 - static int ccp_perform_xts_aes(struct ccp_op *op) 156 - { 157 - u32 cr[6]; 158 - 159 - /* Fill out the register contents for REQ1 through REQ6 */ 160 - cr[0] = (CCP_ENGINE_XTS_AES_128 << REQ1_ENGINE_SHIFT) 161 - | (op->u.xts.action << REQ1_AES_ACTION_SHIFT) 162 - | (op->u.xts.unit_size << REQ1_XTS_AES_SIZE_SHIFT) 163 - | (op->ksb_key << REQ1_KEY_KSB_SHIFT); 164 - cr[1] = op->src.u.dma.length - 1; 165 - cr[2] = ccp_addr_lo(&op->src.u.dma); 166 - cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 167 - | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 168 - | ccp_addr_hi(&op->src.u.dma); 169 - cr[4] = ccp_addr_lo(&op->dst.u.dma); 170 - cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 171 - | ccp_addr_hi(&op->dst.u.dma); 172 - 173 - if (op->eom) 174 - cr[0] |= REQ1_EOM; 175 - 176 - if (op->init) 177 - cr[0] |= REQ1_INIT; 178 - 179 - return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 180 - } 181 - 182 - static int ccp_perform_sha(struct ccp_op *op) 183 - { 184 - u32 cr[6]; 185 - 186 - /* Fill out the register contents for REQ1 through REQ6 */ 187 - cr[0] = (CCP_ENGINE_SHA << REQ1_ENGINE_SHIFT) 188 - | (op->u.sha.type << REQ1_SHA_TYPE_SHIFT) 189 - | REQ1_INIT; 190 - cr[1] = op->src.u.dma.length - 1; 191 - cr[2] = ccp_addr_lo(&op->src.u.dma); 192 - cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 193 - | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 194 - | ccp_addr_hi(&op->src.u.dma); 195 - 196 - if (op->eom) { 197 - cr[0] |= REQ1_EOM; 198 - cr[4] = lower_32_bits(op->u.sha.msg_bits); 199 - cr[5] = upper_32_bits(op->u.sha.msg_bits); 200 - } else { 201 - cr[4] = 0; 202 - cr[5] = 0; 203 - } 204 - 205 - return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 206 - } 207 - 208 - static int ccp_perform_rsa(struct ccp_op *op) 209 - { 210 - u32 cr[6]; 211 - 212 - /* Fill out the register contents for REQ1 through REQ6 */ 213 - cr[0] = (CCP_ENGINE_RSA << REQ1_ENGINE_SHIFT) 214 - | (op->u.rsa.mod_size << REQ1_RSA_MOD_SIZE_SHIFT) 215 - | (op->ksb_key << REQ1_KEY_KSB_SHIFT) 216 - | REQ1_EOM; 217 - cr[1] = op->u.rsa.input_len - 1; 218 - cr[2] = ccp_addr_lo(&op->src.u.dma); 219 - cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT) 220 - | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 221 - | ccp_addr_hi(&op->src.u.dma); 222 - cr[4] = ccp_addr_lo(&op->dst.u.dma); 223 - cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 224 - | ccp_addr_hi(&op->dst.u.dma); 225 - 226 - return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 227 - } 228 - 229 - static int ccp_perform_passthru(struct ccp_op *op) 230 - { 231 - u32 cr[6]; 232 - 233 - /* Fill out the register contents for REQ1 through REQ6 */ 234 - cr[0] = (CCP_ENGINE_PASSTHRU << REQ1_ENGINE_SHIFT) 235 - | (op->u.passthru.bit_mod << REQ1_PT_BW_SHIFT) 236 - | (op->u.passthru.byte_swap << REQ1_PT_BS_SHIFT); 237 - 238 - if (op->src.type == CCP_MEMTYPE_SYSTEM) 239 - cr[1] = op->src.u.dma.length - 1; 240 - else 241 - cr[1] = op->dst.u.dma.length - 1; 242 - 243 - if (op->src.type == CCP_MEMTYPE_SYSTEM) { 244 - cr[2] = ccp_addr_lo(&op->src.u.dma); 245 - cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 246 - | ccp_addr_hi(&op->src.u.dma); 247 - 248 - if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP) 249 - cr[3] |= (op->ksb_key << REQ4_KSB_SHIFT); 250 - } else { 251 - cr[2] = op->src.u.ksb * CCP_KSB_BYTES; 252 - cr[3] = (CCP_MEMTYPE_KSB << REQ4_MEMTYPE_SHIFT); 253 - } 254 - 255 - if (op->dst.type == CCP_MEMTYPE_SYSTEM) { 256 - cr[4] = ccp_addr_lo(&op->dst.u.dma); 257 - cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 258 - | ccp_addr_hi(&op->dst.u.dma); 259 - } else { 260 - cr[4] = op->dst.u.ksb * CCP_KSB_BYTES; 261 - cr[5] = (CCP_MEMTYPE_KSB << REQ6_MEMTYPE_SHIFT); 262 - } 263 - 264 - if (op->eom) 265 - cr[0] |= REQ1_EOM; 266 - 267 - return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 268 - } 269 - 270 - static int ccp_perform_ecc(struct ccp_op *op) 271 - { 272 - u32 cr[6]; 273 - 274 - /* Fill out the register contents for REQ1 through REQ6 */ 275 - cr[0] = REQ1_ECC_AFFINE_CONVERT 276 - | (CCP_ENGINE_ECC << REQ1_ENGINE_SHIFT) 277 - | (op->u.ecc.function << REQ1_ECC_FUNCTION_SHIFT) 278 - | REQ1_EOM; 279 - cr[1] = op->src.u.dma.length - 1; 280 - cr[2] = ccp_addr_lo(&op->src.u.dma); 281 - cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 282 - | ccp_addr_hi(&op->src.u.dma); 283 - cr[4] = ccp_addr_lo(&op->dst.u.dma); 284 - cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 285 - | ccp_addr_hi(&op->dst.u.dma); 286 - 287 - return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 288 - } 289 154 290 155 static u32 ccp_alloc_ksb(struct ccp_device *ccp, unsigned int count) 291 156 { ··· 478 837 479 838 op.u.passthru.byte_swap = byte_swap; 480 839 481 - return ccp_perform_passthru(&op); 840 + return cmd_q->ccp->vdata->perform->perform_passthru(&op); 482 841 } 483 842 484 843 static int ccp_copy_to_ksb(struct ccp_cmd_queue *cmd_q, ··· 610 969 } 611 970 } 612 971 613 - ret = ccp_perform_aes(&op); 972 + ret = cmd_q->ccp->vdata->perform->perform_aes(&op); 614 973 if (ret) { 615 974 cmd->engine_error = cmd_q->cmd_error; 616 975 goto e_src; ··· 772 1131 op.soc = 1; 773 1132 } 774 1133 775 - ret = ccp_perform_aes(&op); 1134 + ret = cmd_q->ccp->vdata->perform->perform_aes(&op); 776 1135 if (ret) { 777 1136 cmd->engine_error = cmd_q->cmd_error; 778 1137 goto e_dst; ··· 937 1296 if (!src.sg_wa.bytes_left) 938 1297 op.eom = 1; 939 1298 940 - ret = ccp_perform_xts_aes(&op); 1299 + ret = cmd_q->ccp->vdata->perform->perform_xts_aes(&op); 941 1300 if (ret) { 942 1301 cmd->engine_error = cmd_q->cmd_error; 943 1302 goto e_dst; ··· 1094 1453 if (sha->final && !src.sg_wa.bytes_left) 1095 1454 op.eom = 1; 1096 1455 1097 - ret = ccp_perform_sha(&op); 1456 + ret = cmd_q->ccp->vdata->perform->perform_sha(&op); 1098 1457 if (ret) { 1099 1458 cmd->engine_error = cmd_q->cmd_error; 1100 1459 goto e_data; ··· 1274 1633 op.u.rsa.mod_size = rsa->key_size; 1275 1634 op.u.rsa.input_len = i_len; 1276 1635 1277 - ret = ccp_perform_rsa(&op); 1636 + ret = cmd_q->ccp->vdata->perform->perform_rsa(&op); 1278 1637 if (ret) { 1279 1638 cmd->engine_error = cmd_q->cmd_error; 1280 1639 goto e_dst; ··· 1399 1758 op.dst.u.dma.offset = dst.sg_wa.sg_used; 1400 1759 op.dst.u.dma.length = op.src.u.dma.length; 1401 1760 1402 - ret = ccp_perform_passthru(&op); 1761 + ret = cmd_q->ccp->vdata->perform->perform_passthru(&op); 1403 1762 if (ret) { 1404 1763 cmd->engine_error = cmd_q->cmd_error; 1405 1764 goto e_dst; ··· 1511 1870 1512 1871 op.u.ecc.function = cmd->u.ecc.function; 1513 1872 1514 - ret = ccp_perform_ecc(&op); 1873 + ret = cmd_q->ccp->vdata->perform->perform_ecc(&op); 1515 1874 if (ret) { 1516 1875 cmd->engine_error = cmd_q->cmd_error; 1517 1876 goto e_dst; ··· 1675 2034 1676 2035 op.u.ecc.function = cmd->u.ecc.function; 1677 2036 1678 - ret = ccp_perform_ecc(&op); 2037 + ret = cmd_q->ccp->vdata->perform->perform_ecc(&op); 1679 2038 if (ret) { 1680 2039 cmd->engine_error = cmd_q->cmd_error; 1681 2040 goto e_dst;
+6 -4
drivers/crypto/ccp/ccp-pci.c
··· 62 62 snprintf(ccp_pci->msix[v].name, name_len, "%s-%u", 63 63 ccp->name, v); 64 64 ccp_pci->msix[v].vector = msix_entry[v].vector; 65 - ret = request_irq(ccp_pci->msix[v].vector, ccp_irq_handler, 65 + ret = request_irq(ccp_pci->msix[v].vector, 66 + ccp->vdata->perform->irqhandler, 66 67 0, ccp_pci->msix[v].name, dev); 67 68 if (ret) { 68 69 dev_notice(dev, "unable to allocate MSI-X IRQ (%d)\n", ··· 96 95 return ret; 97 96 98 97 ccp->irq = pdev->irq; 99 - ret = request_irq(ccp->irq, ccp_irq_handler, 0, ccp->name, dev); 98 + ret = request_irq(ccp->irq, ccp->vdata->perform->irqhandler, 0, 99 + ccp->name, dev); 100 100 if (ret) { 101 101 dev_notice(dev, "unable to allocate MSI IRQ (%d)\n", ret); 102 102 goto e_msi; ··· 230 228 231 229 dev_set_drvdata(dev, ccp); 232 230 233 - ret = ccp_init(ccp); 231 + ret = ccp->vdata->perform->init(ccp); 234 232 if (ret) 235 233 goto e_iomap; 236 234 ··· 260 258 if (!ccp) 261 259 return; 262 260 263 - ccp_destroy(ccp); 261 + ccp->vdata->perform->destroy(ccp); 264 262 265 263 pci_iounmap(pdev, ccp->io_map); 266 264
+4 -3
drivers/crypto/ccp/ccp-platform.c
··· 70 70 return ret; 71 71 72 72 ccp->irq = ret; 73 - ret = request_irq(ccp->irq, ccp_irq_handler, 0, ccp->name, dev); 73 + ret = request_irq(ccp->irq, ccp->vdata->perform->irqhandler, 0, 74 + ccp->name, dev); 74 75 if (ret) { 75 76 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret); 76 77 return ret; ··· 172 171 173 172 dev_set_drvdata(dev, ccp); 174 173 175 - ret = ccp_init(ccp); 174 + ret = ccp->vdata->perform->init(ccp); 176 175 if (ret) 177 176 goto e_err; 178 177 ··· 190 189 struct device *dev = &pdev->dev; 191 190 struct ccp_device *ccp = dev_get_drvdata(dev); 192 191 193 - ccp_destroy(ccp); 192 + ccp->vdata->perform->destroy(ccp); 194 193 195 194 dev_notice(dev, "disabled\n"); 196 195