Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 1 | /* |
| 2 | * AMD Cryptographic Coprocessor (CCP) driver |
| 3 | * |
Gary R Hook | 3f19ce2 | 2016-03-01 13:48:54 -0600 | [diff] [blame] | 4 | * Copyright (C) 2013,2016 Advanced Micro Devices, Inc. |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 5 | * |
| 6 | * Author: Tom Lendacky <thomas.lendacky@amd.com> |
Gary R Hook | 956ee21 | 2016-07-26 19:09:40 -0500 | [diff] [blame] | 7 | * Author: Gary R Hook <gary.hook@amd.com> |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -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/kthread.h> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/spinlock.h> |
Mike Galbraith | 7587c40 | 2016-04-05 15:03:21 +0200 | [diff] [blame] | 20 | #include <linux/spinlock_types.h> |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 21 | #include <linux/types.h> |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 22 | #include <linux/mutex.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/hw_random.h> |
| 25 | #include <linux/cpu.h> |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 26 | #ifdef CONFIG_X86 |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 27 | #include <asm/cpu_device_id.h> |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 28 | #endif |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 29 | #include <linux/ccp.h> |
| 30 | |
| 31 | #include "ccp-dev.h" |
| 32 | |
| 33 | MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>"); |
Gary R Hook | ac50b78 | 2017-04-25 08:59:44 -0500 | [diff] [blame] | 34 | MODULE_AUTHOR("Gary R Hook <gary.hook@amd.com>"); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 35 | MODULE_LICENSE("GPL"); |
Gary R Hook | 3cdbe34 | 2017-05-02 17:33:40 -0500 | [diff] [blame] | 36 | MODULE_VERSION("1.1.0"); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 37 | MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver"); |
| 38 | |
Tom Lendacky | 530abd8 | 2014-01-24 16:18:14 -0600 | [diff] [blame] | 39 | struct ccp_tasklet_data { |
| 40 | struct completion completion; |
| 41 | struct ccp_cmd *cmd; |
| 42 | }; |
| 43 | |
Gary R Hook | 81422ba | 2016-09-28 11:53:56 -0500 | [diff] [blame] | 44 | /* Human-readable error strings */ |
Wei Yongjun | ff4f44d | 2016-10-17 15:08:50 +0000 | [diff] [blame] | 45 | static char *ccp_error_codes[] = { |
Gary R Hook | 81422ba | 2016-09-28 11:53:56 -0500 | [diff] [blame] | 46 | "", |
| 47 | "ERR 01: ILLEGAL_ENGINE", |
| 48 | "ERR 02: ILLEGAL_KEY_ID", |
| 49 | "ERR 03: ILLEGAL_FUNCTION_TYPE", |
| 50 | "ERR 04: ILLEGAL_FUNCTION_MODE", |
| 51 | "ERR 05: ILLEGAL_FUNCTION_ENCRYPT", |
| 52 | "ERR 06: ILLEGAL_FUNCTION_SIZE", |
| 53 | "ERR 07: Zlib_MISSING_INIT_EOM", |
| 54 | "ERR 08: ILLEGAL_FUNCTION_RSVD", |
| 55 | "ERR 09: ILLEGAL_BUFFER_LENGTH", |
| 56 | "ERR 10: VLSB_FAULT", |
| 57 | "ERR 11: ILLEGAL_MEM_ADDR", |
| 58 | "ERR 12: ILLEGAL_MEM_SEL", |
| 59 | "ERR 13: ILLEGAL_CONTEXT_ID", |
| 60 | "ERR 14: ILLEGAL_KEY_ADDR", |
| 61 | "ERR 15: 0xF Reserved", |
| 62 | "ERR 16: Zlib_ILLEGAL_MULTI_QUEUE", |
| 63 | "ERR 17: Zlib_ILLEGAL_JOBID_CHANGE", |
| 64 | "ERR 18: CMD_TIMEOUT", |
| 65 | "ERR 19: IDMA0_AXI_SLVERR", |
| 66 | "ERR 20: IDMA0_AXI_DECERR", |
| 67 | "ERR 21: 0x15 Reserved", |
| 68 | "ERR 22: IDMA1_AXI_SLAVE_FAULT", |
| 69 | "ERR 23: IDMA1_AIXI_DECERR", |
| 70 | "ERR 24: 0x18 Reserved", |
| 71 | "ERR 25: ZLIBVHB_AXI_SLVERR", |
| 72 | "ERR 26: ZLIBVHB_AXI_DECERR", |
| 73 | "ERR 27: 0x1B Reserved", |
| 74 | "ERR 27: ZLIB_UNEXPECTED_EOM", |
| 75 | "ERR 27: ZLIB_EXTRA_DATA", |
| 76 | "ERR 30: ZLIB_BTYPE", |
| 77 | "ERR 31: ZLIB_UNDEFINED_SYMBOL", |
| 78 | "ERR 32: ZLIB_UNDEFINED_DISTANCE_S", |
| 79 | "ERR 33: ZLIB_CODE_LENGTH_SYMBOL", |
| 80 | "ERR 34: ZLIB _VHB_ILLEGAL_FETCH", |
| 81 | "ERR 35: ZLIB_UNCOMPRESSED_LEN", |
| 82 | "ERR 36: ZLIB_LIMIT_REACHED", |
| 83 | "ERR 37: ZLIB_CHECKSUM_MISMATCH0", |
| 84 | "ERR 38: ODMA0_AXI_SLVERR", |
| 85 | "ERR 39: ODMA0_AXI_DECERR", |
| 86 | "ERR 40: 0x28 Reserved", |
| 87 | "ERR 41: ODMA1_AXI_SLVERR", |
| 88 | "ERR 42: ODMA1_AXI_DECERR", |
| 89 | "ERR 43: LSB_PARITY_ERR", |
| 90 | }; |
| 91 | |
| 92 | void ccp_log_error(struct ccp_device *d, int e) |
| 93 | { |
| 94 | dev_err(d->dev, "CCP error: %s (0x%x)\n", ccp_error_codes[e], e); |
| 95 | } |
| 96 | |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 97 | /* List of CCPs, CCP count, read-write access lock, and access functions |
| 98 | * |
| 99 | * Lock structure: get ccp_unit_lock for reading whenever we need to |
| 100 | * examine the CCP list. While holding it for reading we can acquire |
| 101 | * the RR lock to update the round-robin next-CCP pointer. The unit lock |
| 102 | * must be acquired before the RR lock. |
| 103 | * |
| 104 | * If the unit-lock is acquired for writing, we have total control over |
| 105 | * the list, so there's no value in getting the RR lock. |
| 106 | */ |
| 107 | static DEFINE_RWLOCK(ccp_unit_lock); |
| 108 | static LIST_HEAD(ccp_units); |
| 109 | |
| 110 | /* Round-robin counter */ |
Gary R Hook | 03a6f29 | 2016-03-16 09:02:26 -0500 | [diff] [blame] | 111 | static DEFINE_SPINLOCK(ccp_rr_lock); |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 112 | static struct ccp_device *ccp_rr; |
| 113 | |
Gary R Hook | ea0375a | 2016-03-01 13:49:25 -0600 | [diff] [blame] | 114 | /** |
| 115 | * ccp_add_device - add a CCP device to the list |
| 116 | * |
| 117 | * @ccp: ccp_device struct pointer |
| 118 | * |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 119 | * Put this CCP on the unit list, which makes it available |
| 120 | * for use. |
Gary R Hook | ea0375a | 2016-03-01 13:49:25 -0600 | [diff] [blame] | 121 | * |
| 122 | * Returns zero if a CCP device is present, -ENODEV otherwise. |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 123 | */ |
Gary R Hook | ea0375a | 2016-03-01 13:49:25 -0600 | [diff] [blame] | 124 | void ccp_add_device(struct ccp_device *ccp) |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 125 | { |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 126 | unsigned long flags; |
| 127 | |
| 128 | write_lock_irqsave(&ccp_unit_lock, flags); |
| 129 | list_add_tail(&ccp->entry, &ccp_units); |
| 130 | if (!ccp_rr) |
| 131 | /* We already have the list lock (we're first) so this |
| 132 | * pointer can't change on us. Set its initial value. |
| 133 | */ |
| 134 | ccp_rr = ccp; |
| 135 | write_unlock_irqrestore(&ccp_unit_lock, flags); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 136 | } |
| 137 | |
Gary R Hook | ea0375a | 2016-03-01 13:49:25 -0600 | [diff] [blame] | 138 | /** |
| 139 | * ccp_del_device - remove a CCP device from the list |
| 140 | * |
| 141 | * @ccp: ccp_device struct pointer |
| 142 | * |
| 143 | * Remove this unit from the list of devices. If the next device |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 144 | * up for use is this one, adjust the pointer. If this is the last |
| 145 | * device, NULL the pointer. |
| 146 | */ |
Gary R Hook | ea0375a | 2016-03-01 13:49:25 -0600 | [diff] [blame] | 147 | void ccp_del_device(struct ccp_device *ccp) |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 148 | { |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 149 | unsigned long flags; |
| 150 | |
| 151 | write_lock_irqsave(&ccp_unit_lock, flags); |
| 152 | if (ccp_rr == ccp) { |
| 153 | /* ccp_unit_lock is read/write; any read access |
| 154 | * will be suspended while we make changes to the |
| 155 | * list and RR pointer. |
| 156 | */ |
| 157 | if (list_is_last(&ccp_rr->entry, &ccp_units)) |
| 158 | ccp_rr = list_first_entry(&ccp_units, struct ccp_device, |
| 159 | entry); |
| 160 | else |
| 161 | ccp_rr = list_next_entry(ccp_rr, entry); |
| 162 | } |
| 163 | list_del(&ccp->entry); |
| 164 | if (list_empty(&ccp_units)) |
| 165 | ccp_rr = NULL; |
| 166 | write_unlock_irqrestore(&ccp_unit_lock, flags); |
| 167 | } |
| 168 | |
Gary R Hook | 084935b | 2016-07-26 19:10:31 -0500 | [diff] [blame] | 169 | |
| 170 | |
| 171 | int ccp_register_rng(struct ccp_device *ccp) |
| 172 | { |
| 173 | int ret = 0; |
| 174 | |
| 175 | dev_dbg(ccp->dev, "Registering RNG...\n"); |
| 176 | /* Register an RNG */ |
| 177 | ccp->hwrng.name = ccp->rngname; |
| 178 | ccp->hwrng.read = ccp_trng_read; |
| 179 | ret = hwrng_register(&ccp->hwrng); |
| 180 | if (ret) |
| 181 | dev_err(ccp->dev, "error registering hwrng (%d)\n", ret); |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | void ccp_unregister_rng(struct ccp_device *ccp) |
| 187 | { |
| 188 | if (ccp->hwrng.name) |
| 189 | hwrng_unregister(&ccp->hwrng); |
| 190 | } |
| 191 | |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 192 | static struct ccp_device *ccp_get_device(void) |
| 193 | { |
| 194 | unsigned long flags; |
| 195 | struct ccp_device *dp = NULL; |
| 196 | |
| 197 | /* We round-robin through the unit list. |
| 198 | * The (ccp_rr) pointer refers to the next unit to use. |
| 199 | */ |
| 200 | read_lock_irqsave(&ccp_unit_lock, flags); |
| 201 | if (!list_empty(&ccp_units)) { |
Gary R Hook | 03a6f29 | 2016-03-16 09:02:26 -0500 | [diff] [blame] | 202 | spin_lock(&ccp_rr_lock); |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 203 | dp = ccp_rr; |
| 204 | if (list_is_last(&ccp_rr->entry, &ccp_units)) |
| 205 | ccp_rr = list_first_entry(&ccp_units, struct ccp_device, |
| 206 | entry); |
| 207 | else |
| 208 | ccp_rr = list_next_entry(ccp_rr, entry); |
Gary R Hook | 03a6f29 | 2016-03-16 09:02:26 -0500 | [diff] [blame] | 209 | spin_unlock(&ccp_rr_lock); |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 210 | } |
| 211 | read_unlock_irqrestore(&ccp_unit_lock, flags); |
| 212 | |
| 213 | return dp; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
Tom Lendacky | c9f21cb | 2014-09-05 10:31:09 -0500 | [diff] [blame] | 217 | * ccp_present - check if a CCP device is present |
| 218 | * |
| 219 | * Returns zero if a CCP device is present, -ENODEV otherwise. |
| 220 | */ |
| 221 | int ccp_present(void) |
| 222 | { |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 223 | unsigned long flags; |
| 224 | int ret; |
Tom Lendacky | c9f21cb | 2014-09-05 10:31:09 -0500 | [diff] [blame] | 225 | |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 226 | read_lock_irqsave(&ccp_unit_lock, flags); |
| 227 | ret = list_empty(&ccp_units); |
| 228 | read_unlock_irqrestore(&ccp_unit_lock, flags); |
| 229 | |
| 230 | return ret ? -ENODEV : 0; |
Tom Lendacky | c9f21cb | 2014-09-05 10:31:09 -0500 | [diff] [blame] | 231 | } |
| 232 | EXPORT_SYMBOL_GPL(ccp_present); |
| 233 | |
| 234 | /** |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 235 | * ccp_version - get the version of the CCP device |
| 236 | * |
| 237 | * Returns the version from the first unit on the list; |
| 238 | * otherwise a zero if no CCP device is present |
| 239 | */ |
| 240 | unsigned int ccp_version(void) |
| 241 | { |
| 242 | struct ccp_device *dp; |
| 243 | unsigned long flags; |
| 244 | int ret = 0; |
| 245 | |
| 246 | read_lock_irqsave(&ccp_unit_lock, flags); |
| 247 | if (!list_empty(&ccp_units)) { |
| 248 | dp = list_first_entry(&ccp_units, struct ccp_device, entry); |
| 249 | ret = dp->vdata->version; |
| 250 | } |
| 251 | read_unlock_irqrestore(&ccp_unit_lock, flags); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | EXPORT_SYMBOL_GPL(ccp_version); |
| 256 | |
| 257 | /** |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 258 | * ccp_enqueue_cmd - queue an operation for processing by the CCP |
| 259 | * |
| 260 | * @cmd: ccp_cmd struct to be processed |
| 261 | * |
| 262 | * Queue a cmd to be processed by the CCP. If queueing the cmd |
| 263 | * would exceed the defined length of the cmd queue the cmd will |
| 264 | * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will |
| 265 | * result in a return code of -EBUSY. |
| 266 | * |
| 267 | * The callback routine specified in the ccp_cmd struct will be |
| 268 | * called to notify the caller of completion (if the cmd was not |
| 269 | * backlogged) or advancement out of the backlog. If the cmd has |
| 270 | * advanced out of the backlog the "err" value of the callback |
| 271 | * will be -EINPROGRESS. Any other "err" value during callback is |
| 272 | * the result of the operation. |
| 273 | * |
| 274 | * The cmd has been successfully queued if: |
| 275 | * the return code is -EINPROGRESS or |
| 276 | * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set |
| 277 | */ |
| 278 | int ccp_enqueue_cmd(struct ccp_cmd *cmd) |
| 279 | { |
Gary R Hook | 7c46844 | 2017-03-10 12:28:18 -0600 | [diff] [blame] | 280 | struct ccp_device *ccp; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 281 | unsigned long flags; |
| 282 | unsigned int i; |
| 283 | int ret; |
| 284 | |
Gary R Hook | 7c46844 | 2017-03-10 12:28:18 -0600 | [diff] [blame] | 285 | /* Some commands might need to be sent to a specific device */ |
| 286 | ccp = cmd->ccp ? cmd->ccp : ccp_get_device(); |
| 287 | |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 288 | if (!ccp) |
| 289 | return -ENODEV; |
| 290 | |
| 291 | /* Caller must supply a callback routine */ |
| 292 | if (!cmd->callback) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | cmd->ccp = ccp; |
| 296 | |
| 297 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 298 | |
| 299 | i = ccp->cmd_q_count; |
| 300 | |
| 301 | if (ccp->cmd_count >= MAX_CMD_QLEN) { |
| 302 | ret = -EBUSY; |
| 303 | if (cmd->flags & CCP_CMD_MAY_BACKLOG) |
| 304 | list_add_tail(&cmd->entry, &ccp->backlog); |
| 305 | } else { |
| 306 | ret = -EINPROGRESS; |
| 307 | ccp->cmd_count++; |
| 308 | list_add_tail(&cmd->entry, &ccp->cmd); |
| 309 | |
| 310 | /* Find an idle queue */ |
| 311 | if (!ccp->suspending) { |
| 312 | for (i = 0; i < ccp->cmd_q_count; i++) { |
| 313 | if (ccp->cmd_q[i].active) |
| 314 | continue; |
| 315 | |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 322 | |
| 323 | /* If we found an idle queue, wake it up */ |
| 324 | if (i < ccp->cmd_q_count) |
| 325 | wake_up_process(ccp->cmd_q[i].kthread); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | EXPORT_SYMBOL_GPL(ccp_enqueue_cmd); |
| 330 | |
| 331 | static void ccp_do_cmd_backlog(struct work_struct *work) |
| 332 | { |
| 333 | struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work); |
| 334 | struct ccp_device *ccp = cmd->ccp; |
| 335 | unsigned long flags; |
| 336 | unsigned int i; |
| 337 | |
| 338 | cmd->callback(cmd->data, -EINPROGRESS); |
| 339 | |
| 340 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 341 | |
| 342 | ccp->cmd_count++; |
| 343 | list_add_tail(&cmd->entry, &ccp->cmd); |
| 344 | |
| 345 | /* Find an idle queue */ |
| 346 | for (i = 0; i < ccp->cmd_q_count; i++) { |
| 347 | if (ccp->cmd_q[i].active) |
| 348 | continue; |
| 349 | |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 354 | |
| 355 | /* If we found an idle queue, wake it up */ |
| 356 | if (i < ccp->cmd_q_count) |
| 357 | wake_up_process(ccp->cmd_q[i].kthread); |
| 358 | } |
| 359 | |
| 360 | static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q) |
| 361 | { |
| 362 | struct ccp_device *ccp = cmd_q->ccp; |
| 363 | struct ccp_cmd *cmd = NULL; |
| 364 | struct ccp_cmd *backlog = NULL; |
| 365 | unsigned long flags; |
| 366 | |
| 367 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 368 | |
| 369 | cmd_q->active = 0; |
| 370 | |
| 371 | if (ccp->suspending) { |
| 372 | cmd_q->suspended = 1; |
| 373 | |
| 374 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 375 | wake_up_interruptible(&ccp->suspend_queue); |
| 376 | |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | if (ccp->cmd_count) { |
| 381 | cmd_q->active = 1; |
| 382 | |
| 383 | cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry); |
| 384 | list_del(&cmd->entry); |
| 385 | |
| 386 | ccp->cmd_count--; |
| 387 | } |
| 388 | |
| 389 | if (!list_empty(&ccp->backlog)) { |
| 390 | backlog = list_first_entry(&ccp->backlog, struct ccp_cmd, |
| 391 | entry); |
| 392 | list_del(&backlog->entry); |
| 393 | } |
| 394 | |
| 395 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 396 | |
| 397 | if (backlog) { |
| 398 | INIT_WORK(&backlog->work, ccp_do_cmd_backlog); |
| 399 | schedule_work(&backlog->work); |
| 400 | } |
| 401 | |
| 402 | return cmd; |
| 403 | } |
| 404 | |
Tom Lendacky | 530abd8 | 2014-01-24 16:18:14 -0600 | [diff] [blame] | 405 | static void ccp_do_cmd_complete(unsigned long data) |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 406 | { |
Tom Lendacky | 530abd8 | 2014-01-24 16:18:14 -0600 | [diff] [blame] | 407 | struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data; |
| 408 | struct ccp_cmd *cmd = tdata->cmd; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 409 | |
| 410 | cmd->callback(cmd->data, cmd->ret); |
Gary R Hook | 77af0ae | 2017-06-27 08:58:04 -0500 | [diff] [blame] | 411 | |
Tom Lendacky | 530abd8 | 2014-01-24 16:18:14 -0600 | [diff] [blame] | 412 | complete(&tdata->completion); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 413 | } |
| 414 | |
Gary R Hook | ea0375a | 2016-03-01 13:49:25 -0600 | [diff] [blame] | 415 | /** |
| 416 | * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue |
| 417 | * |
| 418 | * @data: thread-specific data |
| 419 | */ |
| 420 | int ccp_cmd_queue_thread(void *data) |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 421 | { |
| 422 | struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data; |
| 423 | struct ccp_cmd *cmd; |
Tom Lendacky | 530abd8 | 2014-01-24 16:18:14 -0600 | [diff] [blame] | 424 | struct ccp_tasklet_data tdata; |
| 425 | struct tasklet_struct tasklet; |
| 426 | |
| 427 | tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 428 | |
| 429 | set_current_state(TASK_INTERRUPTIBLE); |
| 430 | while (!kthread_should_stop()) { |
| 431 | schedule(); |
| 432 | |
| 433 | set_current_state(TASK_INTERRUPTIBLE); |
| 434 | |
| 435 | cmd = ccp_dequeue_cmd(cmd_q); |
| 436 | if (!cmd) |
| 437 | continue; |
| 438 | |
| 439 | __set_current_state(TASK_RUNNING); |
| 440 | |
| 441 | /* Execute the command */ |
| 442 | cmd->ret = ccp_run_cmd(cmd_q, cmd); |
| 443 | |
| 444 | /* Schedule the completion callback */ |
Tom Lendacky | 530abd8 | 2014-01-24 16:18:14 -0600 | [diff] [blame] | 445 | tdata.cmd = cmd; |
| 446 | init_completion(&tdata.completion); |
| 447 | tasklet_schedule(&tasklet); |
| 448 | wait_for_completion(&tdata.completion); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | __set_current_state(TASK_RUNNING); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 456 | /** |
| 457 | * ccp_alloc_struct - allocate and initialize the ccp_device struct |
| 458 | * |
| 459 | * @dev: device struct of the CCP |
| 460 | */ |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 461 | struct ccp_device *ccp_alloc_struct(struct sp_device *sp) |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 462 | { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 463 | struct device *dev = sp->dev; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 464 | struct ccp_device *ccp; |
| 465 | |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 466 | ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL); |
Tom Lendacky | 8db8846 | 2015-02-03 13:07:05 -0600 | [diff] [blame] | 467 | if (!ccp) |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 468 | return NULL; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 469 | ccp->dev = dev; |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 470 | ccp->sp = sp; |
| 471 | ccp->axcache = sp->axcache; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 472 | |
| 473 | INIT_LIST_HEAD(&ccp->cmd); |
| 474 | INIT_LIST_HEAD(&ccp->backlog); |
| 475 | |
| 476 | spin_lock_init(&ccp->cmd_lock); |
| 477 | mutex_init(&ccp->req_mutex); |
Gary R Hook | 956ee21 | 2016-07-26 19:09:40 -0500 | [diff] [blame] | 478 | mutex_init(&ccp->sb_mutex); |
| 479 | ccp->sb_count = KSB_COUNT; |
| 480 | ccp->sb_start = 0; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 481 | |
Gary R Hook | 103600a | 2016-10-18 17:33:37 -0500 | [diff] [blame] | 482 | /* Initialize the wait queues */ |
| 483 | init_waitqueue_head(&ccp->sb_queue); |
| 484 | init_waitqueue_head(&ccp->suspend_queue); |
| 485 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 486 | snprintf(ccp->name, MAX_CCP_NAME_LEN, "ccp-%u", sp->ord); |
| 487 | snprintf(ccp->rngname, MAX_CCP_NAME_LEN, "ccp-%u-rng", sp->ord); |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 488 | |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 489 | return ccp; |
| 490 | } |
| 491 | |
Gary R Hook | 8256e68 | 2016-07-26 19:10:02 -0500 | [diff] [blame] | 492 | int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait) |
| 493 | { |
| 494 | struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng); |
| 495 | u32 trng_value; |
| 496 | int len = min_t(int, sizeof(trng_value), max); |
| 497 | |
| 498 | /* Locking is provided by the caller so we can update device |
| 499 | * hwrng-related fields safely |
| 500 | */ |
| 501 | trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG); |
| 502 | if (!trng_value) { |
| 503 | /* Zero is returned if not data is available or if a |
| 504 | * bad-entropy error is present. Assume an error if |
| 505 | * we exceed TRNG_RETRIES reads of zero. |
| 506 | */ |
| 507 | if (ccp->hwrng_retries++ > TRNG_RETRIES) |
| 508 | return -EIO; |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* Reset the counter and save the rng value */ |
| 514 | ccp->hwrng_retries = 0; |
| 515 | memcpy(data, &trng_value, len); |
| 516 | |
| 517 | return len; |
| 518 | } |
| 519 | |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 520 | #ifdef CONFIG_PM |
| 521 | bool ccp_queues_suspended(struct ccp_device *ccp) |
| 522 | { |
| 523 | unsigned int suspended = 0; |
| 524 | unsigned long flags; |
| 525 | unsigned int i; |
| 526 | |
| 527 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 528 | |
| 529 | for (i = 0; i < ccp->cmd_q_count; i++) |
| 530 | if (ccp->cmd_q[i].suspended) |
| 531 | suspended++; |
| 532 | |
| 533 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 534 | |
| 535 | return ccp->cmd_q_count == suspended; |
| 536 | } |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 537 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 538 | int ccp_dev_suspend(struct sp_device *sp, pm_message_t state) |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 539 | { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 540 | struct ccp_device *ccp = sp->ccp_data; |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 541 | unsigned long flags; |
| 542 | unsigned int i; |
| 543 | |
| 544 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 545 | |
| 546 | ccp->suspending = 1; |
| 547 | |
| 548 | /* Wake all the queue kthreads to prepare for suspend */ |
| 549 | for (i = 0; i < ccp->cmd_q_count; i++) |
| 550 | wake_up_process(ccp->cmd_q[i].kthread); |
| 551 | |
| 552 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 553 | |
| 554 | /* Wait for all queue kthreads to say they're done */ |
| 555 | while (!ccp_queues_suspended(ccp)) |
| 556 | wait_event_interruptible(ccp->suspend_queue, |
| 557 | ccp_queues_suspended(ccp)); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 562 | int ccp_dev_resume(struct sp_device *sp) |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 563 | { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 564 | struct ccp_device *ccp = sp->ccp_data; |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 565 | unsigned long flags; |
| 566 | unsigned int i; |
| 567 | |
| 568 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 569 | |
| 570 | ccp->suspending = 0; |
| 571 | |
| 572 | /* Wake up all the kthreads */ |
| 573 | for (i = 0; i < ccp->cmd_q_count; i++) { |
| 574 | ccp->cmd_q[i].suspended = 0; |
| 575 | wake_up_process(ccp->cmd_q[i].kthread); |
| 576 | } |
| 577 | |
| 578 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 579 | |
| 580 | return 0; |
| 581 | } |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 582 | #endif |
| 583 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 584 | int ccp_dev_init(struct sp_device *sp) |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 585 | { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 586 | struct device *dev = sp->dev; |
| 587 | struct ccp_device *ccp; |
| 588 | int ret; |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 589 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 590 | ret = -ENOMEM; |
| 591 | ccp = ccp_alloc_struct(sp); |
| 592 | if (!ccp) |
| 593 | goto e_err; |
| 594 | sp->ccp_data = ccp; |
| 595 | |
| 596 | ccp->vdata = (struct ccp_vdata *)sp->dev_vdata->ccp_vdata; |
| 597 | if (!ccp->vdata || !ccp->vdata->version) { |
| 598 | ret = -ENODEV; |
| 599 | dev_err(dev, "missing driver data\n"); |
| 600 | goto e_err; |
| 601 | } |
| 602 | |
Brijesh Singh | f4d18d6 | 2017-07-06 09:59:15 -0500 | [diff] [blame^] | 603 | ccp->use_tasklet = sp->use_tasklet; |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 604 | |
| 605 | ccp->io_regs = sp->io_map + ccp->vdata->offset; |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 606 | if (ccp->vdata->setup) |
| 607 | ccp->vdata->setup(ccp); |
| 608 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 609 | ret = ccp->vdata->perform->init(ccp); |
| 610 | if (ret) |
| 611 | goto e_err; |
| 612 | |
| 613 | dev_notice(dev, "ccp enabled\n"); |
| 614 | |
| 615 | return 0; |
| 616 | |
| 617 | e_err: |
| 618 | sp->ccp_data = NULL; |
| 619 | |
| 620 | dev_notice(dev, "ccp initialization failed\n"); |
| 621 | |
| 622 | return ret; |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 623 | } |
| 624 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 625 | void ccp_dev_destroy(struct sp_device *sp) |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 626 | { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame] | 627 | struct ccp_device *ccp = sp->ccp_data; |
| 628 | |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 629 | if (!ccp) |
| 630 | return; |
| 631 | |
| 632 | ccp->vdata->perform->destroy(ccp); |
| 633 | } |