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