blob: 09df26f9621dc3a8de6227e9c427437176cd4b5d [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;
jmlatten@linux.vnet.ibm.com230a7ee2015-02-20 18:11:24 -0600127 __be64 *word = (__be64 *)&crq;
Ashley Lai132f7622012-08-22 16:17:43 -0500128 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;
jmlatten@linux.vnet.ibm.com230a7ee2015-02-20 18:11:24 -0600148 crq.len = cpu_to_be16(count);
149 crq.data = cpu_to_be32(ibmvtpm->rtce_dma_handle);
Ashley Lai132f7622012-08-22 16:17:43 -0500150
jmlatten@linux.vnet.ibm.com230a7ee2015-02-20 18:11:24 -0600151 rc = ibmvtpm_send_crq(ibmvtpm->vdev, be64_to_cpu(word[0]),
152 be64_to_cpu(word[1]));
Ashley Lai132f7622012-08-22 16:17:43 -0500153 if (rc != H_SUCCESS) {
154 dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
155 rc = 0;
156 } else
157 rc = count;
158
159 spin_unlock(&ibmvtpm->rtce_lock);
160 return rc;
161}
162
163static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
164{
165 return;
166}
167
168static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
169{
170 return 0;
171}
172
173/**
174 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
175 * @ibmvtpm: vtpm device struct
176 *
177 * Return value:
178 * 0 - Success
179 * Non-zero - Failure
180 */
181static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
182{
183 struct ibmvtpm_crq crq;
184 u64 *buf = (u64 *) &crq;
185 int rc;
186
187 crq.valid = (u8)IBMVTPM_VALID_CMD;
188 crq.msg = (u8)VTPM_GET_RTCE_BUFFER_SIZE;
189
honcloc213da82015-02-12 21:02:24 -0500190 rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
191 cpu_to_be64(buf[1]));
Ashley Lai132f7622012-08-22 16:17:43 -0500192 if (rc != H_SUCCESS)
193 dev_err(ibmvtpm->dev,
194 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
195
196 return rc;
197}
198
199/**
200 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
201 * - Note that this is vtpm version and not tpm version
202 * @ibmvtpm: vtpm device struct
203 *
204 * Return value:
205 * 0 - Success
206 * Non-zero - Failure
207 */
208static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
209{
210 struct ibmvtpm_crq crq;
211 u64 *buf = (u64 *) &crq;
212 int rc;
213
214 crq.valid = (u8)IBMVTPM_VALID_CMD;
215 crq.msg = (u8)VTPM_GET_VERSION;
216
honcloc213da82015-02-12 21:02:24 -0500217 rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
218 cpu_to_be64(buf[1]));
Ashley Lai132f7622012-08-22 16:17:43 -0500219 if (rc != H_SUCCESS)
220 dev_err(ibmvtpm->dev,
221 "ibmvtpm_crq_get_version failed rc=%d\n", rc);
222
223 return rc;
224}
225
226/**
227 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
228 * @ibmvtpm: vtpm device struct
229 *
230 * Return value:
231 * 0 - Success
232 * Non-zero - Failure
233 */
234static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
235{
236 int rc;
237
238 rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_COMP_CMD, 0);
239 if (rc != H_SUCCESS)
240 dev_err(ibmvtpm->dev,
241 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
242
243 return rc;
244}
245
246/**
247 * ibmvtpm_crq_send_init - Send a CRQ initialize message
248 * @ibmvtpm: vtpm device struct
249 *
250 * Return value:
251 * 0 - Success
252 * Non-zero - Failure
253 */
254static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
255{
256 int rc;
257
258 rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_CMD, 0);
259 if (rc != H_SUCCESS)
260 dev_err(ibmvtpm->dev,
261 "ibmvtpm_crq_send_init failed rc=%d\n", rc);
262
263 return rc;
264}
265
266/**
267 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
268 * @vdev: vio device struct
269 *
270 * Return value:
271 * 0
272 */
Bill Pemberton39af33f2012-11-19 13:26:26 -0500273static int tpm_ibmvtpm_remove(struct vio_dev *vdev)
Ashley Lai132f7622012-08-22 16:17:43 -0500274{
275 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev);
276 int rc = 0;
277
278 free_irq(vdev->irq, ibmvtpm);
Ashley Lai132f7622012-08-22 16:17:43 -0500279
280 do {
281 if (rc)
282 msleep(100);
283 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
284 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
285
286 dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
287 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
288 free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
289
290 if (ibmvtpm->rtce_buf) {
291 dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
292 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
293 kfree(ibmvtpm->rtce_buf);
294 }
295
296 tpm_remove_hardware(ibmvtpm->dev);
297
298 kfree(ibmvtpm);
299
300 return 0;
301}
302
303/**
304 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
305 * @vdev: vio device struct
306 *
307 * Return value:
308 * Number of bytes the driver needs to DMA map
309 */
310static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
311{
312 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev);
Hon Ching (Vicky) Lo1c243c22014-11-30 15:01:28 +0100313
314 /* ibmvtpm initializes at probe time, so the data we are
315 * asking for may not be set yet. Estimate that 4K required
316 * for TCE-mapped buffer in addition to CRQ.
317 */
318 if (!ibmvtpm)
319 return CRQ_RES_BUF_SIZE + PAGE_SIZE;
320
Ashley Lai132f7622012-08-22 16:17:43 -0500321 return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
322}
323
324/**
325 * tpm_ibmvtpm_suspend - Suspend
326 * @dev: device struct
327 *
328 * Return value:
329 * 0
330 */
331static int tpm_ibmvtpm_suspend(struct device *dev)
332{
333 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev);
334 struct ibmvtpm_crq crq;
335 u64 *buf = (u64 *) &crq;
336 int rc = 0;
337
338 crq.valid = (u8)IBMVTPM_VALID_CMD;
339 crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND;
340
honcloc213da82015-02-12 21:02:24 -0500341 rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
342 cpu_to_be64(buf[1]));
Ashley Lai132f7622012-08-22 16:17:43 -0500343 if (rc != H_SUCCESS)
344 dev_err(ibmvtpm->dev,
345 "tpm_ibmvtpm_suspend failed rc=%d\n", rc);
346
347 return rc;
348}
349
350/**
351 * ibmvtpm_reset_crq - Reset CRQ
352 * @ibmvtpm: ibm vtpm struct
353 *
354 * Return value:
355 * 0 - Success
356 * Non-zero - Failure
357 */
358static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
359{
360 int rc = 0;
361
362 do {
363 if (rc)
364 msleep(100);
365 rc = plpar_hcall_norets(H_FREE_CRQ,
366 ibmvtpm->vdev->unit_address);
367 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
368
369 memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
370 ibmvtpm->crq_queue.index = 0;
371
372 return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
373 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
374}
375
376/**
377 * tpm_ibmvtpm_resume - Resume from suspend
378 * @dev: device struct
379 *
380 * Return value:
381 * 0
382 */
383static int tpm_ibmvtpm_resume(struct device *dev)
384{
385 struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev);
Ashley Lai132f7622012-08-22 16:17:43 -0500386 int rc = 0;
387
388 do {
389 if (rc)
390 msleep(100);
391 rc = plpar_hcall_norets(H_ENABLE_CRQ,
392 ibmvtpm->vdev->unit_address);
393 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
394
395 if (rc) {
396 dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
397 return rc;
398 }
399
Ashley Laib5666502012-09-12 12:49:50 -0500400 rc = vio_enable_interrupts(ibmvtpm->vdev);
401 if (rc) {
402 dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
403 return rc;
404 }
Ashley Lai132f7622012-08-22 16:17:43 -0500405
406 rc = ibmvtpm_crq_send_init(ibmvtpm);
407 if (rc)
408 dev_err(dev, "Error send_init rc=%d\n", rc);
409
410 return rc;
411}
412
Stefan Berger1f866052013-01-22 13:52:35 -0600413static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
414{
415 return (status == 0);
416}
417
Ashley Lai132f7622012-08-22 16:17:43 -0500418static const struct file_operations ibmvtpm_ops = {
419 .owner = THIS_MODULE,
420 .llseek = no_llseek,
421 .open = tpm_open,
422 .read = tpm_read,
423 .write = tpm_write,
424 .release = tpm_release,
425};
426
427static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
428static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
429static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
430static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
431static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
432static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
433 NULL);
434static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
435static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
436static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
437static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
438
439static struct attribute *ibmvtpm_attrs[] = {
440 &dev_attr_pubek.attr,
441 &dev_attr_pcrs.attr,
442 &dev_attr_enabled.attr,
443 &dev_attr_active.attr,
444 &dev_attr_owned.attr,
445 &dev_attr_temp_deactivated.attr,
446 &dev_attr_caps.attr,
447 &dev_attr_cancel.attr,
448 &dev_attr_durations.attr,
449 &dev_attr_timeouts.attr, NULL,
450};
451
452static struct attribute_group ibmvtpm_attr_grp = { .attrs = ibmvtpm_attrs };
453
454static const struct tpm_vendor_specific tpm_ibmvtpm = {
455 .recv = tpm_ibmvtpm_recv,
456 .send = tpm_ibmvtpm_send,
457 .cancel = tpm_ibmvtpm_cancel,
458 .status = tpm_ibmvtpm_status,
459 .req_complete_mask = 0,
460 .req_complete_val = 0,
Stefan Berger1f866052013-01-22 13:52:35 -0600461 .req_canceled = tpm_ibmvtpm_req_canceled,
Ashley Lai132f7622012-08-22 16:17:43 -0500462 .attr_group = &ibmvtpm_attr_grp,
463 .miscdev = { .fops = &ibmvtpm_ops, },
464};
465
466static 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 * @ibmvtpm vtpm device struct
474 *
475 * Return value:
476 * vtpm crq pointer
477 */
478static 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;
Ashley Laib5666502012-09-12 12:49:50 -0500486 smp_rmb();
Ashley Lai132f7622012-08-22 16:17:43 -0500487 } else
488 crq = NULL;
489 return crq;
490}
491
492/**
493 * ibmvtpm_crq_process - Process responded crq
494 * @crq crq to be processed
495 * @ibmvtpm vtpm device struct
496 *
497 * Return value:
498 * Nothing
499 */
500static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
501 struct ibmvtpm_dev *ibmvtpm)
502{
503 int rc = 0;
504
505 switch (crq->valid) {
506 case VALID_INIT_CRQ:
507 switch (crq->msg) {
508 case INIT_CRQ_RES:
509 dev_info(ibmvtpm->dev, "CRQ initialized\n");
510 rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
511 if (rc)
512 dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
513 return;
514 case INIT_CRQ_COMP_RES:
515 dev_info(ibmvtpm->dev,
516 "CRQ initialization completed\n");
517 return;
518 default:
519 dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
520 return;
521 }
522 return;
523 case IBMVTPM_VALID_CMD:
524 switch (crq->msg) {
525 case VTPM_GET_RTCE_BUFFER_SIZE_RES:
honcloc213da82015-02-12 21:02:24 -0500526 if (be16_to_cpu(crq->len) <= 0) {
Ashley Lai132f7622012-08-22 16:17:43 -0500527 dev_err(ibmvtpm->dev, "Invalid rtce size\n");
528 return;
529 }
honcloc213da82015-02-12 21:02:24 -0500530 ibmvtpm->rtce_size = be16_to_cpu(crq->len);
Ashley Lai132f7622012-08-22 16:17:43 -0500531 ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
532 GFP_KERNEL);
533 if (!ibmvtpm->rtce_buf) {
534 dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
535 return;
536 }
537
538 ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
539 ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
540 DMA_BIDIRECTIONAL);
541
542 if (dma_mapping_error(ibmvtpm->dev,
543 ibmvtpm->rtce_dma_handle)) {
544 kfree(ibmvtpm->rtce_buf);
545 ibmvtpm->rtce_buf = NULL;
546 dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
547 }
548
549 return;
550 case VTPM_GET_VERSION_RES:
honcloc213da82015-02-12 21:02:24 -0500551 ibmvtpm->vtpm_version = be32_to_cpu(crq->data);
Ashley Lai132f7622012-08-22 16:17:43 -0500552 return;
553 case VTPM_TPM_COMMAND_RES:
Ashley Laib5666502012-09-12 12:49:50 -0500554 /* len of the data in rtce buffer */
honcloc213da82015-02-12 21:02:24 -0500555 ibmvtpm->res_len = be16_to_cpu(crq->len);
Ashley Laib5666502012-09-12 12:49:50 -0500556 wake_up_interruptible(&ibmvtpm->wq);
Ashley Lai132f7622012-08-22 16:17:43 -0500557 return;
558 default:
559 return;
560 }
561 }
562 return;
563}
564
565/**
566 * ibmvtpm_interrupt - Interrupt handler
567 * @irq: irq number to handle
568 * @vtpm_instance: vtpm that received interrupt
569 *
570 * Returns:
571 * IRQ_HANDLED
572 **/
573static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
574{
575 struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
Ashley Lai132f7622012-08-22 16:17:43 -0500576 struct ibmvtpm_crq *crq;
Ashley Lai132f7622012-08-22 16:17:43 -0500577
Ashley Laib5666502012-09-12 12:49:50 -0500578 /* 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 */
Ashley Lai132f7622012-08-22 16:17:43 -0500582 while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
583 ibmvtpm_crq_process(crq, ibmvtpm);
584 crq->valid = 0;
Ashley Laib5666502012-09-12 12:49:50 -0500585 smp_wmb();
Ashley Lai132f7622012-08-22 16:17:43 -0500586 }
587
Ashley Laib5666502012-09-12 12:49:50 -0500588 return IRQ_HANDLED;
Ashley Lai132f7622012-08-22 16:17:43 -0500589}
590
591/**
592 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
593 * @vio_dev: vio device struct
594 * @id: vio device id struct
595 *
596 * Return value:
597 * 0 - Success
598 * Non-zero - Failure
599 */
Bill Pembertonafc6d362012-11-19 13:22:42 -0500600static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
Ashley Lai132f7622012-08-22 16:17:43 -0500601 const struct vio_device_id *id)
602{
603 struct ibmvtpm_dev *ibmvtpm;
604 struct device *dev = &vio_dev->dev;
605 struct ibmvtpm_crq_queue *crq_q;
606 struct tpm_chip *chip;
607 int rc = -ENOMEM, rc1;
608
609 chip = tpm_register_hardware(dev, &tpm_ibmvtpm);
610 if (!chip) {
611 dev_err(dev, "tpm_register_hardware failed\n");
612 return -ENODEV;
613 }
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 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);
629 ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
630 CRQ_RES_BUF_SIZE,
631 DMA_BIDIRECTIONAL);
632
633 if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
634 dev_err(dev, "dma mapping failed\n");
635 goto cleanup;
636 }
637
638 rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
639 ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
640 if (rc == H_RESOURCE)
641 rc = ibmvtpm_reset_crq(ibmvtpm);
642
643 if (rc) {
644 dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
645 goto reg_crq_cleanup;
646 }
647
Ashley Lai132f7622012-08-22 16:17:43 -0500648 rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
649 tpm_ibmvtpm_driver_name, ibmvtpm);
650 if (rc) {
651 dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
652 goto init_irq_cleanup;
653 }
654
655 rc = vio_enable_interrupts(vio_dev);
656 if (rc) {
657 dev_err(dev, "Error %d enabling interrupts\n", rc);
658 goto init_irq_cleanup;
659 }
660
Ashley Laib5666502012-09-12 12:49:50 -0500661 init_waitqueue_head(&ibmvtpm->wq);
662
Ashley Lai132f7622012-08-22 16:17:43 -0500663 crq_q->index = 0;
664
665 ibmvtpm->dev = dev;
666 ibmvtpm->vdev = vio_dev;
Kent Yoder775585e2012-12-05 11:36:20 -0600667 TPM_VPRIV(chip) = (void *)ibmvtpm;
Ashley Lai132f7622012-08-22 16:17:43 -0500668
Ashley Lai132f7622012-08-22 16:17:43 -0500669 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 rc;
684init_irq_cleanup:
Ashley Lai132f7622012-08-22 16:17:43 -0500685 do {
686 rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
687 } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
688reg_crq_cleanup:
689 dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
690 DMA_BIDIRECTIONAL);
691cleanup:
692 if (ibmvtpm) {
693 if (crq_q->crq_addr)
694 free_page((unsigned long)crq_q->crq_addr);
695 kfree(ibmvtpm);
696 }
697
698 tpm_remove_hardware(dev);
699
700 return rc;
701}
702
703static struct vio_driver ibmvtpm_driver = {
704 .id_table = tpm_ibmvtpm_device_table,
705 .probe = tpm_ibmvtpm_probe,
706 .remove = tpm_ibmvtpm_remove,
707 .get_desired_dma = tpm_ibmvtpm_get_desired_dma,
708 .name = tpm_ibmvtpm_driver_name,
709 .pm = &tpm_ibmvtpm_pm_ops,
710};
711
712/**
713 * ibmvtpm_module_init - Initialize ibm vtpm module
714 *
715 * Return value:
716 * 0 -Success
717 * Non-zero - Failure
718 */
719static int __init ibmvtpm_module_init(void)
720{
721 return vio_register_driver(&ibmvtpm_driver);
722}
723
724/**
725 * ibmvtpm_module_exit - Teardown ibm vtpm module
726 *
727 * Return value:
728 * Nothing
729 */
730static void __exit ibmvtpm_module_exit(void)
731{
732 vio_unregister_driver(&ibmvtpm_driver);
733}
734
735module_init(ibmvtpm_module_init);
736module_exit(ibmvtpm_module_exit);
737
738MODULE_AUTHOR("adlai@us.ibm.com");
739MODULE_DESCRIPTION("IBM vTPM Driver");
740MODULE_VERSION("1.0");
741MODULE_LICENSE("GPL");