18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2004 IBM Corporation
48c2ecf20Sopenharmony_ci * Authors:
58c2ecf20Sopenharmony_ci * Leendert van Doorn <leendert@watson.ibm.com>
68c2ecf20Sopenharmony_ci * Dave Safford <safford@watson.ibm.com>
78c2ecf20Sopenharmony_ci * Reiner Sailer <sailer@watson.ibm.com>
88c2ecf20Sopenharmony_ci * Kylene Hall <kjhall@us.ibm.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright (C) 2013 Obsidian Research Corp
118c2ecf20Sopenharmony_ci * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Device file system interface to the TPM
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci#include <linux/poll.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
188c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
198c2ecf20Sopenharmony_ci#include "tpm.h"
208c2ecf20Sopenharmony_ci#include "tpm-dev.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic struct workqueue_struct *tpm_dev_wq;
238c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(tpm_dev_wq_lock);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space,
268c2ecf20Sopenharmony_ci				u8 *buf, size_t bufsiz)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	struct tpm_header *header = (void *)buf;
298c2ecf20Sopenharmony_ci	ssize_t ret, len;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	ret = tpm2_prepare_space(chip, space, buf, bufsiz);
328c2ecf20Sopenharmony_ci	/* If the command is not implemented by the TPM, synthesize a
338c2ecf20Sopenharmony_ci	 * response with a TPM2_RC_COMMAND_CODE return for user-space.
348c2ecf20Sopenharmony_ci	 */
358c2ecf20Sopenharmony_ci	if (ret == -EOPNOTSUPP) {
368c2ecf20Sopenharmony_ci		header->length = cpu_to_be32(sizeof(*header));
378c2ecf20Sopenharmony_ci		header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
388c2ecf20Sopenharmony_ci		header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
398c2ecf20Sopenharmony_ci						  TSS2_RESMGR_TPM_RC_LAYER);
408c2ecf20Sopenharmony_ci		ret = sizeof(*header);
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	if (ret)
438c2ecf20Sopenharmony_ci		goto out_rc;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	len = tpm_transmit(chip, buf, bufsiz);
468c2ecf20Sopenharmony_ci	if (len < 0)
478c2ecf20Sopenharmony_ci		ret = len;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (!ret)
508c2ecf20Sopenharmony_ci		ret = tpm2_commit_space(chip, space, buf, &len);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciout_rc:
538c2ecf20Sopenharmony_ci	return ret ? ret : len;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void tpm_dev_async_work(struct work_struct *work)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct file_priv *priv =
598c2ecf20Sopenharmony_ci			container_of(work, struct file_priv, async_work);
608c2ecf20Sopenharmony_ci	ssize_t ret;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	mutex_lock(&priv->buffer_mutex);
638c2ecf20Sopenharmony_ci	priv->command_enqueued = false;
648c2ecf20Sopenharmony_ci	ret = tpm_try_get_ops(priv->chip);
658c2ecf20Sopenharmony_ci	if (ret) {
668c2ecf20Sopenharmony_ci		priv->response_length = ret;
678c2ecf20Sopenharmony_ci		goto out;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
718c2ecf20Sopenharmony_ci			       sizeof(priv->data_buffer));
728c2ecf20Sopenharmony_ci	tpm_put_ops(priv->chip);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	/*
758c2ecf20Sopenharmony_ci	 * If ret is > 0 then tpm_dev_transmit returned the size of the
768c2ecf20Sopenharmony_ci	 * response. If ret is < 0 then tpm_dev_transmit failed and
778c2ecf20Sopenharmony_ci	 * returned an error code.
788c2ecf20Sopenharmony_ci	 */
798c2ecf20Sopenharmony_ci	if (ret != 0) {
808c2ecf20Sopenharmony_ci		priv->response_length = ret;
818c2ecf20Sopenharmony_ci		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ciout:
848c2ecf20Sopenharmony_ci	mutex_unlock(&priv->buffer_mutex);
858c2ecf20Sopenharmony_ci	wake_up_interruptible(&priv->async_wait);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic void user_reader_timeout(struct timer_list *t)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct file_priv *priv = from_timer(priv, t, user_read_timer);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
938c2ecf20Sopenharmony_ci		task_tgid_nr(current));
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	schedule_work(&priv->timeout_work);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void tpm_timeout_work(struct work_struct *work)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct file_priv *priv = container_of(work, struct file_priv,
1018c2ecf20Sopenharmony_ci					      timeout_work);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	mutex_lock(&priv->buffer_mutex);
1048c2ecf20Sopenharmony_ci	priv->response_read = true;
1058c2ecf20Sopenharmony_ci	priv->response_length = 0;
1068c2ecf20Sopenharmony_ci	memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
1078c2ecf20Sopenharmony_ci	mutex_unlock(&priv->buffer_mutex);
1088c2ecf20Sopenharmony_ci	wake_up_interruptible(&priv->async_wait);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_civoid tpm_common_open(struct file *file, struct tpm_chip *chip,
1128c2ecf20Sopenharmony_ci		     struct file_priv *priv, struct tpm_space *space)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	priv->chip = chip;
1158c2ecf20Sopenharmony_ci	priv->space = space;
1168c2ecf20Sopenharmony_ci	priv->response_read = true;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	mutex_init(&priv->buffer_mutex);
1198c2ecf20Sopenharmony_ci	timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
1208c2ecf20Sopenharmony_ci	INIT_WORK(&priv->timeout_work, tpm_timeout_work);
1218c2ecf20Sopenharmony_ci	INIT_WORK(&priv->async_work, tpm_dev_async_work);
1228c2ecf20Sopenharmony_ci	init_waitqueue_head(&priv->async_wait);
1238c2ecf20Sopenharmony_ci	file->private_data = priv;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cissize_t tpm_common_read(struct file *file, char __user *buf,
1278c2ecf20Sopenharmony_ci			size_t size, loff_t *off)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct file_priv *priv = file->private_data;
1308c2ecf20Sopenharmony_ci	ssize_t ret_size = 0;
1318c2ecf20Sopenharmony_ci	int rc;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	mutex_lock(&priv->buffer_mutex);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (priv->response_length) {
1368c2ecf20Sopenharmony_ci		priv->response_read = true;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		ret_size = min_t(ssize_t, size, priv->response_length);
1398c2ecf20Sopenharmony_ci		if (ret_size <= 0) {
1408c2ecf20Sopenharmony_ci			priv->response_length = 0;
1418c2ecf20Sopenharmony_ci			goto out;
1428c2ecf20Sopenharmony_ci		}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
1458c2ecf20Sopenharmony_ci		if (rc) {
1468c2ecf20Sopenharmony_ci			memset(priv->data_buffer, 0, TPM_BUFSIZE);
1478c2ecf20Sopenharmony_ci			priv->response_length = 0;
1488c2ecf20Sopenharmony_ci			ret_size = -EFAULT;
1498c2ecf20Sopenharmony_ci		} else {
1508c2ecf20Sopenharmony_ci			memset(priv->data_buffer + *off, 0, ret_size);
1518c2ecf20Sopenharmony_ci			priv->response_length -= ret_size;
1528c2ecf20Sopenharmony_ci			*off += ret_size;
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciout:
1578c2ecf20Sopenharmony_ci	if (!priv->response_length) {
1588c2ecf20Sopenharmony_ci		*off = 0;
1598c2ecf20Sopenharmony_ci		del_singleshot_timer_sync(&priv->user_read_timer);
1608c2ecf20Sopenharmony_ci		flush_work(&priv->timeout_work);
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci	mutex_unlock(&priv->buffer_mutex);
1638c2ecf20Sopenharmony_ci	return ret_size;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cissize_t tpm_common_write(struct file *file, const char __user *buf,
1678c2ecf20Sopenharmony_ci			 size_t size, loff_t *off)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct file_priv *priv = file->private_data;
1708c2ecf20Sopenharmony_ci	int ret = 0;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	if (size > TPM_BUFSIZE)
1738c2ecf20Sopenharmony_ci		return -E2BIG;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	mutex_lock(&priv->buffer_mutex);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* Cannot perform a write until the read has cleared either via
1788c2ecf20Sopenharmony_ci	 * tpm_read or a user_read_timer timeout. This also prevents split
1798c2ecf20Sopenharmony_ci	 * buffered writes from blocking here.
1808c2ecf20Sopenharmony_ci	 */
1818c2ecf20Sopenharmony_ci	if ((!priv->response_read && priv->response_length) ||
1828c2ecf20Sopenharmony_ci	    priv->command_enqueued) {
1838c2ecf20Sopenharmony_ci		ret = -EBUSY;
1848c2ecf20Sopenharmony_ci		goto out;
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (copy_from_user(priv->data_buffer, buf, size)) {
1888c2ecf20Sopenharmony_ci		ret = -EFAULT;
1898c2ecf20Sopenharmony_ci		goto out;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (size < 6 ||
1938c2ecf20Sopenharmony_ci	    size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) {
1948c2ecf20Sopenharmony_ci		ret = -EINVAL;
1958c2ecf20Sopenharmony_ci		goto out;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	priv->response_length = 0;
1998c2ecf20Sopenharmony_ci	priv->response_read = false;
2008c2ecf20Sopenharmony_ci	*off = 0;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	/*
2038c2ecf20Sopenharmony_ci	 * If in nonblocking mode schedule an async job to send
2048c2ecf20Sopenharmony_ci	 * the command return the size.
2058c2ecf20Sopenharmony_ci	 * In case of error the err code will be returned in
2068c2ecf20Sopenharmony_ci	 * the subsequent read call.
2078c2ecf20Sopenharmony_ci	 */
2088c2ecf20Sopenharmony_ci	if (file->f_flags & O_NONBLOCK) {
2098c2ecf20Sopenharmony_ci		priv->command_enqueued = true;
2108c2ecf20Sopenharmony_ci		queue_work(tpm_dev_wq, &priv->async_work);
2118c2ecf20Sopenharmony_ci		mutex_unlock(&priv->buffer_mutex);
2128c2ecf20Sopenharmony_ci		return size;
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* atomic tpm command send and result receive. We only hold the ops
2168c2ecf20Sopenharmony_ci	 * lock during this period so that the tpm can be unregistered even if
2178c2ecf20Sopenharmony_ci	 * the char dev is held open.
2188c2ecf20Sopenharmony_ci	 */
2198c2ecf20Sopenharmony_ci	if (tpm_try_get_ops(priv->chip)) {
2208c2ecf20Sopenharmony_ci		ret = -EPIPE;
2218c2ecf20Sopenharmony_ci		goto out;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
2258c2ecf20Sopenharmony_ci			       sizeof(priv->data_buffer));
2268c2ecf20Sopenharmony_ci	tpm_put_ops(priv->chip);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	if (ret > 0) {
2298c2ecf20Sopenharmony_ci		priv->response_length = ret;
2308c2ecf20Sopenharmony_ci		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
2318c2ecf20Sopenharmony_ci		ret = size;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ciout:
2348c2ecf20Sopenharmony_ci	mutex_unlock(&priv->buffer_mutex);
2358c2ecf20Sopenharmony_ci	return ret;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci__poll_t tpm_common_poll(struct file *file, poll_table *wait)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct file_priv *priv = file->private_data;
2418c2ecf20Sopenharmony_ci	__poll_t mask = 0;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	poll_wait(file, &priv->async_wait, wait);
2448c2ecf20Sopenharmony_ci	mutex_lock(&priv->buffer_mutex);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/*
2478c2ecf20Sopenharmony_ci	 * The response_length indicates if there is still response
2488c2ecf20Sopenharmony_ci	 * (or part of it) to be consumed. Partial reads decrease it
2498c2ecf20Sopenharmony_ci	 * by the number of bytes read, and write resets it the zero.
2508c2ecf20Sopenharmony_ci	 */
2518c2ecf20Sopenharmony_ci	if (priv->response_length)
2528c2ecf20Sopenharmony_ci		mask = EPOLLIN | EPOLLRDNORM;
2538c2ecf20Sopenharmony_ci	else
2548c2ecf20Sopenharmony_ci		mask = EPOLLOUT | EPOLLWRNORM;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	mutex_unlock(&priv->buffer_mutex);
2578c2ecf20Sopenharmony_ci	return mask;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/*
2618c2ecf20Sopenharmony_ci * Called on file close
2628c2ecf20Sopenharmony_ci */
2638c2ecf20Sopenharmony_civoid tpm_common_release(struct file *file, struct file_priv *priv)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	flush_work(&priv->async_work);
2668c2ecf20Sopenharmony_ci	del_singleshot_timer_sync(&priv->user_read_timer);
2678c2ecf20Sopenharmony_ci	flush_work(&priv->timeout_work);
2688c2ecf20Sopenharmony_ci	file->private_data = NULL;
2698c2ecf20Sopenharmony_ci	priv->response_length = 0;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ciint __init tpm_dev_common_init(void)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	return !tpm_dev_wq ? -ENOMEM : 0;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_civoid __exit tpm_dev_common_exit(void)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	if (tpm_dev_wq) {
2828c2ecf20Sopenharmony_ci		destroy_workqueue(tpm_dev_wq);
2838c2ecf20Sopenharmony_ci		tpm_dev_wq = NULL;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci}
286