From patchwork Thu Sep 14 06:49:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1834027 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=mux1FhwJ; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=BrWprnoy; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RmSd10zKgz1yhn for ; Thu, 14 Sep 2023 16:50:27 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=kF0bLr8nfZn+Ml9jUDULpm0xMDSbrS+HsAtRWq3HvnE=; b=mux1FhwJJtgXla rpPcFLlqBJnUzg4h8NKe8BdbDKmpkIZuUNm2/oxlRjPeQYEvIa7iY6pELiDSUvnqdQflfIm3XRu0q P6SkEX13Q/mcb5XHiZM+u9gtMw6LcvdZTdSkqiHuhAfSWijN7/Z9m3DLxKRPZao6IjexuNgtBNwFd X6OWleNTb/GV3K5/7EhpxY4w3HCJ8QYZI9RPPTL/+lQ0fv+D4P5+1iUeg1HXLmiVvZtud9t/ORWOu Y+DHFbQ8REhXcHrY0lt+uClsSZZ39Zt1A8/XBnkWEkEp1rbetc9CUOwZRLCW2XIvprg/Qi+760VQb RSyqHO2o5bmPFa3VnVQw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qggAZ-007Y5D-2R; Thu, 14 Sep 2023 06:49:43 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qggAW-007Y4L-1i; Thu, 14 Sep 2023 06:49:42 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2bfb12b24e5so9284261fa.0; Wed, 13 Sep 2023 23:49:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694674176; x=1695278976; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nLt56x/+byFP2a42jCI16rLI5Oh2/81pcuquOpoEQwU=; b=BrWprnoysZ+qU/d4GNicXSoIWc2GAlSCkQpMFD6X6hw+EU0MDCi9TVHTVHSV8nQwn8 Sw4NnaTiHuEQbqhBZ+rg9vCtU2IrNQdXwhU8yjUoxDWFZ1Swvg7NS46LZ58gl8tAbRQs NKZhC0tcLvqosn3XiBLKkxEq+d+zo5og+/SIyc6e5GKX0p0BWP2deVAK4SPKc3CHK5Ah rtnXrXjgEIeoaQCgCMZTu2dwv7GC/8m/DxgLf1HXRIxyXVEBAiCQqF/ZPI6Rs4eDmtt8 C0bGJI2xS1dBUkUgCPY+4Ah7MxX9t3Dnujf2L5leLM8F8LXNWK23xFMr5g1I1Ifg10Iw FdFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694674176; x=1695278976; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nLt56x/+byFP2a42jCI16rLI5Oh2/81pcuquOpoEQwU=; b=RzqohgOq4tS66DdPtcwIW9iPT8dJmEO3uYVKAPr1/c/HBvvnbJvCBM/zw6FUbb3gVp UGokbXfmSEDWtge/zvi8+LrIDAHSwlXsk30D/3yNW7Gu44JYAhDg/MkW9Oa7PGASDfbR qMIxqRrScHRZcHc42J6Ymp/0DHIKc6s+o7DIFU4UTCDjvHBWoEgGWDqRgW1rpi6OJ1z8 EcpJNtv82xuPGXAczvogz1hmzSl4QTVodVRPlaPLOv81TPwhYcMIkgh+07hK24OZzCyG r80YnSZ79skC2gUnKc5ReFxb09EKiTCs2ACXCF5zxSf2MDBkIUVdiqE+jWQtfDk72MQP YQ3w== X-Gm-Message-State: AOJu0YySehsRjaMir+Qj4oybcY5OKtJHUFi2qrlTx9g/onAeTFnLBboh JHUU8vHfpx6bQG9NjyZT0gU= X-Google-Smtp-Source: AGHT+IGmZW4nrpil9KXEo+bbhmVfahNQoU3dZnFCrmBmZD4hG2m18HihW7kyboCIQAU1X8pIArdHsA== X-Received: by 2002:a2e:8850:0:b0:2bb:c22a:f28c with SMTP id z16-20020a2e8850000000b002bbc22af28cmr4775114ljj.32.1694674175493; Wed, 13 Sep 2023 23:49:35 -0700 (PDT) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id l4-20020a2e7004000000b002b9f0b25ff6sm159739ljc.4.2023.09.13.23.49.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 23:49:35 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla Cc: linux-mtd@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , =?utf-8?b?QXLEsW7DpyA=?= =?utf-8?b?w5xOQUw=?= , Florian Fainelli , Scott Branden Subject: [PATCH] nvmem: brcm_nvram: store a copy of NVRAM content Date: Thu, 14 Sep 2023 08:49:22 +0200 Message-Id: <20230914064922.3986-1-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230913_234940_591490_1C37B1E4 X-CRM114-Status: GOOD ( 19.26 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22bombadil=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__From=3A_Rafa=C5=82_Mi=C5=82ecki_This_d?= =?unknown-8bit?q?river_uses_MMIO_access_for_reading?= =?unknown-8bit?q?_NVRAM_from_a_flash_device=2E_Underneath_there_is_a_flash_c?= =?unknown-8bit?q?ontroller_that_reads?= =?unknown-8bit?q?_data_and_provides_mapping_window=2E_Using_MMIO_interface_a?= =?unknown-8bit?q?ffects_controller?= =?unknown-8bit?q?_configuration_and_may_break_real_controller_driver=2E_It_w?= =?unknown-8bit?q?as_reported_by_multiple?= =?unknown-8bit?q?_users_of_devices_with_NVRAM_stored_on_NAND=2E_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_req?= =?unknown-8bit?q?uired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail?= =?unknown-8bit?q?_provider?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends?= =?unknown-8bit?q?_in_digit?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C?= =?unknown-8bit?q?_no_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoyMzQgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_author=27s_domain?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily?= =?unknown-8bit?q?_valid?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_envelope-from_domain?= X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Rafał Miłecki This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window. Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND. Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested. Link: https://lore.kernel.org/linux-mtd/CACna6rwf3_9QVjYcM+847biTX=K0EoWXuXcSMkJO1Vy_5vmVqA@mail.gmail.com/ Cc: Arınç ÜNAL Cc: Florian Fainelli Cc: Scott Branden Signed-off-by: Rafał Miłecki --- drivers/nvmem/brcm_nvram.c | 77 +++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/drivers/nvmem/brcm_nvram.c b/drivers/nvmem/brcm_nvram.c index 9737104f3b76..cfc1c485e20a 100644 --- a/drivers/nvmem/brcm_nvram.c +++ b/drivers/nvmem/brcm_nvram.c @@ -19,7 +19,8 @@ struct brcm_nvram { struct device *dev; - void __iomem *base; + uint8_t *data; + size_t size; struct nvmem_cell_info *cells; int ncells; }; @@ -36,10 +37,8 @@ static int brcm_nvram_read(void *context, unsigned int offset, void *val, size_t bytes) { struct brcm_nvram *priv = context; - u8 *dst = val; - while (bytes--) - *dst++ = readb(priv->base + offset++); + memcpy(val, priv->data + offset, bytes); return 0; } @@ -68,7 +67,12 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, { struct device *dev = priv->dev; char *var, *value, *eq; + uint8_t tmp; int idx; + int err = 0; + + tmp = priv->data[len - 1]; + priv->data[len - 1] = '\0'; priv->ncells = 0; for (var = data + sizeof(struct brcm_nvram_header); @@ -78,8 +82,10 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, } priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL); - if (!priv->cells) - return -ENOMEM; + if (!priv->cells) { + err = -ENOMEM; + goto out; + } for (var = data + sizeof(struct brcm_nvram_header), idx = 0; var < (char *)data + len && *var; @@ -91,8 +97,10 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, value = eq + 1; priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL); - if (!priv->cells[idx].name) - return -ENOMEM; + if (!priv->cells[idx].name) { + err = -ENOMEM; + goto out; + } priv->cells[idx].offset = value - (char *)data; priv->cells[idx].bytes = strlen(value); priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name); @@ -105,40 +113,32 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, } } - return 0; +out: + priv->data[len - 1] = tmp; + return err; } static int brcm_nvram_parse(struct brcm_nvram *priv) { + struct brcm_nvram_header *header = (struct brcm_nvram_header *)priv->data; struct device *dev = priv->dev; - struct brcm_nvram_header header; - uint8_t *data; size_t len; int err; - memcpy_fromio(&header, priv->base, sizeof(header)); - - if (memcmp(header.magic, NVRAM_MAGIC, 4)) { + if (memcmp(header->magic, NVRAM_MAGIC, 4)) { dev_err(dev, "Invalid NVRAM magic\n"); return -EINVAL; } - len = le32_to_cpu(header.len); - - data = kzalloc(len, GFP_KERNEL); - if (!data) - return -ENOMEM; - - memcpy_fromio(data, priv->base, len); - data[len - 1] = '\0'; - - err = brcm_nvram_add_cells(priv, data, len); - if (err) { - dev_err(dev, "Failed to add cells: %d\n", err); - return err; + len = le32_to_cpu(header->len); + if (len > priv->size) { + dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len, priv->size); + return -EINVAL; } - kfree(data); + err = brcm_nvram_add_cells(priv, priv->data, len); + if (err) + dev_err(dev, "Failed to add cells: %d\n", err); return 0; } @@ -150,8 +150,9 @@ static int brcm_nvram_probe(struct platform_device *pdev) .reg_read = brcm_nvram_read, }; struct device *dev = &pdev->dev; - struct resource *res; struct brcm_nvram *priv; + struct resource *res; + void __iomem *base; int err; priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); @@ -159,21 +160,29 @@ static int brcm_nvram_probe(struct platform_device *pdev) return -ENOMEM; priv->dev = dev; - priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); - if (IS_ERR(priv->base)) - return PTR_ERR(priv->base); + base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); + if (IS_ERR(base)) + return PTR_ERR(base); + + priv->size = resource_size(res); + + priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL); + if (!priv->data) + return -ENOMEM; + + memcpy_fromio(priv->data, base, priv->size); err = brcm_nvram_parse(priv); if (err) return err; - bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res)); + bcm47xx_nvram_init_from_iomem(base, priv->size); config.dev = dev; config.cells = priv->cells; config.ncells = priv->ncells; config.priv = priv; - config.size = resource_size(res); + config.size = priv->size; return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); }