From patchwork Fri Aug 11 01:36:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134294 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp826325vqi; Thu, 10 Aug 2023 20:13:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFM5gZgIpDjAldZg006fMEVja/a/9vlepUVIkFrAO7cTkcqpt9BTaVse8GEv9gvghKTmxf+ X-Received: by 2002:a05:6870:82a4:b0:1bb:5480:4b4 with SMTP id q36-20020a05687082a400b001bb548004b4mr830518oae.8.1691723631569; Thu, 10 Aug 2023 20:13:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691723631; cv=none; d=google.com; s=arc-20160816; b=S+SuQ9cTFQRA+IGNb8zaUZxbr/wltNJzVsBXZKCSLO59N1gFL+cPxRNTIYlaqkOGan wQEsB9zWERckoyFXJ23Ktpr2BDoiC7jSkoTPOs3iJepAgLEk1MHLErgwZaGNK+tAL2wR 0ZBmOq+tzUOXVhqjMCsrAYN9rfAiBtWBLCIuSxQCB49NA+4gxqtrvIX+HJg63WI91ARe uOG9b49YqI3wf8Txw/qQJrexBNZf3FHFrI+ZxUwtkJ9TeM0Qwhjg33Rmq7+uudP0cftj 2A0Zyn6CZMLSIK7hbfmKBWMjWfLS7jaG13+vORqAhL3nKBD51gDqk5laOj1FKc2noMyK XfoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=x+VRO1GA0fkXhho+VyjcJKVNnc+XMjPqj1/0hr4r2h8=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=T7Ao5HrVQkaaaiOYCc2N3Epiw6d81zDxZbfGLEiHtxLU/eJwb2Mg18iLNfmDGU2wzO oUEV8735hqM6wX0nszcEFYkGOAdF7p9y/HQjCRVXkK7N3oOe44EC5LDRi2bxuwbs3pcH 6aQomwqbYPlrLYnk5htEqk4scUsq8KCZTsLzw2/Mi2CcHToUDeyJVVpY9kMJIjALJSJa 4Ux0YKa4prK0+RXCGX1YXhfy1TUwrdY5UZzrN7x+4ktUsr3CFaoFUqvZViXku4bJCCzd f7FWwaQZd0SCAnfkogV9zZkuwCL29/JggRS1VpgUUhm+BkZSLZjrM9J7wLma7c5IkBWI lu6Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l2-20020a17090a598200b0025eeb3cc4b2si2568929pji.9.2023.08.10.20.13.38; Thu, 10 Aug 2023 20:13:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232071AbjHKBhE (ORCPT + 99 others); Thu, 10 Aug 2023 21:37:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231989AbjHKBhC (ORCPT ); Thu, 10 Aug 2023 21:37:02 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 760412D59; Thu, 10 Aug 2023 18:37:02 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH52-000530-0d; Fri, 11 Aug 2023 01:36:45 +0000 Date: Fri, 11 Aug 2023 02:36:37 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/8] dt-bindings: mtd: add basic bindings for UBI Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773900798500125230 X-GMAIL-MSGID: 1773900798500125230 Add basic bindings for UBI devices and volumes. Signed-off-by: Daniel Golle Reviewed-by: Rob Herring --- .../bindings/mtd/partitions/linux,ubi.yaml | 65 +++++++++++++++++++ .../bindings/mtd/partitions/ubi-volume.yaml | 36 ++++++++++ 2 files changed, 101 insertions(+) create mode 100644 Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml create mode 100644 Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml diff --git a/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml b/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml new file mode 100644 index 0000000000000..a06c1666d5184 --- /dev/null +++ b/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mtd/partitions/linux,ubi.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Unsorted Block Images + +description: | + UBI ("Unsorted Block Images") is a volume management system for raw + flash devices which manages multiple logical volumes on a single + physical flash device and spreads the I/O load (i.e, wear-leveling) + across whole flash chip. + +maintainers: + - Daniel Golle + +allOf: + - $ref: partition.yaml# + +properties: + compatible: + const: linux,ubi + + volumes: + type: object + description: UBI Volumes + + patternProperties: + "^ubi-volume-.*$": + $ref: /schemas/mtd/partitions/ubi-volume.yaml + + unevaluatedProperties: false + +required: + - compatible + +unevaluatedProperties: false + +examples: + - | + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + reg = <0x0 0x100000>; + label = "bootloader"; + read-only; + }; + + partition@100000 { + reg = <0x100000 0x1ff00000>; + label = "ubi"; + compatible = "linux,ubi"; + + volumes { + ubi-volume-caldata { + volume-id = <2>; + volume-name = "rf"; + }; + }; + }; + }; diff --git a/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml b/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml new file mode 100644 index 0000000000000..e14c85bbeeb91 --- /dev/null +++ b/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mtd/partitions/ubi-volume.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: UBI volume + +description: | + This binding describes a single UBI volume. Volumes can be matches either + by their ID or their name, or both. + +maintainers: + - Daniel Golle + +properties: + compatible: true + + volume-id: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + Match UBI volume ID + + volume-name: + $ref: /schemas/types.yaml#/definitions/string + description: + Match UBI volume name + +anyOf: + - required: + - volume-id + + - required: + - volume-name + +unevaluatedProperties: false From patchwork Fri Aug 11 01:36:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134282 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp814210vqi; Thu, 10 Aug 2023 19:36:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFifSzWH00YaPz7ty/J7tjd40HnVNDdVk0WPnW25HvWVRCwOK/om6fduuFoRsGnVuWQmDRa X-Received: by 2002:a05:6402:550:b0:523:a45f:419a with SMTP id i16-20020a056402055000b00523a45f419amr536882edx.41.1691721374722; Thu, 10 Aug 2023 19:36:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691721374; cv=none; d=google.com; s=arc-20160816; b=pV8w1NLBPWO5QGHZCWpZ43UeFTdgaV0kOUGsnYL6w12bKRlFKXAwTMuSIZUMHmUYff Hl1c8VTcgkHGRyreZqf1tcEAIDXM4lRv9yG7vXWGtDgchZ6ov10I5adL+BDdI7VcpKzX f4l12tIeO9ldmqVWymU1nG+NV8Rxy2EtIqr38LrMvlP0L5Jyq9bk6GVMQN0a88ms+4jv cIWGd4vDcvutzUAF+Z8MHNvo0q6o5qGCd+aXkbLfaY9BSenoyXOJ7ZYBQRKz17gu/xMm k+oxoZm9SPyFwmA9McEkaLnjZ4cl4C8NAtb7qj65zLwXKRH/83LhZLRlV42bJ8ARFBIR 6kmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=CrLwN11sEqzcq63sH3TOkU2u/Ixvid/xlOoELWdv4yY=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=sa8wTTTC6/htB8yDm5i0ZlSGMksAGTP4hB9/S3ATyNROeypkz/8kaevq95V2KC7FsC c3N+wKNO9Ol57L2lA7TV3vuLeeYih+Y5UGA7tcY7NtJjo+zRQzONK2pUpCCii7TMxaF/ bedbd219cjWCtOWqqFu7oD/DVmRyiK5lwsA6BaLJQY3fkacVNLwO2TuNsT6CuuQsgUyg RBDmEH7H5yMpmzg4mpheJhdsBztxc/WKUG2pchcC2OWychASEzKArOsxcJar4xIs3UWL 4ISAhstfg65NLS7/kxvSiYjhnp1biJNEYgqt2Lq0ejt7VO7tke7xbuIOtMkn8PYZWh21 oopA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j15-20020aa7c40f000000b005231005e43asi2560013edq.2.2023.08.10.19.35.51; Thu, 10 Aug 2023 19:36:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230297AbjHKBhM (ORCPT + 99 others); Thu, 10 Aug 2023 21:37:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232462AbjHKBhK (ORCPT ); Thu, 10 Aug 2023 21:37:10 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90F052D5F; Thu, 10 Aug 2023 18:37:09 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH5K-00053b-0T; Fri, 11 Aug 2023 01:37:02 +0000 Date: Fri, 11 Aug 2023 02:36:55 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/8] dt-bindings: mtd: nvmem-cells: add support for UBI volumes Message-ID: <7df4543f5dacef006bd7067117cf959b59842d69.1691717480.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773898431746034282 X-GMAIL-MSGID: 1773898431746034282 UBI volumes may be used to contain NVMEM bits, typically device MAC addresses or wireless radio calibration data. Signed-off-by: Daniel Golle --- .../devicetree/bindings/mtd/partitions/linux,ubi.yaml | 3 ++- .../devicetree/bindings/mtd/partitions/nvmem-cells.yaml | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml b/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml index a06c1666d5184..e834441fe5e6a 100644 --- a/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml +++ b/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml @@ -56,9 +56,10 @@ examples: compatible = "linux,ubi"; volumes { - ubi-volume-caldata { + wifi_caldata: ubi-volume-caldata { volume-id = <2>; volume-name = "rf"; + compatible = "nvmem-cells"; }; }; }; diff --git a/Documentation/devicetree/bindings/mtd/partitions/nvmem-cells.yaml b/Documentation/devicetree/bindings/mtd/partitions/nvmem-cells.yaml index 5474d63268dc5..b92a0b35df094 100644 --- a/Documentation/devicetree/bindings/mtd/partitions/nvmem-cells.yaml +++ b/Documentation/devicetree/bindings/mtd/partitions/nvmem-cells.yaml @@ -17,9 +17,12 @@ maintainers: - Ansuel Smith allOf: - - $ref: /schemas/mtd/partitions/partition.yaml# - $ref: /schemas/nvmem/nvmem.yaml# +oneOf: + - $ref: /schemas/mtd/partitions/partition.yaml# + - $ref: /schemas/mtd/partitions/ubi-volume.yaml# + properties: compatible: const: nvmem-cells From patchwork Fri Aug 11 01:37:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134272 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp807097vqi; Thu, 10 Aug 2023 19:14:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqaK60EGcYtmKxMGvFmBmUIP7r3q8V4EifpAaDBoELGY6HcU+UB6GkVcipmXaqVIX2t4oT X-Received: by 2002:a05:6a00:2305:b0:682:5634:3df1 with SMTP id h5-20020a056a00230500b0068256343df1mr712001pfh.10.1691720057323; Thu, 10 Aug 2023 19:14:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691720057; cv=none; d=google.com; s=arc-20160816; b=RhdD1dKh4LYLgpfjyxOX4CS3ZXSAaqHgf3xtHMWbbmM2QuLiY2cSk+/aslDewa5gfW 2UayDvLMUsfun+p1ukhd+d9snBM//sSJ4zUXXljnWWHQyJAsZn35NNWHe3tYE2PeGZkr e9z501nbpyeCkDRPyadvxKmdO1SvUtUlT4e/SvTxinruwizQ1THAsBkuys6IGW3VITy5 WjpCgzEO52bZze7u/z/GB5k9BJfO1WpMNzFgaGEwbBApsHxUneoKL+1IqYlgufom1AvN GhLF/ixoGfLAsf4Thxoh02wc04z4CFaNpT0CJggqFlKX6q62uEBoCB9+RBOinb+eeE2y tpaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=/17mzegf8JZHibMrzQvxR5LoTlQRR3yQ0c1DUaT3IG0=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=ertJRKhErNWlNtAfLrKiSvWZDgscezqVqL7a9815PBcNhB4JeLkOYwyN4QuQwCCXtk qoijnBufNaJhLDaXX2UOez8JZvXU/YNRiO9E4jAViPig/9ZQDS5TTZhehQ4JO1+FbdGl 8ZkBM77IFOthNxApoy1OMRqVYJ8/FwhpY0ERdww2CTw3vCNRLymGCdQDIXF0ttO0FwJY oebJVLp5/abJULGfgy1K+VL+rZDtGo8d5NfDhKlP/+O4UFLfVwdJquodmrKyOpffTy6U j96imGjUKt28dnKi1W9Jd2gQWFoNpu3s7nTYixEga7O0uDFXlOP2orPbJO2h//FOkqkK cwRQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e14-20020a056a001a8e00b006665f0400a7si2569554pfv.197.2023.08.10.19.14.03; Thu, 10 Aug 2023 19:14:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232564AbjHKBh3 (ORCPT + 99 others); Thu, 10 Aug 2023 21:37:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232749AbjHKBh1 (ORCPT ); Thu, 10 Aug 2023 21:37:27 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08EAD2D66; Thu, 10 Aug 2023 18:37:27 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH5b-00053x-15; Fri, 11 Aug 2023 01:37:19 +0000 Date: Fri, 11 Aug 2023 02:37:12 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/8] mtd: ubi: block: don't return on error when removing Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773897050488310360 X-GMAIL-MSGID: 1773897050488310360 There is no point on returning the error from ubiblock_remove in case it is being called due to a volume removal event -- the volume is gone, we should destroy and remove the ubiblock device no matter what. Introduce new boolean parameter 'force' to tell ubiblock_remove to go on even in case the ubiblock device is still busy. Use that new option when calling ubiblock_remove due to a UBI_VOLUME_REMOVED event. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 6 +++--- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 437c5b83ffe51..69fa6fecb8494 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -456,7 +456,7 @@ static void ubiblock_cleanup(struct ubiblock *dev) idr_remove(&ubiblock_minor_idr, dev->gd->first_minor); } -int ubiblock_remove(struct ubi_volume_info *vi) +int ubiblock_remove(struct ubi_volume_info *vi, bool force) { struct ubiblock *dev; int ret; @@ -470,7 +470,7 @@ int ubiblock_remove(struct ubi_volume_info *vi) /* Found a device, let's lock it so we can check if it's busy */ mutex_lock(&dev->dev_mutex); - if (dev->refcnt > 0) { + if (dev->refcnt > 0 && !force) { ret = -EBUSY; goto out_unlock_dev; } @@ -545,7 +545,7 @@ static int ubiblock_notify(struct notifier_block *nb, */ break; case UBI_VOLUME_REMOVED: - ubiblock_remove(&nt->vi); + ubiblock_remove(&nt->vi, true); break; case UBI_VOLUME_RESIZED: ubiblock_resize(&nt->vi); diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c index f43430b9c1e65..bb55e863dd296 100644 --- a/drivers/mtd/ubi/cdev.c +++ b/drivers/mtd/ubi/cdev.c @@ -572,7 +572,7 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd, struct ubi_volume_info vi; ubi_get_volume_info(desc, &vi); - err = ubiblock_remove(&vi); + err = ubiblock_remove(&vi, false); break; } diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index c8f1bd4fa1008..44c0eeaf1e1b0 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -979,7 +979,7 @@ static inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {} int ubiblock_init(void); void ubiblock_exit(void); int ubiblock_create(struct ubi_volume_info *vi); -int ubiblock_remove(struct ubi_volume_info *vi); +int ubiblock_remove(struct ubi_volume_info *vi, bool force); #else static inline int ubiblock_init(void) { return 0; } static inline void ubiblock_exit(void) {} @@ -987,7 +987,7 @@ static inline int ubiblock_create(struct ubi_volume_info *vi) { return -ENOSYS; } -static inline int ubiblock_remove(struct ubi_volume_info *vi) +static inline int ubiblock_remove(struct ubi_volume_info *vi, bool force) { return -ENOSYS; } From patchwork Fri Aug 11 01:37:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134277 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp810441vqi; Thu, 10 Aug 2023 19:24:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHcLoP3rsSwDTxlS82XQkzF1FFK0rPZ5gqy4Rp0qFtu8Eh/vslNdyB0KEcqE885E615ZpQK X-Received: by 2002:a05:6300:808d:b0:140:94b8:3b70 with SMTP id ap13-20020a056300808d00b0014094b83b70mr707851pzc.17.1691720690647; Thu, 10 Aug 2023 19:24:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691720690; cv=none; d=google.com; s=arc-20160816; b=Rg0By6970OHUJY7tUc9QcnwGdkXuJTRCFcMmLtglBw4xrrQF4ZhRt35b18+9gjlxDF VKvTwQlqWElkWMgF/XUBdKfUZcDGq9RK4SdjLJBOhcsG1R9F0PPLhogi+pScwawPlKWd Em5zpKxBjepxKG4s4RheS+zCejgfb9bDc5lpMLRPhogCrLY4c8DM7GmODdkcmeQjjnyr qyivdoZlmHYSuGSyBPFtdJyTwaagdIkqo5km6fTeXzX7MP38DkIV2RDWvlqkWJYvCmkW gHilbgWzfoPWPIjNuPFXb+qTy8P1xP7t9xixrFydmwlUc178WTZi9W0FEI5DFp0luk4h ygRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=gyt4xAsIrq5txTp5OjpxkNoVi3HwSGUGngOZAnCrLkE=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=ZErtDNsmrj8R1saWFoS8B6OgZ3KnAJ3aMoIPOS21k/7DEBNgpeNWNwf3C8F1nAJGf1 N9gZcr+SbHQt4DcQFp/VrB5CkSbmjdNi3tsSd/CIpeWvIwnV6afm+0mj09uUkA+9TvWr zaeQeZqr3OL617n72LMSzpA9vpHxRfG3M1yJ+gHd+sjQtRmPEE3P06yNsy7q8qnLK7yg S8TfgVwNzcWqNuIxdrpn8/+/7Vyb/mOdPJDj/zwCMgstSZuAbTkBSGYA5+A/YGP4dtbL icMezyWXYr//bHUav8RNuU9otDfhvA2FWy1hJzYyjCMCnLVzRtqEj1ZDW1JLmt1IZKey fetQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y7-20020a170902b48700b001bb2093efb6si2418473plr.45.2023.08.10.19.24.37; Thu, 10 Aug 2023 19:24:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231823AbjHKBh4 (ORCPT + 99 others); Thu, 10 Aug 2023 21:37:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229659AbjHKBhy (ORCPT ); Thu, 10 Aug 2023 21:37:54 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 180C52D62; Thu, 10 Aug 2023 18:37:52 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH5u-00054L-0t; Fri, 11 Aug 2023 01:37:39 +0000 Date: Fri, 11 Aug 2023 02:37:31 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/8] mtd: ubi: block: use notifier to create ubiblock Message-ID: <1bb74a9feeeb64dcd94024fbfa3a8259fcdac53d.1691717480.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773897714599676155 X-GMAIL-MSGID: 1773897714599676155 Use UBI_VOLUME_ADDED notification to create ubiblock device specified on kernel cmdline or module parameter. This makes thing more simple and has the advantage that ubiblock devices on volumes which are not present at the time the ubi module is probed will still be created. Suggested-by: Zhihao Cheng Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 152 ++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 68 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 69fa6fecb8494..e0618bbde3613 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -65,10 +66,10 @@ struct ubiblock_pdu { }; /* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs __initdata; +static int ubiblock_devs; /* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES]; struct ubiblock { struct ubi_volume_desc *desc; @@ -532,6 +533,85 @@ static int ubiblock_resize(struct ubi_volume_info *vi) return 0; } +static bool +match_volume_desc(struct ubi_volume_info *vi, const char *name, int ubi_num, int vol_id) +{ + int err, len; + struct path path; + struct kstat stat; + + if (ubi_num == -1) { + /* No ubi num, name must be a vol device path */ + err = kern_path(name, LOOKUP_FOLLOW, &path); + if (err) + return false; + + err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT); + path_put(&path); + if (err) + return false; + + if (!S_ISCHR(stat.mode)) + return false; + + if (vi->ubi_num != ubi_major2num(MAJOR(stat.rdev))) + return false; + + if (vi->vol_id != MINOR(stat.rdev) - 1) + return false; + + return true; + } + + if (vol_id == -1) { + if (vi->ubi_num != ubi_num) + return false; + + len = strnlen(name, UBI_VOL_NAME_MAX + 1); + if (len < 1 || vi->name_len != len) + return false; + + if (strcmp(name, vi->name)) + return false; + + return true; + } + + if (vi->ubi_num != ubi_num) + return false; + + if (vi->vol_id != vol_id) + return false; + + return true; +} + +static void +ubiblock_create_from_param(struct ubi_volume_info *vi) +{ + int i, ret = 0; + struct ubiblock_param *p; + + /* + * Iterate over ubiblock cmdline parameters. If a parameter matches the + * newly added volume create the ubiblock device for it. + */ + for (i = 0; i < ubiblock_devs; i++) { + p = &ubiblock_param[i]; + + if (!match_volume_desc(vi, p->name, p->ubi_num, p->vol_id)) + continue; + + ret = ubiblock_create(vi); + if (ret) { + pr_err( + "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n", + vi->name, p->ubi_num, p->vol_id, ret); + } + break; + } +} + static int ubiblock_notify(struct notifier_block *nb, unsigned long notification_type, void *ns_ptr) { @@ -539,10 +619,7 @@ static int ubiblock_notify(struct notifier_block *nb, switch (notification_type) { case UBI_VOLUME_ADDED: - /* - * We want to enforce explicit block device creation for - * volumes, so when a volume is added we do nothing. - */ + ubiblock_create_from_param(&nt->vi); break; case UBI_VOLUME_REMOVED: ubiblock_remove(&nt->vi, true); @@ -568,56 +645,6 @@ static struct notifier_block ubiblock_notifier = { .notifier_call = ubiblock_notify, }; -static struct ubi_volume_desc * __init -open_volume_desc(const char *name, int ubi_num, int vol_id) -{ - if (ubi_num == -1) - /* No ubi num, name must be a vol device path */ - return ubi_open_volume_path(name, UBI_READONLY); - else if (vol_id == -1) - /* No vol_id, must be vol_name */ - return ubi_open_volume_nm(ubi_num, name, UBI_READONLY); - else - return ubi_open_volume(ubi_num, vol_id, UBI_READONLY); -} - -static void __init ubiblock_create_from_param(void) -{ - int i, ret = 0; - struct ubiblock_param *p; - struct ubi_volume_desc *desc; - struct ubi_volume_info vi; - - /* - * If there is an error creating one of the ubiblocks, continue on to - * create the following ubiblocks. This helps in a circumstance where - * the kernel command-line specifies multiple block devices and some - * may be broken, but we still want the working ones to come up. - */ - for (i = 0; i < ubiblock_devs; i++) { - p = &ubiblock_param[i]; - - desc = open_volume_desc(p->name, p->ubi_num, p->vol_id); - if (IS_ERR(desc)) { - pr_err( - "UBI: block: can't open volume on ubi%d_%d, err=%ld\n", - p->ubi_num, p->vol_id, PTR_ERR(desc)); - continue; - } - - ubi_get_volume_info(desc, &vi); - ubi_close_volume(desc); - - ret = ubiblock_create(&vi); - if (ret) { - pr_err( - "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n", - vi.name, p->ubi_num, p->vol_id, ret); - continue; - } - } -} - static void ubiblock_remove_all(void) { struct ubiblock *next; @@ -643,18 +670,7 @@ int __init ubiblock_init(void) if (ubiblock_major < 0) return ubiblock_major; - /* - * Attach block devices from 'block=' module param. - * Even if one block device in the param list fails to come up, - * still allow the module to load and leave any others up. - */ - ubiblock_create_from_param(); - - /* - * Block devices are only created upon user requests, so we ignore - * existing volumes. - */ - ret = ubi_register_volume_notifier(&ubiblock_notifier, 1); + ret = ubi_register_volume_notifier(&ubiblock_notifier, 0); if (ret) goto err_unreg; return 0; From patchwork Fri Aug 11 01:37:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134281 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp813816vqi; Thu, 10 Aug 2023 19:35:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGBPGrOv224qrA5GiBu/g81+IrdDUkZ8nmVM/0+FBBhZR07gYokiMD5GkOPfgnq4a1pDdtu X-Received: by 2002:a05:6402:514a:b0:523:38b1:9bbc with SMTP id n10-20020a056402514a00b0052338b19bbcmr503282edd.41.1691721308392; Thu, 10 Aug 2023 19:35:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691721308; cv=none; d=google.com; s=arc-20160816; b=DPXFS9bUJ+rvvIHUiW0/skCLOgiZSDd+D1Fi5KokaSeeeuja03l/6vaf1lmh0KUbbs 94+jBE+b4TQdXJs9H2WDXMwM9CnJ+DJGewP7YkevREwVSLW2LEi/h24hk8Z9WOBFANCu rcpKRENWTyFeyE9e4I/0QOGz+nXJUP1jCX6EsSEvWQx2zsP9KEE7Xr8vnu7+mAvF/jbJ qs68USjMoOhxBTmsJjs7uADHtVGsUlZ0TsfolRP9n08GqL39g5cxA/8RWK8PzTN79AqH +CMe04nnTk0P9xBNnHK1KUKgg/NA79lfvz8fh3/P7aC6JdYXNRYDfIwE3bg4T3QW4eQZ VLUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=p82vAH+RXovqt2uw8K4mlLa9xvc65TYjnJmLTkDdKsg=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=JT+5buqeJ7lN0JTxALZKXrpvTqstIbgfzqI1cAxVRWMJZlGnfgQ4nGBMHO3rTsdPfR oGEt1Sy9ZzcoEx8Tpa5EDmC0og4tEe6JDhglqIeGphPJmX0w6zuXQ9taKmJU9adn/9r9 /mJl5E4MOrYN+iRibfQSw3WAcN6+IFeh7VnZ94nCLSBsGqTDyRrRzylETEnTNejB/2B7 DTnULTLBAE7IllrYskQNNmh2drNnQUU3PkNrExBfnurzbVz6in9nQ6Dww6GRABvjyNii mFgIIjCVF7Mh55+xvaFP5UKnSlc6Nt6crqAVuX7E/kNZyEnPU4KP21eIzktSF797WlF/ pcDA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h19-20020aa7c613000000b00523372ace05si2434023edq.530.2023.08.10.19.34.44; Thu, 10 Aug 2023 19:35:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232795AbjHKBiL (ORCPT + 99 others); Thu, 10 Aug 2023 21:38:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231285AbjHKBiK (ORCPT ); Thu, 10 Aug 2023 21:38:10 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81675196; Thu, 10 Aug 2023 18:38:09 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH6C-00055K-0m; Fri, 11 Aug 2023 01:37:57 +0000 Date: Fri, 11 Aug 2023 02:37:49 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 5/8] mtd: ubi: attach MTD partition from device-tree Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773898362427599622 X-GMAIL-MSGID: 1773898362427599622 Split ubi_init() function into early function to be called by device_initcall() and keep cmdline attachment in late_initcall(). (when building ubi as module, both is still done in a single module_init() call) Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed. For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed. For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time. Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g. drivers/nvmem/u-boot-env.c can now be extended to support U-Boot environment stored in UBI volumes. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 2 +- drivers/mtd/ubi/build.c | 153 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 2 +- 4 files changed, 115 insertions(+), 44 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index e0618bbde3613..99b5f502c9dbc 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -470,7 +470,7 @@ int ubiblock_remove(struct ubi_volume_info *vi, bool force) } /* Found a device, let's lock it so we can check if it's busy */ - mutex_lock(&dev->dev_mutex); + mutex_lock_nested(&dev->dev_mutex, SINGLE_DEPTH_NESTING); if (dev->refcnt > 0 && !force) { ret = -EBUSY; goto out_unlock_dev; diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 8b91a55ec0d28..c153373c13dab 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, * ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero + * @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, * Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev(int ubi_num, int anyway) +int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock) { struct ubi_device *ubi; @@ -1111,6 +1113,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) if (!ubi_dbg_chk_fastmap(ubi)) ubi_update_fastmap(ubi); #endif + /* * Before freeing anything, we have to stop the background thread to * prevent it from doing anything on this device while we are freeing. @@ -1130,7 +1133,11 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) vfree(ubi->peb_buf); vfree(ubi->fm_buf); ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index); - put_mtd_device(ubi->mtd); + if (have_lock) + __put_mtd_device(ubi->mtd); + else + put_mtd_device(ubi->mtd); + put_device(&ubi->dev); return 0; } @@ -1207,43 +1214,51 @@ static struct mtd_info * __init open_mtd_device(const char *mtd_dev) return mtd; } -static int __init ubi_init(void) +static void ubi_notify_add(struct mtd_info *mtd) { - int err, i, k; + struct device_node *np = mtd_get_of_node(mtd); + int err; - /* Ensure that EC and VID headers have correct size */ - BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64); - BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64); + if (!of_device_is_compatible(np, "linux,ubi")) + return; - if (mtd_devs > UBI_MAX_DEVICES) { - pr_err("UBI error: too many MTD devices, maximum is %d\n", - UBI_MAX_DEVICES); - return -EINVAL; - } + /* + * we are already holding &mtd_table_mutex, but still need + * to bump refcount + */ + err = __get_mtd_device(mtd); + if (err) + return; - /* Create base sysfs directory and sysfs files */ - err = class_register(&ubi_class); + /* called while holding mtd_table_mutex */ + mutex_lock_nested(&ubi_devices_mutex, SINGLE_DEPTH_NESTING); + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0, false); + mutex_unlock(&ubi_devices_mutex); if (err < 0) - return err; + __put_mtd_device(mtd); +} - err = misc_register(&ubi_ctrl_cdev); - if (err) { - pr_err("UBI error: cannot register device\n"); - goto out; - } +static void ubi_notify_remove(struct mtd_info *mtd) +{ + int i; - ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab", - sizeof(struct ubi_wl_entry), - 0, 0, NULL); - if (!ubi_wl_entry_slab) { - err = -ENOMEM; - goto out_dev_unreg; - } + /* called while holding mtd_table_mutex */ + mutex_lock_nested(&ubi_devices_mutex, SINGLE_DEPTH_NESTING); + for (i = 0; i < UBI_MAX_DEVICES; i++) + if (ubi_devices[i] && + ubi_devices[i]->mtd->index == mtd->index) + ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1, true); + mutex_unlock(&ubi_devices_mutex); +} - err = ubi_debugfs_init(); - if (err) - goto out_slab; +static struct mtd_notifier ubi_mtd_notifier = { + .add = ubi_notify_add, + .remove = ubi_notify_remove, +}; +static int __init ubi_init_attach(void) +{ + int err, i, k; /* Attach MTD devices */ for (i = 0; i < mtd_devs; i++) { @@ -1291,25 +1306,79 @@ static int __init ubi_init(void) } } + return 0; + +out_detach: + for (k = 0; k < i; k++) + if (ubi_devices[k]) { + mutex_lock(&ubi_devices_mutex); + ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1, false); + mutex_unlock(&ubi_devices_mutex); + } + return err; +} +#ifndef CONFIG_MTD_UBI_MODULE +late_initcall(ubi_init_attach); +#endif + +static int __init ubi_init(void) +{ + int err; + + /* Ensure that EC and VID headers have correct size */ + BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64); + BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64); + + if (mtd_devs > UBI_MAX_DEVICES) { + pr_err("UBI error: too many MTD devices, maximum is %d\n", + UBI_MAX_DEVICES); + return -EINVAL; + } + + /* Create base sysfs directory and sysfs files */ + err = class_register(&ubi_class); + if (err < 0) + return err; + + err = misc_register(&ubi_ctrl_cdev); + if (err) { + pr_err("UBI error: cannot register device\n"); + goto out; + } + + ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab", + sizeof(struct ubi_wl_entry), + 0, 0, NULL); + if (!ubi_wl_entry_slab) { + err = -ENOMEM; + goto out_dev_unreg; + } + + err = ubi_debugfs_init(); + if (err) + goto out_slab; + err = ubiblock_init(); if (err) { pr_err("UBI error: block: cannot initialize, error %d\n", err); /* See comment above re-ubi_is_module(). */ if (ubi_is_module()) - goto out_detach; + goto out_slab; + } + + register_mtd_user(&ubi_mtd_notifier); + + if (ubi_is_module()) { + err = ubi_init_attach(); + if (err) + goto out_mtd_notifier; } return 0; -out_detach: - for (k = 0; k < i; k++) - if (ubi_devices[k]) { - mutex_lock(&ubi_devices_mutex); - ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1); - mutex_unlock(&ubi_devices_mutex); - } - ubi_debugfs_exit(); +out_mtd_notifier: + unregister_mtd_user(&ubi_mtd_notifier); out_slab: kmem_cache_destroy(ubi_wl_entry_slab); out_dev_unreg: @@ -1319,18 +1388,20 @@ static int __init ubi_init(void) pr_err("UBI error: cannot initialize UBI, error %d\n", err); return err; } -late_initcall(ubi_init); +device_initcall(ubi_init); + static void __exit ubi_exit(void) { int i; ubiblock_exit(); + unregister_mtd_user(&ubi_mtd_notifier); for (i = 0; i < UBI_MAX_DEVICES; i++) if (ubi_devices[i]) { mutex_lock(&ubi_devices_mutex); - ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1); + ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1, false); mutex_unlock(&ubi_devices_mutex); } ubi_debugfs_exit(); diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c index bb55e863dd296..0ba6aa6a2e11d 100644 --- a/drivers/mtd/ubi/cdev.c +++ b/drivers/mtd/ubi/cdev.c @@ -1065,7 +1065,7 @@ static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd, } mutex_lock(&ubi_devices_mutex); - err = ubi_detach_mtd_dev(ubi_num, 0); + err = ubi_detach_mtd_dev(ubi_num, 0, false); mutex_unlock(&ubi_devices_mutex); break; } diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index 44c0eeaf1e1b0..54093858f3385 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -939,7 +939,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset, int max_beb_per1024, bool disable_fm); -int ubi_detach_mtd_dev(int ubi_num, int anyway); +int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock); struct ubi_device *ubi_get_device(int ubi_num); void ubi_put_device(struct ubi_device *ubi); struct ubi_device *ubi_get_by_major(int major); From patchwork Fri Aug 11 01:38:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134292 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp825629vqi; Thu, 10 Aug 2023 20:11:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLjlSczLrYex2vtZyNQ43x/vzz+XWpb3Z7VKGGOZ0619S79RYA47AobLtqzV14x/GMD0Mf X-Received: by 2002:a05:6a00:4690:b0:64d:42b9:6895 with SMTP id de16-20020a056a00469000b0064d42b96895mr4369386pfb.5.1691723515430; Thu, 10 Aug 2023 20:11:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691723515; cv=none; d=google.com; s=arc-20160816; b=Nbm06wm5xQ2+pORYa0oqjc0T4Mzhbi9cpGn+b1bF5jGXZz+1qzFyCWpf8OXz4L0C/v txDLqLKE4ogdoVyauywKlh7uZBxr3kKk8wfCW9V0BSbK3s1kLL7asILUmQZMeZ+l7LmS BFmvu1yrT641gOUta0GeKC63G9zeZb6gb5VzXscjqWonQPHVpZ48DZ1ZBDsu/iNd8Pu1 xS0ibAjjwFzDcppsnLjDDgyOh+ZT8y8j+XESsW53z/u4bOi/kRlXE8t5AalXdrbIpoDM 6vSnOoLIoswzcOz0odVQ+54iTOhO/oaEq73iBI3uXjL6+nGxUVIdn5vdjRN1ymEJWQwm YNYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=2ZMFvnqg2YpdkSzv6vBNptDi787ZNGsdqaeBN9c5mX8=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=v5kZzNK0AuP1j0xkcNf9UXVjMFRuiU+kztDdB8/NghE4JdpOP63vj1vAALro3dBEc/ I7jImkaNjWzVj6ISmLNdNh9ZUI1/d+JypmgP/HBkPM5jiJeGWYljhn6X8KdqAjJhRbgQ svctMVCJ0mksNvIUc2zsgpAjuUNn0aLvIBPWbhiS9siPhdNOMdfKaH9irl1+5+fT5b6V uq1y6n8Z732zaO3+6cSnkgh5eK6KewYTQmF+s6fmB1AE68Dtf7pKbOzWrXDENSM+QsHD qZwDhwNiik60bNaXUkggfvMhVQmOvx5jSgxhqDy4KJtR/1CtDxMVDi9GBZH0jrh8/bFK q5MA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y7-20020a056a00180700b00682909b9671si2699714pfa.75.2023.08.10.20.11.41; Thu, 10 Aug 2023 20:11:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232879AbjHKBiW (ORCPT + 99 others); Thu, 10 Aug 2023 21:38:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231285AbjHKBiU (ORCPT ); Thu, 10 Aug 2023 21:38:20 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C6492123; Thu, 10 Aug 2023 18:38:20 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH6S-00055e-0g; Fri, 11 Aug 2023 01:38:12 +0000 Date: Fri, 11 Aug 2023 02:38:05 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 6/8] mtd: ubi: introduce pre-removal notification for UBI volumes Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773900677142329201 X-GMAIL-MSGID: 1773900677142329201 Introduce a new notification type UBI_VOLUME_SHUTDOWN to inform users that a volume is just about to be removed. This is needed because users (such as the NVMEM subsystem) expect that at the time their removal function is called, the parenting device is still available (for removal of sysfs nodes, for example, in case of NVMEM which otherwise WARNs on volume removal). Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 26 ++++++++++++++++++++++++++ drivers/mtd/ubi/build.c | 7 ++++++- drivers/mtd/ubi/vmt.c | 5 +++++ include/linux/mtd/ubi.h | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 99b5f502c9dbc..1d5148371991b 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -533,6 +533,29 @@ static int ubiblock_resize(struct ubi_volume_info *vi) return 0; } +static int ubiblock_shutdown(struct ubi_volume_info *vi) +{ + struct ubiblock *dev; + struct gendisk *disk; + int ret = 0; + + mutex_lock(&devices_mutex); + dev = find_dev_nolock(vi->ubi_num, vi->vol_id); + if (!dev) { + ret = -ENODEV; + goto out_unlock; + } + disk = dev->gd; + +out_unlock: + mutex_unlock(&devices_mutex); + + if (!ret) + blk_mark_disk_dead(disk); + + return ret; +}; + static bool match_volume_desc(struct ubi_volume_info *vi, const char *name, int ubi_num, int vol_id) { @@ -624,6 +647,9 @@ static int ubiblock_notify(struct notifier_block *nb, case UBI_VOLUME_REMOVED: ubiblock_remove(&nt->vi, true); break; + case UBI_VOLUME_SHUTDOWN: + ubiblock_shutdown(&nt->vi); + break; case UBI_VOLUME_RESIZED: ubiblock_resize(&nt->vi); break; diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index c153373c13dab..ccee4a28ffe97 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -1088,7 +1088,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock) return -EINVAL; spin_lock(&ubi_devices_lock); - put_device(&ubi->dev); ubi->ref_count -= 1; if (ubi->ref_count) { if (!anyway) { @@ -1099,6 +1098,12 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock) ubi_err(ubi, "%s reference count %d, destroy anyway", ubi->ubi_name, ubi->ref_count); } + spin_unlock(&ubi_devices_lock); + + ubi_notify_all(ubi, UBI_VOLUME_SHUTDOWN, NULL); + + spin_lock(&ubi_devices_lock); + put_device(&ubi->dev); ubi_devices[ubi_num] = NULL; spin_unlock(&ubi_devices_lock); diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index 2c867d16f89f7..eed4b57c61bda 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -352,6 +352,11 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) err = -EBUSY; goto out_unlock; } + spin_unlock(&ubi->volumes_lock); + + ubi_volume_notify(ubi, vol, UBI_VOLUME_SHUTDOWN); + + spin_lock(&ubi->volumes_lock); ubi->volumes[vol_id] = NULL; spin_unlock(&ubi->volumes_lock); diff --git a/include/linux/mtd/ubi.h b/include/linux/mtd/ubi.h index a529347fd75b2..562f92504f2b7 100644 --- a/include/linux/mtd/ubi.h +++ b/include/linux/mtd/ubi.h @@ -192,6 +192,7 @@ struct ubi_device_info { * or a volume was removed) * @UBI_VOLUME_RESIZED: a volume has been re-sized * @UBI_VOLUME_RENAMED: a volume has been re-named + * @UBI_VOLUME_SHUTDOWN: a volume is going to removed, shutdown users * @UBI_VOLUME_UPDATED: data has been written to a volume * * These constants define which type of event has happened when a volume @@ -202,6 +203,7 @@ enum { UBI_VOLUME_REMOVED, UBI_VOLUME_RESIZED, UBI_VOLUME_RENAMED, + UBI_VOLUME_SHUTDOWN, UBI_VOLUME_UPDATED, }; From patchwork Fri Aug 11 01:38:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134276 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp810072vqi; Thu, 10 Aug 2023 19:23:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEAqZXg0eKYatidyEQYLMjokutLOEoOzJZF6Z7UT4y+X2BgPSCOWxSdxXekMTbldk5ba07b X-Received: by 2002:a05:6808:1144:b0:3a7:20a9:ae8e with SMTP id u4-20020a056808114400b003a720a9ae8emr793388oiu.21.1691720616867; Thu, 10 Aug 2023 19:23:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691720616; cv=none; d=google.com; s=arc-20160816; b=qbf6mco9l4z27Ii2J6jnHXJKnU/GxF/2GSFz7HrWgGgBzHg8pRHYW0sPma+AhSkfXL CyrvEF06wQWv1Ag+wMtieWX59jd8KIkg7WOtO2EZatzffYUXX1zLPMl7oKolAAdIlfyd zXDJ2YOR+HuwBv+lwK07reLx/OoXTLIUwUR1/AKQV5/HxaCi9EroXkevhTkTPxt2VRq4 QvS2lcCl51ikSv8X4Ohiyc9Udva7LrdbSe3HwmKkUjdOk1uw01U+oUkTfgtYRlekEn5/ 4HHsKl+PhkD0qoQ3YnD4f60i8whhbgSGGjrlLoT8ZoSP4bHN0r8zvWRYIHtuOiem3wlc /ySA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=J/nVLXYo3NncRPaPPmeGJWUENvGn6ZHbNlENk8zMnR4=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=DszTOeDY+neaRTlK5S8Qs5YFqsOxBR9lM9IhfdifoIe4xeO9su9OYGC2OH0/FUBOOX VzaEjgu9qv0y84OVAaYShNrZZkWTxLo7XgQmegFLXQxD71LMbYvyfRCiQ8V6+9pWUWTd Lodu5MrUh5jyjv2EG2T+kMqIdBpjIa/sQzMZrrRUuVrSdwRSt71yMoUo+YpPgC+nkhRt fSVgXddsQekgeNyiw7+D+DC6yb8Dgys9oqIxUA0HXeG8l97s2dezFvDliJyhT01/oFNv p3F41UYl8oCcs5ofF+mgOCu6Ya37xhIU4Ti8KgRSq3sTtQ+ogL+ByyNcFKZ/6cyYsj8b wgog== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e3-20020a636903000000b005578994f212si2537084pgc.425.2023.08.10.19.23.23; Thu, 10 Aug 2023 19:23:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232898AbjHKBik (ORCPT + 99 others); Thu, 10 Aug 2023 21:38:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231285AbjHKBij (ORCPT ); Thu, 10 Aug 2023 21:38:39 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C634010D; Thu, 10 Aug 2023 18:38:38 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH6h-000569-34; Fri, 11 Aug 2023 01:38:28 +0000 Date: Fri, 11 Aug 2023 02:38:20 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 7/8] mtd: ubi: populate ubi volume fwnode Message-ID: <1faf99d4ae75fa9ac2b36e18fb71075469f246e1.1691717480.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773897637330726376 X-GMAIL-MSGID: 1773897637330726376 Look for the 'volumes' subnode of an MTD partition attached to a UBI device and attach matching child nodes to UBI volumes. This allows UBI volumes to be referenced in device tree, e.g. for use as NVMEM providers. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/vmt.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index eed4b57c61bda..9db845a43ecfc 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -124,6 +124,31 @@ static void vol_release(struct device *dev) kfree(vol); } +static struct fwnode_handle *find_volume_fwnode(struct ubi_volume *vol) +{ + struct fwnode_handle *fw_vols, *fw_vol; + const char *volname; + u32 volid; + + fw_vols = device_get_named_child_node(vol->dev.parent->parent, "volumes"); + if (!fw_vols) + return NULL; + + fwnode_for_each_child_node(fw_vols, fw_vol) { + if (!fwnode_property_read_string(fw_vol, "volume-name", &volname) && + strncmp(volname, vol->name, vol->name_len)) + continue; + + if (!fwnode_property_read_u32(fw_vol, "volume-id", &volid) && + vol->vol_id != volid) + continue; + + return fw_vol; + } + + return NULL; +} + /** * ubi_create_volume - create volume. * @ubi: UBI device description object @@ -223,6 +248,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) vol->name_len = req->name_len; memcpy(vol->name, req->name, vol->name_len); vol->ubi = ubi; + device_set_node(&vol->dev, find_volume_fwnode(vol)); /* * Finish all pending erases because there may be some LEBs belonging @@ -597,6 +623,7 @@ int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) vol->dev.class = &ubi_class; vol->dev.groups = volume_dev_groups; dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id); + device_set_node(&vol->dev, find_volume_fwnode(vol)); err = device_register(&vol->dev); if (err) { cdev_del(&vol->cdev); From patchwork Fri Aug 11 01:38:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 134287 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp820086vqi; Thu, 10 Aug 2023 19:56:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG79DcPhz8BHEmet3Hh/QP0hvBDHzmZLyQ4r2RgzmAmu8TBmOdBI0IN8CAEtP8g1Uc/P7/0 X-Received: by 2002:aa7:d681:0:b0:51d:ece5:afd9 with SMTP id d1-20020aa7d681000000b0051dece5afd9mr563894edr.21.1691722575302; Thu, 10 Aug 2023 19:56:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691722575; cv=none; d=google.com; s=arc-20160816; b=Cb0/sBUQOHTkTfAh94jjgp+hnrgLUsMRTuycf5/daQ+LMms43bG6xdOKBm81RntGHW 4hNSnRxNZjOx5tdVdD3tEXYP3+faBi59/ErBBYtRxOiKHTfnhaN5Kc3X7uKFujXsHK6s Bx3Ejoby5Ie/KIqhxu6o+odiQX3JAmWqTDe2wR0mw11DXfxTGvWDrkoTdx5enDe8J2N8 HPglNZmIyoTAoqB/WJ3POMXniJEotkcNlllOCOFBhME5K/clhvj8Dw8/3Dotp9hxcYEa hM8Qi3BdmmK9so4hLq7zVncvvbFYNhtU1WS11WZzbOhAmAeIY6kMQJtFS715a6n/EmDC 7+kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:to:from:date; bh=p4j6vuXTezXUG8MbvCblDOrf8b5MGG/Exb4xeyp9Ero=; fh=fqW8oMY9ydixFpWZdFhmrmd4m3lvFBw58fIoJyq/l8o=; b=kbdpxc5TBJ48l4ng6ZSQcs2RTPWSXFQTR7lJaPPHPZrJs8uCKmGfXSBwuOID6NOK8g msHEJtSf5Cy6qTFAncUqqGWOCb+Cy66bgy5jHiMMxrhHpzNh2HV7vnU7KdWMP+mirRAZ cw2BbCG4S0hml9dyIW90QNzF0mQW+EwAm4d3krqPZNfabnuxDCvGktpfXShzNsopSNFI ofjoLuuz2rksuOXtSlLw9o1t+bzqTlywf+RhSDjy5vVXq6P/YuO7z5qHEnsAtnYUEShw MpPUEOSpdVjZFpU2BU/9k3t0wOFtaDu3YaGALXOrUsAm5RwGcx5pSsiBpfDYBt3Skrsp CpaQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z21-20020aa7d415000000b005222d16f2d4si2515725edq.533.2023.08.10.19.55.42; Thu, 10 Aug 2023 19:56:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233007AbjHKBjB (ORCPT + 99 others); Thu, 10 Aug 2023 21:39:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232970AbjHKBi6 (ORCPT ); Thu, 10 Aug 2023 21:38:58 -0400 Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E16592690; Thu, 10 Aug 2023 18:38:53 -0700 (PDT) Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1qUH6v-00056Z-2f; Fri, 11 Aug 2023 01:38:42 +0000 Date: Fri, 11 Aug 2023 02:38:34 +0100 From: Daniel Golle To: Randy Dunlap , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Daniel Golle , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 8/8] mtd: ubi: provide NVMEM layer over UBI volumes Message-ID: <431de44b006c4c90f54cd3c7378b395369cbea30.1691717480.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773899691177545647 X-GMAIL-MSGID: 1773899691177545647 In an ideal world we would like UBI to be used where ever possible on a NAND chip. And with UBI support in ARM Trusted Firmware and U-Boot it is possible to achieve an (almost-)all-UBI flash layout. Hence the need for a way to also use UBI volumes to store board-level constants, such as MAC addresses and calibration data of wireless interfaces. Add UBI volume NVMEM driver module exposing UBI volumes as NVMEM providers. Allow UBI devices to have a "volumes" firmware subnode with volumes which may be compatible with "nvmem-cells". Access to UBI volumes via the NVMEM interface at this point is read-only, and it is slow, opening and closing the UBI volume for each access due to limitations of the NVMEM provider API. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/Kconfig | 12 +++ drivers/mtd/ubi/Makefile | 1 + drivers/mtd/ubi/nvmem.c | 189 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 drivers/mtd/ubi/nvmem.c diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig index 2ed77b7b3fcb5..45d939bbfa853 100644 --- a/drivers/mtd/ubi/Kconfig +++ b/drivers/mtd/ubi/Kconfig @@ -104,4 +104,16 @@ config MTD_UBI_BLOCK If in doubt, say "N". +config MTD_UBI_NVMEM + tristate "UBI virtual NVMEM" + default n + depends on NVMEM + help + This option enabled an additional driver exposing UBI volumes as NVMEM + providers, intended for platforms where UBI is part of the firmware + specification and used to store also e.g. MAC addresses or board- + specific Wi-Fi calibration data. + + If in doubt, say "N". + endif # MTD_UBI diff --git a/drivers/mtd/ubi/Makefile b/drivers/mtd/ubi/Makefile index 543673605ca72..4b51aaf00d1a2 100644 --- a/drivers/mtd/ubi/Makefile +++ b/drivers/mtd/ubi/Makefile @@ -7,3 +7,4 @@ ubi-$(CONFIG_MTD_UBI_FASTMAP) += fastmap.o ubi-$(CONFIG_MTD_UBI_BLOCK) += block.o obj-$(CONFIG_MTD_UBI_GLUEBI) += gluebi.o +obj-$(CONFIG_MTD_UBI_NVMEM) += nvmem.o diff --git a/drivers/mtd/ubi/nvmem.c b/drivers/mtd/ubi/nvmem.c new file mode 100644 index 0000000000000..dd7cc6afb8d00 --- /dev/null +++ b/drivers/mtd/ubi/nvmem.c @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2023 Daniel Golle + */ + +/* UBI NVMEM provider */ +#include "ubi.h" +#include + +/* List of all NVMEM devices */ +static LIST_HEAD(nvmem_devices); +static DEFINE_MUTEX(devices_mutex); + +struct ubi_nvmem { + struct nvmem_device *nvmem; + int ubi_num; + int vol_id; + int usable_leb_size; + struct list_head list; +}; + +static int ubi_nvmem_reg_read(void *priv, unsigned int from, + void *val, size_t bytes) +{ + struct ubi_nvmem *unv = priv; + struct ubi_volume_desc *desc; + int err = 0, lnum, offs, bytes_left; + size_t to_read; + + desc = ubi_open_volume(unv->ubi_num, unv->vol_id, UBI_READONLY); + if (IS_ERR(desc)) + return PTR_ERR(desc); + + lnum = div_u64_rem(from, unv->usable_leb_size, &offs); + bytes_left = bytes; + while (bytes_left) { + to_read = unv->usable_leb_size - offs; + + if (to_read > bytes_left) + to_read = bytes_left; + + err = ubi_read(desc, lnum, val, offs, to_read); + if (err) + break; + + lnum += 1; + offs = 0; + bytes_left -= to_read; + val += to_read; + } + ubi_close_volume(desc); + + if (err) + return err; + + return bytes_left == 0 ? 0 : -EIO; +} + +static int ubi_nvmem_add(struct ubi_volume_info *vi) +{ + struct nvmem_config config = {}; + struct ubi_nvmem *unv; + int ret; + + if (!device_is_compatible(vi->dev, "nvmem-cells")) + return 0; + + unv = kzalloc(sizeof(struct ubi_nvmem), GFP_KERNEL); + if (!unv) + return -ENOMEM; + + config.id = NVMEM_DEVID_NONE; + config.dev = vi->dev; + config.name = dev_name(vi->dev); + config.owner = THIS_MODULE; + config.priv = unv; + config.reg_read = ubi_nvmem_reg_read; + config.size = vi->usable_leb_size * vi->size; + config.word_size = 1; + config.stride = 1; + config.read_only = true; + config.root_only = true; + config.ignore_wp = true; + config.of_node = dev_of_node(vi->dev); + + if (!config.of_node) + config.no_of_node = true; + + unv->ubi_num = vi->ubi_num; + unv->vol_id = vi->vol_id; + unv->usable_leb_size = vi->usable_leb_size; + unv->nvmem = nvmem_register(&config); + if (IS_ERR(unv->nvmem)) { + /* Just ignore if there is no NVMEM support in the kernel */ + if (PTR_ERR(unv->nvmem) == -EOPNOTSUPP) + ret = 0; + else + ret = dev_err_probe(vi->dev, PTR_ERR(unv->nvmem), + "Failed to register NVMEM device\n"); + + kfree(unv); + return ret; + } + + mutex_lock(&devices_mutex); + list_add_tail(&unv->list, &nvmem_devices); + mutex_unlock(&devices_mutex); + + return 0; +} + +static void ubi_nvmem_remove(struct ubi_volume_info *vi) +{ + struct ubi_nvmem *unv_c, *unv = NULL; + + mutex_lock(&devices_mutex); + list_for_each_entry(unv_c, &nvmem_devices, list) + if (unv_c->ubi_num == vi->ubi_num && unv_c->vol_id == vi->vol_id) { + unv = unv_c; + break; + } + + if (!unv) { + mutex_unlock(&devices_mutex); + return; + } + + list_del(&unv->list); + mutex_unlock(&devices_mutex); + nvmem_unregister(unv->nvmem); + kfree(unv); +} + +/** + * nvmem_notify - UBI notification handler. + * @nb: registered notifier block + * @l: notification type + * @ns_ptr: pointer to the &struct ubi_notification object + */ +static int nvmem_notify(struct notifier_block *nb, unsigned long l, + void *ns_ptr) +{ + struct ubi_notification *nt = ns_ptr; + + switch (l) { + case UBI_VOLUME_RESIZED: + ubi_nvmem_remove(&nt->vi); + fallthrough; + case UBI_VOLUME_ADDED: + ubi_nvmem_add(&nt->vi); + break; + case UBI_VOLUME_SHUTDOWN: + ubi_nvmem_remove(&nt->vi); + break; + default: + break; + } + return NOTIFY_OK; +} + +static struct notifier_block nvmem_notifier = { + .notifier_call = nvmem_notify, +}; + +static int __init ubi_nvmem_init(void) +{ + return ubi_register_volume_notifier(&nvmem_notifier, 0); +} + +static void __exit ubi_nvmem_exit(void) +{ + struct ubi_nvmem *unv, *tmp; + + mutex_lock(&devices_mutex); + list_for_each_entry_safe(unv, tmp, &nvmem_devices, list) { + nvmem_unregister(unv->nvmem); + list_del(&unv->list); + kfree(unv); + } + mutex_unlock(&devices_mutex); + + ubi_unregister_volume_notifier(&nvmem_notifier); +} + +module_init(ubi_nvmem_init); +module_exit(ubi_nvmem_exit); +MODULE_DESCRIPTION("NVMEM layer over UBI volumes"); +MODULE_AUTHOR("Daniel Golle"); +MODULE_LICENSE("GPL");