1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 IBM Corporation
10 * Copyright (C) 2013 Obsidian Research Corp
13 * Device file system interface to the TPM
15 #include <linux/poll.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/workqueue.h>
22 static struct workqueue_struct *tpm_dev_wq;
23 static DEFINE_MUTEX(tpm_dev_wq_lock);
25 static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space,
26 u8 *buf, size_t bufsiz)
28 struct tpm_header *header = (void *)buf;
31 ret = tpm2_prepare_space(chip, space, buf, bufsiz);
32 /* If the command is not implemented by the TPM, synthesize a
33 * response with a TPM2_RC_COMMAND_CODE return for user-space.
35 if (ret == -EOPNOTSUPP) {
36 header->length = cpu_to_be32(sizeof(*header));
37 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
38 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
39 TSS2_RESMGR_TPM_RC_LAYER);
40 ret = sizeof(*header);
45 len = tpm_transmit(chip, buf, bufsiz);
50 ret = tpm2_commit_space(chip, space, buf, &len);
53 return ret ? ret : len;
56 static void tpm_dev_async_work(struct work_struct *work)
58 struct file_priv *priv =
59 container_of(work, struct file_priv, async_work);
62 mutex_lock(&priv->buffer_mutex);
63 priv->command_enqueued = false;
64 ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
65 sizeof(priv->data_buffer));
66 tpm_put_ops(priv->chip);
68 priv->response_length = ret;
69 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
71 mutex_unlock(&priv->buffer_mutex);
72 wake_up_interruptible(&priv->async_wait);
75 static void user_reader_timeout(struct timer_list *t)
77 struct file_priv *priv = from_timer(priv, t, user_read_timer);
79 pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
80 task_tgid_nr(current));
82 schedule_work(&priv->timeout_work);
85 static void tpm_timeout_work(struct work_struct *work)
87 struct file_priv *priv = container_of(work, struct file_priv,
90 mutex_lock(&priv->buffer_mutex);
91 priv->response_read = true;
92 priv->response_length = 0;
93 memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
94 mutex_unlock(&priv->buffer_mutex);
95 wake_up_interruptible(&priv->async_wait);
98 void tpm_common_open(struct file *file, struct tpm_chip *chip,
99 struct file_priv *priv, struct tpm_space *space)
103 priv->response_read = true;
105 mutex_init(&priv->buffer_mutex);
106 timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
107 INIT_WORK(&priv->timeout_work, tpm_timeout_work);
108 INIT_WORK(&priv->async_work, tpm_dev_async_work);
109 init_waitqueue_head(&priv->async_wait);
110 file->private_data = priv;
113 ssize_t tpm_common_read(struct file *file, char __user *buf,
114 size_t size, loff_t *off)
116 struct file_priv *priv = file->private_data;
117 ssize_t ret_size = 0;
120 mutex_lock(&priv->buffer_mutex);
122 if (priv->response_length) {
123 priv->response_read = true;
125 ret_size = min_t(ssize_t, size, priv->response_length);
127 priv->response_length = 0;
131 rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
133 memset(priv->data_buffer, 0, TPM_BUFSIZE);
134 priv->response_length = 0;
137 memset(priv->data_buffer + *off, 0, ret_size);
138 priv->response_length -= ret_size;
144 if (!priv->response_length) {
146 del_singleshot_timer_sync(&priv->user_read_timer);
147 flush_work(&priv->timeout_work);
149 mutex_unlock(&priv->buffer_mutex);
153 ssize_t tpm_common_write(struct file *file, const char __user *buf,
154 size_t size, loff_t *off)
156 struct file_priv *priv = file->private_data;
159 if (size > TPM_BUFSIZE)
162 mutex_lock(&priv->buffer_mutex);
164 /* Cannot perform a write until the read has cleared either via
165 * tpm_read or a user_read_timer timeout. This also prevents split
166 * buffered writes from blocking here.
168 if ((!priv->response_read && priv->response_length) ||
169 priv->command_enqueued) {
174 if (copy_from_user(priv->data_buffer, buf, size)) {
180 size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) {
185 /* atomic tpm command send and result receive. We only hold the ops
186 * lock during this period so that the tpm can be unregistered even if
187 * the char dev is held open.
189 if (tpm_try_get_ops(priv->chip)) {
194 priv->response_length = 0;
195 priv->response_read = false;
199 * If in nonblocking mode schedule an async job to send
200 * the command return the size.
201 * In case of error the err code will be returned in
202 * the subsequent read call.
204 if (file->f_flags & O_NONBLOCK) {
205 priv->command_enqueued = true;
206 queue_work(tpm_dev_wq, &priv->async_work);
207 mutex_unlock(&priv->buffer_mutex);
211 ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
212 sizeof(priv->data_buffer));
213 tpm_put_ops(priv->chip);
216 priv->response_length = ret;
217 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
221 mutex_unlock(&priv->buffer_mutex);
225 __poll_t tpm_common_poll(struct file *file, poll_table *wait)
227 struct file_priv *priv = file->private_data;
230 poll_wait(file, &priv->async_wait, wait);
231 mutex_lock(&priv->buffer_mutex);
234 * The response_length indicates if there is still response
235 * (or part of it) to be consumed. Partial reads decrease it
236 * by the number of bytes read, and write resets it the zero.
238 if (priv->response_length)
239 mask = EPOLLIN | EPOLLRDNORM;
241 mask = EPOLLOUT | EPOLLWRNORM;
243 mutex_unlock(&priv->buffer_mutex);
248 * Called on file close
250 void tpm_common_release(struct file *file, struct file_priv *priv)
252 flush_work(&priv->async_work);
253 del_singleshot_timer_sync(&priv->user_read_timer);
254 flush_work(&priv->timeout_work);
255 file->private_data = NULL;
256 priv->response_length = 0;
259 int __init tpm_dev_common_init(void)
261 tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);
263 return !tpm_dev_wq ? -ENOMEM : 0;
266 void __exit tpm_dev_common_exit(void)
269 destroy_workqueue(tpm_dev_wq);