From nobody Sun Nov 24 21:22:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1720184422; cv=none; d=zohomail.com; s=zohoarc; b=UDp6eIhgPtBtzqAZsLvRO05eLVqwqUihanedbS3WIRewXchwpzMnErNWq3k7VOrOWjApz8HRnT+WlLlQcDWdlcPRAaRPeDDhDd6whiui1rbNBPFTJd6Gfjn0ffprKGqypH5cmX0zB97Kpc8b8o4tNnbTt3VQWVCgByGjlQsf0D0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720184422; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=gVfHAfEHxXxYeh0S1CyNbpzfOhPz5kccPa2sNveUIkE=; b=f9AitDKI5Q+lc1iObmzdh56qkFDDwcCYpE7ta3QBSQAmnmIbY6JNh3bFGnvTLQBw+zuloM+AWEDFfJzTg/8v5bTkHkfAXdLPmnquCbYaR7RNRY4wxDl/xWSHsZsBLgs1cISuoV0EWRPPJr84liUOwP6NLUl58z3ARF1ngAX08sw= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17201844222401019.6676483669636; Fri, 5 Jul 2024 06:00:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sPiXZ-0007JF-8i; Fri, 05 Jul 2024 08:59:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sPiXW-0007F6-Sb for qemu-devel@nongnu.org; Fri, 05 Jul 2024 08:59:50 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sPiXU-0004Zo-Im for qemu-devel@nongnu.org; Fri, 05 Jul 2024 08:59:50 -0400 Received: from mail.maildlp.com (unknown [172.18.186.231]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4WFtqc5yvJz6K6ZH; Fri, 5 Jul 2024 20:58:36 +0800 (CST) Received: from lhrpeml500005.china.huawei.com (unknown [7.191.163.240]) by mail.maildlp.com (Postfix) with ESMTPS id 00257140A30; Fri, 5 Jul 2024 20:59:47 +0800 (CST) Received: from SecurePC-101-06.china.huawei.com (10.122.19.247) by lhrpeml500005.china.huawei.com (7.191.163.240) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 5 Jul 2024 13:59:46 +0100 To: , , CC: , Davidlohr Bueso , Fan Ni , Subject: [PATCH v4 1/1] hw/cxl: Support firmware updates Date: Fri, 5 Jul 2024 13:59:15 +0100 Message-ID: <20240705125915.991672-2-Jonathan.Cameron@huawei.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240705125915.991672-1-Jonathan.Cameron@huawei.com> References: <20240705125915.991672-1-Jonathan.Cameron@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.122.19.247] X-ClientProxiedBy: lhrpeml500003.china.huawei.com (7.191.162.67) To lhrpeml500005.china.huawei.com (7.191.163.240) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=185.176.79.56; envelope-from=jonathan.cameron@huawei.com; helo=frasgout.his.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Jonathan Cameron From: Jonathan Cameron via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1720184424226100004 Content-Type: text/plain; charset="utf-8" From: Davidlohr Bueso Implement transfer and activate functionality per 3.1 spec for supporting update metadata (no actual buffers). Transfer times are arbitrarily set to ten and two seconds for full and part transfers, respectively. cxl update-firmware mem0 -F fw.img cxl update-firmware mem0 "memdev":"mem0", "pmem_size":"1024.00 MiB (1073.74 MB)", "serial":"0", "host":"0000:0d:00.0", "firmware":{ "num_slots":2, "active_slot":1, "online_activate_capable":true, "slot_1_version":"BWFW VERSION 0", "fw_update_in_progress":true, "remaining_size":22400 } } cxl update-firmware mem0 { "memdev":"mem0", "pmem_size":"1024.00 MiB (1073.74 MB)", "serial":"0", "host":"0000:0d:00.0", "firmware":{ "num_slots":2, "active_slot":1, "staged_slot":2, "online_activate_capable":true, "slot_1_version":"BWFW VERSION 0", "slot_2_version":"BWFW VERSION 1", "fw_update_in_progress":false } } Signed-off-by: Davidlohr Bueso Link: https://lore.kernel.org/r/20240627164912.25630-1-dave@stgolabs.net Signed-off-by: Jonathan Cameron --- v4: Rebased due to tree reordering. Fixed a couple of tab / space issues reported by checkpatch. --- include/hw/cxl/cxl_device.h | 15 +++ hw/cxl/cxl-mailbox-utils.c | 205 +++++++++++++++++++++++++++++++++++- 2 files changed, 215 insertions(+), 5 deletions(-) diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h index 5cae7159e6..fdd0f4e62b 100644 --- a/include/hw/cxl/cxl_device.h +++ b/include/hw/cxl/cxl_device.h @@ -181,6 +181,21 @@ typedef struct CXLCCI { uint64_t runtime; QEMUTimer *timer; } bg; + + /* firmware update */ + struct { + uint8_t active_slot; + uint8_t staged_slot; + bool slot[4]; + uint8_t curr_action; + uint8_t curr_slot; + /* handle partial transfers */ + bool transferring; + size_t prev_offset; + size_t prev_len; + time_t last_partxfer; + } fw; + size_t payload_max; /* Pointer to device hosting the CCI */ DeviceState *d; diff --git a/hw/cxl/cxl-mailbox-utils.c b/hw/cxl/cxl-mailbox-utils.c index df919ad7ed..9cfcde8ffc 100644 --- a/hw/cxl/cxl-mailbox-utils.c +++ b/hw/cxl/cxl-mailbox-utils.c @@ -63,6 +63,8 @@ enum { #define SET_INTERRUPT_POLICY 0x3 FIRMWARE_UPDATE =3D 0x02, #define GET_INFO 0x0 + #define TRANSFER 0x1 + #define ACTIVATE 0x2 TIMESTAMP =3D 0x03, #define GET 0x0 #define SET 0x1 @@ -626,6 +628,9 @@ static CXLRetCode cmd_infostat_bg_op_sts(const struct c= xl_cmd *cmd, return CXL_MBOX_SUCCESS; } =20 +#define CXL_FW_SLOTS 2 +#define CXL_FW_SIZE 0x02000000 /* 32 mb */ + /* CXL r3.1 Section 8.2.9.3.1: Get FW Info (Opcode 0200h) */ static CXLRetCode cmd_firmware_update_get_info(const struct cxl_cmd *cmd, uint8_t *payload_in, @@ -657,15 +662,192 @@ static CXLRetCode cmd_firmware_update_get_info(const= struct cxl_cmd *cmd, fw_info =3D (void *)payload_out; memset(fw_info, 0, sizeof(*fw_info)); =20 - fw_info->slots_supported =3D 2; - fw_info->slot_info =3D BIT(0) | BIT(3); - fw_info->caps =3D 0; - pstrcpy(fw_info->fw_rev1, sizeof(fw_info->fw_rev1), "BWFW VERSION 0"); + fw_info->slots_supported =3D CXL_FW_SLOTS; + fw_info->slot_info =3D (cci->fw.active_slot & 0x7) | + ((cci->fw.staged_slot & 0x7) << 3); + fw_info->caps =3D BIT(0); /* online update supported */ + + if (cci->fw.slot[0]) { + pstrcpy(fw_info->fw_rev1, sizeof(fw_info->fw_rev1), "BWFW VERSION = 0"); + } + if (cci->fw.slot[1]) { + pstrcpy(fw_info->fw_rev2, sizeof(fw_info->fw_rev2), "BWFW VERSION = 1"); + } =20 *len_out =3D sizeof(*fw_info); return CXL_MBOX_SUCCESS; } =20 +/* CXL r3.1 section 8.2.9.3.2: Transfer FW (Opcode 0201h) */ +#define CXL_FW_XFER_ALIGNMENT 128 + +#define CXL_FW_XFER_ACTION_FULL 0x0 +#define CXL_FW_XFER_ACTION_INIT 0x1 +#define CXL_FW_XFER_ACTION_CONTINUE 0x2 +#define CXL_FW_XFER_ACTION_END 0x3 +#define CXL_FW_XFER_ACTION_ABORT 0x4 + +static CXLRetCode cmd_firmware_update_transfer(const struct cxl_cmd *cmd, + uint8_t *payload_in, + size_t len, + uint8_t *payload_out, + size_t *len_out, + CXLCCI *cci) +{ + struct { + uint8_t action; + uint8_t slot; + uint8_t rsvd1[2]; + uint32_t offset; + uint8_t rsvd2[0x78]; + uint8_t data[]; + } QEMU_PACKED *fw_transfer =3D (void *)payload_in; + size_t offset, length; + + if (fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_ABORT) { + /* + * At this point there aren't any on-going transfers + * running in the bg - this is serialized before this + * call altogether. Just mark the state machine and + * disregard any other input. + */ + cci->fw.transferring =3D false; + return CXL_MBOX_SUCCESS; + } + + offset =3D fw_transfer->offset * CXL_FW_XFER_ALIGNMENT; + length =3D len - sizeof(*fw_transfer); + if (offset + length > CXL_FW_SIZE) { + return CXL_MBOX_INVALID_INPUT; + } + + if (cci->fw.transferring) { + if (fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_FULL || + fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_INIT) { + return CXL_MBOX_FW_XFER_IN_PROGRESS; + } + /* + * Abort partitioned package transfer if over 30 secs + * between parts. As opposed to the explicit ABORT action, + * semantically treat this condition as an error - as + * if a part action were passed without a previous INIT. + */ + if (difftime(time(NULL), cci->fw.last_partxfer) > 30.0) { + cci->fw.transferring =3D false; + return CXL_MBOX_INVALID_INPUT; + } + } else if (fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_CONTINUE || + fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_END) { + return CXL_MBOX_INVALID_INPUT; + } + + /* allow back-to-back retransmission */ + if ((offset !=3D cci->fw.prev_offset || length !=3D cci->fw.prev_len) = && + (fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_CONTINUE || + fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_END)) { + /* verify no overlaps */ + if (offset < cci->fw.prev_offset + cci->fw.prev_len) { + return CXL_MBOX_FW_XFER_OUT_OF_ORDER; + } + } + + switch (fw_transfer->action) { + case CXL_FW_XFER_ACTION_FULL: /* ignores offset */ + case CXL_FW_XFER_ACTION_END: + if (fw_transfer->slot =3D=3D 0 || + fw_transfer->slot =3D=3D cci->fw.active_slot || + fw_transfer->slot > CXL_FW_SLOTS) { + return CXL_MBOX_FW_INVALID_SLOT; + } + + /* mark the slot used upon bg completion */ + break; + case CXL_FW_XFER_ACTION_INIT: + if (offset !=3D 0) { + return CXL_MBOX_INVALID_INPUT; + } + + cci->fw.transferring =3D true; + cci->fw.prev_offset =3D offset; + cci->fw.prev_len =3D length; + break; + case CXL_FW_XFER_ACTION_CONTINUE: + cci->fw.prev_offset =3D offset; + cci->fw.prev_len =3D length; + break; + default: + return CXL_MBOX_INVALID_INPUT; + } + + if (fw_transfer->action =3D=3D CXL_FW_XFER_ACTION_FULL) { + cci->bg.runtime =3D 10 * 1000UL; + } else { + cci->bg.runtime =3D 2 * 1000UL; + } + /* keep relevant context for bg completion */ + cci->fw.curr_action =3D fw_transfer->action; + cci->fw.curr_slot =3D fw_transfer->slot; + *len_out =3D 0; + + return CXL_MBOX_BG_STARTED; +} + +static void __do_firmware_xfer(CXLCCI *cci) +{ + switch (cci->fw.curr_action) { + case CXL_FW_XFER_ACTION_FULL: + case CXL_FW_XFER_ACTION_END: + cci->fw.slot[cci->fw.curr_slot - 1] =3D true; + cci->fw.transferring =3D false; + break; + case CXL_FW_XFER_ACTION_INIT: + case CXL_FW_XFER_ACTION_CONTINUE: + time(&cci->fw.last_partxfer); + break; + default: + break; + } +} + +/* CXL r3.1 section 8.2.9.3.3: Activate FW (Opcode 0202h) */ +static CXLRetCode cmd_firmware_update_activate(const struct cxl_cmd *cmd, + uint8_t *payload_in, + size_t len, + uint8_t *payload_out, + size_t *len_out, + CXLCCI *cci) +{ + struct { + uint8_t action; + uint8_t slot; + } QEMU_PACKED *fw_activate =3D (void *)payload_in; + QEMU_BUILD_BUG_ON(sizeof(*fw_activate) !=3D 0x2); + + if (fw_activate->slot =3D=3D 0 || + fw_activate->slot =3D=3D cci->fw.active_slot || + fw_activate->slot > CXL_FW_SLOTS) { + return CXL_MBOX_FW_INVALID_SLOT; + } + + /* ensure that an actual fw package is there */ + if (!cci->fw.slot[fw_activate->slot - 1]) { + return CXL_MBOX_FW_INVALID_SLOT; + } + + switch (fw_activate->action) { + case 0: /* online */ + cci->fw.active_slot =3D fw_activate->slot; + break; + case 1: /* reset */ + cci->fw.staged_slot =3D fw_activate->slot; + break; + default: + return CXL_MBOX_INVALID_INPUT; + } + + return CXL_MBOX_SUCCESS; +} + /* CXL r3.1 Section 8.2.9.4.1: Get Timestamp (Opcode 0300h) */ static CXLRetCode cmd_timestamp_get(const struct cxl_cmd *cmd, uint8_t *payload_in, @@ -2501,6 +2683,10 @@ static const struct cxl_cmd cxl_cmd_set[256][256] = =3D { ~0, CXL_MBOX_IMMEDIATE_CONFIG_CHANGE= }, [FIRMWARE_UPDATE][GET_INFO] =3D { "FIRMWARE_UPDATE_GET_INFO", cmd_firmware_update_get_info, 0, 0 }, + [FIRMWARE_UPDATE][TRANSFER] =3D { "FIRMWARE_UPDATE_TRANSFER", + cmd_firmware_update_transfer, ~0, CXL_MBOX_BACKGROUND_OPERATION }, + [FIRMWARE_UPDATE][ACTIVATE] =3D { "FIRMWARE_UPDATE_ACTIVATE", + cmd_firmware_update_activate, 2, CXL_MBOX_BACKGROUND_OPERATION }, [TIMESTAMP][GET] =3D { "TIMESTAMP_GET", cmd_timestamp_get, 0, 0 }, [TIMESTAMP][SET] =3D { "TIMESTAMP_SET", cmd_timestamp_set, 8, CXL_MBOX_IMMEDIATE_POLICY_CHANGE }, @@ -2629,7 +2815,9 @@ int cxl_process_cci_message(CXLCCI *cci, uint8_t set,= uint8_t cmd, h =3D=3D cmd_media_get_poison_list || h =3D=3D cmd_media_inject_poison || h =3D=3D cmd_media_clear_poison || - h =3D=3D cmd_sanitize_overwrite) { + h =3D=3D cmd_sanitize_overwrite || + h =3D=3D cmd_firmware_update_transfer || + h =3D=3D cmd_firmware_update_activate) { return CXL_MBOX_MEDIA_DISABLED; } } @@ -2674,6 +2862,9 @@ static void bg_timercb(void *opaque) cci->bg.complete_pct =3D 100; cci->bg.ret_code =3D ret; switch (cci->bg.opcode) { + case 0x0201: /* fw transfer */ + __do_firmware_xfer(cci); + break; case 0x4400: /* sanitize */ { CXLType3Dev *ct3d =3D CXL_TYPE3(cci->d); @@ -2745,6 +2936,10 @@ void cxl_init_cci(CXLCCI *cci, size_t payload_max) cci->bg.runtime =3D 0; cci->bg.timer =3D timer_new_ms(QEMU_CLOCK_VIRTUAL, bg_timercb, cci); + + memset(&cci->fw, 0, sizeof(cci->fw)); + cci->fw.active_slot =3D 1; + cci->fw.slot[cci->fw.active_slot - 1] =3D true; } =20 static void cxl_copy_cci_commands(CXLCCI *cci, const struct cxl_cmd (*cxl_= cmds)[256]) --=20 2.43.0