blob: b2d176164402e2bf5131e3e415626ba89fbc914d [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
Gary R Hookea0375a2016-03-01 13:49:25 -0600114/**
115 * ccp_add_device - add a CCP device to the list
116 *
117 * @ccp: ccp_device struct pointer
118 *
Gary R Hook553d2372016-03-01 13:49:04 -0600119 * Put this CCP on the unit list, which makes it available
120 * for use.
Gary R Hookea0375a2016-03-01 13:49:25 -0600121 *
122 * Returns zero if a CCP device is present, -ENODEV otherwise.
Gary R Hook553d2372016-03-01 13:49:04 -0600123 */
Gary R Hookea0375a2016-03-01 13:49:25 -0600124void ccp_add_device(struct ccp_device *ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600125{
Gary R Hook553d2372016-03-01 13:49:04 -0600126 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 Lendacky63b94502013-11-12 11:46:16 -0600136}
137
Gary R Hookea0375a2016-03-01 13:49:25 -0600138/**
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 Hook553d2372016-03-01 13:49:04 -0600144 * up for use is this one, adjust the pointer. If this is the last
145 * device, NULL the pointer.
146 */
Gary R Hookea0375a2016-03-01 13:49:25 -0600147void ccp_del_device(struct ccp_device *ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600148{
Gary R Hook553d2372016-03-01 13:49:04 -0600149 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 Hook084935b2016-07-26 19:10:31 -0500169
170
171int 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
186void ccp_unregister_rng(struct ccp_device *ccp)
187{
188 if (ccp->hwrng.name)
189 hwrng_unregister(&ccp->hwrng);
190}
191
Gary R Hook553d2372016-03-01 13:49:04 -0600192static 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 Hook03a6f292016-03-16 09:02:26 -0500202 spin_lock(&ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -0600203 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 Hook03a6f292016-03-16 09:02:26 -0500209 spin_unlock(&ccp_rr_lock);
Gary R Hook553d2372016-03-01 13:49:04 -0600210 }
211 read_unlock_irqrestore(&ccp_unit_lock, flags);
212
213 return dp;
Tom Lendacky63b94502013-11-12 11:46:16 -0600214}
215
216/**
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500217 * ccp_present - check if a CCP device is present
218 *
219 * Returns zero if a CCP device is present, -ENODEV otherwise.
220 */
221int ccp_present(void)
222{
Gary R Hook553d2372016-03-01 13:49:04 -0600223 unsigned long flags;
224 int ret;
Tom Lendackyc9f21cb2014-09-05 10:31:09 -0500225
Gary R Hook553d2372016-03-01 13:49:04 -0600226 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 Lendackyc9f21cb2014-09-05 10:31:09 -0500231}
232EXPORT_SYMBOL_GPL(ccp_present);
233
234/**
Gary R Hookc7019c42016-03-01 13:49:15 -0600235 * 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 */
240unsigned 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}
255EXPORT_SYMBOL_GPL(ccp_version);
256
257/**
Tom Lendacky63b94502013-11-12 11:46:16 -0600258 * 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 */
278int ccp_enqueue_cmd(struct ccp_cmd *cmd)
279{
Gary R Hook7c468442017-03-10 12:28:18 -0600280 struct ccp_device *ccp;
Tom Lendacky63b94502013-11-12 11:46:16 -0600281 unsigned long flags;
282 unsigned int i;
283 int ret;
284
Gary R Hook7c468442017-03-10 12:28:18 -0600285 /* Some commands might need to be sent to a specific device */
286 ccp = cmd->ccp ? cmd->ccp : ccp_get_device();
287
Tom Lendacky63b94502013-11-12 11:46:16 -0600288 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}
329EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
330
331static 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
360static 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 Lendacky530abd82014-01-24 16:18:14 -0600405static void ccp_do_cmd_complete(unsigned long data)
Tom Lendacky63b94502013-11-12 11:46:16 -0600406{
Tom Lendacky530abd82014-01-24 16:18:14 -0600407 struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
408 struct ccp_cmd *cmd = tdata->cmd;
Tom Lendacky63b94502013-11-12 11:46:16 -0600409
410 cmd->callback(cmd->data, cmd->ret);
Gary R Hook77af0ae2017-06-27 08:58:04 -0500411
Tom Lendacky530abd82014-01-24 16:18:14 -0600412 complete(&tdata->completion);
Tom Lendacky63b94502013-11-12 11:46:16 -0600413}
414
Gary R Hookea0375a2016-03-01 13:49:25 -0600415/**
416 * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue
417 *
418 * @data: thread-specific data
419 */
420int ccp_cmd_queue_thread(void *data)
Tom Lendacky63b94502013-11-12 11:46:16 -0600421{
422 struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
423 struct ccp_cmd *cmd;
Tom Lendacky530abd82014-01-24 16:18:14 -0600424 struct ccp_tasklet_data tdata;
425 struct tasklet_struct tasklet;
426
427 tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
Tom Lendacky63b94502013-11-12 11:46:16 -0600428
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 Lendacky530abd82014-01-24 16:18:14 -0600445 tdata.cmd = cmd;
446 init_completion(&tdata.completion);
447 tasklet_schedule(&tasklet);
448 wait_for_completion(&tdata.completion);
Tom Lendacky63b94502013-11-12 11:46:16 -0600449 }
450
451 __set_current_state(TASK_RUNNING);
452
453 return 0;
454}
455
Tom Lendacky63b94502013-11-12 11:46:16 -0600456/**
457 * ccp_alloc_struct - allocate and initialize the ccp_device struct
458 *
459 * @dev: device struct of the CCP
460 */
Brijesh Singh720419f2017-07-06 09:59:14 -0500461struct ccp_device *ccp_alloc_struct(struct sp_device *sp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600462{
Brijesh Singh720419f2017-07-06 09:59:14 -0500463 struct device *dev = sp->dev;
Tom Lendacky63b94502013-11-12 11:46:16 -0600464 struct ccp_device *ccp;
465
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600466 ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL);
Tom Lendacky8db88462015-02-03 13:07:05 -0600467 if (!ccp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600468 return NULL;
Tom Lendacky63b94502013-11-12 11:46:16 -0600469 ccp->dev = dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500470 ccp->sp = sp;
471 ccp->axcache = sp->axcache;
Tom Lendacky63b94502013-11-12 11:46:16 -0600472
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 Hook956ee212016-07-26 19:09:40 -0500478 mutex_init(&ccp->sb_mutex);
479 ccp->sb_count = KSB_COUNT;
480 ccp->sb_start = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -0600481
Gary R Hook103600a2016-10-18 17:33:37 -0500482 /* Initialize the wait queues */
483 init_waitqueue_head(&ccp->sb_queue);
484 init_waitqueue_head(&ccp->suspend_queue);
485
Brijesh Singh720419f2017-07-06 09:59:14 -0500486 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 Hook553d2372016-03-01 13:49:04 -0600488
Tom Lendacky63b94502013-11-12 11:46:16 -0600489 return ccp;
490}
491
Gary R Hook8256e682016-07-26 19:10:02 -0500492int 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 Lendacky63b94502013-11-12 11:46:16 -0600520#ifdef CONFIG_PM
521bool 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 Singh970e8302017-07-06 09:59:13 -0500537
Brijesh Singh720419f2017-07-06 09:59:14 -0500538int ccp_dev_suspend(struct sp_device *sp, pm_message_t state)
Brijesh Singh970e8302017-07-06 09:59:13 -0500539{
Brijesh Singh720419f2017-07-06 09:59:14 -0500540 struct ccp_device *ccp = sp->ccp_data;
Brijesh Singh970e8302017-07-06 09:59:13 -0500541 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 Singh720419f2017-07-06 09:59:14 -0500562int ccp_dev_resume(struct sp_device *sp)
Brijesh Singh970e8302017-07-06 09:59:13 -0500563{
Brijesh Singh720419f2017-07-06 09:59:14 -0500564 struct ccp_device *ccp = sp->ccp_data;
Brijesh Singh970e8302017-07-06 09:59:13 -0500565 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 Lendacky63b94502013-11-12 11:46:16 -0600582#endif
583
Brijesh Singh720419f2017-07-06 09:59:14 -0500584int ccp_dev_init(struct sp_device *sp)
Brijesh Singh970e8302017-07-06 09:59:13 -0500585{
Brijesh Singh720419f2017-07-06 09:59:14 -0500586 struct device *dev = sp->dev;
587 struct ccp_device *ccp;
588 int ret;
Brijesh Singh970e8302017-07-06 09:59:13 -0500589
Brijesh Singh720419f2017-07-06 09:59:14 -0500590 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 Singhf4d18d62017-07-06 09:59:15 -0500603 ccp->use_tasklet = sp->use_tasklet;
Brijesh Singh720419f2017-07-06 09:59:14 -0500604
605 ccp->io_regs = sp->io_map + ccp->vdata->offset;
Brijesh Singh970e8302017-07-06 09:59:13 -0500606 if (ccp->vdata->setup)
607 ccp->vdata->setup(ccp);
608
Brijesh Singh720419f2017-07-06 09:59:14 -0500609 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
617e_err:
618 sp->ccp_data = NULL;
619
620 dev_notice(dev, "ccp initialization failed\n");
621
622 return ret;
Brijesh Singh970e8302017-07-06 09:59:13 -0500623}
624
Brijesh Singh720419f2017-07-06 09:59:14 -0500625void ccp_dev_destroy(struct sp_device *sp)
Brijesh Singh970e8302017-07-06 09:59:13 -0500626{
Brijesh Singh720419f2017-07-06 09:59:14 -0500627 struct ccp_device *ccp = sp->ccp_data;
628
Brijesh Singh970e8302017-07-06 09:59:13 -0500629 if (!ccp)
630 return;
631
632 ccp->vdata->perform->destroy(ccp);
633}