blob: 02a8850d3a69022dcf58346c7e69413e9f34ea27 [file] [log] [blame]
Jason Gunthorpeafdba322013-11-26 13:30:40 -07001/*
2 * Copyright (C) 2004 IBM Corporation
3 * Authors:
4 * Leendert van Doorn <leendert@watson.ibm.com>
5 * Dave Safford <safford@watson.ibm.com>
6 * Reiner Sailer <sailer@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
9 * Copyright (C) 2013 Obsidian Research Corp
10 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
11 *
12 * Device file system interface to the TPM
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation, version 2 of the
17 * License.
18 *
19 */
Jason Gunthorpeafdba322013-11-26 13:30:40 -070020#include <linux/slab.h>
21#include <linux/uaccess.h>
22#include "tpm.h"
23
Jason Gunthorpee3302e02013-11-26 13:30:45 -070024struct file_priv {
25 struct tpm_chip *chip;
26
27 /* Data passed to and from the tpm via the read/write calls */
28 atomic_t data_pending;
29 struct mutex buffer_mutex;
30
31 struct timer_list user_read_timer; /* user needs to claim result */
32 struct work_struct work;
33
34 u8 data_buffer[TPM_BUFSIZE];
35};
36
Jason Gunthorpeafdba322013-11-26 13:30:40 -070037static void user_reader_timeout(unsigned long ptr)
38{
Jason Gunthorpee3302e02013-11-26 13:30:45 -070039 struct file_priv *priv = (struct file_priv *)ptr;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070040
Jason Gunthorpefa2825d2017-01-23 17:06:08 -070041 pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
42 task_tgid_nr(current));
43
Jason Gunthorpee3302e02013-11-26 13:30:45 -070044 schedule_work(&priv->work);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070045}
46
47static void timeout_work(struct work_struct *work)
48{
Jason Gunthorpee3302e02013-11-26 13:30:45 -070049 struct file_priv *priv = container_of(work, struct file_priv, work);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070050
Jason Gunthorpee3302e02013-11-26 13:30:45 -070051 mutex_lock(&priv->buffer_mutex);
52 atomic_set(&priv->data_pending, 0);
53 memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
54 mutex_unlock(&priv->buffer_mutex);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070055}
56
57static int tpm_open(struct inode *inode, struct file *file)
58{
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080059 struct tpm_chip *chip =
60 container_of(inode->i_cdev, struct tpm_chip, cdev);
Jason Gunthorpee3302e02013-11-26 13:30:45 -070061 struct file_priv *priv;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070062
63 /* It's assured that the chip will be opened just once,
64 * by the check of is_open variable, which is protected
65 * by driver_lock. */
66 if (test_and_set_bit(0, &chip->is_open)) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -050067 dev_dbg(&chip->dev, "Another process owns this TPM\n");
Jason Gunthorpeafdba322013-11-26 13:30:40 -070068 return -EBUSY;
69 }
70
Jason Gunthorpee3302e02013-11-26 13:30:45 -070071 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
72 if (priv == NULL) {
Jason Gunthorpeafdba322013-11-26 13:30:40 -070073 clear_bit(0, &chip->is_open);
74 return -ENOMEM;
75 }
76
Jason Gunthorpee3302e02013-11-26 13:30:45 -070077 priv->chip = chip;
78 atomic_set(&priv->data_pending, 0);
79 mutex_init(&priv->buffer_mutex);
80 setup_timer(&priv->user_read_timer, user_reader_timeout,
81 (unsigned long)priv);
82 INIT_WORK(&priv->work, timeout_work);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070083
Jason Gunthorpee3302e02013-11-26 13:30:45 -070084 file->private_data = priv;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070085 return 0;
86}
87
88static ssize_t tpm_read(struct file *file, char __user *buf,
89 size_t size, loff_t *off)
90{
Jason Gunthorpee3302e02013-11-26 13:30:45 -070091 struct file_priv *priv = file->private_data;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070092 ssize_t ret_size;
93 int rc;
94
Jason Gunthorpee3302e02013-11-26 13:30:45 -070095 del_singleshot_timer_sync(&priv->user_read_timer);
96 flush_work(&priv->work);
97 ret_size = atomic_read(&priv->data_pending);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070098 if (ret_size > 0) { /* relay data */
99 ssize_t orig_ret_size = ret_size;
100 if (size < ret_size)
101 ret_size = size;
102
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700103 mutex_lock(&priv->buffer_mutex);
104 rc = copy_to_user(buf, priv->data_buffer, ret_size);
105 memset(priv->data_buffer, 0, orig_ret_size);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700106 if (rc)
107 ret_size = -EFAULT;
108
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700109 mutex_unlock(&priv->buffer_mutex);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700110 }
111
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700112 atomic_set(&priv->data_pending, 0);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700113
114 return ret_size;
115}
116
117static ssize_t tpm_write(struct file *file, const char __user *buf,
118 size_t size, loff_t *off)
119{
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700120 struct file_priv *priv = file->private_data;
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700121 size_t in_size = size;
122 ssize_t out_size;
123
124 /* cannot perform a write until the read has cleared
125 either via tpm_read or a user_read_timer timeout.
126 This also prevents splitted buffered writes from blocking here.
127 */
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700128 if (atomic_read(&priv->data_pending) != 0)
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700129 return -EBUSY;
130
131 if (in_size > TPM_BUFSIZE)
132 return -E2BIG;
133
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700134 mutex_lock(&priv->buffer_mutex);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700135
136 if (copy_from_user
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700137 (priv->data_buffer, (void __user *) buf, in_size)) {
138 mutex_unlock(&priv->buffer_mutex);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700139 return -EFAULT;
140 }
141
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700142 /* atomic tpm command send and result receive. We only hold the ops
143 * lock during this period so that the tpm can be unregistered even if
144 * the char dev is held open.
145 */
146 if (tpm_try_get_ops(priv->chip)) {
147 mutex_unlock(&priv->buffer_mutex);
148 return -EPIPE;
149 }
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700150 out_size = tpm_transmit(priv->chip, priv->data_buffer,
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300151 sizeof(priv->data_buffer), 0);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700152
153 tpm_put_ops(priv->chip);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700154 if (out_size < 0) {
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700155 mutex_unlock(&priv->buffer_mutex);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700156 return out_size;
157 }
158
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700159 atomic_set(&priv->data_pending, out_size);
160 mutex_unlock(&priv->buffer_mutex);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700161
162 /* Set a timeout by which the reader must come claim the result */
Jason Gunthorpefa2825d2017-01-23 17:06:08 -0700163 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700164
165 return in_size;
166}
167
168/*
169 * Called on file close
170 */
171static int tpm_release(struct inode *inode, struct file *file)
172{
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700173 struct file_priv *priv = file->private_data;
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700174
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700175 del_singleshot_timer_sync(&priv->user_read_timer);
176 flush_work(&priv->work);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700177 file->private_data = NULL;
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700178 atomic_set(&priv->data_pending, 0);
179 clear_bit(0, &priv->chip->is_open);
Jason Gunthorpee3302e02013-11-26 13:30:45 -0700180 kfree(priv);
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700181 return 0;
182}
183
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800184const struct file_operations tpm_fops = {
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700185 .owner = THIS_MODULE,
186 .llseek = no_llseek,
187 .open = tpm_open,
188 .read = tpm_read,
189 .write = tpm_write,
190 .release = tpm_release,
191};
192
Jason Gunthorpeafdba322013-11-26 13:30:40 -0700193