From patchwork Fri Sep 22 17:48:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 143634 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5832564vqi; Fri, 22 Sep 2023 12:58:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG/lJ6sBBzolrSc8cWix86Dq7aoDq3P+JGqeC5WkE84ZmTpTkEPY0vi9ynETxKqWiugqPgk X-Received: by 2002:a17:902:9b8f:b0:1c3:2df4:8791 with SMTP id y15-20020a1709029b8f00b001c32df48791mr478273plp.27.1695412729852; Fri, 22 Sep 2023 12:58:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695412729; cv=none; d=google.com; s=arc-20160816; b=pkCwtgFH45TcdcQBqLzzYf7bDXMi04WGhbtkOtEtGkHvayWrgXeXYTAPk5im73AxKP GjsQ9tsLjf/sr54pVfOOzKuy5kH5GSLVY3xkzHao/5oTYJhwTeSdHK66HV2rEryZmXjP +tVv8e8MApHP122EHXh3COgxOJwdktKzR6y3Hp/xLcZo9B92y8UZr456ezUexFz4YGLe nGo3r2uEhGl69s4JyduEXtkui2QDn+jgr1C01aHOCpKmQ1CiTzBK/nH6KvcpKwlYgF7L dg12O26j+FOg8AjDzkldAtr7loiFPX5i9N/4xslp0Heu9G0ldyqSBWQ0Zs4btP/zze3p o0Ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IOlSiA8SmOjLNDfVpiluhiC8NmmHYphyzXQK/vvl7L0=; fh=uNRYBw15K+4lj0RowSR5ppkZ/jQAGNdwwLd8P1k8N6c=; b=cPBDSQh7u8nTZMsLUMb3XmQ8afvKoSxLPrf3q3xJJAXffYZtIEj58kOYq4HgqGQsBe OygWya40KKIZVqI9zX+2drx01BJXpRDuCkP+nUYRm8ugX813ofSExq566MEmL4y1EgjD jnkV+UrC7WDX5lv3NB9nRMlk6sSVjsO6jmm6QPrZWDLAabgoks3yhTdfZjB6ysL3sx1F EwCDqQBmQihUr2yfhPA7Nrotov71ynhC96p5rUd5PtRsTVOHwHZMdDbscw9M6EZc4y6T S/cpg9OIooS69ESaOwx4RzeiBPg/UXd9ZHDIzstNxeXix52DqOSFFBvrDTrjdk6Wd//m gbqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=BO1WNZn0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id d7-20020a170902cec700b001c5c632aea4si4898802plg.217.2023.09.22.12.58.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 12:58:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=BO1WNZn0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 55E3081D6A99; Fri, 22 Sep 2023 10:49:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233025AbjIVRtN (ORCPT + 28 others); Fri, 22 Sep 2023 13:49:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232910AbjIVRtK (ORCPT ); Fri, 22 Sep 2023 13:49:10 -0400 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EEBB88F for ; Fri, 22 Sep 2023 10:49:03 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id AB92FE0006; Fri, 22 Sep 2023 17:49:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1695404942; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IOlSiA8SmOjLNDfVpiluhiC8NmmHYphyzXQK/vvl7L0=; b=BO1WNZn0uY/l3ZjSlcHGwGSodH2iNuVpdzSTaIGaW2y5tGUmL1H/IonC5ON39X7HqlL4ja EjKXmLx3FO6CsW/q0TxdIEQ1nfEdyKtHrsjj0spha4ymwmxHQILwDsGMbpow/xZDXrTQZR BP/ImwNsnWL6x5ORMywe35Z1NS82rz8G094Jqdu3d2okY2J7VlOcLg3KiFl+PfxBl4HYbK GT0fL4bCdoySMPTlpfWrh+TvYl/dSWeHNW3shW9RSTQVXTH0ZHa9JNzjIvCFAg0CVGkeUK xhlEzdL0ZHuxaLBVT4aMwUqNeHOIHtivfx+f32CEW9wls4b6onp9ChfO/CfUwQ== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , Thomas Petazzoni , Robert Marko , Luka Perkov , , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Miquel Raynal Subject: [PATCH v10 1/3] nvmem: core: Rework layouts to become platform devices Date: Fri, 22 Sep 2023 19:48:52 +0200 Message-Id: <20230922174854.611975-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230922174854.611975-1-miquel.raynal@bootlin.com> References: <20230922174854.611975-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 22 Sep 2023 10:49:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777769098528209420 X-GMAIL-MSGID: 1777769098528209420 Current layout support was initially written without modules support in mind. When the requirement for module support rose, the existing base was improved to adopt modularization support, but kind of a design flaw was introduced. With the existing implementation, when a storage device registers into NVMEM, the core tries to hook a layout (if any) and populates its cells immediately. This means, if the hardware description expects a layout to be hooked up, but no driver was provided for that, the storage medium will fail to probe and try later from scratch. Technically, the layouts are more like a "plus" and, even we consider that the hardware description shall be correct, we could still probe the storage device (especially if it contains the rootfs). One way to overcome this situation is to consider the layouts as devices, and leverage the existing notifier mechanism. When a new NVMEM device is registered, we can: - populate its nvmem-layout child, if any - try to modprobe the relevant driver, if relevant - try to hook the NVMEM device with a layout in the notifier And when a new layout is registered: - try to hook all the existing NVMEM devices which are not yet hooked to a layout with the new layout This way, there is no strong order to enforce, any NVMEM device creation or NVMEM layout driver insertion will be observed as a new event which may lead to the creation of additional cells, without disturbing the probes with costly (and sometimes endless) deferrals. Signed-off-by: Miquel Raynal Tested-by: Rafał Miłecki --- drivers/nvmem/core.c | 140 ++++++++++++++++++++++++------- drivers/nvmem/layouts/onie-tlv.c | 39 +++++++-- drivers/nvmem/layouts/sl28vpd.c | 39 +++++++-- include/linux/nvmem-provider.h | 11 +-- 4 files changed, 180 insertions(+), 49 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index eaf6a3fe8ca6..14dd3ae777bb 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -17,11 +17,13 @@ #include #include #include +#include #include struct nvmem_device { struct module *owner; struct device dev; + struct list_head node; int stride; int word_size; int id; @@ -75,6 +77,7 @@ static LIST_HEAD(nvmem_cell_tables); static DEFINE_MUTEX(nvmem_lookup_mutex); static LIST_HEAD(nvmem_lookup_list); +struct notifier_block nvmem_nb; static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); static DEFINE_SPINLOCK(nvmem_layout_lock); @@ -790,23 +793,16 @@ EXPORT_SYMBOL_GPL(nvmem_layout_unregister); static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem) { struct device_node *layout_np; - struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER); + struct nvmem_layout *l, *layout = NULL; layout_np = of_nvmem_layout_get_container(nvmem); if (!layout_np) return NULL; - /* - * In case the nvmem device was built-in while the layout was built as a - * module, we shall manually request the layout driver loading otherwise - * we'll never have any match. - */ - of_request_module(layout_np); - spin_lock(&nvmem_layout_lock); list_for_each_entry(l, &nvmem_layouts, node) { - if (of_match_node(l->of_match_table, layout_np)) { + if (of_match_node(l->dev->driver->of_match_table, layout_np)) { if (try_module_get(l->owner)) layout = l; @@ -863,7 +859,7 @@ const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, const struct of_device_id *match; layout_np = of_nvmem_layout_get_container(nvmem); - match = of_match_node(layout->of_match_table, layout_np); + match = of_match_node(layout->dev->driver->of_match_table, layout_np); return match ? match->data : NULL; } @@ -882,6 +878,7 @@ EXPORT_SYMBOL_GPL(nvmem_layout_get_match_data); struct nvmem_device *nvmem_register(const struct nvmem_config *config) { struct nvmem_device *nvmem; + struct device_node *layout_np; int rval; if (!config->dev) @@ -974,19 +971,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) goto err_put_device; } - /* - * If the driver supplied a layout by config->layout, the module - * pointer will be NULL and nvmem_layout_put() will be a noop. - */ - nvmem->layout = config->layout ?: nvmem_layout_get(nvmem); - if (IS_ERR(nvmem->layout)) { - rval = PTR_ERR(nvmem->layout); - nvmem->layout = NULL; - - if (rval == -EPROBE_DEFER) - goto err_teardown_compat; - } - if (config->cells) { rval = nvmem_add_cells(nvmem, config->cells, config->ncells); if (rval) @@ -1005,24 +989,27 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_cells; - rval = nvmem_add_cells_from_layout(nvmem); - if (rval) - goto err_remove_cells; - dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); rval = device_add(&nvmem->dev); if (rval) goto err_remove_cells; + /* Populate layouts as devices */ + layout_np = of_nvmem_layout_get_container(nvmem); + if (layout_np) { + rval = of_platform_populate(nvmem->dev.of_node, NULL, NULL, NULL); + of_node_put(layout_np); + if (rval) + goto err_remove_cells; + } + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; err_remove_cells: nvmem_device_remove_all_cells(nvmem); - nvmem_layout_put(nvmem->layout); -err_teardown_compat: if (config->compat) nvmem_sysfs_remove_compat(nvmem, config); err_put_device: @@ -2124,13 +2111,106 @@ const char *nvmem_dev_name(struct nvmem_device *nvmem) } EXPORT_SYMBOL_GPL(nvmem_dev_name); +static void nvmem_try_loading_layout_driver(struct nvmem_device *nvmem) +{ + struct device_node *layout_np; + + layout_np = of_nvmem_layout_get_container(nvmem); + if (layout_np) { + of_request_module(layout_np); + of_node_put(layout_np); + } +} + +static int nvmem_match_available_layout(struct nvmem_device *nvmem) +{ + int ret; + + if (nvmem->layout) + return 0; + + nvmem->layout = nvmem_layout_get(nvmem); + if (!nvmem->layout) + return 0; + + ret = nvmem_add_cells_from_layout(nvmem); + if (ret) { + nvmem_layout_put(nvmem->layout); + nvmem->layout = NULL; + return ret; + } + + return 0; +} + +static int nvmem_dev_match_available_layout(struct device *dev, void *data) +{ + struct nvmem_device *nvmem = to_nvmem_device(dev); + + return nvmem_match_available_layout(nvmem); +} + +static int nvmem_for_each_dev(int (*fn)(struct device *dev, void *data)) +{ + return bus_for_each_dev(&nvmem_bus_type, NULL, NULL, fn); +} + +/* + * When an NVMEM device is registered, try to match against a layout and + * populate the cells. When an NVMEM layout is probed, ensure all NVMEM devices + * which could use it properly expose their cells. + */ +static int nvmem_notifier_call(struct notifier_block *notifier, + unsigned long event_flags, void *context) +{ + struct nvmem_device *nvmem = NULL; + int ret; + + switch (event_flags) { + case NVMEM_ADD: + nvmem = context; + break; + case NVMEM_LAYOUT_ADD: + break; + default: + return NOTIFY_DONE; + } + + if (nvmem) { + /* + * In case the nvmem device was built-in while the layout was + * built as a module, manually request loading the layout driver. + */ + nvmem_try_loading_layout_driver(nvmem); + + /* Populate the cells of the new nvmem device from its layout, if any */ + ret = nvmem_match_available_layout(nvmem); + } else { + /* NVMEM devices might be "waiting" for this layout */ + ret = nvmem_for_each_dev(nvmem_dev_match_available_layout); + } + + if (ret) + return notifier_from_errno(ret); + + return NOTIFY_OK; +} + static int __init nvmem_init(void) { - return bus_register(&nvmem_bus_type); + int ret; + + ret = bus_register(&nvmem_bus_type); + if (ret) + return ret; + + nvmem_nb.notifier_call = &nvmem_notifier_call; + return nvmem_register_notifier(&nvmem_nb); } static void __exit nvmem_exit(void) { + nvmem_unregister_notifier(&nvmem_nb); bus_unregister(&nvmem_bus_type); } diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c index 59fc87ccfcff..3d54d3be2c93 100644 --- a/drivers/nvmem/layouts/onie-tlv.c +++ b/drivers/nvmem/layouts/onie-tlv.c @@ -13,6 +13,7 @@ #include #include #include +#include #define ONIE_TLV_MAX_LEN 2048 #define ONIE_TLV_CRC_FIELD_SZ 6 @@ -226,18 +227,46 @@ static int onie_tlv_parse_table(struct device *dev, struct nvmem_device *nvmem, return 0; } +static int onie_tlv_probe(struct platform_device *pdev) +{ + struct nvmem_layout *layout; + + layout = devm_kzalloc(&pdev->dev, sizeof(*layout), GFP_KERNEL); + if (!layout) + return -ENOMEM; + + layout->add_cells = onie_tlv_parse_table; + layout->dev = &pdev->dev; + + platform_set_drvdata(pdev, layout); + + return nvmem_layout_register(layout); +} + +static int onie_tlv_remove(struct platform_device *pdev) +{ + struct nvmem_layout *layout = platform_get_drvdata(pdev); + + nvmem_layout_unregister(layout); + + return 0; +} + static const struct of_device_id onie_tlv_of_match_table[] = { { .compatible = "onie,tlv-layout", }, {}, }; MODULE_DEVICE_TABLE(of, onie_tlv_of_match_table); -static struct nvmem_layout onie_tlv_layout = { - .name = "ONIE tlv layout", - .of_match_table = onie_tlv_of_match_table, - .add_cells = onie_tlv_parse_table, +static struct platform_driver onie_tlv_layout = { + .driver = { + .name = "onie-tlv-layout", + .of_match_table = onie_tlv_of_match_table, + }, + .probe = onie_tlv_probe, + .remove = onie_tlv_remove, }; -module_nvmem_layout_driver(onie_tlv_layout); +module_platform_driver(onie_tlv_layout); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Miquel Raynal "); diff --git a/drivers/nvmem/layouts/sl28vpd.c b/drivers/nvmem/layouts/sl28vpd.c index 05671371f631..ad0c39fc7943 100644 --- a/drivers/nvmem/layouts/sl28vpd.c +++ b/drivers/nvmem/layouts/sl28vpd.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #define SL28VPD_MAGIC 'V' @@ -135,18 +136,46 @@ static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem, return 0; } +static int sl28vpd_probe(struct platform_device *pdev) +{ + struct nvmem_layout *layout; + + layout = devm_kzalloc(&pdev->dev, sizeof(*layout), GFP_KERNEL); + if (!layout) + return -ENOMEM; + + layout->add_cells = sl28vpd_add_cells; + layout->dev = &pdev->dev; + + platform_set_drvdata(pdev, layout); + + return nvmem_layout_register(layout); +} + +static int sl28vpd_remove(struct platform_device *pdev) +{ + struct nvmem_layout *layout = platform_get_drvdata(pdev); + + nvmem_layout_unregister(layout); + + return 0; +} + static const struct of_device_id sl28vpd_of_match_table[] = { { .compatible = "kontron,sl28-vpd" }, {}, }; MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table); -static struct nvmem_layout sl28vpd_layout = { - .name = "sl28-vpd", - .of_match_table = sl28vpd_of_match_table, - .add_cells = sl28vpd_add_cells, +static struct platform_driver sl28vpd_layout = { + .driver = { + .name = "kontron-sl28vpd-layout", + .of_match_table = sl28vpd_of_match_table, + }, + .probe = sl28vpd_probe, + .remove = sl28vpd_remove, }; -module_nvmem_layout_driver(sl28vpd_layout); +module_platform_driver(sl28vpd_layout); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Michael Walle "); diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index dae26295e6be..c72064780b50 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -154,8 +154,7 @@ struct nvmem_cell_table { /** * struct nvmem_layout - NVMEM layout definitions * - * @name: Layout name. - * @of_match_table: Open firmware match table. + * @dev: Device-model layout device. * @add_cells: Will be called if a nvmem device is found which * has this layout. The function will add layout * specific cells with nvmem_add_one_cell(). @@ -170,8 +169,7 @@ struct nvmem_cell_table { * cells. */ struct nvmem_layout { - const char *name; - const struct of_device_id *of_match_table; + struct device *dev; int (*add_cells)(struct device *dev, struct nvmem_device *nvmem, struct nvmem_layout *layout); void (*fixup_cell_info)(struct nvmem_device *nvmem, @@ -243,9 +241,4 @@ nvmem_layout_get_match_data(struct nvmem_device *nvmem, } #endif /* CONFIG_NVMEM */ - -#define module_nvmem_layout_driver(__layout_driver) \ - module_driver(__layout_driver, nvmem_layout_register, \ - nvmem_layout_unregister) - #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */ From patchwork Fri Sep 22 17:48:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 143786 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:910f:0:b0:403:3b70:6f57 with SMTP id r15csp40980vqg; Fri, 22 Sep 2023 19:24:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE39LIGOISWfePNYR4EAWx0YkMnlz+OblBJF3YT3c2URJ0U/t6ZX1aXxBmMTm5NBZCJwfLG X-Received: by 2002:a9d:7654:0:b0:6c4:ce0e:29ae with SMTP id o20-20020a9d7654000000b006c4ce0e29aemr1193648otl.21.1695435893631; Fri, 22 Sep 2023 19:24:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695435893; cv=none; d=google.com; s=arc-20160816; b=IbFMfDHcdUCVrxa+bfPtp3sgk0DmE81+/u88dgt5UM3WAgUDWUncVYU0Sq2SGBa2IW jZsSbHlJ3kfwOqI9hwUZtDHBsurHR2Ci+IzcbYfHH/HCLzBtN/WSvZOpCEocpad22TOz jRcV6LYjki3CBMW11IB1ggE1Yqylq11r+IjjTa6IEvymqLcto/VU3DpULt527Vpayt5l OAnwDrjKalhC0DBUNn771vIKm92CKGz1CVXu9ofsm/52FD6BtD/wtOOmiDOleil34IoO 1gHyBckfcy3NORSgfPIzAXewoDQsx53DYjLwNwVSaWe6UKyXCZcEi3lKL9ECryTfHldR IUwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dfOp8t/SWXoAMbVxsmdWr6p/Yay0CylcPPBrARczmbM=; fh=uNRYBw15K+4lj0RowSR5ppkZ/jQAGNdwwLd8P1k8N6c=; b=LJMqBVFDKqYF8vlqCBjtdDXnMppDkP+DhjKCM4QRNzMaH5HGl0sd4GoIKFuYH6Xw7b YmNpjRQ/rHNf2JPcdr1FJvJ7jdj3nRWXg64bt3ss7C0YP7liJ5I3wLaSk3Ixar8hMsRB L8VptoPmz1420iQwzPyYRQJfofIrC5ZAok1UuSwxzGyyUqlfBiYUkPPvpgpwhQZG6CP8 kEbfBTNvDVL5kA4WAHoO/kfAWDA3zzd3sdmgA7SLaIIotKtQQAoXVgYnbDDliSy8EC6G kJL7NB9xbXCjO0TgEokNyJX0XIJoqj1vSXj0vTF/ngfc4wj+ZkvIGH+s5nTlXwzw6Oa9 A2zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=cUjfLTWd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id y63-20020a636442000000b00578e4816c9asi5131803pgb.19.2023.09.22.19.24.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 19:24:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=cUjfLTWd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id AC014821707E; Fri, 22 Sep 2023 10:49:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233102AbjIVRtU (ORCPT + 28 others); Fri, 22 Sep 2023 13:49:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232987AbjIVRtM (ORCPT ); Fri, 22 Sep 2023 13:49:12 -0400 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::224]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 090C299 for ; Fri, 22 Sep 2023 10:49:05 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id DD990E0003; Fri, 22 Sep 2023 17:49:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1695404944; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dfOp8t/SWXoAMbVxsmdWr6p/Yay0CylcPPBrARczmbM=; b=cUjfLTWdIYMKuMb63IFz5LtWQxIs46prbk3G4JAlSMOp+uDL3HTdPZyUwhl9BXgEFYElEd x5g+N9LHLyH0C0gIQZ5szWDBqgnI9a+9VQCZl/SfDh88YFnU0d5WMKmLxS/xPQvD9v5wuR leInR8cKhka+/Pvlyj2RDLFb0ILjDw55WVyhdeMKZCHQQNTiyBbqwiePOiKuFvBpO3e0Od 2Pp1QprnAQpG8kZetLNN0BzhmB2EZTNVbilAkMSywOvwW0ossH38uPhWSAfMpnLKO7rlHT VNpZGpa50DBJHOb4WTwt6nZ1M/twGUBuYRsVZWX7HkdSbkfO9wkV5TEK+O+8Cg== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , Thomas Petazzoni , Robert Marko , Luka Perkov , , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Miquel Raynal Subject: [PATCH v10 2/3] ABI: sysfs-nvmem-cells: Expose cells through sysfs Date: Fri, 22 Sep 2023 19:48:53 +0200 Message-Id: <20230922174854.611975-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230922174854.611975-1-miquel.raynal@bootlin.com> References: <20230922174854.611975-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 22 Sep 2023 10:49:21 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777793387401578551 X-GMAIL-MSGID: 1777793387401578551 The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell regardless of their position/size in the underlying device, but these information were not accessible to the user. By exposing the nvmem cells to the user through a dedicated cell/ folder containing one file per cell, we provide a straightforward access to useful user information without the need for re-writing a userland parser. Content of nvmem cells is usually: product names, manufacturing date, MAC addresses, etc, Signed-off-by: Miquel Raynal Reviewed-by: Greg Kroah-Hartman --- Documentation/ABI/testing/sysfs-nvmem-cells | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-nvmem-cells diff --git a/Documentation/ABI/testing/sysfs-nvmem-cells b/Documentation/ABI/testing/sysfs-nvmem-cells new file mode 100644 index 000000000000..7af70adf3690 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-nvmem-cells @@ -0,0 +1,21 @@ +What: /sys/bus/nvmem/devices/.../cells/ +Date: May 2023 +KernelVersion: 6.5 +Contact: Miquel Raynal +Description: + The "cells" folder contains one file per cell exposed by the + NVMEM device. The name of the file is: @, with + being the cell name and its location in the NVMEM + device, in hexadecimal (without the '0x' prefix, to mimic device + tree node names). The length of the file is the size of the cell + (when known). The content of the file is the binary content of + the cell (may sometimes be ASCII, likely without trailing + character). + Note: This file is only present if CONFIG_NVMEM_SYSFS + is enabled. + + Example:: + + hexdump -C /sys/bus/nvmem/devices/1-00563/cells/product-name@d + 00000000 54 4e 34 38 4d 2d 50 2d 44 4e |TN48M-P-DN| + 0000000a From patchwork Fri Sep 22 17:48:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 143728 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5936962vqi; Fri, 22 Sep 2023 17:00:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEXn7453p3T/H3mRmLMCB3TnQlYdMjSvstJX9yYpDasKIA704RCRy6H4O3rxPGylyJJ87Kx X-Received: by 2002:a17:903:24d:b0:1c3:2423:8e24 with SMTP id j13-20020a170903024d00b001c324238e24mr1252125plh.8.1695427220745; Fri, 22 Sep 2023 17:00:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695427220; cv=none; d=google.com; s=arc-20160816; b=dK9iHzK/y5VI3hSk49+7yUJg2pTD8v6uUeKpsVkXCKEzJ5jt3wjKVQbZJwXg9J5Jft BM7MTTKihSM6mNkGb/ltFvdci8N1htnCvDeCGLg71sCqBrnh1Hyujgc1kZah6cGeeDpl ZBI/k29oHocyQiKI/BOwvs5pyVBU5La/yLKCl1b+uXFzX5OWswSQ6t2UHaiI8q2vN04/ YnI7ZilOUrIaD6YN0hdqKDx7DdldxWsEK+QSIEuaBtzOMIPTMM2ERs8AkA1FTYYYF82+ FtM6/roaPN4r189d/kZ0VQq9VvI1QCSOZsmSo+gGVmODpJ+fXqDUm/FAHtrkGYwQm7wG 4icA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4k0Z84iQwNUIVc6FLE5hSxSxMdXUZ7GlWGeFkWoDCwY=; fh=uNRYBw15K+4lj0RowSR5ppkZ/jQAGNdwwLd8P1k8N6c=; b=VrWslcN2NBVvBOoRpmGndIkm4jGHAJcET1PAgucsxiGGCiKp4CY1NxHlzUzyRn+5KO E88UlB7i1WjXCxejHX52foUNapoA8CRqlZX999a5sr976C92+uuzC4rXqaelL5UhkOeQ lmk4DnqJcmJczkj/53b6ENxfC4fDRlDwqCTtdUl7IZlOfTjyG5s85xr2A/SY4ZWf7zb2 TGr/RsH6L1tcwarooto/koeEv9AbHZtwSVwYI8HE9R/pUT6LVEBE/UKmyN2RhGnn8EzI OR5u96UoNn5r7AyH7jNiulClMX2lroykPP73IaefTvOmeLP2CM5eo4ao4PUE4NVawUC4 IA+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=Q0wEF9C7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b8-20020a170902d50800b001bb8f59aca3si4599279plg.583.2023.09.22.17.00.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 17:00:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=Q0wEF9C7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 44CE582C1225; Fri, 22 Sep 2023 10:49:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233057AbjIVRtW (ORCPT + 28 others); Fri, 22 Sep 2023 13:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233050AbjIVRtT (ORCPT ); Fri, 22 Sep 2023 13:49:19 -0400 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::224]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69E2FFB for ; Fri, 22 Sep 2023 10:49:08 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id E4FDEE000A; Fri, 22 Sep 2023 17:49:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1695404947; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4k0Z84iQwNUIVc6FLE5hSxSxMdXUZ7GlWGeFkWoDCwY=; b=Q0wEF9C7xM1CT1kgwI/XWJ6VtQ2uyD8P+JNF2259pxLIOqN7/2hORl4XMG8umqgjCNQpv8 sQfn8Mu3JpfQHEl5d6PL6Lq36dCXWVZpS8s6j4sjKPf28OxsnOW+vDiDtBfC3NyGC9ViMC SHIqZR4qBiuvqll4+9ThGpvTnQERiQJAm7xymlVpddj3J+Vof3TMyrKmWkQ3OsaSwx64P2 3Jm0+dhWTud//stPg8wQb+HJ52ETUNaX8YKXQdN3vklKVa39OQLHGH+TNojOUBJs08QhiK 6cp6yAtkkbDA6EoE6GZyNXpsEW1lMgt9QLW4R9ZU00Ycw0v8Grv7LLYyzcalMA== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , Thomas Petazzoni , Robert Marko , Luka Perkov , , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Miquel Raynal Subject: [PATCH v10 3/3] nvmem: core: Expose cells through sysfs Date: Fri, 22 Sep 2023 19:48:54 +0200 Message-Id: <20230922174854.611975-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230922174854.611975-1-miquel.raynal@bootlin.com> References: <20230922174854.611975-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 22 Sep 2023 10:49:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777784293393407432 X-GMAIL-MSGID: 1777784293393407432 The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell, regardless of its position/size in the underlying device. Unfortunately, these information are not accessible by users, unless by fully re-implementing the parser logic in userland. Let's expose the cells and their content through sysfs to avoid these situations. Of course the relevant NVMEM sysfs Kconfig option must be enabled for this support to be available. Not all nvmem devices expose cells. Indeed, the .bin_attrs attribute group member will be filled at runtime only when relevant and will remain empty otherwise. In this case, as the cells attribute group will be empty, it will not lead to any additional folder/file creation. Exposed cells are read-only. There is, in practice, everything in the core to support a write path, but as I don't see any need for that, I prefer to keep the interface simple (and probably safer). The interface is documented as being in the "testing" state which means we can later add a write attribute if though relevant. Signed-off-by: Miquel Raynal Tested-by: Rafał Miłecki --- drivers/nvmem/core.c | 117 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 14dd3ae777bb..b3c4345ab48a 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -43,6 +43,7 @@ struct nvmem_device { struct gpio_desc *wp_gpio; struct nvmem_layout *layout; void *priv; + bool sysfs_cells_populated; }; #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) @@ -327,6 +328,43 @@ static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj, return nvmem_bin_attr_get_umode(nvmem); } +static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, + const char *id, int index); + +static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, char *buf, + loff_t pos, size_t count) +{ + struct nvmem_cell_entry *entry; + struct nvmem_cell *cell = NULL; + size_t cell_sz, read_len; + void *content; + + entry = attr->private; + cell = nvmem_create_cell(entry, entry->name, 0); + if (IS_ERR(cell)) + return PTR_ERR(cell); + + if (!cell) + return -EINVAL; + + content = nvmem_cell_read(cell, &cell_sz); + if (IS_ERR(content)) { + read_len = PTR_ERR(content); + goto destroy_cell; + } + + read_len = min_t(unsigned int, cell_sz - pos, count); + memcpy(buf, content + pos, read_len); + kfree(content); + +destroy_cell: + kfree_const(cell->id); + kfree(cell); + + return read_len; +} + /* default read/write permissions */ static struct bin_attribute bin_attr_rw_nvmem = { .attr = { @@ -348,11 +386,21 @@ static const struct attribute_group nvmem_bin_group = { .is_bin_visible = nvmem_bin_attr_is_visible, }; +/* Cell attributes will be dynamically allocated */ +static struct attribute_group nvmem_cells_group = { + .name = "cells", +}; + static const struct attribute_group *nvmem_dev_groups[] = { &nvmem_bin_group, NULL, }; +static const struct attribute_group *nvmem_cells_groups[] = { + &nvmem_cells_group, + NULL, +}; + static struct bin_attribute bin_attr_nvmem_eeprom_compat = { .attr = { .name = "eeprom", @@ -408,6 +456,69 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); } +static int nvmem_dev_populate_sysfs_cells(struct device *dev, void *data) +{ + struct nvmem_device *nvmem = to_nvmem_device(dev); + struct bin_attribute **cells_attrs, *attrs; + struct nvmem_cell_entry *entry; + unsigned int ncells = 0, i = 0; + int ret = 0; + + mutex_lock(&nvmem_mutex); + + if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) { + nvmem_cells_group.bin_attrs = NULL; + goto unlock_mutex; + } + + /* Allocate an array of attributes with a sentinel */ + ncells = list_count_nodes(&nvmem->cells); + cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1, + sizeof(struct bin_attribute *), GFP_KERNEL); + if (!cells_attrs) { + ret = -ENOMEM; + goto unlock_mutex; + } + + attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL); + if (!attrs) { + ret = -ENOMEM; + goto unlock_mutex; + } + + /* Initialize each attribute to take the name and size of the cell */ + list_for_each_entry(entry, &nvmem->cells, node) { + sysfs_bin_attr_init(&attrs[i]); + attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL, + "%s@%x", entry->name, + entry->offset); + attrs[i].attr.mode = 0444; + attrs[i].size = entry->bytes; + attrs[i].read = &nvmem_cell_attr_read; + attrs[i].private = entry; + if (!attrs[i].attr.name) { + ret = -ENOMEM; + goto unlock_mutex; + } + + cells_attrs[i] = &attrs[i]; + i++; + } + + nvmem_cells_group.bin_attrs = cells_attrs; + + ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups); + if (ret) + goto unlock_mutex; + + nvmem->sysfs_cells_populated = true; + +unlock_mutex: + mutex_unlock(&nvmem_mutex); + + return ret; +} + #else /* CONFIG_NVMEM_SYSFS */ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, @@ -2193,6 +2304,12 @@ static int nvmem_notifier_call(struct notifier_block *notifier, if (ret) return notifier_from_errno(ret); +#ifdef CONFIG_NVMEM_SYSFS + ret = nvmem_for_each_dev(nvmem_dev_populate_sysfs_cells); + if (ret) + return notifier_from_errno(ret); +#endif + return NOTIFY_OK; }