blob: 83a0ce5661adb549f395304812d74b6c468103dc [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook3f19ce22016-03-01 13:48:54 -06004 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06005 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hook956ee212016-07-26 19:09:40 -05007 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky63b94502013-11-12 11:46:16 -06008 *
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 Galbraith7587c402016-04-05 15:03:21 +020020#include <linux/spinlock_types.h>
Gary R Hook553d2372016-03-01 13:49:04 -060021#include <linux/types.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060022#include <linux/mutex.h>
23#include <linux/delay.h>
24#include <linux/hw_random.h>
25#include <linux/cpu.h>
Tom Lendackyc4f4b322014-06-05 10:17:57 -050026#ifdef CONFIG_X86
Tom Lendacky63b94502013-11-12 11:46:16 -060027#include <asm/cpu_device_id.h>
Tom Lendackyc4f4b322014-06-05 10:17:57 -050028#endif
Tom Lendacky63b94502013-11-12 11:46:16 -060029#include <linux/ccp.h>
30
31#include "ccp-dev.h"
32
33MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
Gary R Hookac50b782017-04-25 08:59:44 -050034MODULE_AUTHOR("Gary R Hook <gary.hook@amd.com>");
Tom Lendacky63b94502013-11-12 11:46:16 -060035MODULE_LICENSE("GPL");
Gary R Hook3cdbe342017-05-02 17:33:40 -050036MODULE_VERSION("1.1.0");
Tom Lendacky63b94502013-11-12 11:46:16 -060037MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver");
38
Tom Lendacky530abd82014-01-24 16:18:14 -060039struct ccp_tasklet_data {
40 struct completion completion;
41 struct ccp_cmd *cmd;
42};
43
Gary R Hook81422ba2016-09-28 11:53:56 -050044/* Human-readable error strings */
Wei Yongjunff4f44d2016-10-17 15:08:50 +000045static char *ccp_error_codes[] = {
Gary R Hook81422ba2016-09-28 11:53:56 -050046 "",
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
92void 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 Hook553d2372016-03-01 13:49:04 -060097/* 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 */
107static DEFINE_RWLOCK(ccp_unit_lock);
108static LIST_HEAD(ccp_units);
109
110/* Round-robin counter */
Gary R Hook03a6f292016-03-16 09:02:26 -0500111static DEFINE_SPINLOCK(ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -0600112static struct ccp_device *ccp_rr;
113
114/* Ever-increasing value to produce unique unit numbers */
115static atomic_t ccp_unit_ordinal;
Wei Yongjundabc7902016-08-12 00:00:09 +0000116static unsigned int ccp_increment_unit_ordinal(void)
Tom Lendacky63b94502013-11-12 11:46:16 -0600117{
Gary R Hook553d2372016-03-01 13:49:04 -0600118 return atomic_inc_return(&ccp_unit_ordinal);
Tom Lendacky63b94502013-11-12 11:46:16 -0600119}
120
Gary R Hookea0375a2016-03-01 13:49:25 -0600121/**
122 * ccp_add_device - add a CCP device to the list
123 *
124 * @ccp: ccp_device struct pointer
125 *
Gary R Hook553d2372016-03-01 13:49:04 -0600126 * Put this CCP on the unit list, which makes it available
127 * for use.
Gary R Hookea0375a2016-03-01 13:49:25 -0600128 *
129 * Returns zero if a CCP device is present, -ENODEV otherwise.
Gary R Hook553d2372016-03-01 13:49:04 -0600130 */
Gary R Hookea0375a2016-03-01 13:49:25 -0600131void ccp_add_device(struct ccp_device *ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600132{
Gary R Hook553d2372016-03-01 13:49:04 -0600133 unsigned long flags;
134
135 write_lock_irqsave(&ccp_unit_lock, flags);
136 list_add_tail(&ccp->entry, &ccp_units);
137 if (!ccp_rr)
138 /* We already have the list lock (we're first) so this
139 * pointer can't change on us. Set its initial value.
140 */
141 ccp_rr = ccp;
142 write_unlock_irqrestore(&ccp_unit_lock, flags);
Tom Lendacky63b94502013-11-12 11:46:16 -0600143}
144
Gary R Hookea0375a2016-03-01 13:49:25 -0600145/**
146 * ccp_del_device - remove a CCP device from the list
147 *
148 * @ccp: ccp_device struct pointer
149 *
150 * Remove this unit from the list of devices. If the next device
Gary R Hook553d2372016-03-01 13:49:04 -0600151 * up for use is this one, adjust the pointer. If this is the last
152 * device, NULL the pointer.
153 */
Gary R Hookea0375a2016-03-01 13:49:25 -0600154void ccp_del_device(struct ccp_device *ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600155{
Gary R Hook553d2372016-03-01 13:49:04 -0600156 unsigned long flags;
157
158 write_lock_irqsave(&ccp_unit_lock, flags);
159 if (ccp_rr == ccp) {
160 /* ccp_unit_lock is read/write; any read access
161 * will be suspended while we make changes to the
162 * list and RR pointer.
163 */
164 if (list_is_last(&ccp_rr->entry, &ccp_units))
165 ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
166 entry);
167 else
168 ccp_rr = list_next_entry(ccp_rr, entry);
169 }
170 list_del(&ccp->entry);
171 if (list_empty(&ccp_units))
172 ccp_rr = NULL;
173 write_unlock_irqrestore(&ccp_unit_lock, flags);
174}
175
Gary R Hook084935b2016-07-26 19:10:31 -0500176
177
178int ccp_register_rng(struct ccp_device *ccp)
179{
180 int ret = 0;
181
182 dev_dbg(ccp->dev, "Registering RNG...\n");
183 /* Register an RNG */
184 ccp->hwrng.name = ccp->rngname;
185 ccp->hwrng.read = ccp_trng_read;
186 ret = hwrng_register(&ccp->hwrng);
187 if (ret)
188 dev_err(ccp->dev, "error registering hwrng (%d)\n", ret);
189
190 return ret;
191}
192
193void ccp_unregister_rng(struct ccp_device *ccp)
194{
195 if (ccp->hwrng.name)
196 hwrng_unregister(&ccp->hwrng);
197}
198
Gary R Hook553d2372016-03-01 13:49:04 -0600199static struct ccp_device *ccp_get_device(void)
200{
201 unsigned long flags;
202 struct ccp_device *dp = NULL;
203
204 /* We round-robin through the unit list.
205 * The (ccp_rr) pointer refers to the next unit to use.
206 */
207 read_lock_irqsave(&ccp_unit_lock, flags);
208 if (!list_empty(&ccp_units)) {
Gary R Hook03a6f292016-03-16 09:02:26 -0500209 spin_lock(&ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -0600210 dp = ccp_rr;
211 if (list_is_last(&ccp_rr->entry, &ccp_units))
212 ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
213 entry);
214 else
215 ccp_rr = list_next_entry(ccp_rr, entry);
Gary R Hook03a6f292016-03-16 09:02:26 -0500216 spin_unlock(&ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -0600217 }
218 read_unlock_irqrestore(&ccp_unit_lock, flags);
219
220 return dp;
Tom Lendacky63b94502013-11-12 11:46:16 -0600221}
222
223/**
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500224 * ccp_present - check if a CCP device is present
225 *
226 * Returns zero if a CCP device is present, -ENODEV otherwise.
227 */
228int ccp_present(void)
229{
Gary R Hook553d2372016-03-01 13:49:04 -0600230 unsigned long flags;
231 int ret;
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500232
Gary R Hook553d2372016-03-01 13:49:04 -0600233 read_lock_irqsave(&ccp_unit_lock, flags);
234 ret = list_empty(&ccp_units);
235 read_unlock_irqrestore(&ccp_unit_lock, flags);
236
237 return ret ? -ENODEV : 0;
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500238}
239EXPORT_SYMBOL_GPL(ccp_present);
240
241/**
Gary R Hookc7019c42016-03-01 13:49:15 -0600242 * ccp_version - get the version of the CCP device
243 *
244 * Returns the version from the first unit on the list;
245 * otherwise a zero if no CCP device is present
246 */
247unsigned int ccp_version(void)
248{
249 struct ccp_device *dp;
250 unsigned long flags;
251 int ret = 0;
252
253 read_lock_irqsave(&ccp_unit_lock, flags);
254 if (!list_empty(&ccp_units)) {
255 dp = list_first_entry(&ccp_units, struct ccp_device, entry);
256 ret = dp->vdata->version;
257 }
258 read_unlock_irqrestore(&ccp_unit_lock, flags);
259
260 return ret;
261}
262EXPORT_SYMBOL_GPL(ccp_version);
263
264/**
Tom Lendacky63b94502013-11-12 11:46:16 -0600265 * ccp_enqueue_cmd - queue an operation for processing by the CCP
266 *
267 * @cmd: ccp_cmd struct to be processed
268 *
269 * Queue a cmd to be processed by the CCP. If queueing the cmd
270 * would exceed the defined length of the cmd queue the cmd will
271 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
272 * result in a return code of -EBUSY.
273 *
274 * The callback routine specified in the ccp_cmd struct will be
275 * called to notify the caller of completion (if the cmd was not
276 * backlogged) or advancement out of the backlog. If the cmd has
277 * advanced out of the backlog the "err" value of the callback
278 * will be -EINPROGRESS. Any other "err" value during callback is
279 * the result of the operation.
280 *
281 * The cmd has been successfully queued if:
282 * the return code is -EINPROGRESS or
283 * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
284 */
285int ccp_enqueue_cmd(struct ccp_cmd *cmd)
286{
Gary R Hook7c468442017-03-10 12:28:18 -0600287 struct ccp_device *ccp;
Tom Lendacky63b94502013-11-12 11:46:16 -0600288 unsigned long flags;
289 unsigned int i;
290 int ret;
291
Gary R Hook7c468442017-03-10 12:28:18 -0600292 /* Some commands might need to be sent to a specific device */
293 ccp = cmd->ccp ? cmd->ccp : ccp_get_device();
294
Tom Lendacky63b94502013-11-12 11:46:16 -0600295 if (!ccp)
296 return -ENODEV;
297
298 /* Caller must supply a callback routine */
299 if (!cmd->callback)
300 return -EINVAL;
301
302 cmd->ccp = ccp;
303
304 spin_lock_irqsave(&ccp->cmd_lock, flags);
305
306 i = ccp->cmd_q_count;
307
308 if (ccp->cmd_count >= MAX_CMD_QLEN) {
309 ret = -EBUSY;
310 if (cmd->flags & CCP_CMD_MAY_BACKLOG)
311 list_add_tail(&cmd->entry, &ccp->backlog);
312 } else {
313 ret = -EINPROGRESS;
314 ccp->cmd_count++;
315 list_add_tail(&cmd->entry, &ccp->cmd);
316
317 /* Find an idle queue */
318 if (!ccp->suspending) {
319 for (i = 0; i < ccp->cmd_q_count; i++) {
320 if (ccp->cmd_q[i].active)
321 continue;
322
323 break;
324 }
325 }
326 }
327
328 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
329
330 /* If we found an idle queue, wake it up */
331 if (i < ccp->cmd_q_count)
332 wake_up_process(ccp->cmd_q[i].kthread);
333
334 return ret;
335}
336EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
337
338static void ccp_do_cmd_backlog(struct work_struct *work)
339{
340 struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work);
341 struct ccp_device *ccp = cmd->ccp;
342 unsigned long flags;
343 unsigned int i;
344
345 cmd->callback(cmd->data, -EINPROGRESS);
346
347 spin_lock_irqsave(&ccp->cmd_lock, flags);
348
349 ccp->cmd_count++;
350 list_add_tail(&cmd->entry, &ccp->cmd);
351
352 /* Find an idle queue */
353 for (i = 0; i < ccp->cmd_q_count; i++) {
354 if (ccp->cmd_q[i].active)
355 continue;
356
357 break;
358 }
359
360 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
361
362 /* If we found an idle queue, wake it up */
363 if (i < ccp->cmd_q_count)
364 wake_up_process(ccp->cmd_q[i].kthread);
365}
366
367static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q)
368{
369 struct ccp_device *ccp = cmd_q->ccp;
370 struct ccp_cmd *cmd = NULL;
371 struct ccp_cmd *backlog = NULL;
372 unsigned long flags;
373
374 spin_lock_irqsave(&ccp->cmd_lock, flags);
375
376 cmd_q->active = 0;
377
378 if (ccp->suspending) {
379 cmd_q->suspended = 1;
380
381 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
382 wake_up_interruptible(&ccp->suspend_queue);
383
384 return NULL;
385 }
386
387 if (ccp->cmd_count) {
388 cmd_q->active = 1;
389
390 cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
391 list_del(&cmd->entry);
392
393 ccp->cmd_count--;
394 }
395
396 if (!list_empty(&ccp->backlog)) {
397 backlog = list_first_entry(&ccp->backlog, struct ccp_cmd,
398 entry);
399 list_del(&backlog->entry);
400 }
401
402 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
403
404 if (backlog) {
405 INIT_WORK(&backlog->work, ccp_do_cmd_backlog);
406 schedule_work(&backlog->work);
407 }
408
409 return cmd;
410}
411
Tom Lendacky530abd82014-01-24 16:18:14 -0600412static void ccp_do_cmd_complete(unsigned long data)
Tom Lendacky63b94502013-11-12 11:46:16 -0600413{
Tom Lendacky530abd82014-01-24 16:18:14 -0600414 struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
415 struct ccp_cmd *cmd = tdata->cmd;
Tom Lendacky63b94502013-11-12 11:46:16 -0600416
417 cmd->callback(cmd->data, cmd->ret);
Gary R Hook77af0ae2017-06-27 08:58:04 -0500418
Tom Lendacky530abd82014-01-24 16:18:14 -0600419 complete(&tdata->completion);
Tom Lendacky63b94502013-11-12 11:46:16 -0600420}
421
Gary R Hookea0375a2016-03-01 13:49:25 -0600422/**
423 * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue
424 *
425 * @data: thread-specific data
426 */
427int ccp_cmd_queue_thread(void *data)
Tom Lendacky63b94502013-11-12 11:46:16 -0600428{
429 struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
430 struct ccp_cmd *cmd;
Tom Lendacky530abd82014-01-24 16:18:14 -0600431 struct ccp_tasklet_data tdata;
432 struct tasklet_struct tasklet;
433
434 tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
Tom Lendacky63b94502013-11-12 11:46:16 -0600435
436 set_current_state(TASK_INTERRUPTIBLE);
437 while (!kthread_should_stop()) {
438 schedule();
439
440 set_current_state(TASK_INTERRUPTIBLE);
441
442 cmd = ccp_dequeue_cmd(cmd_q);
443 if (!cmd)
444 continue;
445
446 __set_current_state(TASK_RUNNING);
447
448 /* Execute the command */
449 cmd->ret = ccp_run_cmd(cmd_q, cmd);
450
451 /* Schedule the completion callback */
Tom Lendacky530abd82014-01-24 16:18:14 -0600452 tdata.cmd = cmd;
453 init_completion(&tdata.completion);
454 tasklet_schedule(&tasklet);
455 wait_for_completion(&tdata.completion);
Tom Lendacky63b94502013-11-12 11:46:16 -0600456 }
457
458 __set_current_state(TASK_RUNNING);
459
460 return 0;
461}
462
Tom Lendacky63b94502013-11-12 11:46:16 -0600463/**
464 * ccp_alloc_struct - allocate and initialize the ccp_device struct
465 *
466 * @dev: device struct of the CCP
467 */
468struct ccp_device *ccp_alloc_struct(struct device *dev)
469{
470 struct ccp_device *ccp;
471
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600472 ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL);
Tom Lendacky8db88462015-02-03 13:07:05 -0600473 if (!ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600474 return NULL;
Tom Lendacky63b94502013-11-12 11:46:16 -0600475 ccp->dev = dev;
476
477 INIT_LIST_HEAD(&ccp->cmd);
478 INIT_LIST_HEAD(&ccp->backlog);
479
480 spin_lock_init(&ccp->cmd_lock);
481 mutex_init(&ccp->req_mutex);
Gary R Hook956ee212016-07-26 19:09:40 -0500482 mutex_init(&ccp->sb_mutex);
483 ccp->sb_count = KSB_COUNT;
484 ccp->sb_start = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -0600485
Gary R Hook103600a2016-10-18 17:33:37 -0500486 /* Initialize the wait queues */
487 init_waitqueue_head(&ccp->sb_queue);
488 init_waitqueue_head(&ccp->suspend_queue);
489
Gary R Hook553d2372016-03-01 13:49:04 -0600490 ccp->ord = ccp_increment_unit_ordinal();
491 snprintf(ccp->name, MAX_CCP_NAME_LEN, "ccp-%u", ccp->ord);
492 snprintf(ccp->rngname, MAX_CCP_NAME_LEN, "ccp-%u-rng", ccp->ord);
493
Tom Lendacky63b94502013-11-12 11:46:16 -0600494 return ccp;
495}
496
Gary R Hook8256e682016-07-26 19:10:02 -0500497int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
498{
499 struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng);
500 u32 trng_value;
501 int len = min_t(int, sizeof(trng_value), max);
502
503 /* Locking is provided by the caller so we can update device
504 * hwrng-related fields safely
505 */
506 trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG);
507 if (!trng_value) {
508 /* Zero is returned if not data is available or if a
509 * bad-entropy error is present. Assume an error if
510 * we exceed TRNG_RETRIES reads of zero.
511 */
512 if (ccp->hwrng_retries++ > TRNG_RETRIES)
513 return -EIO;
514
515 return 0;
516 }
517
518 /* Reset the counter and save the rng value */
519 ccp->hwrng_retries = 0;
520 memcpy(data, &trng_value, len);
521
522 return len;
523}
524
Tom Lendacky63b94502013-11-12 11:46:16 -0600525#ifdef CONFIG_PM
526bool ccp_queues_suspended(struct ccp_device *ccp)
527{
528 unsigned int suspended = 0;
529 unsigned long flags;
530 unsigned int i;
531
532 spin_lock_irqsave(&ccp->cmd_lock, flags);
533
534 for (i = 0; i < ccp->cmd_q_count; i++)
535 if (ccp->cmd_q[i].suspended)
536 suspended++;
537
538 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
539
540 return ccp->cmd_q_count == suspended;
541}
Brijesh Singh970e8302017-07-06 09:59:13 -0500542
543int ccp_dev_suspend(struct ccp_device *ccp, pm_message_t state)
544{
545 unsigned long flags;
546 unsigned int i;
547
548 spin_lock_irqsave(&ccp->cmd_lock, flags);
549
550 ccp->suspending = 1;
551
552 /* Wake all the queue kthreads to prepare for suspend */
553 for (i = 0; i < ccp->cmd_q_count; i++)
554 wake_up_process(ccp->cmd_q[i].kthread);
555
556 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
557
558 /* Wait for all queue kthreads to say they're done */
559 while (!ccp_queues_suspended(ccp))
560 wait_event_interruptible(ccp->suspend_queue,
561 ccp_queues_suspended(ccp));
562
563 return 0;
564}
565
566int ccp_dev_resume(struct ccp_device *ccp)
567{
568 unsigned long flags;
569 unsigned int i;
570
571 spin_lock_irqsave(&ccp->cmd_lock, flags);
572
573 ccp->suspending = 0;
574
575 /* Wake up all the kthreads */
576 for (i = 0; i < ccp->cmd_q_count; i++) {
577 ccp->cmd_q[i].suspended = 0;
578 wake_up_process(ccp->cmd_q[i].kthread);
579 }
580
581 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
582
583 return 0;
584}
Tom Lendacky63b94502013-11-12 11:46:16 -0600585#endif
586
Brijesh Singh970e8302017-07-06 09:59:13 -0500587int ccp_dev_init(struct ccp_device *ccp)
588{
589 ccp->io_regs = ccp->io_map + ccp->vdata->offset;
590
591 if (ccp->vdata->setup)
592 ccp->vdata->setup(ccp);
593
594 return ccp->vdata->perform->init(ccp);
595}
596
597void ccp_dev_destroy(struct ccp_device *ccp)
598{
599 if (!ccp)
600 return;
601
602 ccp->vdata->perform->destroy(ccp);
603}
604
Tom Lendacky63b94502013-11-12 11:46:16 -0600605static int __init ccp_mod_init(void)
606{
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500607#ifdef CONFIG_X86
Tom Lendackydb34cf92014-01-06 13:34:29 -0600608 int ret;
Tom Lendacky63b94502013-11-12 11:46:16 -0600609
Gary R Hook3f19ce22016-03-01 13:48:54 -0600610 ret = ccp_pci_init();
611 if (ret)
612 return ret;
613
614 /* Don't leave the driver loaded if init failed */
Gary R Hook553d2372016-03-01 13:49:04 -0600615 if (ccp_present() != 0) {
Gary R Hook3f19ce22016-03-01 13:48:54 -0600616 ccp_pci_exit();
Tom Lendacky63b94502013-11-12 11:46:16 -0600617 return -ENODEV;
Fengguang Wud1dd2062013-12-09 20:08:19 +0800618 }
Gary R Hook3f19ce22016-03-01 13:48:54 -0600619
620 return 0;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500621#endif
622
623#ifdef CONFIG_ARM64
624 int ret;
625
626 ret = ccp_platform_init();
627 if (ret)
628 return ret;
629
630 /* Don't leave the driver loaded if init failed */
Gary R Hook553d2372016-03-01 13:49:04 -0600631 if (ccp_present() != 0) {
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500632 ccp_platform_exit();
633 return -ENODEV;
634 }
635
636 return 0;
637#endif
Tom Lendacky63b94502013-11-12 11:46:16 -0600638
639 return -ENODEV;
640}
641
642static void __exit ccp_mod_exit(void)
643{
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500644#ifdef CONFIG_X86
Gary R Hook3f19ce22016-03-01 13:48:54 -0600645 ccp_pci_exit();
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500646#endif
647
648#ifdef CONFIG_ARM64
649 ccp_platform_exit();
650#endif
Tom Lendacky63b94502013-11-12 11:46:16 -0600651}
652
653module_init(ccp_mod_init);
654module_exit(ccp_mod_exit);