From patchwork Wed Sep 6 07:34:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 810417 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xnFkS3XxJz9sCZ for ; Wed, 6 Sep 2017 17:36:00 +1000 (AEST) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3xnFkS1pjdzDrMP for ; Wed, 6 Sep 2017 17:36:00 +1000 (AEST) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=au1.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=andrew.donnellan@au1.ibm.com; receiver=) Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3xnFkH4nDKzDqkc for ; Wed, 6 Sep 2017 17:35:50 +1000 (AEST) Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v867Zcuo095210 for ; Wed, 6 Sep 2017 03:35:47 -0400 Received: from e23smtp07.au.ibm.com (e23smtp07.au.ibm.com [202.81.31.140]) by mx0a-001b2d01.pphosted.com with ESMTP id 2ct8y2vc0b-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 06 Sep 2017 03:35:44 -0400 Received: from localhost by e23smtp07.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 6 Sep 2017 17:34:49 +1000 Received: from d23relay08.au.ibm.com (202.81.31.227) by e23smtp07.au.ibm.com (202.81.31.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 6 Sep 2017 17:34:46 +1000 Received: from d23av06.au.ibm.com (d23av06.au.ibm.com [9.190.235.151]) by d23relay08.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v867YkS334013354 for ; Wed, 6 Sep 2017 17:34:46 +1000 Received: from d23av06.au.ibm.com (localhost [127.0.0.1]) by d23av06.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id v867Yjia011761 for ; Wed, 6 Sep 2017 17:34:46 +1000 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.192.253.14]) by d23av06.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id v867YjOS011758; Wed, 6 Sep 2017 17:34:45 +1000 Received: from intelligence.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 71B29A0117; Wed, 6 Sep 2017 17:34:45 +1000 (AEST) From: Andrew Donnellan To: skiboot@lists.ozlabs.org Date: Wed, 6 Sep 2017 17:34:35 +1000 X-Mailer: git-send-email 2.11.0 X-TM-AS-MML: disable x-cbid: 17090607-0044-0000-0000-00000284E4A0 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17090607-0045-0000-0000-000007187EBB Message-Id: <20170906073435.26609-1-andrew.donnellan@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-09-06_02:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1709060101 Subject: [Skiboot] [PATCH] i2c: Move tpm i2c wrapper code into core X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" The TPM code has a wrapper around the main i2c API to allow synchronous use. Move it into core/i2c.c so it can be used by other possible users. In particular, a future patch will use this to drive OpenCAPI device resets during boot time. Cc: Claudio Carvalho Cc: Frederic Barrat Signed-off-by: Andrew Donnellan --- I'm using this successfully on my OpenCAPI development ZZ. No changes from RFC version. --- core/i2c.c | 107 +++++++++++++++++++++++++++++++++++++ include/i2c.h | 5 ++ libstb/drivers/tpm_i2c_interface.c | 105 ++++-------------------------------- libstb/drivers/tpm_i2c_interface.h | 1 - 4 files changed, 123 insertions(+), 95 deletions(-) diff --git a/core/i2c.c b/core/i2c.c index c1646249..d4311c27 100644 --- a/core/i2c.c +++ b/core/i2c.c @@ -19,6 +19,7 @@ #include #include #include +#include static LIST_HEAD(i2c_bus_list); @@ -134,4 +135,110 @@ static int opal_i2c_request(uint64_t async_token, uint32_t bus_id, } opal_call(OPAL_I2C_REQUEST, opal_i2c_request, 3); +#define MAX_NACK_RETRIES 2 +#define REQ_COMPLETE_POLLING 5 /* Check if req is complete + in 5ms interval */ +struct i2c_sync_userdata { + int rc; + bool done; +}; + +static void i2c_sync_request_complete(int rc, struct i2c_request *req) +{ + struct i2c_sync_userdata *ud = req->user_data; + ud->rc = rc; + ud->done = true; +} + +/** + * i2c_request_send - send request to i2c bus synchronously + * @bus_id: i2c bus id + * @dev_addr: address of the 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 + * @timeout: request timeout in milliseconds + * + * Send an I2C request to a device synchronously + * + * Returns: Zero on success otherwise a negative error code + */ +int i2c_request_send(int bus_id, int dev_addr, int read_write, + uint32_t offset, uint32_t offset_bytes, void* buf, + size_t buflen, int timeout) +{ + int rc, waited, retries; + struct i2c_request *req; + struct i2c_bus *bus; + uint64_t time_to_wait = 0; + struct i2c_sync_userdata ud; + + bus = i2c_find_bus_by_id(bus_id); + if (!bus) { + /** + * @fwts-label I2CInvalidBusID + * @fwts-advice i2c_request_send was passed an invalid bus + * ID. This indicates a bug. + */ + prlog(PR_ERR, "I2C: Invalid bus_id=%x\n", bus_id); + return OPAL_PARAMETER; + } + + req = i2c_alloc_req(bus); + if (!req) { + /** + * @fwts-label I2CAllocationFailed + * @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, "I2C: i2c_alloc_req failed\n"); + return OPAL_INTERNAL_ERROR; + } + + req->dev_addr = 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 = i2c_sync_request_complete; + ud.done = false; + req->user_data = &ud; + + for (retries = 0; retries <= MAX_NACK_RETRIES; retries++) { + waited = 0; + i2c_set_req_timeout(req, timeout); + i2c_queue_req(req); + + do { + time_to_wait = i2c_run_req(req); + if (!time_to_wait) + time_to_wait = REQ_COMPLETE_POLLING; + time_wait(time_to_wait); + waited += time_to_wait; + } while (!ud.done); + + rc = ud.rc; + + if (rc == OPAL_I2C_NACK_RCVD) + continue; + else + /* error or success */ + break; + } + + prlog(PR_DEBUG, "I2C: %s req op=%x offset=%x buf=%016llx buflen=%d " + "delay=%lu/%d rc=%d\n", + (rc) ? "!!!!" : "----", req->op, req->offset, + *(uint64_t*) buf, req->rw_len, tb_to_msecs(waited), timeout, rc); + + i2c_free_req(req); + if (rc) + return OPAL_HARDWARE; + + return OPAL_SUCCESS; +} diff --git a/include/i2c.h b/include/i2c.h index e5e8584b..669c30a2 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -106,6 +106,11 @@ static inline int i2c_check_quirk(struct i2c_request *req, int *rc) return 0; } +/* I2C synchronous request API */ +int i2c_request_send(int bus_id, int dev_addr, int read_write, + uint32_t offset, uint32_t offset_bytes, void* buf, + size_t buflen, int timeout); + /* P8 implementation details */ extern void p8_i2c_init(void); extern void p8_i2c_interrupt(uint32_t chip_id); diff --git a/libstb/drivers/tpm_i2c_interface.c b/libstb/drivers/tpm_i2c_interface.c index cc7bf1ad..a6342e0c 100644 --- a/libstb/drivers/tpm_i2c_interface.c +++ b/libstb/drivers/tpm_i2c_interface.c @@ -15,32 +15,13 @@ */ #include -#include #include #include #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 30 /* 30ms/byte timeout */ -#define TPM_MAX_NACK_RETRIES 2 -#define REQ_COMPLETE_POLLING 5 /* Check if req is complete - in 5ms interval */ - -struct tpm_i2c_userdata { - int rc; - bool done; -}; - -void tpm_i2c_request_complete(int rc, struct i2c_request *req) -{ - struct tpm_i2c_userdata *ud = req->user_data; - ud->rc = rc; - ud->done = true; -} /** * tpm_i2c_request_send - send request to i2c bus @@ -57,88 +38,24 @@ void tpm_i2c_request_complete(int rc, struct i2c_request *req) * * 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, +int tpm_i2c_request_send(int bus_id, int 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; - uint64_t time_to_wait = 0; - struct tpm_i2c_userdata ud; - - 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; - ud.done = false; - req->user_data = &ud; + int rc, timeout; /* - * Set the request timeout to 10ms per byte. Otherwise, we get - * an I2C master timeout for all requests sent to the TPM device + * Set the request timeout to 30ms per byte. Otherwise, we get + * an I2C master timeout for all requests sent to the device * since the I2C master's timeout is too short (1ms per byte). */ timeout = (buflen + offset_bytes + 2) * I2C_BYTE_TIMEOUT_MS; - for (retries = 0; retries <= TPM_MAX_NACK_RETRIES; retries++) { - waited = 0; - i2c_set_req_timeout(req, timeout); - i2c_queue_req(req); - - do { - time_to_wait = i2c_run_req(req); - if (!time_to_wait) - time_to_wait = REQ_COMPLETE_POLLING; - time_wait(time_to_wait); - waited += time_to_wait; - } while (!ud.done); - - rc = ud.rc; - - 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=%lu/%d," - "rc=%d\n", - (rc) ? "!!!!" : "----", req->op, req->offset, - *(uint64_t*) buf, req->rw_len, tb_to_msecs(waited), timeout, rc); - - i2c_free_req(req); - if (rc) - rc = STB_DRIVER_ERROR; -out: - return rc; + rc = i2c_request_send(bus_id, dev_addr, read_write, offset, + offset_bytes, buf, buflen, timeout); + if (rc == OPAL_PARAMETER) + return STB_ARG_ERROR; + else if (rc < 0) + return STB_DRIVER_ERROR; + return 0; } diff --git a/libstb/drivers/tpm_i2c_interface.h b/libstb/drivers/tpm_i2c_interface.h index 0ac021ed..d2acc1ae 100644 --- a/libstb/drivers/tpm_i2c_interface.h +++ b/libstb/drivers/tpm_i2c_interface.h @@ -20,7 +20,6 @@ #include #include -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);