This source file includes following definitions.
- ibmvtpm_send_crq_word
- ibmvtpm_send_crq
- tpm_ibmvtpm_recv
- ibmvtpm_crq_send_init
- tpm_ibmvtpm_resume
- tpm_ibmvtpm_send
- tpm_ibmvtpm_cancel
- tpm_ibmvtpm_status
- ibmvtpm_crq_get_rtce_size
- ibmvtpm_crq_get_version
- ibmvtpm_crq_send_init_complete
- tpm_ibmvtpm_remove
- tpm_ibmvtpm_get_desired_dma
- tpm_ibmvtpm_suspend
- ibmvtpm_reset_crq
- tpm_ibmvtpm_req_canceled
- ibmvtpm_crq_get_next
- ibmvtpm_crq_process
- ibmvtpm_interrupt
- tpm_ibmvtpm_probe
- ibmvtpm_module_init
- ibmvtpm_module_exit
1
2
3
4
5
6
7
8
9
10
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
38
39
40
41
42
43
44
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
96
97
98
99
100
101
102
103
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
138
139
140
141
142
143
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
158
159
160
161
162
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
196
197
198
199
200
201
202
203
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
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
237
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
248
249
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
264 static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
265 {
266 return;
267 }
268
269 static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
270 {
271 return 0;
272 }
273
274
275
276
277
278
279
280
281
282
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
298
299
300
301
302
303
304
305
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
321
322
323
324
325
326
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
341
342
343
344
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
373 dev_set_drvdata(&vdev->dev, NULL);
374
375 return 0;
376 }
377
378
379
380
381
382
383
384
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
392
393
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
405
406
407
408
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
426
427
428
429
430
431
432
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
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
473
474
475
476
477
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
494
495
496
497
498
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
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
566
567
568
569
570
571
572
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
579
580
581
582 while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
583 ibmvtpm_crq_process(crq, ibmvtpm);
584 crq->valid = 0;
585 smp_wmb();
586 }
587
588 return IRQ_HANDLED;
589 }
590
591
592
593
594
595
596
597
598
599
600
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 ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
632 CRQ_RES_BUF_SIZE,
633 DMA_BIDIRECTIONAL);
634
635 if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
636 dev_err(dev, "dma mapping failed\n");
637 goto cleanup;
638 }
639
640 rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
641 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
642 if (rc == H_RESOURCE)
643 rc = ibmvtpm_reset_crq(ibmvtpm);
644
645 if (rc) {
646 dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
647 goto reg_crq_cleanup;
648 }
649
650 rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
651 tpm_ibmvtpm_driver_name, ibmvtpm);
652 if (rc) {
653 dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
654 goto init_irq_cleanup;
655 }
656
657 rc = vio_enable_interrupts(vio_dev);
658 if (rc) {
659 dev_err(dev, "Error %d enabling interrupts\n", rc);
660 goto init_irq_cleanup;
661 }
662
663 init_waitqueue_head(&ibmvtpm->wq);
664
665 crq_q->index = 0;
666
667 dev_set_drvdata(&chip->dev, ibmvtpm);
668
669 spin_lock_init(&ibmvtpm->rtce_lock);
670
671 rc = ibmvtpm_crq_send_init(ibmvtpm);
672 if (rc)
673 goto init_irq_cleanup;
674
675 rc = ibmvtpm_crq_get_version(ibmvtpm);
676 if (rc)
677 goto init_irq_cleanup;
678
679 rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
680 if (rc)
681 goto init_irq_cleanup;
682
683 return tpm_chip_register(chip);
684 init_irq_cleanup:
685 do {
686 rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
687 } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
688 reg_crq_cleanup:
689 dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
690 DMA_BIDIRECTIONAL);
691 cleanup:
692 if (ibmvtpm) {
693 if (crq_q->crq_addr)
694 free_page((unsigned long)crq_q->crq_addr);
695 kfree(ibmvtpm);
696 }
697
698 return rc;
699 }
700
701 static struct vio_driver ibmvtpm_driver = {
702 .id_table = tpm_ibmvtpm_device_table,
703 .probe = tpm_ibmvtpm_probe,
704 .remove = tpm_ibmvtpm_remove,
705 .get_desired_dma = tpm_ibmvtpm_get_desired_dma,
706 .name = tpm_ibmvtpm_driver_name,
707 .pm = &tpm_ibmvtpm_pm_ops,
708 };
709
710
711
712
713
714
715
716
717
718 static int __init ibmvtpm_module_init(void)
719 {
720 return vio_register_driver(&ibmvtpm_driver);
721 }
722
723
724
725
726 static void __exit ibmvtpm_module_exit(void)
727 {
728 vio_unregister_driver(&ibmvtpm_driver);
729 }
730
731 module_init(ibmvtpm_module_init);
732 module_exit(ibmvtpm_module_exit);
733
734 MODULE_AUTHOR("adlai@us.ibm.com");
735 MODULE_DESCRIPTION("IBM vTPM Driver");
736 MODULE_VERSION("1.0");
737 MODULE_LICENSE("GPL");