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