From patchwork Mon Feb 26 00:22:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 206150 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1796091dyb; Sun, 25 Feb 2024 16:23:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWry2U8BtHk8+UXwsAmX9+MxoxRc5dxU/W+jBXNqjylexmxvqE/Tm+a+m+wxcYZ6fjnlxxI4gHo4YeOhbKPGGA/pN9NWw== X-Google-Smtp-Source: AGHT+IHqFSriHan0XJdPBMwByqG6wW5N4ArS/cVDsHGs6flI4qb3INVbWLfXcLlvwh9p3Wc9UAxm X-Received: by 2002:a05:6a21:8cc5:b0:1a0:debc:e9d9 with SMTP id ta5-20020a056a218cc500b001a0debce9d9mr7290011pzb.47.1708907008273; Sun, 25 Feb 2024 16:23:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907008; cv=pass; d=google.com; s=arc-20160816; b=HXx7D2DDRnyB3joEpOxQiWIbzBtAkG0iyuo8cs4tgtb8E9d/SHC3mViaP1U0ncBv3T absq30bhh6QNDlb0JoVQFiCClSi9jyrdQ69ehKE/iQIO+1G6pQBlrvtDI+6nkihnZ8N8 wYw/XqA32og44WJfTuIpqhQmUIG13AnnyqWGXW4bcUvabNmYFXFI7dc2+SdaGBM3PaFj gfDZgFj5y3FJNE+FzW2qnU+XH59DrzlULQmv67fre/NIUmp9O93PeoR/gu/8QCwyHl1Q Jh4gvRC76eH5IuZhR7XvTTjioLDAAwQ25yUXQxe1sdlCKG8BWuv+JSjmrTX97ntp+B/D DJkg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:to :from:date; bh=K897jtd7agDoAyjFfuSzLu4uK9t4wqV1HleoEHOHRwM=; fh=iCY7pZACTeEM/aV1VSRdKj3GWTB74ZtqN+MwTbm3QUU=; b=IHY8qv3+Eeb+QHCxhgtxWXzo8XhsMVJ38ZRuEVMY2hcuzSfYXSAUfeUbSh5G0WBiI+ s6dKUEOasy6e+j7e8UNDVd5tJs2iCMoaME9UotgVyn3kfu7tpPrcz5rD6gHzID85wyDV a28s6LdBo0UEfo1yRM+G4nkMtZe35VNWR7y2hpnqFKU9fZlJ/rSRs9ylb0DfTp9orEzB 7WiHywsXY3jse/+8c1TUZ3y1u0q08Nq661t037PgjzY3oMXt8q0MtiF4/qnLPLEXfz1x jeQUGcw3Um6367ZZLNJaqxf3ucGJG5+edzcAXlFak/Ts9ZEcGWvocbr5u5A4RaHpf015 wIBg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80355-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80355-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id fu13-20020a17090ad18d00b0029897526522si4617265pjb.103.2024.02.25.16.23.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:23:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80355-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80355-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80355-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 29CB9B20FD8 for ; Mon, 26 Feb 2024 00:23:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E64BAEAF0; Mon, 26 Feb 2024 00:23:00 +0000 (UTC) Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B72B263CF; Mon, 26 Feb 2024 00:22:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.142.180.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708906979; cv=none; b=hd0y2R7Eush67pqpZoxChdOtezzt33n27RrQsbPMLqvjlzX35cjOnZdnaAax0AfVYgfpGW+I88Zu322uxPPpkRbH3VmEiX7/BywRGY49DeUMA5FV/bwjvsFCGUMtGdf5HQGUAll2nuWCkOelmOEQtWxTlG3kHiKt+XtnnGQQSb4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708906979; c=relaxed/simple; bh=AgDEO5cuzyw+bS553r/f/Vla8GBJw1Usm5KqzQ0CBMw=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lMwBjK/nHQc7yj4wF22yj9R3TfROnUYwdBzpsojWdK1cyzyw1BKIfipNjsPkQTiNlOuOOWZqvuuCQxc9i44nmNL4oR6kd3E8efxcroHOW40j/Pjn71wFEstXs8+Xcv4xqfAZUnEBcXDWVqRP9L7vGXGqZDqn/ZGJMlUKsuvyxJQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org; spf=pass smtp.mailfrom=makrotopia.org; arc=none smtp.client-ip=185.142.180.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=makrotopia.org Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1reOlf-0001nU-17; Mon, 26 Feb 2024 00:22:51 +0000 Date: Mon, 26 Feb 2024 00:22:48 +0000 From: Daniel Golle To: 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 v8 1/7] dt-bindings: mtd: add basic bindings for UBI Message-ID: <6c53f1cb8eb3b47fd51eec9797118e13e104c768.1708906456.git.daniel@makrotopia.org> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791918875092583884 X-GMAIL-MSGID: 1791918875092583884 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 | 35 ++++++++++ 2 files changed, 100 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..7084a1945b310 --- /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 the 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 { + volid = <2>; + volname = "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..1e3f04dedc01d --- /dev/null +++ b/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml @@ -0,0 +1,35 @@ +# 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: + volid: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + Match UBI volume ID + + volname: + $ref: /schemas/types.yaml#/definitions/string + description: + Match UBI volume ID + +anyOf: + - required: + - volid + + - required: + - volname + +# This is a generic file other binding inherit from and extend +additionalProperties: true From patchwork Mon Feb 26 00:22:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 206151 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1796122dyb; Sun, 25 Feb 2024 16:23:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWe9NPSMAGPgbzwK11iodapbAv+mTQ8REIKTQgKV5LqN9ZxszrWfLO7YtKw3dppoJ1XvK+Ugmyzye2F0vmEDIgGBTwyBw== X-Google-Smtp-Source: AGHT+IFpCdvePUKRxprjXf94f6DGKHbFr18snOHlKwCINyEd0byBDVSFeBwWnupCL2Lgjx8OPVOF X-Received: by 2002:a05:6358:5912:b0:17b:5a16:a712 with SMTP id g18-20020a056358591200b0017b5a16a712mr7208122rwf.29.1708907014777; Sun, 25 Feb 2024 16:23:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907014; cv=pass; d=google.com; s=arc-20160816; b=mofZaTJs/9QqnbjPWflMbz5ejrW2YkBf0IUt0wwbHwOQAbTzWF0gtupvrkIeZkWRx4 dv+ZWBfC2z3f4weYZwjFCwGH5B95lClHpM0EUUJ8jPSfJ5EB0LLor+U5rW5qZhofj1ou U5gIxUnEuladmtgYNJyVVs1rVM8wFrraljgqP5II0/sVzVbBezhjap7W90y7KzV4rNDd tiwPOmJ+MZcemWgF/lwdJdDlEHEKrB+FPO6VvXJtZITaQ4+6THpSEJDst0zcLOAM4Fb6 TDWtvjhjBYzRCFcW7878q/0orFRB2sIWfJEw6/j5t6MVAZjA+/uCGfcDs/w/70YOQzVH 4fCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:to :from:date; bh=+MwcgXoOIYtWNDkvFuM5zhdq6twu3sXkEfYhCPFLHFY=; fh=iCY7pZACTeEM/aV1VSRdKj3GWTB74ZtqN+MwTbm3QUU=; b=rCB+jlea+XcKPSjRr97TD0T+PKUSgWh2OwMiwFWjKUoHtZbvs+IJnul557kta5Hjjc jmeI1IEghJGljRgTt/re8SMoKpK8yFNyeomO/mv6SDxDn/cLx3E0Y1CnKiNJ/bU9mc/B ouffutW8RoHiOiwvqlfEo92qBrio/+qwyOxRT6k7K3quGt2I/GZuQsplqeQS6hCOvBK2 qI0NomGTVetWVj5xIIunk6hKm/HQr6YWFd/uBI/pihJpLJLZSGNN6vSvehtsyFaUYluA Fvn09Tz5FcLXi1jy2m2PCxZr5pMAa+14ngvlp/CTtXpys88d36YP1dJitsTvU/o1OX7J FSdg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80356-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80356-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id r11-20020a62e40b000000b006e11d34903csi2793165pfh.344.2024.02.25.16.23.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:23:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80356-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80356-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80356-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 90F152818A0 for ; Mon, 26 Feb 2024 00:23:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A8FDF1401B; Mon, 26 Feb 2024 00:23:07 +0000 (UTC) Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90D8A101C4; Mon, 26 Feb 2024 00:23:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.142.180.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708906986; cv=none; b=Jrc43syaclhzp+ImM08PtpwqQTUcx1oUfHWKbqEL2KFN1mpUPQwaGroKb0bEpsn6CwJIGNDcEtHX0/ngomo4sIY6HLqfpB0lZi/EhtefC+H5gCo0PAe+HyojDafNwKXC9D2tkhv4IdzN6d8mLW/mxKNB9uZ+65vjH71kDqoyTWc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708906986; c=relaxed/simple; bh=N97TvJmYkqRtFZI8fRGBr8qsUHt2POR7C0eus3UEHh0=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Q14vubhqo6/hKQzh468R9Tid+BvGeWDfffoyDM6nFE20rTZK+uf1AKMumG2zOUddSAr8rYSL16C1iTwoWthC1AMXk9yblirprBUUctgqfBSp4LSCc0D7t1D277Y1kNQmlfK6rlUtk1LmKUoXDjbEifb4XGcV0cEkar3HBi+/jeY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org; spf=pass smtp.mailfrom=makrotopia.org; arc=none smtp.client-ip=185.142.180.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=makrotopia.org Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1reOlm-0001no-2S; Mon, 26 Feb 2024 00:22:58 +0000 Date: Mon, 26 Feb 2024 00:22:56 +0000 From: Daniel Golle To: 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 v8 2/7] dt-bindings: mtd: ubi-volume: allow UBI volumes to provide NVMEM Message-ID: <6b45ddaa344cc3958fa25f2e8f15a73c006f58fa.1708906456.git.daniel@makrotopia.org> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791918882238035560 X-GMAIL-MSGID: 1791918882238035560 UBI volumes may be used to contain NVMEM bits, typically device MAC addresses or wireless radio calibration data. Signed-off-by: Daniel Golle Reviewed-by: Rob Herring --- .../devicetree/bindings/mtd/partitions/linux,ubi.yaml | 10 ++++++++++ .../devicetree/bindings/mtd/partitions/ubi-volume.yaml | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml b/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml index 7084a1945b310..27e1ac1f252e4 100644 --- a/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml +++ b/Documentation/devicetree/bindings/mtd/partitions/linux,ubi.yaml @@ -59,6 +59,16 @@ examples: ubi-volume-caldata { volid = <2>; volname = "rf"; + + nvmem-layout { + compatible = "fixed-layout"; + #address-cells = <1>; + #size-cells = <1>; + + eeprom@0 { + reg = <0x0 0x1000>; + }; + }; }; }; }; diff --git a/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml b/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml index 1e3f04dedc01d..19736b26056b2 100644 --- a/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml +++ b/Documentation/devicetree/bindings/mtd/partitions/ubi-volume.yaml @@ -24,6 +24,11 @@ properties: description: Match UBI volume ID + nvmem-layout: + $ref: /schemas/nvmem/layouts/nvmem-layout.yaml# + description: + This container may reference an NVMEM layout parser. + anyOf: - required: - volid From patchwork Mon Feb 26 00:23:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 206152 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1796197dyb; Sun, 25 Feb 2024 16:23:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU0jx6XQqvSCueOvm/9u2VgFHChVAkxULKWRVa97CeVLytgHCeuljHlVruhiqCHeP0Ogw0WD7qYqKykEXcngykXtLfeRA== X-Google-Smtp-Source: AGHT+IE06FwX8SxfvcdB4py4hAo5/pAnFxHBrhYqOnsX/plU6JxyFaGidIimHTbRmlKKpHq8A2h9 X-Received: by 2002:ae9:e701:0:b0:787:bb6b:4d2b with SMTP id m1-20020ae9e701000000b00787bb6b4d2bmr6790601qka.75.1708907029267; Sun, 25 Feb 2024 16:23:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907029; cv=pass; d=google.com; s=arc-20160816; b=L9mFrb3NuAHhE0iBDfDdGSFEeS/TAIET8YSywF7J42pTOGBK89lSdV7cxTJ5tf2Vkl +xneKRSDh9q9JpJpuwBGVhYEk0hPNGw7jQazL+AvUhbfiF5bD2Mql/W5+mlrrL7qQjt+ Rcz7pJ2oOixHrhqOPjysxH02BoT0Arq4qIZVq+goBh27wchEQYxExQjqMaDm8WxtDri5 MUpjLks3XHLgK9Zzwj4Gjem1WilADQh+gnQgQe/nyZmnXopJNCy2LfF6+YY0PLO4FP+h PznK793iD3acVPtRQzfkdSBuOL13rlwJY8QrQFAXi9gJXoBkqorZymHfyz0/42jvauUk /3Cg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:to :from:date; bh=PGRpoy5XiH9sNFfSwzdqNh7yY4cieiucmWRfIw+BpFI=; fh=iCY7pZACTeEM/aV1VSRdKj3GWTB74ZtqN+MwTbm3QUU=; b=jp0b/8+r2Q0GipUO5jcvk4SaiU6i4VKroV6FZeVld2EaRIalY7Lop/WQgHbdMvPeNz z3zknT37WXuy8a7k0Ehd88Kh6NstDYUNVqhG16docP3skDBf5/EBN1SmVSUB7nKqqDnN 8xyVUcwjYFuSPyWMCAJT0zEy0Ch5oov7k5LLlBWOgk12kMwqGSrICczRTLqYse6JfjVD WuUC4qPonzeTC+/2y2etdqCZARAhxcyRyH/0tuhZP03UiP2VVaH23OHO0FVsEPOu7kpc H5N3BR+bcnnMFm7Ged/1SKqbpPgX/mgYti4rzc4IxT/MFH9MkP+m4hI7DvS06V141cK2 8R7w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80357-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80357-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id b23-20020a05620a04f700b00787d527cea4si474896qkh.30.2024.02.25.16.23.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:23:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80357-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80357-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80357-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 068551C20B07 for ; Mon, 26 Feb 2024 00:23:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 76BB414A98; Mon, 26 Feb 2024 00:23:20 +0000 (UTC) Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25B2E4C66; Mon, 26 Feb 2024 00:23:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.142.180.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708906998; cv=none; b=ZNL3AJAH0aR63VpIjrDgJjlVXsMm0RPzpoFfCmfPoumr1rpGUwXrTTAc8E/UCJ8gXczZ8bIr3Qt3I5fD+/YmDHmskIBwzjNQfMiaZNWQ+08Ed+i950ReZ6NgtEOtLD85VsKNNodjQ3JCuIdTKFqq84E5/YKARJNAX9AjUhRJ3G0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708906998; c=relaxed/simple; bh=M7CwXG0B0bk/KLmw+dx90M0VBkPlRYhd7XqsA5P3myI=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RIIqmQDeXQX8ydt31LB8VFQwcSo3rMr+ZlK7b+sPBv4K+fsacsNgViEcBBaX0ZRMK6StlMVS2i6R5/v2cOrygmpvm6pSRV/Mx6+Q/uCYSTdAxXaAN3m7mGi3aPyi6zcsOSm6K8Uu+K9invk+1XAuEGsBACjTVly0zVUQw8xddXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org; spf=pass smtp.mailfrom=makrotopia.org; arc=none smtp.client-ip=185.142.180.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=makrotopia.org Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1reOlv-0001oR-2E; Mon, 26 Feb 2024 00:23:07 +0000 Date: Mon, 26 Feb 2024 00:23:04 +0000 From: Daniel Golle To: 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 v8 3/7] mtd: ubi: block: use notifier to create ubiblock from parameter Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791918897049528273 X-GMAIL-MSGID: 1791918897049528273 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 | 136 ++++++++++++++++++++-------------------- drivers/mtd/ubi/kapi.c | 54 +++++++++++----- drivers/mtd/ubi/ubi.h | 1 + 3 files changed, 106 insertions(+), 85 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 5c8fdcc088a0d..f82e3423acb9f 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -65,10 +65,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; @@ -536,6 +536,70 @@ 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, cur_ubi_num, cur_vol_id; + + if (ubi_num == -1) { + /* No ubi num, name must be a vol device path */ + err = ubi_get_num_by_path(name, &cur_ubi_num, &cur_vol_id); + if (err || vi->ubi_num != cur_ubi_num || vi->vol_id != cur_vol_id) + return false; + + return true; + } + + if (vol_id == -1) { + /* Got ubi_num, but no vol_id, name must be volume name */ + 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) { @@ -543,10 +607,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); @@ -572,56 +633,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; @@ -647,18 +658,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; diff --git a/drivers/mtd/ubi/kapi.c b/drivers/mtd/ubi/kapi.c index 5db653eacbd45..fbf3a7fe2af79 100644 --- a/drivers/mtd/ubi/kapi.c +++ b/drivers/mtd/ubi/kapi.c @@ -279,6 +279,41 @@ struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, } EXPORT_SYMBOL_GPL(ubi_open_volume_nm); +/** + * ubi_get_num_by_path - get UBI device and volume number from device path + * @pathname: volume character device node path + * @ubi_num: pointer to UBI device number to be set + * @vol_id: pointer to UBI volume ID to be set + * + * Returns 0 on success and sets ubi_num and vol_id, returns error otherwise. + */ +int ubi_get_num_by_path(const char *pathname, int *ubi_num, int *vol_id) +{ + int error; + struct path path; + struct kstat stat; + + error = kern_path(pathname, LOOKUP_FOLLOW, &path); + if (error) + return error; + + error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT); + path_put(&path); + if (error) + return error; + + if (!S_ISCHR(stat.mode)) + return -EINVAL; + + *ubi_num = ubi_major2num(MAJOR(stat.rdev)); + *vol_id = MINOR(stat.rdev) - 1; + + if (*vol_id < 0 || *ubi_num < 0) + return -ENODEV; + + return 0; +} + /** * ubi_open_volume_path - open UBI volume by its character device node path. * @pathname: volume character device node path @@ -290,32 +325,17 @@ EXPORT_SYMBOL_GPL(ubi_open_volume_nm); struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode) { int error, ubi_num, vol_id; - struct path path; - struct kstat stat; dbg_gen("open volume %s, mode %d", pathname, mode); if (!pathname || !*pathname) return ERR_PTR(-EINVAL); - error = kern_path(pathname, LOOKUP_FOLLOW, &path); - if (error) - return ERR_PTR(error); - - error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT); - path_put(&path); + error = ubi_get_num_by_path(pathname, &ubi_num, &vol_id); if (error) return ERR_PTR(error); - if (!S_ISCHR(stat.mode)) - return ERR_PTR(-EINVAL); - - ubi_num = ubi_major2num(MAJOR(stat.rdev)); - vol_id = MINOR(stat.rdev) - 1; - - if (vol_id >= 0 && ubi_num >= 0) - return ubi_open_volume(ubi_num, vol_id, mode); - return ERR_PTR(-ENODEV); + return ubi_open_volume(ubi_num, vol_id, mode); } EXPORT_SYMBOL_GPL(ubi_open_volume_path); diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index 0b42bb45dd840..a588381c50adc 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -955,6 +955,7 @@ void ubi_free_internal_volumes(struct ubi_device *ubi); void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di); void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol, struct ubi_volume_info *vi); +int ubi_get_num_by_path(const char *pathname, int *ubi_num, int *vol_id); /* scan.c */ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, int pnum, const struct ubi_vid_hdr *vid_hdr); From patchwork Mon Feb 26 00:23:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 206153 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1796264dyb; Sun, 25 Feb 2024 16:24:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV5QA3cqnpxTxC+JKIRUcPPUYp35w2upwCBmRzUqE4gj517axqvo15cjnRfuURQghh0ZKnb279EV7V1dRIz+9LLzwKA5w== X-Google-Smtp-Source: AGHT+IFJE7MBOY8L4A+VlKCrZ+Dtc7VahsA2gDtTBINvfcqEXeb3EB0Zjm+93QXbqyjVqb8XbfDT X-Received: by 2002:a17:906:c9d3:b0:a3e:3a1d:f4b5 with SMTP id hk19-20020a170906c9d300b00a3e3a1df4b5mr3247383ejb.65.1708907044008; Sun, 25 Feb 2024 16:24:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907043; cv=pass; d=google.com; s=arc-20160816; b=yDB6kTdY6+hrnS+74Bns2iuh8mAfODNZhsXxTSu/fKsjjg93bIsPx39Y4AkMnMXshD sX9gQ+lVpaozZxW3vJpfzBQOSFUnqBNYROJOzznii4d2SdT31O8focfkXQcXm1FyhgWp Tsvb5R/QZxB0IvtXqoGcQGrUJjpC5T6/n4qWqc/BVsJuMWs9ZrfD5DVH/LDF+t47mdE0 OsrMNtwQP7/5bIeVSeUPc5osOZeHQ/Hunwx742691I87NkE5oIRQwll7IgXT3mf/aP26 dcGxJt9eCanC63j2SdVu7lXIKtg4yf4mieEXjXkhNXU4bWY8SWIth4LWVuVKG6+uFZ7Z C82Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:to :from:date; bh=OIETOnxJnmMu1gII2zkU0/IbgoC2GPo9rs3sS9n7WN0=; fh=iCY7pZACTeEM/aV1VSRdKj3GWTB74ZtqN+MwTbm3QUU=; b=Da9Hv7kFyLAYRm6mXGBF8nfP1Si3HpS8ZIMCuola8Z/0ZvNEJOGbDJ0NY/LRMeROp8 dRxmKg/xkBlaOntzfzUxQ91wdtkDQJdtZKXWsnT/ufcUBLvY3XwsKvDKZlnhODehpdyr QvfkonJrVw6S0nNzJwEH9M3Im/fT174FpPoWLUpKwQ9me6v35WuCJ9ZpQ3sSzaymB8dV EEln6CalaqoPxkbWJyz4ZQYwwyt1xmgcju5pRQ0o4PJzfe+Ybz0s8nTmg2roSO9f9CaW uz4NVzdanyN+O6/p//7hL43Jtp62TDlaDU7I8A0HHDGUlA5CzQm19TGp85LQ8xOPRqaM lZgg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80358-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80358-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id gv12-20020a170906f10c00b00a42e0305eb2si1526339ejb.707.2024.02.25.16.24.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:24:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80358-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80358-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80358-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 759641F213E3 for ; Mon, 26 Feb 2024 00:24:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6967D1773D; Mon, 26 Feb 2024 00:23:26 +0000 (UTC) Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEBE4D53C; Mon, 26 Feb 2024 00:23:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.142.180.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907004; cv=none; b=S5DfV0XQ1N3wdBU9tmTFSMi+heqNFrVILDoGxHKqOdSBqNQtDK7nTxI3W80XYV0xsA5gsxoRHRDCMKVUs2AHm9fICWtassT2zgNox25AVtS8xEdxbQ6n9os+IVRC0z5GwJlJASKb79H11ZhDiLm+kEViWOJ6oa6CUyLjUbIxJFA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907004; c=relaxed/simple; bh=3D5uJZwpEc9S1ejpS3cy4pEqDjj5nFVvrKVE6FijDxc=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=O6D+G8jMfiFgnB8cAXiwqvK6H7fgeBID5uzGgcvVt0zffpGyaSYD6+EYa1DE7q7fGZOPICB0OTVaf25Z1JhfJWHOZV1H0K06SEf60QpR7QsKI1n+mbnAf89fiGPEpTtB2UA67/f7qqNZ9pW3ADgLiLQs8IqE21qH9LpnQFG8XLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org; spf=pass smtp.mailfrom=makrotopia.org; arc=none smtp.client-ip=185.142.180.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=makrotopia.org Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1reOm4-0001ou-2U; Mon, 26 Feb 2024 00:23:16 +0000 Date: Mon, 26 Feb 2024 00:23:14 +0000 From: Daniel Golle To: 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 v8 4/7] mtd: ubi: attach from device tree Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791918912574059567 X-GMAIL-MSGID: 1791918912574059567 Introduce device tree compatible 'linux,ubi' and attach compatible MTD devices using the MTD add notifier. This is needed for a UBI device to be available early at boot (and not only after late_initcall), so volumes on them can be used eg. as NVMEM providers for other drivers. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/build.c | 135 ++++++++++++++++++++++++++++------------ 1 file changed, 96 insertions(+), 39 deletions(-) diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 7d4ff1193db6f..8c3f763e4ddb8 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" @@ -1219,43 +1220,43 @@ 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, false); + mutex_unlock(&ubi_devices_mutex); if (err < 0) - return err; - - err = misc_register(&ubi_ctrl_cdev); - if (err) { - pr_err("UBI error: cannot register device\n"); - goto out; - } + __put_mtd_device(mtd); +} - 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; - } +static void ubi_notify_remove(struct mtd_info *mtd) +{ + /* do nothing for now */ +} - 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++) { @@ -1304,25 +1305,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); + 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: @@ -1332,13 +1387,15 @@ 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]) { From patchwork Mon Feb 26 00:23:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 206154 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1796325dyb; Sun, 25 Feb 2024 16:24:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXFMDJldpTaZHwoGKOoCc95lDXsk89fpROqKn5XnoaDX8J75cE1rZQXBwV47WKNeSncZzSf3sEG30qRK9VXpZeb3dRGTg== X-Google-Smtp-Source: AGHT+IH2VLUwKko3GAc9hiFot75yeUQHt92B6ZR/ezatlhSPNC3DFz8hSV3SOqOjD+Odk1hAxmoq X-Received: by 2002:a17:902:f681:b0:1db:c649:cff0 with SMTP id l1-20020a170902f68100b001dbc649cff0mr9196004plg.25.1708907056506; Sun, 25 Feb 2024 16:24:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907056; cv=pass; d=google.com; s=arc-20160816; b=IMDFe31/9cN36wooTHri857Qj+Adx7BosYooEaWaX5yamBJ4xz/nYL2JLeppEBTwz7 PZXgB1zMiqM6N9eGM7rWuGSpjAvxSfiV8nOVFOXIBVGTLY9QVf944YjnYpqLNml50AHW Qp1pR6vfy0EkvHEOb2FX5xsq2mw6PUh6s4AdR/iibwwLhEAQ9dhR/S85LvSrzJTYRkY1 BkjyRSWMr5ibTnd70TvFh8vciggnIbKat201/dREsl+jf55Nl8RMqUi2/vlwDx1Rdxqo GcMfDKm429fthehLzEGa2XwYT+1xISvNwwB6SrqYx+P0yOJu5XyYOoY34JvrwKZM/ey2 9iqg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:to :from:date; bh=LHv4YsYDDj7iRDa6CMvjNvephM2sVRs7kWt3F46L//E=; fh=iCY7pZACTeEM/aV1VSRdKj3GWTB74ZtqN+MwTbm3QUU=; b=n2azV0jkw6alYsByypPU7nWXWXHHthhK+oKqiGjIm/n06yM8p6yJLbxQTgsrAUzqsI +F8gcI5/TMlDDoftNOCnswYEvp1fzkcRixkzOz+VxsH3wMWjIZdwuxykwzBjcVoDYHzp PzbAgks+L82lcdVM2XP4fb16VbhG3ZzpM3ae5xjM0dNq/AaJXWC+4R1UP1nrTGMlen3D JXTZMuHBaI1UJzMIYShG5rW85Wa0rVztcVccSsQdwbNaUf/a2B6agSKieaYwcakDaxex Y45RFVP1dmvV1Y5hv7gK5kkGGaw3Jzgha0wZUrHRAlHOOX3CZjhwsiOYpxSCCMNbgBXS vadw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80359-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80359-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id i18-20020a170902eb5200b001dbf3786659si2694581pli.222.2024.02.25.16.24.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:24:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80359-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80359-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80359-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 51B982818BD for ; Mon, 26 Feb 2024 00:24:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4FFDA18AF6; Mon, 26 Feb 2024 00:23:37 +0000 (UTC) Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C21003D68; Mon, 26 Feb 2024 00:23:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.142.180.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907015; cv=none; b=kJmrpEp6fGNjiJdx2uXFxsW0cUWJsb1searKbMYDXHbPWENlTIg9N6G3BCJLDmYD6+gBmUFB3icYecIFhXsFeoRTCh3XtkTGK2zg7yXxBHi+livcz7T/0qWqAUpqxhyk8luyyq7EqTb2XjfbBIsDuqJO9TJHOrj2mf6t/7E8eu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907015; c=relaxed/simple; bh=yTFc4beglI9rbyOb9+Q4ujHeT6pnL5mYKJ5fGCwraj4=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jyaunC/eOPWkCA1+NTkX+lYubBnvzNisalH2BSbsrml5YRXPaKu74qYqZdEARO/iL0UZCyP/8TAeJ2ZaOdXv7cGniTi1N974XrPiAe+2djcMp7pE+QweVzPYusPzczU6xEjI9Jg7j7vr80X40HzvGD+OCohrC4gG237TH7W/6u4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org; spf=pass smtp.mailfrom=makrotopia.org; arc=none smtp.client-ip=185.142.180.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=makrotopia.org Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1reOmE-0001pn-25; Mon, 26 Feb 2024 00:23:26 +0000 Date: Mon, 26 Feb 2024 00:23:23 +0000 From: Daniel Golle To: 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 v8 5/7] mtd: ubi: introduce pre-removal notification for UBI volumes Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791918925523079257 X-GMAIL-MSGID: 1791918925523079257 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/build.c | 19 ++++++++++++++----- drivers/mtd/ubi/kapi.c | 2 +- drivers/mtd/ubi/ubi.h | 2 ++ drivers/mtd/ubi/vmt.c | 17 +++++++++++++++-- include/linux/mtd/ubi.h | 2 ++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 8c3f763e4ddb8..a7e3a6246c0e9 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -93,7 +93,7 @@ static struct ubi_device *ubi_devices[UBI_MAX_DEVICES]; /* Serializes UBI devices creations and removals */ DEFINE_MUTEX(ubi_devices_mutex); -/* Protects @ubi_devices and @ubi->ref_count */ +/* Protects @ubi_devices, @ubi->ref_count and @ubi->is_dead */ static DEFINE_SPINLOCK(ubi_devices_lock); /* "Show" method for files in '//class/ubi/' */ @@ -261,6 +261,9 @@ struct ubi_device *ubi_get_device(int ubi_num) spin_lock(&ubi_devices_lock); ubi = ubi_devices[ubi_num]; + if (ubi && ubi->is_dead) + ubi = NULL; + if (ubi) { ubi_assert(ubi->ref_count >= 0); ubi->ref_count += 1; @@ -298,7 +301,7 @@ struct ubi_device *ubi_get_by_major(int major) spin_lock(&ubi_devices_lock); for (i = 0; i < UBI_MAX_DEVICES; i++) { ubi = ubi_devices[i]; - if (ubi && MAJOR(ubi->cdev.dev) == major) { + if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) { ubi_assert(ubi->ref_count >= 0); ubi->ref_count += 1; get_device(&ubi->dev); @@ -327,7 +330,7 @@ int ubi_major2num(int major) for (i = 0; i < UBI_MAX_DEVICES; i++) { struct ubi_device *ubi = ubi_devices[i]; - if (ubi && MAJOR(ubi->cdev.dev) == major) { + if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) { ubi_num = ubi->ubi_num; break; } @@ -514,7 +517,7 @@ static void ubi_free_volumes_from(struct ubi_device *ubi, int from) int i; for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { - if (!ubi->volumes[i]) + if (!ubi->volumes[i] || ubi->volumes[i]->is_dead) continue; ubi_eba_replace_table(ubi->volumes[i], NULL); ubi_fastmap_destroy_checkmap(ubi->volumes[i]); @@ -1099,7 +1102,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) return -EINVAL; spin_lock(&ubi_devices_lock); - put_device(&ubi->dev); ubi->ref_count -= 1; if (ubi->ref_count) { if (!anyway) { @@ -1110,6 +1112,13 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) ubi_err(ubi, "%s reference count %d, destroy anyway", ubi->ubi_name, ubi->ref_count); } + ubi->is_dead = true; + 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/kapi.c b/drivers/mtd/ubi/kapi.c index fbf3a7fe2af79..f1ea8677467fb 100644 --- a/drivers/mtd/ubi/kapi.c +++ b/drivers/mtd/ubi/kapi.c @@ -152,7 +152,7 @@ struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) spin_lock(&ubi->volumes_lock); vol = ubi->volumes[vol_id]; - if (!vol) + if (!vol || vol->is_dead) goto out_unlock; err = -EBUSY; diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index a588381c50adc..32009a24869e7 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -337,6 +337,7 @@ struct ubi_volume { int writers; int exclusive; int metaonly; + bool is_dead; int reserved_pebs; int vol_type; @@ -561,6 +562,7 @@ struct ubi_device { spinlock_t volumes_lock; int ref_count; int image_seq; + bool is_dead; int rsvd_pebs; int avail_pebs; diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index 2c867d16f89f7..ee80c04d1d5d0 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -59,7 +59,7 @@ static ssize_t vol_attribute_show(struct device *dev, struct ubi_device *ubi = vol->ubi; spin_lock(&ubi->volumes_lock); - if (!ubi->volumes[vol->vol_id]) { + if (!ubi->volumes[vol->vol_id] || ubi->volumes[vol->vol_id]->is_dead) { spin_unlock(&ubi->volumes_lock); return -ENODEV; } @@ -189,7 +189,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) /* Ensure that the name is unique */ for (i = 0; i < ubi->vtbl_slots; i++) - if (ubi->volumes[i] && + if (ubi->volumes[i] && !ubi->volumes[i]->is_dead && ubi->volumes[i]->name_len == req->name_len && !strcmp(ubi->volumes[i]->name, req->name)) { ubi_err(ubi, "volume \"%s\" exists (ID %d)", @@ -352,6 +352,19 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) err = -EBUSY; goto out_unlock; } + + /* + * Mark volume as dead at this point to prevent that anyone + * can take a reference to the volume from now on. + * This is necessary as we have to release the spinlock before + * calling ubi_volume_notify. + */ + vol->is_dead = true; + 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 Mon Feb 26 00:23:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 206156 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1796542dyb; Sun, 25 Feb 2024 16:24:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUkYrWC6/d6J1hDLbDukS5vbHFHNwhBoloAe8bNEntGXEdql8QUVPobYI+bPi3zDZ6MxWF5Arg8UZnGO5nLJfmwG/cbMA== X-Google-Smtp-Source: AGHT+IF9h/9/7F82ooFNQtKsfy6RHCZwSSmrrqoFO/8ZCkFDmhWUjs2Mbmiaj98/gCoSzmJll0VD X-Received: by 2002:a05:6a20:e68f:b0:1a0:7f3f:8d5d with SMTP id mz15-20020a056a20e68f00b001a07f3f8d5dmr8311643pzb.39.1708907094843; Sun, 25 Feb 2024 16:24:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907094; cv=pass; d=google.com; s=arc-20160816; b=eRJKQWrgMhfxvOzkbCPtz99gmAWXu1H5/69z3OyCeFqJ+vAHTj++I2KTF5deQcdfbR 0T22bSHhqwSkH1wMOymYZ1PiWyIaj/sWqQxUpissnsH1aW6fxDj3LduxBsdMhuBhxb+F 8b9zFxpoIyniQomgjcZoY03RqlWXY0NjuJVc24P33dbxpdS06fd4CiCrschyIOYvKIyw rSyF/w/3TzNKI0pN8b/xP71FUm1ruhVjtC7LH33LEFotNmTCxbMzu1teyVgOgtIj5vpC Crfcd7iO1tLhfUrwR5rwxciBXHYHMEB3SmeLKZaNSS7x34flGTPj1tdtJjTi1cf6MIF8 IZYA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:to :from:date; bh=gjiGoy/Mov0rgSSFJJmFqafqYd9QeNEUJN3GIfTJa6E=; fh=iCY7pZACTeEM/aV1VSRdKj3GWTB74ZtqN+MwTbm3QUU=; b=zD//yzPW22V9Tu+ixG3B+GF7rE43TExpe9Kqdn7o3wA+yN05L2wUUn9nuxexxAOYFj h5mRNVVPExnzDHNH7IBsDgw+yb5j2boe0d29IMvJni+2qHxOGH/8XWjf4AZ2Mwx2hEi0 hztGNYAMxOJSXB8kBymQF41hOZDNrdGncjyEYcZf3Cdki15t7JpIKoVBy/98Ixiiq8x4 dZcxccdxnmrroKVnOkb/ukLumAGoatMflCZpHz5WwdseuFa8nBBynUW/xByjHxO6kTOt YmPpiYIx216/Ag2GYKm0sUbHZg1SqQZznpVyRM7FOBAPFoBnRd3VlqSEn+e+IrBuKBZn bu2w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80360-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80360-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id y1-20020a17090a474100b0029948fdf5ffsi4642553pjg.19.2024.02.25.16.24.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:24:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80360-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80360-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80360-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id D59D9B20A55 for ; Mon, 26 Feb 2024 00:24:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8B9CD1B59D; Mon, 26 Feb 2024 00:23:43 +0000 (UTC) Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5856B46AF; Mon, 26 Feb 2024 00:23:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.142.180.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907021; cv=none; b=BiRujBbk24qc5TLmhmOXgdgajXqa7qFNc0mcO1vznTydQWFWqYZ4sWo845cxa5LzELjrHqsSgsX4FI+z3TBvOQJcfVDhhXNmHYqmZxdoT6kCTiO6SuVzGvvfaSxxoSU82nG1AN+eEP13f6FcQgwdlEvy9l2RIAUryW6qDuFsLzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907021; c=relaxed/simple; bh=8ZN/uqPCf9gZGtjw4nLeVdgvb5J82r/tQ0SJ2C+AHBE=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OpxMTZvB/K9WfWB9w0o2dOXuWzSWbBVta5zoqRX4Sfmf+Tcn7/mb6cSIDsKTEfBjrJnQbJx8PPJHTKGyJMkdpzuuap77oCBcVkFZ+Yn5dXuODNO7T6P46SIAB2u3po7iCXMGiGxhSjrxYS3sdR1FlTNUwJC7LRW+UA2YDEUQbIA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org; spf=pass smtp.mailfrom=makrotopia.org; arc=none smtp.client-ip=185.142.180.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=makrotopia.org Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1reOmM-0001qF-1C; Mon, 26 Feb 2024 00:23:34 +0000 Date: Mon, 26 Feb 2024 00:23:31 +0000 From: Daniel Golle To: 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 v8 6/7] mtd: ubi: populate ubi volume fwnode Message-ID: <8097c9d13f92b51145ed747610de0bc9ad26d079.1708906456.git.daniel@makrotopia.org> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791918965675894703 X-GMAIL-MSGID: 1791918965675894703 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 ee80c04d1d5d0..7824ff069bd62 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, "volname", &volname) && + strncmp(volname, vol->name, vol->name_len)) + continue; + + if (!fwnode_property_read_u32(fw_vol, "volid", &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 @@ -605,6 +631,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 Mon Feb 26 00:23:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Golle X-Patchwork-Id: 206155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1796496dyb; Sun, 25 Feb 2024 16:24:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXRAewW79nE5fAdmMtHfKM/8BNPkGgorQG2qgNyTRSFoIsn8kJIp6JzWWiwAeVzJ11ht3RB/sMJpuATkXK7MSRxe9Bw2w== X-Google-Smtp-Source: AGHT+IG7s6+PodEFeVh0qxO356Cy0a+MgbGkjusSeOYu32xn8lqbcUBrMMwicovqEyRJgATxqSB2 X-Received: by 2002:a05:6512:3d0e:b0:512:fa7a:813f with SMTP id d14-20020a0565123d0e00b00512fa7a813fmr1608463lfv.10.1708907085386; Sun, 25 Feb 2024 16:24:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907085; cv=pass; d=google.com; s=arc-20160816; b=Gl/4Qk/6whyqGjD87J/opIs96tySHrTT18EsPWZBF1Sl+O1R16POXmMi9LsTeM7YUb ONVUJWU89dbuc7IvGdFaKxO5mR/Fd6jylbkE/jpdTVZOoaZ6ffHhIfGyZKYF2SA9tMby LDzdA7Ovm5oRaRtWQe+QfIRaR27ZpgOLpJjz4Y5D9mt/MnUs13z8NFC3C+Bd1RhiT5L4 u4KDQFp2bvbnWM0yzWOiYdUx+E5p/37JIOW7kjG/tLsDE/LJbvNIslJEmNwOt0NtZXpn gfmmw49xBE0aJ4JgcHooOdHCgqXcDLBRn0dLgMmvSxVhNIOovrVDLuicaha0UgThUpMp CEGw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:to :from:date; bh=V7gaaz/I3lVkdB2sJ76BXQ8iZfViqFCHVbhgfjEqsqo=; fh=iCY7pZACTeEM/aV1VSRdKj3GWTB74ZtqN+MwTbm3QUU=; b=JTfGjDKYOtmdUYEBmWkZ0Ymf3FGAHH4vMWQBye8bMpyF25mPTt+rhpilwCfJT0YkOX m8OVZoWEFSWt6jGuy4QXQSOCGtfDTZoHwoiFlfpJaeu+cGxzjZz3uqvp9X9cKDv6d7TQ PcRzFHuGfQwk9qGyjlfPKPegQNawHq2mXfI7NMwVe22Sgqzu2vPmtaSqwSDa+1B0ZSwj grqz5VI5tPLSW+Rm5/WnONT971jMZNwMHKPV/nOBPlZwldfmm6sWFo3L+/G96ulq7+Yr SJEFnQRkok3KLJoMXy81ss6zN9HR6nAzAYF3rO8DXs6ptmITcmfGc0vR8W1gqQHFRZM5 /GoQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80361-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80361-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id go7-20020a1709070d8700b00a432a9875c6si780283ejc.598.2024.02.25.16.24.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:24:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80361-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=makrotopia.org); spf=pass (google.com: domain of linux-kernel+bounces-80361-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80361-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id CECED1F212E2 for ; Mon, 26 Feb 2024 00:24:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4530218E25; Mon, 26 Feb 2024 00:23:56 +0000 (UTC) Received: from pidgin.makrotopia.org (pidgin.makrotopia.org [185.142.180.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84B3F33EC; Mon, 26 Feb 2024 00:23:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.142.180.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907034; cv=none; b=vE5rD4VYqdmhfBeQgX0LPZIoBrSeJBNES6qoDFAFrf3FWQlq/p7uXkv6fQzfo8Sc3C6kGNpSd0mCLPPIJC/VnoK0KT7jurm2jC224JlqxEkYDwYMaMr/NZ65IiPp8ybTZm2qaZhy9zmO7fmgU4M96T0ae7jqXRxMQ1bDSnk3SoM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708907034; c=relaxed/simple; bh=CF56sxrMUbhfd9jdc++hHknqQjW6Pw4Hu7N6DnVeMhE=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=KeAHZH83r/HNzshF3XnT395FZkZlfLK8vbMLiImKsB2Go8oKmKY5jAxSxjk9ntsjfYkbt54CV8aVQHIyHs2Rs2cSEhiQtghqM0+UbZrP75/gNLQGWvSVMLlNYVbAjboMkSXHIvIueUdsRuu41arGN47HWBd3cgRsTBkGpWJt5cA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org; spf=pass smtp.mailfrom=makrotopia.org; arc=none smtp.client-ip=185.142.180.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=makrotopia.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=makrotopia.org Received: from local by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96.2) (envelope-from ) id 1reOmW-0001qY-0d; Mon, 26 Feb 2024 00:23:44 +0000 Date: Mon, 26 Feb 2024 00:23:41 +0000 From: Daniel Golle To: 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 v8 7/7] mtd: ubi: provide NVMEM layer over UBI volumes Message-ID: <3ac88da4adc342af428b359cd27037b1498f6c91.1708906456.git.daniel@makrotopia.org> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791918956114096984 X-GMAIL-MSGID: 1791918956114096984 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 | 13 +++ drivers/mtd/ubi/Makefile | 1 + drivers/mtd/ubi/nvmem.c | 191 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 drivers/mtd/ubi/nvmem.c diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig index 7499a540121e8..e28a3af83c0eb 100644 --- a/drivers/mtd/ubi/Kconfig +++ b/drivers/mtd/ubi/Kconfig @@ -113,4 +113,17 @@ config MTD_UBI_FAULT_INJECTION testing purposes. 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..5820a170d2512 --- /dev/null +++ b/drivers/mtd/ubi/nvmem.c @@ -0,0 +1,191 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2023 Daniel Golle + */ + +/* UBI NVMEM provider */ +#include "ubi.h" +#include +#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) +{ + uint32_t bytes_left, offs, to_read; + struct ubi_nvmem *unv = priv; + struct ubi_volume_desc *desc; + uint64_t lnum = from; + int err = 0; + + desc = ubi_open_volume(unv->ubi_num, unv->vol_id, UBI_READONLY); + if (IS_ERR(desc)) + return PTR_ERR(desc); + + bytes_left = bytes; + offs = do_div(lnum, unv->usable_leb_size); + 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 device_node *np = dev_of_node(vi->dev); + struct nvmem_config config = {}; + struct ubi_nvmem *unv; + int ret; + + if (!np) + return 0; + + if (!of_get_child_by_name(np, "nvmem-layout")) + return 0; + + if (WARN_ON_ONCE(vi->usable_leb_size <= 0) || + WARN_ON_ONCE(vi->size <= 0)) + return -EINVAL; + + 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 = np; + + 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)) { + 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");