[Skiboot] [PATCH v2 12/31] libstb/drivers: add tpm_i2c interface

Claudio Carvalho cclaudio at linux.vnet.ibm.com
Wed Sep 28 18:01:11 AEST 2016


This adds the functions that TPM I2C drivers can use to send
requests to I2C master.

Signed-off-by: Claudio Carvalho <cclaudio at linux.vnet.ibm.com>
---
 libstb/drivers/Makefile.inc        |   2 +-
 libstb/drivers/tpm_i2c_interface.c | 130 +++++++++++++++++++++++++++++++++++++
 libstb/drivers/tpm_i2c_interface.h |  27 ++++++++
 libstb/status_codes.h              |   2 +
 4 files changed, 160 insertions(+), 1 deletion(-)
 create mode 100644 libstb/drivers/tpm_i2c_interface.c
 create mode 100644 libstb/drivers/tpm_i2c_interface.h

diff --git a/libstb/drivers/Makefile.inc b/libstb/drivers/Makefile.inc
index 63dead2..f0f3d70 100644
--- a/libstb/drivers/Makefile.inc
+++ b/libstb/drivers/Makefile.inc
@@ -4,7 +4,7 @@ DRIVERS_DIR = libstb/drivers
 
 SUBDIRS += $(DRIVERS_DIR)
 
-DRIVERS_SRCS = romcode.c
+DRIVERS_SRCS = romcode.c tpm_i2c_interface.c
 DRIVERS_OBJS = $(DRIVERS_SRCS:%.c=%.o)
 DRIVERS = $(DRIVERS_DIR)/built-in.o
 
diff --git a/libstb/drivers/tpm_i2c_interface.c b/libstb/drivers/tpm_i2c_interface.c
new file mode 100644
index 0000000..77a7ece
--- /dev/null
+++ b/libstb/drivers/tpm_i2c_interface.c
@@ -0,0 +1,130 @@
+/* Copyright 2013-2016 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <skiboot.h>
+#include <timebase.h>
+#include <opal-api.h>
+#include <i2c.h>
+
+#include "tpm_i2c_interface.h"
+#include "../status_codes.h"
+
+//#define DBG(fmt, ...) prlog(PR_DEBUG, fmt, ##__VA_ARGS__)
+#define DBG(fmt, ...)
+
+#define I2C_BYTE_TIMEOUT_MS		10  /* 10ms/byte timeout */
+#define TPM_MAX_NACK_RETRIES		 2
+#define REQ_COMPLETE_POLLING		 5  /* Check if req is complete
+					       in 5ms interval */
+
+void tpm_i2c_request_complete(int rc, struct i2c_request *req)
+{
+	*(int*)req->user_data = rc;
+}
+
+/**
+ * tpm_i2c_request_send - send request to i2c bus
+ * @tpm_bus_id: i2c bus id
+ * @tpm_dev_addr: address of the tpm device
+ * @read_write: SMBUS_READ or SMBUS_WRITE
+ * @offset: any of the I2C interface offset defined
+ * @offset_bytes: offset size in bytes
+ * @buf: data to be read or written
+ * @buflen: buf length
+ *
+ * This interacts with skiboot i2c API to send an I2C request to the tpm
+ * device
+ *
+ * Returns: Zero on success otherwise a negative error code
+ */
+int tpm_i2c_request_send(int tpm_bus_id, int tpm_dev_addr, int read_write,
+			 uint32_t offset, uint32_t offset_bytes, void* buf,
+			 size_t buflen)
+{
+	int rc, waited, retries, timeout;
+	struct i2c_request *req;
+	struct i2c_bus *bus;
+
+	bus = i2c_find_bus_by_id(tpm_bus_id);
+	if (!bus) {
+		/**
+		 * @fwts-label TPMI2CInvalidBusID
+		 * @fwts-advice tpm_i2c_request_send was passed an invalid bus
+		 * ID. This indicates a tb_init() bug.
+		 */
+		prlog(PR_ERR, "TPM: Invalid bus_id=%x\n", tpm_bus_id);
+		rc = STB_ARG_ERROR;
+		goto out;
+	}
+
+	req = i2c_alloc_req(bus);
+	if (!req) {
+		/**
+		 * @fwts-label TPMI2CAllocationFailed
+		 * @fwts-advice OPAL failed to allocate memory for an
+		 * i2c_request. This points to an OPAL bug as OPAL run out of
+		 * memory and this should never happen.
+		 */
+		prlog(PR_ERR, "TPM: i2c_alloc_req failed\n");
+		rc = STB_DRIVER_ERROR;
+		goto out;
+	}
+
+	req->dev_addr   = tpm_dev_addr;
+	req->op         = read_write;
+	req->offset     = offset;
+	req->offset_bytes = offset_bytes;
+	req->rw_buf     = (void*) buf;
+	req->rw_len     = buflen;
+	req->completion = tpm_i2c_request_complete;
+	req->user_data = &rc;
+
+	/*
+	 * Set the request timeout to 10ms per byte. Otherwise, we get
+	 * an I2C master timeout for all requests sent to the TPM device
+	 * since the I2C master's timeout is too short (1ms per byte).
+	 */
+	timeout = (buflen + offset_bytes + 2) * I2C_BYTE_TIMEOUT_MS;
+	i2c_set_req_timeout(req, timeout);
+
+	for (retries = 0; retries <= TPM_MAX_NACK_RETRIES; retries++) {
+		rc = 1;
+		waited = 0;
+		i2c_queue_req(req);
+
+		do {
+			time_wait_ms(REQ_COMPLETE_POLLING);
+			waited += REQ_COMPLETE_POLLING;
+		} while (waited < timeout && rc == 1);
+
+		if (rc == OPAL_I2C_NACK_RCVD)
+			continue;
+		else
+			/* error or success */
+			break;
+	}
+
+	DBG("%s tpm req op=%x offset=%x buf=%016llx buflen=%d delay=%d/%d,"
+	    "rc=%d\n",
+	    (rc) ? "!!!!" : "----", req->op, req->offset,
+	    *(uint64_t*) buf, req->rw_len, waited, timeout, rc);
+
+	i2c_free_req(req);
+	if (rc)
+		rc = STB_DRIVER_ERROR;
+out:
+	return rc;
+}
diff --git a/libstb/drivers/tpm_i2c_interface.h b/libstb/drivers/tpm_i2c_interface.h
new file mode 100644
index 0000000..0ac021e
--- /dev/null
+++ b/libstb/drivers/tpm_i2c_interface.h
@@ -0,0 +1,27 @@
+/* Copyright 2013-2016 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TPM_I2C_H
+#define __TPM_I2C_H
+
+#include <i2c.h>
+#include <stdlib.h>
+
+extern void tpm_i2c_request_complete(int rc, struct i2c_request *req);
+extern int tpm_i2c_request_send(int tpm_bus_id, int tpm_dev_addr, int read_write,
+				uint32_t offset, uint32_t offset_bytes, void* buf,
+				size_t buflen);
+#endif /* __TPM_I2C_H */
diff --git a/libstb/status_codes.h b/libstb/status_codes.h
index 0d7e5fb..385e764 100644
--- a/libstb/status_codes.h
+++ b/libstb/status_codes.h
@@ -19,6 +19,8 @@
 
 /*  general return codes */
 #define STB_ERROR		-1
+#define STB_ARG_ERROR		-2
+#define STB_DRIVER_ERROR	-3
 
 /* secure boot */
 #define STB_VERIFY_FAILED  		-100
-- 
1.9.1



More information about the Skiboot mailing list