Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1032833rbb; Sun, 25 Feb 2024 16:23:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXdYjOfBIkt0PDXlWrUZ7kMXbGubqSK9OH16fn2TybHsjCL3npyj19keXqU0dR5yhEfLZEy8fglpmag1GlQFu2IF3jlQ259Sz0I5B6Eiw== X-Google-Smtp-Source: AGHT+IHRNuiyRU0CdJgQHOmPkjPvIzpGh7UhQpjVY8JPX/VfhS5jNVq8JFzZvTHDcrAD3ptsAT8U X-Received: by 2002:a05:6e02:1aac:b0:365:89a4:a7ab with SMTP id l12-20020a056e021aac00b0036589a4a7abmr6299710ilv.11.1708907035551; Sun, 25 Feb 2024 16:23:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708907035; cv=pass; d=google.com; s=arc-20160816; b=Do+TslItXH/qy8jXZ+gf65DN7uFJV/nkCVrHKlp0vPXLq335oGOd4mTCtdhobnG2Ww pDtUh8JmgmpmFqb2wjm5ia7Cft3NhhSL17xLM3Y0IVMDP+ap5/FnFISfy68Qmf6Y0vQI DqJiv90cfnbxeoY+pChCXpLzjTdaSz+uhZLOKF7XIOYTNebAiQwLdbjx6vtIJ04ArKo2 eDLaTA9q5FCSqDbtj+5PVr5fSwbjaxgk/XDgYpoNo3WC43K5eznZTCBhbaNzG43Kk4Qk cORX52kAtZ32jg4p5kq2YlSOJolLzryIkcUJOQCXGk3Xs5EFJ2V2lZOScZz2dGFFmLiY Tt3Q== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80358-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 134-20020a63018c000000b005dc892e1807si2788539pgb.646.2024.02.25.16.23.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 16:23:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80358-linux.lists.archive=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-80358-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80358-linux.lists.archive=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 3E1CA281741 for ; Mon, 26 Feb 2024 00:23:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4335EEEDA; Mon, 26 Feb 2024 00:23:25 +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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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]) { -- 2.44.0