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