From patchwork Tue Sep 13 10:27:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Lombard X-Patchwork-Id: 1677246 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=M6V/p3vL; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MRfrV42kSz1ync for ; Tue, 13 Sep 2022 20:31:06 +1000 (AEST) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4MRfrV3C2Hz3chX for ; Tue, 13 Sep 2022 20:31:06 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=M6V/p3vL; dkim-atps=neutral X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=linux.vnet.ibm.com (client-ip=148.163.158.5; helo=mx0b-001b2d01.pphosted.com; envelope-from=clombard@linux.vnet.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=M6V/p3vL; dkim-atps=neutral Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (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 4MRfmQ2sXzz3c3B for ; Tue, 13 Sep 2022 20:27:34 +1000 (AEST) Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 28D9LeBR010818 for ; Tue, 13 Sep 2022 10:27:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=qjlDzOFNshXMoZM8qInFRZJq0iyV7HORTMMGR1vEQBU=; b=M6V/p3vLSkXY6jUwM01PHeA8ecOznMhER7U5HSK7+fEQ0o9j7n4cOr7a/tZ+eMRnt9RM 4E1Uqej9deK5gdYjpyu2lDrU7FIpHobXwRuCw8pbfmUZQDzFeIBzS8V/WEFrnXq38QR6 wJHrqr9ob8zXysossNpGQ2JBtxCOhfrvuujOM0hUJq3HUgVs7hgmDMzTuCTGuL5VX+UC HjVmFKkRxBn1YLaCbeNU3STW0h0n9CgkFLzu8JEISIkA234M9jzLrNzPkd3KdEYROClZ TP4mNJVWCtA0x7NZweKSgRoWgwT7/kwdMRXF/2J8i5OIi8a90i7+qNUQWAhY4tV9yJQP YA== Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3jjq5rhxqf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 13 Sep 2022 10:27:32 +0000 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 28DAKa7C006761 for ; Tue, 13 Sep 2022 10:27:30 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma03ams.nl.ibm.com with ESMTP id 3jgj79knd1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 13 Sep 2022 10:27:30 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 28DARRZg35913994 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 13 Sep 2022 10:27:28 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DB82E11C052 for ; Tue, 13 Sep 2022 10:27:27 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AAE4C11C04A for ; Tue, 13 Sep 2022 10:27:27 +0000 (GMT) Received: from li-ac0ca24c-3330-11b2-a85c-93224c50ad7a.ibm.com.com (unknown [9.171.35.136]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Tue, 13 Sep 2022 10:27:27 +0000 (GMT) From: Christophe Lombard To: skiboot@lists.ozlabs.org Date: Tue, 13 Sep 2022 12:27:22 +0200 Message-Id: <20220913102724.65563-14-clombard@linux.vnet.ibm.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913102724.65563-1-clombard@linux.vnet.ibm.com> References: <20220913102724.65563-1-clombard@linux.vnet.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: KfNGeNFRXONi9zM7WBZnWf34luFdZtg7 X-Proofpoint-ORIG-GUID: KfNGeNFRXONi9zM7WBZnWf34luFdZtg7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-13_03,2022-09-13_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 clxscore=1015 phishscore=0 spamscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 mlxscore=0 bulkscore=0 lowpriorityscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2209130045 Subject: [Skiboot] [PATCH V3 13/15] core/pldm: Register OPAL_RTC_READ/WRITE calls back X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" OPAL_RTC_READ/WRITE are used to retrieve and write the time. PLDM stack provides GetBiosDateTimeReq and SetBiosDateTimeReq commands to exercise. Signed-off-by: Christophe Lombard --- core/pldm/Makefile.inc | 2 +- core/pldm/pldm-rtc.c | 244 +++++++++++++++++++++++++++++++++++++++++ include/pldm.h | 5 + 3 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 core/pldm/pldm-rtc.c diff --git a/core/pldm/Makefile.inc b/core/pldm/Makefile.inc index 5d10d572..00d6c458 100644 --- a/core/pldm/Makefile.inc +++ b/core/pldm/Makefile.inc @@ -16,7 +16,7 @@ PLDM_OBJS = pldm-mctp.o pldm-responder.o pldm-requester.o PLDM_OBJS += pldm-base-requests.o pldm-platform-requests.o PLDM_OBJS += pldm-bios-requests.o pldm-fru-requests.o PLDM_OBJS += pldm-file-io-requests.o pldm-lid-files.o -PLDM_OBJS += pldm-watchdog.o +PLDM_OBJS += pldm-watchdog.o pldm-rtc.o PLDM = $(PLDM_DIR)/built-in.a $(PLDM): $(PLDM_OBJS:%=$(PLDM_DIR)/%) diff --git a/core/pldm/pldm-rtc.c b/core/pldm/pldm-rtc.c new file mode 100644 index 00000000..b6702a0b --- /dev/null +++ b/core/pldm/pldm-rtc.c @@ -0,0 +1,244 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +// Copyright 2022 IBM Corp. + +#define pr_fmt(fmt) "PLDM: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "pldm.h" + +struct get_date_time_resp { + uint8_t completion_code; + uint8_t seconds; + uint8_t minutes; + uint8_t hours; + uint8_t day; + uint8_t month; + uint16_t year; +}; + +static enum {idle, waiting, updated, error} time_status; + +static void cache_get_datetime(struct tm *tm) +{ + if (tm == NULL) + time_status = error; + else { + rtc_cache_update(tm); + time_status = updated; + } +} + +static void get_date_time_req_complete(struct pldm_rx_data *rx, + void *data __unused) +{ + struct get_date_time_resp response; + size_t payload_len; + struct tm tm; + int rc; + + if (rx == NULL) { + prlog(PR_ERR, "%s: Response not received\n", __func__); + cache_get_datetime(NULL); + return; + } + + /* Decode the message */ + payload_len = rx->msg_len - sizeof(struct pldm_msg_hdr); + rc = decode_get_date_time_resp( + rx->msg, + payload_len, + &response.completion_code, + &response.seconds, + &response.minutes, + &response.hours, + &response.day, + &response.month, + &response.year); + if (rc != PLDM_SUCCESS || response.completion_code != PLDM_SUCCESS) { + prlog(PR_ERR, "Decode GetBiosDateTimeReq Error, rc: %d, cc: %d\n", + rc, response.completion_code); + cache_get_datetime(NULL); + return; + } + + /* The data arrives from BMC in BCD format. Convert it to + * decimal for processing + */ + tm.tm_sec = bcd2dec8(response.seconds); + tm.tm_min = bcd2dec8(response.minutes); + tm.tm_hour = bcd2dec8(response.hours); + tm.tm_mday = bcd2dec8(response.day); + tm.tm_mon = bcd2dec8(response.month); + tm.tm_year = bcd2dec16(response.year); + + if (!is_time_legal(tm.tm_sec, tm.tm_min, tm.tm_hour, + tm.tm_mday, tm.tm_mon, tm.tm_year)) { + prlog(PR_ERR, "%s: Invalid date time value\n", __func__); + cache_get_datetime(NULL); + return; + } + + cache_get_datetime(&tm); +} + +/* + * Send a PLDM GetBiosDateTime request message + */ +static int get_date_time_req(void) +{ + char request_msg[PKT_SIZE(0)]; /* the command doesn't have a message payload */ + int rc; + + /* Encode the date time request */ + rc = encode_get_date_time_req(DEFAULT_INSTANCE_ID, + (struct pldm_msg *)request_msg); + if (rc != PLDM_SUCCESS) { + prlog(PR_ERR, "Encode GetBiosDateTimeReq Error, rc: %d\n", rc); + return OPAL_PARAMETER; + } + + /* Queue and get the response message bytes */ + rc = pldm_requester_queue(request_msg, sizeof(request_msg), + get_date_time_req_complete, NULL); + if (rc) { + prlog(PR_ERR, "Communication Error, req: GetBiosDateTimeReq, rc: %d\n", rc); + return rc; + } + + return OPAL_SUCCESS; +} + +static int64_t pldm_opal_rtc_read(__be32 *__ymd, __be64 *__hmsm) +{ + uint32_t ymd; + uint64_t hmsm; + int rc = OPAL_SUCCESS; + + if (!__ymd || !__hmsm) + return OPAL_PARAMETER; + + switch (time_status) { + case idle: + rc = get_date_time_req(); + if (rc) + return OPAL_HARDWARE; + time_status = waiting; + rc = OPAL_BUSY_EVENT; + break; + + case waiting: + rc = OPAL_BUSY_EVENT; + break; + + case updated: + rtc_cache_get_datetime(&ymd, &hmsm); + *__ymd = cpu_to_be32(ymd); + *__hmsm = cpu_to_be64(hmsm); + time_status = idle; + rc = OPAL_SUCCESS; + break; + + case error: + time_status = idle; + rc = OPAL_HARDWARE; + break; + } + + return rc; +} + +/* + * Receive the PLDM SetBiosDateTime response + */ +static void set_date_time_req_complete(struct pldm_rx_data *rx, + void *data __unused) +{ + uint8_t completion_code; + size_t payload_len; + int rc; + + if (rx == NULL) { + prlog(PR_ERR, "%s: Response not received\n", __func__); + return; + } + + /* Decode the message */ + payload_len = rx->msg_len - sizeof(struct pldm_msg_hdr); + + rc = decode_set_date_time_resp(rx->msg, + payload_len, + &completion_code); + if (rc != PLDM_SUCCESS || (completion_code > PLDM_ERROR)) { + /* FIXME: Time value from OPAL_RTC_WRITE is never correct */ + prlog(PR_ERR, "Decode SetBiosDateTimeReq Error, rc: %d, cc: %d\n", + rc, completion_code); + } +} + +/* + * Send a PLDM SetBiosDateTime request message + */ +static int set_date_time_req(struct tm *tm) +{ + char request_msg[PKT_SIZE(struct pldm_set_date_time_req)]; + int rc; + + /* Encode the date time request */ + rc = encode_set_date_time_req( + DEFAULT_INSTANCE_ID, + tm->tm_sec, tm->tm_min, tm->tm_hour, + tm->tm_mday, tm->tm_mon, tm->tm_year, + (struct pldm_msg *)request_msg, + sizeof(struct pldm_set_date_time_req)); + if (rc != PLDM_SUCCESS) { + prlog(PR_ERR, "Encode SetBiosDateTimeReq Error, rc: %d\n", + rc); + return OPAL_PARAMETER; + } + + /* Queue and get the response message bytes */ + rc = pldm_requester_queue(request_msg, sizeof(request_msg), + set_date_time_req_complete, NULL); + if (rc) { + prlog(PR_ERR, "Communication Error, req: SetBiosDateTimeReq, rc: %d\n", rc); + return rc; + } + + return OPAL_SUCCESS; +} + +static int64_t pldm_opal_rtc_write(uint32_t year_month_day, + uint64_t hour_minute_second_millisecond) +{ + struct tm tm; + int rc; + + datetime_to_tm(year_month_day, hour_minute_second_millisecond, &tm); + + rc = set_date_time_req(&tm); + if (rc == OPAL_BUSY) + return OPAL_BUSY_EVENT; + + return OPAL_SUCCESS; +} + +void pldm_rtc_init(void) +{ + struct dt_node *np = dt_new(opal_node, "rtc"); + + dt_add_property_strings(np, "compatible", "ibm,opal-rtc"); + + opal_register(OPAL_RTC_READ, pldm_opal_rtc_read, 2); + opal_register(OPAL_RTC_WRITE, pldm_opal_rtc_write, 2); + + /* Initialise the rtc cache */ + get_date_time_req(); +} diff --git a/include/pldm.h b/include/pldm.h index 2100c12c..db24040d 100644 --- a/include/pldm.h +++ b/include/pldm.h @@ -54,4 +54,9 @@ int pldm_watchdog_init(void); int pldm_platform_send_progress_state_change( enum pldm_state_set_boot_progress_state_values state); +/** + * Configure real-time clock + */ +void pldm_rtc_init(void); + #endif /* __PLDM_H__ */