blob: 56b07c35a13e173bf1cc5d10321d30d5a11f259e [file] [log] [blame]
Ashley Lai132f7622012-08-22 16:17:43 -05001/*
2 * Copyright (C) 2012 IBM Corporation
3 *
4 * Author: Ashley Lai <adlai@us.ibm.com>
5 *
6 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
7 *
8 * Device driver for TCG/TCPA TPM (trusted platform module).
9 * Specifications at www.trustedcomputinggroup.org
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, version 2 of the
14 * License.
15 *
16 */
17
18#include <linux/dma-mapping.h>
19#include <linux/dmapool.h>
20#include <linux/slab.h>
21#include <asm/vio.h>
22#include <asm/irq.h>
23#include <linux/types.h>
24#include <linux/list.h>
25#include <linux/spinlock.h>
26#include <linux/interrupt.h>
27#include <linux/wait.h>
28#include <asm/prom.h>
29
30#include "tpm.h"
31#include "tpm_ibmvtpm.h"
32
33static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
34
Bill Pemberton0bbed202012-11-19 13:24:36 -050035static struct vio_device_id tpm_ibmvtpm_device_table[] = {
Ashley Lai132f7622012-08-22 16:17:43 -050036 { "IBM,vtpm", "IBM,vtpm"},
37 { "", "" }
38};
39MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
40
Ashley Lai132f7622012-08-22 16:17:43 -050041/**
42 * ibmvtpm_send_crq - Send a CRQ request
43 * @vdev: vio device struct
44 * @w1: first word
45 * @w2: second word
46 *
47 * Return value:
48 * 0 -Sucess
49 * Non-zero - Failure
50 */
51static int ibmvtpm_send_crq(struct vio_dev *vdev, u64 w1, u64 w2)
52{
53 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, w2);
54}
55
56/**
57 * ibmvtpm_get_data - Retrieve ibm vtpm data
58 * @dev: device struct
59 *
60 * Return value:
61 * vtpm device struct
62 */
63static struct ibmvtpm_dev *ibmvtpm_get_data(const struct device *dev)
64{
65 struct tpm_chip *chip = dev_get_drvdata(dev);
66 if (chip)
Kent Yoder775585e2012-12-05 11:36:20 -060067 return (struct ibmvtpm_dev *)TPM_VPRIV(chip);
Ashley Lai132f7622012-08-22 16:17:43 -050068 return NULL;
69}
70
71/**
72 * tpm_ibmvtpm_recv - Receive data after send
73 * @chip: tpm chip struct
74 * @buf: buffer to read
75 * count: size of buffer
76 *
77 * Return value:
78 * Number of bytes read
79 */
80static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
81{
82 struct ibmvtpm_dev *ibmvtpm;
83 u16 len;
Ashley Laib5666502012-09-12 12:49:50 -050084 int sig;
Ashley Lai132f7622012-08-22 16:17:43 -050085
Kent Yoder775585e2012-12-05 11:36:20 -060086 ibmvtpm = (struct ibmvtpm_dev *)TPM_VPRIV(chip);
Ashley Lai132f7622012-08-22 16:17:43 -050087
88 if (!ibmvtpm->rtce_buf) {
89 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
90 return 0;
91 }
92
Ashley Laib5666502012-09-12 12:49:50 -050093 sig = wait_event_interruptible(ibmvtpm->wq, ibmvtpm->res_len != 0);
94 if (sig)
95 return -EINTR;
Ashley Lai132f7622012-08-22 16:17:43 -050096
Ashley Laib5666502012-09-12 12:49:50 -050097 len = ibmvtpm->res_len;
98
99 if (count < len) {
Ashley Lai132f7622012-08-22 16:17:43 -0500100 dev_err(ibmvtpm->dev,
101 "Invalid size in recv: count=%ld, crq_size=%d\n",
Ashley Laib5666502012-09-12 12:49:50 -0500102 count, len);
Ashley Lai132f7622012-08-22 16:17:43 -0500103 return -EIO;
104 }
105
106 spin_lock(&ibmvtpm->rtce_lock);
Ashley Laib5666502012-09-12 12:49:50 -0500107 memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
108 memset(ibmvtpm->rtce_buf, 0, len);
109 ibmvtpm->res_len = 0;
Ashley Lai132f7622012-08-22 16:17:43 -0500110 spin_unlock(&ibmvtpm->rtce_lock);
111 return len;
112}
113
114/**
115 * tpm_ibmvtpm_send - Send tpm request
116 * @chip: tpm chip struct
117 * @buf: buffer contains data to send
118 * count: size of buffer
119 *
120 * Return value:
121 * Number of bytes sent
122 */
123static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
124{
125 struct ibmvtpm_dev *ibmvtpm;
126 struct ibmvtpm_crq crq;
127 u64 *word = (u64 *) &crq;
128 int rc;
129
Kent Yoder775585e2012-12-05 11:36:20 -0600130 ibmvtpm = (struct ibmvtpm_dev *)TPM_VPRIV(chip);
Ashley Lai132f7622012-08-22 16:17:43 -0500131
132 if (!ibmvtpm->rtce_buf) {
133 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
134 return 0;
135 }
136
137 if (count > ibmvtpm->rtce_size) {
138 dev_err(ibmvtpm->dev,
139 "Invalid size in send: count=%ld, rtce_size=%d\n",
140 count, ibmvtpm->rtce_size);
141 return -EIO;
142 }
143
144 spin_lock(&ibmvtpm->rtce_lock);
145 memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
146 crq.valid = (u8)IBMVTPM_VALID_CMD;
147 crq.msg = (u8)VTPM_TPM_COMMAND;
148 crq.len = (u16)count;
149 crq.data = ibmvtpm->rtce_dma_handle;
150
151 rc = ibmvtpm_send_crq(ibmvtpm->vdev, word[0], word[1]);
152 if (rc != H_SUCCESS) {
153 dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
154 rc = 0;
155 } else
156 rc = count;
157
158 spin_unlock(&ibmvtpm->rtce_lock);
159 return rc;
160}
161
162static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
163{
164 return;
165}
166
167static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
168{
169 return 0;
170}
171
172/**
173 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
174 * @ibmvtpm: vtpm device struct
175 *
176 * Return value:
177 * 0 - Success
178 * Non-zero - Failure
179 */
180static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
181{
182 struct ibmvtpm_crq crq;
183 u64 *buf = (u64 *) &crq;
184 int rc;
185
186 crq.valid = (u8)IBMVTPM_VALID_CMD;
187 crq.msg = (u8)VTPM_GET_RTCE_BUFFER_SIZE;
188
189 rc = ibmvtpm_send_crq(ibmvtpm->vdev, buf[0], buf[1]);
190 if (rc != H_SUCCESS)
191 dev_err(ibmvtpm->dev,
192 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
193
194 return rc;
195}
196
197/**
198 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
199 * - Note that this is vtpm version and not tpm version
200 * @ibmvtpm: vtpm device struct
201 *
202 * Return value:
203 * 0 - Success
204 * Non-zero - Failure
205 */
206static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
207{
208 struct ibmvtpm_crq crq;
209 u64 *buf = (u64 *) &crq;
210 int rc;
211
212 crq.valid = (u8)IBMVTPM_VALID_CMD;
213 crq.msg = (u8)VTPM_GET_VERSION;
214
215 rc = ibmvtpm_send_crq(ibmvtpm->vdev, buf[0], buf[1]);
216 if (rc != H_SUCCESS)
217 dev_err(ibmvtpm->dev,
218 "ibmvtpm_crq_get_version failed rc=%d\n", rc);
219
220 return rc;
221}
222
223/**
224 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
225 * @ibmvtpm: vtpm device struct
226 *
227 * Return value:
228 * 0 - Success
229 * Non-zero - Failure
230 */
231static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
232{
233 int rc;
234
235 rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_COMP_CMD, 0);
236 if (rc != H_SUCCESS)
237 dev_err(ibmvtpm->dev,
238 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
239
240 return rc;
241}
242
243/**
244 * ibmvtpm_crq_send_init - Send a CRQ initialize message
245 * @ibmvtpm: vtpm device struct
246 *
247 * Return value:
248 * 0 - Success
249 * Non-zero - Failure
250 */
251static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
252{
253 int rc;
254
255 rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_CMD, 0);
256 if (rc != H_SUCCESS)
257 dev_err(ibmvtpm->dev,
258 "ibmvtpm_crq_send_init failed rc=%d\n", rc);
259
260 return rc;
261}
262
263/**
264 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
265 * @vdev: vio device struct
266 *
267 * Return value:
268 * 0
269 */
Bill Pemberton39af33f2012-11-19 13:26:26 -0500270static int tpm_ibmvtpm_remove(struct vio_dev *vdev)
Ashley Lai132f7622012-08-22 16:17:43 -0500271{
272 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev);
273 int rc = 0;
274
275 free_irq(vdev->irq, ibmvtpm);
Ashley Lai132f7622012-08-22 16:17:43 -0500276
277 do {
278 if (rc)
279 msleep(100);
280 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
281 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
282
283 dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
284 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
285 free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
286
287 if (ibmvtpm->rtce_buf) {
288 dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
289 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
290 kfree(ibmvtpm->rtce_buf);
291 }
292
293 tpm_remove_hardware(ibmvtpm->dev);
294
295 kfree(ibmvtpm);
296
297 return 0;
298}
299
300/**
301 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
302 * @vdev: vio device struct
303 *
304 * Return value:
305 * Number of bytes the driver needs to DMA map
306 */
307static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
308{
309 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev);
310 return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
311}
312
313/**
314 * tpm_ibmvtpm_suspend - Suspend
315 * @dev: device struct
316 *
317 * Return value:
318 * 0
319 */
320static int tpm_ibmvtpm_suspend(struct device *dev)
321{
322 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev);
323 struct ibmvtpm_crq crq;
324 u64 *buf = (u64 *) &crq;
325 int rc = 0;
326
327 crq.valid = (u8)IBMVTPM_VALID_CMD;
328 crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND;
329
330 rc = ibmvtpm_send_crq(ibmvtpm->vdev, buf[0], buf[1]);
331 if (rc != H_SUCCESS)
332 dev_err(ibmvtpm->dev,
333 "tpm_ibmvtpm_suspend failed rc=%d\n", rc);
334
335 return rc;
336}
337
338/**
339 * ibmvtpm_reset_crq - Reset CRQ
340 * @ibmvtpm: ibm vtpm struct
341 *
342 * Return value:
343 * 0 - Success
344 * Non-zero - Failure
345 */
346static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
347{
348 int rc = 0;
349
350 do {
351 if (rc)
352 msleep(100);
353 rc = plpar_hcall_norets(H_FREE_CRQ,
354 ibmvtpm->vdev->unit_address);
355 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
356
357 memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
358 ibmvtpm->crq_queue.index = 0;
359
360 return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
361 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
362}
363
364/**
365 * tpm_ibmvtpm_resume - Resume from suspend
366 * @dev: device struct
367 *
368 * Return value:
369 * 0
370 */
371static int tpm_ibmvtpm_resume(struct device *dev)
372{
373 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev);
Ashley Lai132f7622012-08-22 16:17:43 -0500374 int rc = 0;
375
376 do {
377 if (rc)
378 msleep(100);
379 rc = plpar_hcall_norets(H_ENABLE_CRQ,
380 ibmvtpm->vdev->unit_address);
381 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
382
383 if (rc) {
384 dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
385 return rc;
386 }
387
Ashley Laib5666502012-09-12 12:49:50 -0500388 rc = vio_enable_interrupts(ibmvtpm->vdev);
389 if (rc) {
390 dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
391 return rc;
392 }
Ashley Lai132f7622012-08-22 16:17:43 -0500393
394 rc = ibmvtpm_crq_send_init(ibmvtpm);
395 if (rc)
396 dev_err(dev, "Error send_init rc=%d\n", rc);
397
398 return rc;
399}
400
Stefan Berger1f866052013-01-22 13:52:35 -0600401static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
402{
403 return (status == 0);
404}
405
Ashley Lai132f7622012-08-22 16:17:43 -0500406static const struct file_operations ibmvtpm_ops = {
407 .owner = THIS_MODULE,
408 .llseek = no_llseek,
409 .open = tpm_open,
410 .read = tpm_read,
411 .write = tpm_write,
412 .release = tpm_release,
413};
414
415static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
416static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
417static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
418static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
419static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
420static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
421 NULL);
422static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
423static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
424static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
425static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
426
427static struct attribute *ibmvtpm_attrs[] = {
428 &dev_attr_pubek.attr,
429 &dev_attr_pcrs.attr,
430 &dev_attr_enabled.attr,
431 &dev_attr_active.attr,
432 &dev_attr_owned.attr,
433 &dev_attr_temp_deactivated.attr,
434 &dev_attr_caps.attr,
435 &dev_attr_cancel.attr,
436 &dev_attr_durations.attr,
437 &dev_attr_timeouts.attr, NULL,
438};
439
440static struct attribute_group ibmvtpm_attr_grp = { .attrs = ibmvtpm_attrs };
441
442static const struct tpm_vendor_specific tpm_ibmvtpm = {
443 .recv = tpm_ibmvtpm_recv,
444 .send = tpm_ibmvtpm_send,
445 .cancel = tpm_ibmvtpm_cancel,
446 .status = tpm_ibmvtpm_status,
447 .req_complete_mask = 0,
448 .req_complete_val = 0,
Stefan Berger1f866052013-01-22 13:52:35 -0600449 .req_canceled = tpm_ibmvtpm_req_canceled,
Ashley Lai132f7622012-08-22 16:17:43 -0500450 .attr_group = &ibmvtpm_attr_grp,
451 .miscdev = { .fops = &ibmvtpm_ops, },
452};
453
454static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = {
455 .suspend = tpm_ibmvtpm_suspend,
456 .resume = tpm_ibmvtpm_resume,
457};
458
459/**
460 * ibmvtpm_crq_get_next - Get next responded crq
461 * @ibmvtpm vtpm device struct
462 *
463 * Return value:
464 * vtpm crq pointer
465 */
466static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm)
467{
468 struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue;
469 struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index];
470
471 if (crq->valid & VTPM_MSG_RES) {
472 if (++crq_q->index == crq_q->num_entry)
473 crq_q->index = 0;
Ashley Laib5666502012-09-12 12:49:50 -0500474 smp_rmb();
Ashley Lai132f7622012-08-22 16:17:43 -0500475 } else
476 crq = NULL;
477 return crq;
478}
479
480/**
481 * ibmvtpm_crq_process - Process responded crq
482 * @crq crq to be processed
483 * @ibmvtpm vtpm device struct
484 *
485 * Return value:
486 * Nothing
487 */
488static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
489 struct ibmvtpm_dev *ibmvtpm)
490{
491 int rc = 0;
492
493 switch (crq->valid) {
494 case VALID_INIT_CRQ:
495 switch (crq->msg) {
496 case INIT_CRQ_RES:
497 dev_info(ibmvtpm->dev, "CRQ initialized\n");
498 rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
499 if (rc)
500 dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
501 return;
502 case INIT_CRQ_COMP_RES:
503 dev_info(ibmvtpm->dev,
504 "CRQ initialization completed\n");
505 return;
506 default:
507 dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
508 return;
509 }
510 return;
511 case IBMVTPM_VALID_CMD:
512 switch (crq->msg) {
513 case VTPM_GET_RTCE_BUFFER_SIZE_RES:
514 if (crq->len <= 0) {
515 dev_err(ibmvtpm->dev, "Invalid rtce size\n");
516 return;
517 }
518 ibmvtpm->rtce_size = crq->len;
519 ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
520 GFP_KERNEL);
521 if (!ibmvtpm->rtce_buf) {
522 dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
523 return;
524 }
525
526 ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
527 ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
528 DMA_BIDIRECTIONAL);
529
530 if (dma_mapping_error(ibmvtpm->dev,
531 ibmvtpm->rtce_dma_handle)) {
532 kfree(ibmvtpm->rtce_buf);
533 ibmvtpm->rtce_buf = NULL;
534 dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
535 }
536
537 return;
538 case VTPM_GET_VERSION_RES:
539 ibmvtpm->vtpm_version = crq->data;
540 return;
541 case VTPM_TPM_COMMAND_RES:
Ashley Laib5666502012-09-12 12:49:50 -0500542 /* len of the data in rtce buffer */
543 ibmvtpm->res_len = crq->len;
544 wake_up_interruptible(&ibmvtpm->wq);
Ashley Lai132f7622012-08-22 16:17:43 -0500545 return;
546 default:
547 return;
548 }
549 }
550 return;
551}
552
553/**
554 * ibmvtpm_interrupt - Interrupt handler
555 * @irq: irq number to handle
556 * @vtpm_instance: vtpm that received interrupt
557 *
558 * Returns:
559 * IRQ_HANDLED
560 **/
561static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
562{
563 struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
Ashley Lai132f7622012-08-22 16:17:43 -0500564 struct ibmvtpm_crq *crq;
Ashley Lai132f7622012-08-22 16:17:43 -0500565
Ashley Laib5666502012-09-12 12:49:50 -0500566 /* while loop is needed for initial setup (get version and
567 * get rtce_size). There should be only one tpm request at any
568 * given time.
569 */
Ashley Lai132f7622012-08-22 16:17:43 -0500570 while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
571 ibmvtpm_crq_process(crq, ibmvtpm);
572 crq->valid = 0;
Ashley Laib5666502012-09-12 12:49:50 -0500573 smp_wmb();
Ashley Lai132f7622012-08-22 16:17:43 -0500574 }
575
Ashley Laib5666502012-09-12 12:49:50 -0500576 return IRQ_HANDLED;
Ashley Lai132f7622012-08-22 16:17:43 -0500577}
578
579/**
580 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
581 * @vio_dev: vio device struct
582 * @id: vio device id struct
583 *
584 * Return value:
585 * 0 - Success
586 * Non-zero - Failure
587 */
Bill Pembertonafc6d362012-11-19 13:22:42 -0500588static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
Ashley Lai132f7622012-08-22 16:17:43 -0500589 const struct vio_device_id *id)
590{
591 struct ibmvtpm_dev *ibmvtpm;
592 struct device *dev = &vio_dev->dev;
593 struct ibmvtpm_crq_queue *crq_q;
594 struct tpm_chip *chip;
595 int rc = -ENOMEM, rc1;
596
597 chip = tpm_register_hardware(dev, &tpm_ibmvtpm);
598 if (!chip) {
599 dev_err(dev, "tpm_register_hardware failed\n");
600 return -ENODEV;
601 }
602
603 ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL);
604 if (!ibmvtpm) {
605 dev_err(dev, "kzalloc for ibmvtpm failed\n");
606 goto cleanup;
607 }
608
609 crq_q = &ibmvtpm->crq_queue;
610 crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
611 if (!crq_q->crq_addr) {
612 dev_err(dev, "Unable to allocate memory for crq_addr\n");
613 goto cleanup;
614 }
615
616 crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
617 ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
618 CRQ_RES_BUF_SIZE,
619 DMA_BIDIRECTIONAL);
620
621 if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
622 dev_err(dev, "dma mapping failed\n");
623 goto cleanup;
624 }
625
626 rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
627 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
628 if (rc == H_RESOURCE)
629 rc = ibmvtpm_reset_crq(ibmvtpm);
630
631 if (rc) {
632 dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
633 goto reg_crq_cleanup;
634 }
635
Ashley Lai132f7622012-08-22 16:17:43 -0500636 rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
637 tpm_ibmvtpm_driver_name, ibmvtpm);
638 if (rc) {
639 dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
640 goto init_irq_cleanup;
641 }
642
643 rc = vio_enable_interrupts(vio_dev);
644 if (rc) {
645 dev_err(dev, "Error %d enabling interrupts\n", rc);
646 goto init_irq_cleanup;
647 }
648
Ashley Laib5666502012-09-12 12:49:50 -0500649 init_waitqueue_head(&ibmvtpm->wq);
650
Ashley Lai132f7622012-08-22 16:17:43 -0500651 crq_q->index = 0;
652
653 ibmvtpm->dev = dev;
654 ibmvtpm->vdev = vio_dev;
Kent Yoder775585e2012-12-05 11:36:20 -0600655 TPM_VPRIV(chip) = (void *)ibmvtpm;
Ashley Lai132f7622012-08-22 16:17:43 -0500656
Ashley Lai132f7622012-08-22 16:17:43 -0500657 spin_lock_init(&ibmvtpm->rtce_lock);
658
659 rc = ibmvtpm_crq_send_init(ibmvtpm);
660 if (rc)
661 goto init_irq_cleanup;
662
663 rc = ibmvtpm_crq_get_version(ibmvtpm);
664 if (rc)
665 goto init_irq_cleanup;
666
667 rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
668 if (rc)
669 goto init_irq_cleanup;
670
671 return rc;
672init_irq_cleanup:
Ashley Lai132f7622012-08-22 16:17:43 -0500673 do {
674 rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
675 } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
676reg_crq_cleanup:
677 dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
678 DMA_BIDIRECTIONAL);
679cleanup:
680 if (ibmvtpm) {
681 if (crq_q->crq_addr)
682 free_page((unsigned long)crq_q->crq_addr);
683 kfree(ibmvtpm);
684 }
685
686 tpm_remove_hardware(dev);
687
688 return rc;
689}
690
691static struct vio_driver ibmvtpm_driver = {
692 .id_table = tpm_ibmvtpm_device_table,
693 .probe = tpm_ibmvtpm_probe,
694 .remove = tpm_ibmvtpm_remove,
695 .get_desired_dma = tpm_ibmvtpm_get_desired_dma,
696 .name = tpm_ibmvtpm_driver_name,
697 .pm = &tpm_ibmvtpm_pm_ops,
698};
699
700/**
701 * ibmvtpm_module_init - Initialize ibm vtpm module
702 *
703 * Return value:
704 * 0 -Success
705 * Non-zero - Failure
706 */
707static int __init ibmvtpm_module_init(void)
708{
709 return vio_register_driver(&ibmvtpm_driver);
710}
711
712/**
713 * ibmvtpm_module_exit - Teardown ibm vtpm module
714 *
715 * Return value:
716 * Nothing
717 */
718static void __exit ibmvtpm_module_exit(void)
719{
720 vio_unregister_driver(&ibmvtpm_driver);
721}
722
723module_init(ibmvtpm_module_init);
724module_exit(ibmvtpm_module_exit);
725
726MODULE_AUTHOR("adlai@us.ibm.com");
727MODULE_DESCRIPTION("IBM vTPM Driver");
728MODULE_VERSION("1.0");
729MODULE_LICENSE("GPL");