From patchwork Mon Nov 6 11:03:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 741364 Delivered-To: patch@linaro.org Received: by 2002:adf:fd90:0:b0:32d:baff:b0ca with SMTP id d16csp1032944wrr; Mon, 6 Nov 2023 03:33:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IHW4k9r95NkmFYEqfprtSAwk4Ya8SggGotQ0xQgWYlqG+pkYlKsy0AW8RlN2QMo8QHw+YOr X-Received: by 2002:a05:622a:10f:b0:41c:e702:4df7 with SMTP id u15-20020a05622a010f00b0041ce7024df7mr35572781qtw.63.1699270395881; Mon, 06 Nov 2023 03:33:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699270395; cv=none; d=google.com; s=arc-20160816; b=ygYn2mU3y8/KowF934n2u0w8xdDXVLxr7jXNORSYnZXYFDbNT2n3XGsBSX7F4TNXch cb3C3EhewIMgOOOM+KFx+xt+9rrfRLG9O9zFtu7l/aEeMBhvq7M6M/GemhAqC4AMsBR4 y98UvkdPszxF84/uUocdO1yprB27+EFmY57ABefZpeE/49U8YiDpvrmmss45eu/4uBFe PTMGxoRdqKL9eDTm9lFZ9QxDVyC2kyYuC2eggXcuVNufsIR3U6ADmC8vlRjMj36u7nQ5 NGgQ+bJFOlh8obhhY8Lcwg+RQDv3OKjbCXVCjI2pABrgv5XX1tPe1ECGuOd0gw0WQVzV ilAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MGUqOvUDBSiuVjcJL6HNGd3FosLBI9RMurHd3wgpnc0=; fh=aRIPeX+s2DDBTipkT3PFTnr+paXxUdjkxZpQDuA7k4Q=; b=C3XIIV9iMVhQ2U7b6ce3wzMjKBnwPt0ixx6Hw3mJrSRixx6P1gdHMK8qSPIv3or0Fc 9yw+3NjS7c6Hi0U2YaE+8nsmhfqnTjyzisrHEWjgQJICVTUyFrO74Lm33JXBhK9GDxEi 2wDgfUxva7zA1SAh3UZxvp7UqDrNZ+Z/CnOAOxlOx4iR+/pEPOnSgkoct2Tu6WGbLyVl YbatJyVq5tiSNQQDu8pJ2MIaXmXZPRhhFvCJylV8nKthX8yvO4BUYlxtzncnF9a2wzeJ oZ2wuQaAX6feXxdCR8GnnUtPnGWalxHACXXfwXwCb+Tk5saNiBro6RNx4u8r4LH2jYFW eaZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="UM/kvCtl"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id r18-20020a05622a035200b0041819819519si5608748qtw.694.2023.11.06.03.33.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 06 Nov 2023 03:33:15 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="UM/kvCtl"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qzxV4-0003Da-M0; Mon, 06 Nov 2023 06:10:37 -0500 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 1qzxUc-0002cK-82 for qemu-devel@nongnu.org; Mon, 06 Nov 2023 06:10:12 -0500 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qzxUX-0005iJ-IL for qemu-devel@nongnu.org; Mon, 06 Nov 2023 06:10:05 -0500 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2c6ef6c1ec2so52817151fa.2 for ; Mon, 06 Nov 2023 03:10:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699268999; x=1699873799; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MGUqOvUDBSiuVjcJL6HNGd3FosLBI9RMurHd3wgpnc0=; b=UM/kvCtl5sMb4dU0M0RhM3utLMRVEe44EiQluYmQMRPIJBIeygRHNutW6Axx9RFStf cOyoDiv5mpOZkVW8tQPFhsSoWecJTow03yjIwqaFQ3O41yKBfeWUREylR/kS+wQN2yq9 +UxEuRaRfjekMwmL3YrlkcVnKrOLR+44bAKBi0+W1QZK/uW4ErfSnHP1GvipmlA5U3w2 cf16waTBJyWbLLxRh4GPe8aVWOmInEtyrHdG+9sga/3aIvhTbYHiVM37z8b/dwyeF6T/ vStH1506Pa/tISHjccY+kunBexR+BH+psTEpKMElTFXUi+kRMykgP7KXd1kxVFEd/yjn pjNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699268999; x=1699873799; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MGUqOvUDBSiuVjcJL6HNGd3FosLBI9RMurHd3wgpnc0=; b=SwmtC7TXY7ckmmnTcIEjQ6Gz7YRgR5fOFIsufBR6v7yVmjMgURyxKVfc/8lCZZkkvp x6Qq84h8m+ZUDx03M02vC3Al2fRsTKiifEqLrM1UkFNA3cNPzHkkE68Vmzm5lIFWfPUO GjtILRbkpAdu2FF14dXjxuEnlu0WViHhXX1OrXdiJpA9Hhz8yql31qEgQ0++B5BJwFMd XAMVnVAoDSa00zPCfA898J0JpF3OqTtmqHHXj8kTTBMC4FOwB2Uga5asTKgYbuXTmOHo HBnsumDwIoiPAW3I6X0qTu9OEeNdqxDg1JkyWVFXy2irBk6TcrVVg6b8Kd6LJJqD76kp TV6w== X-Gm-Message-State: AOJu0Yxvcbqd//3hp7/lPSYBChdQlSmBh64GKpgdRe7x4ARyRNW1D1jv eEsup1IZ0NND8/Ad6cYd+kVqVoEwgSjW9Uxnvw8= X-Received: by 2002:a2e:8719:0:b0:2c5:1075:5ec9 with SMTP id m25-20020a2e8719000000b002c510755ec9mr21956567lji.13.1699268999654; Mon, 06 Nov 2023 03:09:59 -0800 (PST) Received: from m1x-phil.lan (176-131-220-199.abo.bbox.fr. [176.131.220.199]) by smtp.gmail.com with ESMTPSA id f13-20020a05600c4e8d00b0040596352951sm11684422wmq.5.2023.11.06.03.09.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 06 Nov 2023 03:09:59 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, qemu-s390x@nongnu.org, qemu-block@nongnu.org, qemu-riscv@nongnu.org, qemu-ppc@nongnu.org, qemu-arm@nongnu.org, Titus Rwantare , Hao Wu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Thomas Huth , Laurent Vivier , Paolo Bonzini Subject: [PULL 56/60] hw/i2c: pmbus: reset page register for out of range reads Date: Mon, 6 Nov 2023 12:03:28 +0100 Message-ID: <20231106110336.358-57-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231106110336.358-1-philmd@linaro.org> References: <20231106110336.358-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::232; envelope-from=philmd@linaro.org; helo=mail-lj1-x232.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Titus Rwantare The linux pmbus driver scans all possible pages and does not reset the current page after the scan, making all future page reads fail as out of range on devices with a single page. This change resets out of range pages immediately on write. Also added a qtest for simultaneous writes to all pages. Reviewed-by: Hao Wu Signed-off-by: Titus Rwantare Message-ID: <20231023-staging-pmbus-v3-v4-8-07a8cb7cd20a@google.com> Signed-off-by: Philippe Mathieu-Daudé --- hw/i2c/pmbus_device.c | 18 +++++++++--------- tests/qtest/max34451-test.c | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/hw/i2c/pmbus_device.c b/hw/i2c/pmbus_device.c index 481e158380..1b978e588f 100644 --- a/hw/i2c/pmbus_device.c +++ b/hw/i2c/pmbus_device.c @@ -1255,6 +1255,15 @@ static int pmbus_write_data(SMBusDevice *smd, uint8_t *buf, uint8_t len) if (pmdev->code == PMBUS_PAGE) { pmdev->page = pmbus_receive8(pmdev); + + if (pmdev->page > pmdev->num_pages - 1 && pmdev->page != PB_ALL_PAGES) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: page %u is out of range\n", + __func__, pmdev->page); + pmdev->page = 0; /* undefined behaviour - reset to page 0 */ + pmbus_cml_error(pmdev); + return PMBUS_ERR_BYTE; + } return 0; } @@ -1268,15 +1277,6 @@ static int pmbus_write_data(SMBusDevice *smd, uint8_t *buf, uint8_t len) return 0; } - if (pmdev->page > pmdev->num_pages - 1) { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: page %u is out of range\n", - __func__, pmdev->page); - pmdev->page = 0; /* undefined behaviour - reset to page 0 */ - pmbus_cml_error(pmdev); - return PMBUS_ERR_BYTE; - } - index = pmdev->page; switch (pmdev->code) { diff --git a/tests/qtest/max34451-test.c b/tests/qtest/max34451-test.c index 0c98d0764c..dbf6ddc829 100644 --- a/tests/qtest/max34451-test.c +++ b/tests/qtest/max34451-test.c @@ -18,6 +18,7 @@ #define TEST_ID "max34451-test" #define TEST_ADDR (0x4e) +#define MAX34451_MFR_MODE 0xD1 #define MAX34451_MFR_VOUT_PEAK 0xD4 #define MAX34451_MFR_IOUT_PEAK 0xD5 #define MAX34451_MFR_TEMPERATURE_PEAK 0xD6 @@ -315,6 +316,28 @@ static void test_ot_faults(void *obj, void *data, QGuestAllocator *alloc) } } +#define RAND_ON_OFF_CONFIG 0x12 +#define RAND_MFR_MODE 0x3456 + +/* test writes to all pages */ +static void test_all_pages(void *obj, void *data, QGuestAllocator *alloc) +{ + uint16_t i2c_value; + QI2CDevice *i2cdev = (QI2CDevice *)obj; + + i2c_set8(i2cdev, PMBUS_PAGE, PB_ALL_PAGES); + i2c_set8(i2cdev, PMBUS_ON_OFF_CONFIG, RAND_ON_OFF_CONFIG); + max34451_i2c_set16(i2cdev, MAX34451_MFR_MODE, RAND_MFR_MODE); + + for (int i = 0; i < MAX34451_NUM_TEMP_DEVICES + MAX34451_NUM_PWR_DEVICES; + i++) { + i2c_value = i2c_get8(i2cdev, PMBUS_ON_OFF_CONFIG); + g_assert_cmphex(i2c_value, ==, RAND_ON_OFF_CONFIG); + i2c_value = max34451_i2c_get16(i2cdev, MAX34451_MFR_MODE); + g_assert_cmphex(i2c_value, ==, RAND_MFR_MODE); + } +} + static void max34451_register_nodes(void) { QOSGraphEdgeOptions opts = { @@ -332,5 +355,6 @@ static void max34451_register_nodes(void) qos_add_test("test_ro_regs", "max34451", test_ro_regs, NULL); qos_add_test("test_ov_faults", "max34451", test_ov_faults, NULL); qos_add_test("test_ot_faults", "max34451", test_ot_faults, NULL); + qos_add_test("test_all_pages", "max34451", test_all_pages, NULL); } libqos_init(max34451_register_nodes);