]> Git Repo - linux.git/blame - drivers/char/tpm/tpm_ibmvtpm.c
tpm: ibmvtpm: Set TPM_OPS_AUTO_STARTUP flag on driver
[linux.git] / drivers / char / tpm / tpm_ibmvtpm.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
132f7629 2/*
eba5cf3d 3 * Copyright (C) 2012-2020 IBM Corporation
132f7629 4 *
1a0f1b27 5 * Author: Ashley Lai <[email protected]>
132f7629
AL
6 *
7 * Maintained by: <[email protected]>
8 *
9 * Device driver for TCG/TCPA TPM (trusted platform module).
10 * Specifications at www.trustedcomputinggroup.org
132f7629
AL
11 */
12
13#include <linux/dma-mapping.h>
14#include <linux/dmapool.h>
15#include <linux/slab.h>
16#include <asm/vio.h>
17#include <asm/irq.h>
18#include <linux/types.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/interrupt.h>
22#include <linux/wait.h>
23#include <asm/prom.h>
24
25#include "tpm.h"
26#include "tpm_ibmvtpm.h"
27
28static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
29
c2a9c4bf 30static const struct vio_device_id tpm_ibmvtpm_device_table[] = {
132f7629 31 { "IBM,vtpm", "IBM,vtpm"},
18b3670d 32 { "IBM,vtpm", "IBM,vtpm20"},
132f7629
AL
33 { "", "" }
34};
35MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
36
132f7629 37/**
09c573ab 38 * ibmvtpm_send_crq_word() - Send a CRQ request
fb154e0e
MS
39 * @vdev: vio device struct
40 * @w1: pre-constructed first word of tpm crq (second word is reserved)
41 *
42 * Return:
43 * 0 - Success
44 * Non-zero - Failure
45 */
46static int ibmvtpm_send_crq_word(struct vio_dev *vdev, u64 w1)
47{
48 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, 0);
49}
50
51/**
09c573ab 52 * ibmvtpm_send_crq() - Send a CRQ request
93c12f29 53 *
132f7629 54 * @vdev: vio device struct
fb154e0e
MS
55 * @valid: Valid field
56 * @msg: Type field
57 * @len: Length field
58 * @data: Data field
59 *
60 * The ibmvtpm crq is defined as follows:
61 *
62 * Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
63 * -----------------------------------------------------------------------
64 * Word0 | Valid | Type | Length | Data
65 * -----------------------------------------------------------------------
66 * Word1 | Reserved
67 * -----------------------------------------------------------------------
68 *
69 * Which matches the following structure (on bigendian host):
70 *
71 * struct ibmvtpm_crq {
72 * u8 valid;
73 * u8 msg;
74 * __be16 len;
75 * __be32 data;
76 * __be64 reserved;
77 * } __attribute__((packed, aligned(8)));
78 *
79 * However, the value is passed in a register so just compute the numeric value
80 * to load into the register avoiding byteswap altogether. Endian only affects
81 * memory loads and stores - registers are internally represented the same.
132f7629 82 *
93c12f29 83 * Return:
fb154e0e 84 * 0 (H_SUCCESS) - Success
132f7629
AL
85 * Non-zero - Failure
86 */
fb154e0e
MS
87static int ibmvtpm_send_crq(struct vio_dev *vdev,
88 u8 valid, u8 msg, u16 len, u32 data)
132f7629 89{
fb154e0e
MS
90 u64 w1 = ((u64)valid << 56) | ((u64)msg << 48) | ((u64)len << 32) |
91 (u64)data;
92 return ibmvtpm_send_crq_word(vdev, w1);
132f7629
AL
93}
94
132f7629
AL
95/**
96 * tpm_ibmvtpm_recv - Receive data after send
93c12f29 97 *
132f7629
AL
98 * @chip: tpm chip struct
99 * @buf: buffer to read
93c12f29 100 * @count: size of buffer
132f7629 101 *
93c12f29 102 * Return:
132f7629
AL
103 * Number of bytes read
104 */
105static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
106{
9e0d39d8 107 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
132f7629
AL
108 u16 len;
109
132f7629
AL
110 if (!ibmvtpm->rtce_buf) {
111 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
112 return 0;
113 }
114
b5666502 115 len = ibmvtpm->res_len;
132f7629 116
b5666502 117 if (count < len) {
132f7629 118 dev_err(ibmvtpm->dev,
37ab0341 119 "Invalid size in recv: count=%zd, crq_size=%d\n",
b5666502 120 count, len);
132f7629
AL
121 return -EIO;
122 }
123
124 spin_lock(&ibmvtpm->rtce_lock);
b5666502
AL
125 memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
126 memset(ibmvtpm->rtce_buf, 0, len);
127 ibmvtpm->res_len = 0;
132f7629
AL
128 spin_unlock(&ibmvtpm->rtce_lock);
129 return len;
130}
131
eba5cf3d
GW
132/**
133 * ibmvtpm_crq_send_init - Send a CRQ initialize message
134 * @ibmvtpm: vtpm device struct
135 *
136 * Return:
137 * 0 on success.
138 * Non-zero on failure.
139 */
140static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
141{
142 int rc;
143
144 rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_CMD);
145 if (rc != H_SUCCESS)
146 dev_err(ibmvtpm->dev,
147 "%s failed rc=%d\n", __func__, rc);
148
149 return rc;
150}
151
152/**
153 * tpm_ibmvtpm_resume - Resume from suspend
154 *
155 * @dev: device struct
156 *
157 * Return: Always 0.
158 */
159static int tpm_ibmvtpm_resume(struct device *dev)
160{
161 struct tpm_chip *chip = dev_get_drvdata(dev);
162 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
163 int rc = 0;
164
165 do {
166 if (rc)
167 msleep(100);
168 rc = plpar_hcall_norets(H_ENABLE_CRQ,
169 ibmvtpm->vdev->unit_address);
170 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
171
172 if (rc) {
173 dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
174 return rc;
175 }
176
177 rc = vio_enable_interrupts(ibmvtpm->vdev);
178 if (rc) {
179 dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
180 return rc;
181 }
182
183 rc = ibmvtpm_crq_send_init(ibmvtpm);
184 if (rc)
185 dev_err(dev, "Error send_init rc=%d\n", rc);
186
187 return rc;
188}
189
132f7629 190/**
f5595f5b 191 * tpm_ibmvtpm_send() - Send a TPM command
132f7629
AL
192 * @chip: tpm chip struct
193 * @buf: buffer contains data to send
93c12f29 194 * @count: size of buffer
132f7629 195 *
93c12f29 196 * Return:
f5595f5b
JS
197 * 0 on success,
198 * -errno on error
132f7629
AL
199 */
200static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
201{
9e0d39d8 202 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
eba5cf3d 203 bool retry = true;
6674ff14 204 int rc, sig;
132f7629 205
132f7629
AL
206 if (!ibmvtpm->rtce_buf) {
207 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
208 return 0;
209 }
210
211 if (count > ibmvtpm->rtce_size) {
212 dev_err(ibmvtpm->dev,
37ab0341 213 "Invalid size in send: count=%zd, rtce_size=%d\n",
132f7629
AL
214 count, ibmvtpm->rtce_size);
215 return -EIO;
216 }
217
6674ff14
SB
218 if (ibmvtpm->tpm_processing_cmd) {
219 dev_info(ibmvtpm->dev,
220 "Need to wait for TPM to finish\n");
221 /* wait for previous command to finish */
222 sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
223 if (sig)
224 return -EINTR;
225 }
226
132f7629 227 spin_lock(&ibmvtpm->rtce_lock);
6674ff14 228 ibmvtpm->res_len = 0;
132f7629 229 memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
132f7629 230
6674ff14
SB
231 /*
232 * set the processing flag before the Hcall, since we may get the
233 * result (interrupt) before even being able to check rc.
234 */
047d4226 235 ibmvtpm->tpm_processing_cmd = 1;
6674ff14 236
eba5cf3d 237again:
fb154e0e
MS
238 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
239 IBMVTPM_VALID_CMD, VTPM_TPM_COMMAND,
240 count, ibmvtpm->rtce_dma_handle);
132f7629 241 if (rc != H_SUCCESS) {
eba5cf3d
GW
242 /*
243 * H_CLOSED can be returned after LPM resume. Call
244 * tpm_ibmvtpm_resume() to re-enable the CRQ then retry
245 * ibmvtpm_send_crq() once before failing.
246 */
247 if (rc == H_CLOSED && retry) {
248 tpm_ibmvtpm_resume(ibmvtpm->dev);
249 retry = false;
250 goto again;
251 }
132f7629 252 dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
047d4226 253 ibmvtpm->tpm_processing_cmd = 0;
eba5cf3d 254 }
132f7629
AL
255
256 spin_unlock(&ibmvtpm->rtce_lock);
eba5cf3d 257 return 0;
132f7629
AL
258}
259
260static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
261{
262 return;
263}
264
265static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
266{
047d4226
SB
267 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
268
269 return ibmvtpm->tpm_processing_cmd;
132f7629
AL
270}
271
272/**
273 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
93c12f29 274 *
132f7629
AL
275 * @ibmvtpm: vtpm device struct
276 *
93c12f29
WT
277 * Return:
278 * 0 on success.
279 * Non-zero on failure.
132f7629
AL
280 */
281static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
282{
132f7629
AL
283 int rc;
284
fb154e0e
MS
285 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
286 IBMVTPM_VALID_CMD, VTPM_GET_RTCE_BUFFER_SIZE, 0, 0);
132f7629
AL
287 if (rc != H_SUCCESS)
288 dev_err(ibmvtpm->dev,
289 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
290
291 return rc;
292}
293
294/**
295 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
296 * - Note that this is vtpm version and not tpm version
93c12f29 297 *
132f7629
AL
298 * @ibmvtpm: vtpm device struct
299 *
93c12f29
WT
300 * Return:
301 * 0 on success.
302 * Non-zero on failure.
132f7629
AL
303 */
304static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
305{
132f7629
AL
306 int rc;
307
fb154e0e
MS
308 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
309 IBMVTPM_VALID_CMD, VTPM_GET_VERSION, 0, 0);
132f7629
AL
310 if (rc != H_SUCCESS)
311 dev_err(ibmvtpm->dev,
312 "ibmvtpm_crq_get_version failed rc=%d\n", rc);
313
314 return rc;
315}
316
317/**
318 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
319 * @ibmvtpm: vtpm device struct
320 *
93c12f29
WT
321 * Return:
322 * 0 on success.
323 * Non-zero on failure.
132f7629
AL
324 */
325static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
326{
327 int rc;
328
fb154e0e 329 rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_COMP_CMD);
132f7629
AL
330 if (rc != H_SUCCESS)
331 dev_err(ibmvtpm->dev,
332 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
333
334 return rc;
335}
336
132f7629
AL
337/**
338 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
339 * @vdev: vio device struct
340 *
93c12f29 341 * Return: Always 0.
132f7629 342 */
386a966f 343static void tpm_ibmvtpm_remove(struct vio_dev *vdev)
132f7629 344{
9e0d39d8
CR
345 struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
346 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
132f7629
AL
347 int rc = 0;
348
afb5abc2
JS
349 tpm_chip_unregister(chip);
350
132f7629 351 free_irq(vdev->irq, ibmvtpm);
132f7629
AL
352
353 do {
354 if (rc)
355 msleep(100);
356 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
357 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
358
359 dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
360 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
361 free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
362
363 if (ibmvtpm->rtce_buf) {
364 dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
365 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
366 kfree(ibmvtpm->rtce_buf);
367 }
368
132f7629 369 kfree(ibmvtpm);
31574d32
HCVL
370 /* For tpm_ibmvtpm_get_desired_dma */
371 dev_set_drvdata(&vdev->dev, NULL);
132f7629
AL
372}
373
374/**
375 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
376 * @vdev: vio device struct
377 *
93c12f29
WT
378 * Return:
379 * Number of bytes the driver needs to DMA map.
132f7629
AL
380 */
381static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
382{
9e0d39d8 383 struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
31574d32 384 struct ibmvtpm_dev *ibmvtpm;
84eb186b 385
93c12f29
WT
386 /*
387 * ibmvtpm initializes at probe time, so the data we are
388 * asking for may not be set yet. Estimate that 4K required
389 * for TCE-mapped buffer in addition to CRQ.
390 */
31574d32
HCVL
391 if (chip)
392 ibmvtpm = dev_get_drvdata(&chip->dev);
393 else
84eb186b
HCVL
394 return CRQ_RES_BUF_SIZE + PAGE_SIZE;
395
132f7629
AL
396 return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
397}
398
399/**
400 * tpm_ibmvtpm_suspend - Suspend
401 * @dev: device struct
402 *
93c12f29 403 * Return: Always 0.
132f7629
AL
404 */
405static int tpm_ibmvtpm_suspend(struct device *dev)
406{
9e0d39d8
CR
407 struct tpm_chip *chip = dev_get_drvdata(dev);
408 struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
132f7629
AL
409 int rc = 0;
410
fb154e0e
MS
411 rc = ibmvtpm_send_crq(ibmvtpm->vdev,
412 IBMVTPM_VALID_CMD, VTPM_PREPARE_TO_SUSPEND, 0, 0);
132f7629
AL
413 if (rc != H_SUCCESS)
414 dev_err(ibmvtpm->dev,
415 "tpm_ibmvtpm_suspend failed rc=%d\n", rc);
416
417 return rc;
418}
419
420/**
421 * ibmvtpm_reset_crq - Reset CRQ
93c12f29 422 *
132f7629
AL
423 * @ibmvtpm: ibm vtpm struct
424 *
93c12f29
WT
425 * Return:
426 * 0 on success.
427 * Non-zero on failure.
132f7629
AL
428 */
429static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
430{
431 int rc = 0;
432
433 do {
434 if (rc)
435 msleep(100);
436 rc = plpar_hcall_norets(H_FREE_CRQ,
437 ibmvtpm->vdev->unit_address);
438 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
439
440 memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
441 ibmvtpm->crq_queue.index = 0;
442
443 return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
444 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
445}
446
1f866057
SB
447static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
448{
449 return (status == 0);
450}
451
01ad1fa7 452static const struct tpm_class_ops tpm_ibmvtpm = {
02e9bda8 453 .flags = TPM_OPS_AUTO_STARTUP,
132f7629
AL
454 .recv = tpm_ibmvtpm_recv,
455 .send = tpm_ibmvtpm_send,
456 .cancel = tpm_ibmvtpm_cancel,
457 .status = tpm_ibmvtpm_status,
047d4226 458 .req_complete_mask = 1,
132f7629 459 .req_complete_val = 0,
1f866057 460 .req_canceled = tpm_ibmvtpm_req_canceled,
132f7629
AL
461};
462
463static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = {
464 .suspend = tpm_ibmvtpm_suspend,
465 .resume = tpm_ibmvtpm_resume,
466};
467
468/**
469 * ibmvtpm_crq_get_next - Get next responded crq
132f7629 470 *
93c12f29
WT
471 * @ibmvtpm: vtpm device struct
472 *
473 * Return: vtpm crq pointer or NULL.
132f7629
AL
474 */
475static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm)
476{
477 struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue;
478 struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index];
479
480 if (crq->valid & VTPM_MSG_RES) {
481 if (++crq_q->index == crq_q->num_entry)
482 crq_q->index = 0;
b5666502 483 smp_rmb();
132f7629
AL
484 } else
485 crq = NULL;
486 return crq;
487}
488
489/**
490 * ibmvtpm_crq_process - Process responded crq
132f7629 491 *
93c12f29
WT
492 * @crq: crq to be processed
493 * @ibmvtpm: vtpm device struct
494 *
132f7629
AL
495 */
496static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
497 struct ibmvtpm_dev *ibmvtpm)
498{
499 int rc = 0;
500
501 switch (crq->valid) {
502 case VALID_INIT_CRQ:
503 switch (crq->msg) {
504 case INIT_CRQ_RES:
505 dev_info(ibmvtpm->dev, "CRQ initialized\n");
506 rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
507 if (rc)
508 dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
509 return;
510 case INIT_CRQ_COMP_RES:
511 dev_info(ibmvtpm->dev,
512 "CRQ initialization completed\n");
513 return;
514 default:
515 dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
516 return;
517 }
132f7629
AL
518 case IBMVTPM_VALID_CMD:
519 switch (crq->msg) {
520 case VTPM_GET_RTCE_BUFFER_SIZE_RES:
eb71f8a5 521 if (be16_to_cpu(crq->len) <= 0) {
132f7629
AL
522 dev_err(ibmvtpm->dev, "Invalid rtce size\n");
523 return;
524 }
eb71f8a5 525 ibmvtpm->rtce_size = be16_to_cpu(crq->len);
132f7629 526 ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
60ecd86c 527 GFP_ATOMIC);
132f7629
AL
528 if (!ibmvtpm->rtce_buf) {
529 dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
530 return;
531 }
532
533 ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
534 ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
535 DMA_BIDIRECTIONAL);
536
537 if (dma_mapping_error(ibmvtpm->dev,
538 ibmvtpm->rtce_dma_handle)) {
539 kfree(ibmvtpm->rtce_buf);
540 ibmvtpm->rtce_buf = NULL;
541 dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
542 }
543
544 return;
545 case VTPM_GET_VERSION_RES:
eb71f8a5 546 ibmvtpm->vtpm_version = be32_to_cpu(crq->data);
132f7629
AL
547 return;
548 case VTPM_TPM_COMMAND_RES:
b5666502 549 /* len of the data in rtce buffer */
eb71f8a5 550 ibmvtpm->res_len = be16_to_cpu(crq->len);
047d4226 551 ibmvtpm->tpm_processing_cmd = 0;
b5666502 552 wake_up_interruptible(&ibmvtpm->wq);
132f7629
AL
553 return;
554 default:
555 return;
556 }
557 }
558 return;
559}
560
561/**
562 * ibmvtpm_interrupt - Interrupt handler
93c12f29 563 *
132f7629
AL
564 * @irq: irq number to handle
565 * @vtpm_instance: vtpm that received interrupt
566 *
567 * Returns:
568 * IRQ_HANDLED
569 **/
570static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
571{
572 struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
132f7629 573 struct ibmvtpm_crq *crq;
132f7629 574
b5666502
AL
575 /* while loop is needed for initial setup (get version and
576 * get rtce_size). There should be only one tpm request at any
577 * given time.
578 */
132f7629
AL
579 while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
580 ibmvtpm_crq_process(crq, ibmvtpm);
d8d74ea3 581 wake_up_interruptible(&ibmvtpm->crq_queue.wq);
132f7629 582 crq->valid = 0;
b5666502 583 smp_wmb();
132f7629
AL
584 }
585
b5666502 586 return IRQ_HANDLED;
132f7629
AL
587}
588
589/**
590 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
93c12f29 591 *
132f7629
AL
592 * @vio_dev: vio device struct
593 * @id: vio device id struct
594 *
93c12f29
WT
595 * Return:
596 * 0 on success.
597 * Non-zero on failure.
132f7629 598 */
afc6d369 599static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
132f7629
AL
600 const struct vio_device_id *id)
601{
602 struct ibmvtpm_dev *ibmvtpm;
603 struct device *dev = &vio_dev->dev;
604 struct ibmvtpm_crq_queue *crq_q;
605 struct tpm_chip *chip;
606 int rc = -ENOMEM, rc1;
607
afb5abc2
JS
608 chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm);
609 if (IS_ERR(chip))
610 return PTR_ERR(chip);
132f7629
AL
611
612 ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL);
613 if (!ibmvtpm) {
614 dev_err(dev, "kzalloc for ibmvtpm failed\n");
615 goto cleanup;
616 }
617
9d75f089
HCVL
618 ibmvtpm->dev = dev;
619 ibmvtpm->vdev = vio_dev;
620
132f7629
AL
621 crq_q = &ibmvtpm->crq_queue;
622 crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
623 if (!crq_q->crq_addr) {
624 dev_err(dev, "Unable to allocate memory for crq_addr\n");
625 goto cleanup;
626 }
627
628 crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
d8d74ea3 629 init_waitqueue_head(&crq_q->wq);
132f7629
AL
630 ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
631 CRQ_RES_BUF_SIZE,
632 DMA_BIDIRECTIONAL);
633
634 if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
635 dev_err(dev, "dma mapping failed\n");
636 goto cleanup;
637 }
638
639 rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
640 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
641 if (rc == H_RESOURCE)
642 rc = ibmvtpm_reset_crq(ibmvtpm);
643
644 if (rc) {
645 dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
646 goto reg_crq_cleanup;
647 }
648
132f7629
AL
649 rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
650 tpm_ibmvtpm_driver_name, ibmvtpm);
651 if (rc) {
652 dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
653 goto init_irq_cleanup;
654 }
655
656 rc = vio_enable_interrupts(vio_dev);
657 if (rc) {
658 dev_err(dev, "Error %d enabling interrupts\n", rc);
659 goto init_irq_cleanup;
660 }
661
b5666502
AL
662 init_waitqueue_head(&ibmvtpm->wq);
663
132f7629
AL
664 crq_q->index = 0;
665
75254557 666 dev_set_drvdata(&chip->dev, ibmvtpm);
132f7629 667
132f7629
AL
668 spin_lock_init(&ibmvtpm->rtce_lock);
669
670 rc = ibmvtpm_crq_send_init(ibmvtpm);
671 if (rc)
672 goto init_irq_cleanup;
673
674 rc = ibmvtpm_crq_get_version(ibmvtpm);
675 if (rc)
676 goto init_irq_cleanup;
677
678 rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
679 if (rc)
680 goto init_irq_cleanup;
681
d8d74ea3
SB
682 if (!wait_event_timeout(ibmvtpm->crq_queue.wq,
683 ibmvtpm->rtce_buf != NULL,
684 HZ)) {
d0dc1a71 685 rc = -ENODEV;
d8d74ea3
SB
686 dev_err(dev, "CRQ response timed out\n");
687 goto init_irq_cleanup;
688 }
689
047d4226
SB
690
691 if (!strcmp(id->compat, "IBM,vtpm20"))
72d0556d 692 chip->flags |= TPM_CHIP_FLAG_TPM2;
047d4226 693
afb5abc2 694 return tpm_chip_register(chip);
132f7629 695init_irq_cleanup:
132f7629
AL
696 do {
697 rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
698 } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
699reg_crq_cleanup:
700 dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
701 DMA_BIDIRECTIONAL);
702cleanup:
703 if (ibmvtpm) {
704 if (crq_q->crq_addr)
705 free_page((unsigned long)crq_q->crq_addr);
706 kfree(ibmvtpm);
707 }
708
132f7629
AL
709 return rc;
710}
711
712static struct vio_driver ibmvtpm_driver = {
713 .id_table = tpm_ibmvtpm_device_table,
714 .probe = tpm_ibmvtpm_probe,
715 .remove = tpm_ibmvtpm_remove,
716 .get_desired_dma = tpm_ibmvtpm_get_desired_dma,
717 .name = tpm_ibmvtpm_driver_name,
718 .pm = &tpm_ibmvtpm_pm_ops,
719};
720
721/**
93c12f29 722 * ibmvtpm_module_init - Initialize ibm vtpm module.
132f7629 723 *
93c12f29
WT
724 *
725 * Return:
726 * 0 on success.
727 * Non-zero on failure.
132f7629
AL
728 */
729static int __init ibmvtpm_module_init(void)
730{
731 return vio_register_driver(&ibmvtpm_driver);
732}
733
734/**
93c12f29 735 * ibmvtpm_module_exit - Tear down ibm vtpm module.
132f7629
AL
736 */
737static void __exit ibmvtpm_module_exit(void)
738{
739 vio_unregister_driver(&ibmvtpm_driver);
740}
741
742module_init(ibmvtpm_module_init);
743module_exit(ibmvtpm_module_exit);
744
745MODULE_AUTHOR("[email protected]");
746MODULE_DESCRIPTION("IBM vTPM Driver");
747MODULE_VERSION("1.0");
748MODULE_LICENSE("GPL");
This page took 0.775717 seconds and 4 git commands to generate.