1 /*
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  */
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include "tpm.h"
23 #include "tpm-dev.h"
24 
25 static void user_reader_timeout(struct timer_list *t)
26 {
27 	struct file_priv *priv = from_timer(priv, t, user_read_timer);
28 
29 	pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
30 		task_tgid_nr(current));
31 
32 	schedule_work(&priv->work);
33 }
34 
35 static void timeout_work(struct work_struct *work)
36 {
37 	struct file_priv *priv = container_of(work, struct file_priv, work);
38 
39 	mutex_lock(&priv->buffer_mutex);
40 	priv->data_pending = 0;
41 	memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
42 	mutex_unlock(&priv->buffer_mutex);
43 }
44 
45 void tpm_common_open(struct file *file, struct tpm_chip *chip,
46 		     struct file_priv *priv)
47 {
48 	priv->chip = chip;
49 	mutex_init(&priv->buffer_mutex);
50 	timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
51 	INIT_WORK(&priv->work, timeout_work);
52 
53 	file->private_data = priv;
54 }
55 
56 ssize_t tpm_common_read(struct file *file, char __user *buf,
57 			size_t size, loff_t *off)
58 {
59 	struct file_priv *priv = file->private_data;
60 	ssize_t ret_size = 0;
61 	int rc;
62 
63 	del_singleshot_timer_sync(&priv->user_read_timer);
64 	flush_work(&priv->work);
65 	mutex_lock(&priv->buffer_mutex);
66 
67 	if (priv->data_pending) {
68 		ret_size = min_t(ssize_t, size, priv->data_pending);
69 		rc = copy_to_user(buf, priv->data_buffer, ret_size);
70 		memset(priv->data_buffer, 0, priv->data_pending);
71 		if (rc)
72 			ret_size = -EFAULT;
73 
74 		priv->data_pending = 0;
75 	}
76 
77 	mutex_unlock(&priv->buffer_mutex);
78 	return ret_size;
79 }
80 
81 ssize_t tpm_common_write(struct file *file, const char __user *buf,
82 			 size_t size, loff_t *off, struct tpm_space *space)
83 {
84 	struct file_priv *priv = file->private_data;
85 	size_t in_size = size;
86 	ssize_t out_size;
87 
88 	if (in_size > TPM_BUFSIZE)
89 		return -E2BIG;
90 
91 	mutex_lock(&priv->buffer_mutex);
92 
93 	/* Cannot perform a write until the read has cleared either via
94 	 * tpm_read or a user_read_timer timeout. This also prevents split
95 	 * buffered writes from blocking here.
96 	 */
97 	if (priv->data_pending != 0) {
98 		mutex_unlock(&priv->buffer_mutex);
99 		return -EBUSY;
100 	}
101 
102 	if (copy_from_user
103 	    (priv->data_buffer, (void __user *) buf, in_size)) {
104 		mutex_unlock(&priv->buffer_mutex);
105 		return -EFAULT;
106 	}
107 
108 	if (in_size < 6 ||
109 	    in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) {
110 		mutex_unlock(&priv->buffer_mutex);
111 		return -EINVAL;
112 	}
113 
114 	/* atomic tpm command send and result receive. We only hold the ops
115 	 * lock during this period so that the tpm can be unregistered even if
116 	 * the char dev is held open.
117 	 */
118 	if (tpm_try_get_ops(priv->chip)) {
119 		mutex_unlock(&priv->buffer_mutex);
120 		return -EPIPE;
121 	}
122 	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
123 				sizeof(priv->data_buffer), 0);
124 
125 	tpm_put_ops(priv->chip);
126 	if (out_size < 0) {
127 		mutex_unlock(&priv->buffer_mutex);
128 		return out_size;
129 	}
130 
131 	priv->data_pending = out_size;
132 	mutex_unlock(&priv->buffer_mutex);
133 
134 	/* Set a timeout by which the reader must come claim the result */
135 	mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
136 
137 	return in_size;
138 }
139 
140 /*
141  * Called on file close
142  */
143 void tpm_common_release(struct file *file, struct file_priv *priv)
144 {
145 	del_singleshot_timer_sync(&priv->user_read_timer);
146 	flush_work(&priv->work);
147 	file->private_data = NULL;
148 	priv->data_pending = 0;
149 }
150