Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp694493lqp; Thu, 21 Mar 2024 12:36:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU93/zrep++jgYelPwWCU4Hk6NecEt5lp+hqSCZ29srOrif1rpBloJFagrosONsggOutuu6S80RYxVDnbi6TYavVq1IFHZQj2IlB2c+9w== X-Google-Smtp-Source: AGHT+IFUGlgTLj7W7a3fOKp/FAVe2UMghXAl4Le1U//xv5Bnj7hFHIc9n0NXKaAkKyJGkvqEOjI3 X-Received: by 2002:ac8:5ad1:0:b0:431:22f1:913f with SMTP id d17-20020ac85ad1000000b0043122f1913fmr136909qtd.27.1711049763445; Thu, 21 Mar 2024 12:36:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711049763; cv=pass; d=google.com; s=arc-20160816; b=f5rxHpCEz+HzCEPslon8xioHX0Fzb3ZpA4w2g5jLdZn4nQFJNsiTzFjae04RpCycgk 0wCiqkvEpbcLHsiRCIj7KpjqFWMs1rsnWvXWFWKR539UMIE21NYC5m3gQcIQv1BZj094 kv0PqHvePXPDH5puP7JURR4qUxXAMG6p+Pcix4Xy+EGjyLd3y+94h553K828sJ24dpaB uzZTZ1jKd6bveQFoEsm8UgXpOgbT/ueSYeQajqbpRq7wX+1sSGcKRYq3N93dIkc+4+fF /LHtAw/pwwsMe1bM51rM/IvGd6eLZACXb0n3ZIZhp74SJ0kSzFjelgHKTBFVnwn+r/1P xlyw== 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=vAOdwKpsFp9X/KFGpCE/zKyw01M4Pcy2q41I+qWPDJU=; fh=3Ay0fG3swzXuvpLryN6GcJlSn7kOqPZ2vxPz4z4ZjiM=; b=K08aPG4wT27dFQ9xKXEx6kp4+whstgGmf7MymbvZDQwxOXTql4jrnCWHohHd+xf3Wg gpWFqqGymKeokri2mQ/A2xdu/Elh8rKxEwvABHZAog3vSonc9jcVShWH6MZmDMKhS8jh lCjN7qUaJreqYbCf1UzyRa8qQwqM6FMntWkSVzWC1mbqUWdrfqozG7W/KaLUFZZkfcU1 SP+TO3LzBL5APdzTf+eD7nQcoNZVRCWWQhWhFumlSsP0gB+xpDRna0UalszkF8xqGOOh opzkForpiloV5xUZzcGQLqWNtai21KFemsxDGW6WzJjQiW+keF0aCO9qdCRNcZxehXAL 7v7A==; 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-110600-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110600-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y17-20020a05622a005100b00430d145e4cbsi407300qtw.539.2024.03.21.12.36.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 12:36:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110600-linux.lists.archive=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-110600-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110600-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 1DB931C222D8 for ; Thu, 21 Mar 2024 19:36:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 42CC6136674; Thu, 21 Mar 2024 19:34:32 +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 6183D13442F; Thu, 21 Mar 2024 19:34:28 +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=1711049671; cv=none; b=CmwRJFfT/Dv+fqGS6BqITLmT9hNC7ULkayfFLTPt2yMAE+t7HdfcT52Ls8dGiOeDRwuifc+6EWFhOm4iiDAIHm/CpmzzeTIK0B8oYWw9D4nfoox6AtEkViVGWOEhIIgbr6KxkCitZNzbjKZIf9lzidpck7EeqikVxmjSt8BSf7U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711049671; c=relaxed/simple; bh=2znTf4TvUOVXG3srG4NVYvSOtJ1j3xaikOmb5ufQK+M=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=quh2ubw0bLy9QEVdha5mjVGleMjyT/ErzcFNIBSsDTPTE8AyymnOq7CMBKCx4So8i8KYyVXBVCj+9QJdjeq8AtYF+ZrhDwg7M5e7gbCOecCHQPNwSIMRIYdxIP4wAf0NNrV82xzYbrdWfuW+VX3ub/vjRcM1YR6iSWBWZbIlbn4= 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 1rnOAu-0000Fp-13; Thu, 21 Mar 2024 19:34:04 +0000 Date: Thu, 21 Mar 2024 19:34:00 +0000 From: Daniel Golle To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Ulf Hansson , Jens Axboe , Daniel Golle , Dave Chinner , Jan Kara , Thomas =?iso-8859-1?Q?Wei=DFschuh?= , Damien Le Moal , Li Lingfeng , Christian Brauner , Christian Heusel , Min Li , Adrian Hunter , Avri Altman , Hannes Reinecke , Christian Loehle , Bean Huo , Yeqi Fu , Victor Shih , Christophe JAILLET , Dominique Martinet , "Ricardo B. Marliere" , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 4/8] block: implement NVMEM provider Message-ID: <7555db6eb71d4ccb2b9d5ebe3b41dc34088c6316.1711048433.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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On embedded devices using an eMMC it is common that one or more partitions on the eMMC are used to store MAC addresses and Wi-Fi calibration EEPROM data. Allow referencing the partition in device tree for the kernel and Wi-Fi drivers accessing it via the NVMEM layer. Signed-off-by: Daniel Golle --- MAINTAINERS | 5 ++ block/Kconfig | 9 +++ block/Makefile | 1 + block/blk-nvmem.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 block/blk-nvmem.c diff --git a/MAINTAINERS b/MAINTAINERS index 8c88f362feb55..242a0a139c00a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3662,6 +3662,11 @@ L: linux-mtd@lists.infradead.org S: Maintained F: drivers/mtd/devices/block2mtd.c +BLOCK NVMEM DRIVER +M: Daniel Golle +S: Maintained +F: block/blk-nvmem.c + BLUETOOTH DRIVERS M: Marcel Holtmann M: Luiz Augusto von Dentz diff --git a/block/Kconfig b/block/Kconfig index 1de4682d48ccb..b1d4c88c70040 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -229,6 +229,15 @@ config BLK_INLINE_ENCRYPTION_FALLBACK by falling back to the kernel crypto API when inline encryption hardware is not present. +config BLK_NVMEM + bool "Block device NVMEM provider" + depends on OF + depends on NVMEM + help + Allow block devices (or partitions) to act as NVMEM prodivers, + typically used with eMMC to store MAC addresses or Wi-Fi + calibration data on embedded devices. + source "block/partitions/Kconfig" config BLK_MQ_PCI diff --git a/block/Makefile b/block/Makefile index 46ada9dc8bbfe..03c0bfa8642df 100644 --- a/block/Makefile +++ b/block/Makefile @@ -34,6 +34,7 @@ obj-$(CONFIG_BLK_DEV_ZONED) += blk-zoned.o obj-$(CONFIG_BLK_WBT) += blk-wbt.o obj-$(CONFIG_BLK_DEBUG_FS) += blk-mq-debugfs.o obj-$(CONFIG_BLK_DEBUG_FS_ZONED)+= blk-mq-debugfs-zoned.o +obj-$(CONFIG_BLK_NVMEM) += blk-nvmem.o obj-$(CONFIG_BLK_SED_OPAL) += sed-opal.o obj-$(CONFIG_BLK_PM) += blk-pm.o obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \ diff --git a/block/blk-nvmem.c b/block/blk-nvmem.c new file mode 100644 index 0000000000000..29f8ac041d76e --- /dev/null +++ b/block/blk-nvmem.c @@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * block device NVMEM provider + * + * Copyright (c) 2024 Daniel Golle + * + * Useful on devices using a partition on an eMMC for MAC addresses or + * Wi-Fi calibration EEPROM data. + */ + +#include "blk.h" +#include +#include +#include +#include + +/* List of all NVMEM devices */ +static LIST_HEAD(nvmem_devices); +static DEFINE_MUTEX(devices_mutex); + +struct blk_nvmem { + struct nvmem_device *nvmem; + struct device *dev; + struct list_head list; +}; + +static int blk_nvmem_reg_read(void *priv, unsigned int from, + void *val, size_t bytes) +{ + blk_mode_t mode = BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES; + unsigned long offs = from & ~PAGE_MASK, to_read; + pgoff_t f_index = from >> PAGE_SHIFT; + struct blk_nvmem *bnv = priv; + size_t bytes_left = bytes; + struct file *bdev_file; + struct folio *folio; + void *p; + int ret = 0; + + bdev_file = bdev_file_open_by_dev(bnv->dev->devt, mode, priv, NULL); + if (!bdev_file) + return -ENODEV; + + if (IS_ERR(bdev_file)) + return PTR_ERR(bdev_file); + + while (bytes_left) { + folio = read_mapping_folio(bdev_file->f_mapping, f_index++, NULL); + if (IS_ERR(folio)) { + ret = PTR_ERR(folio); + goto err_release_bdev; + } + to_read = min_t(unsigned long, bytes_left, PAGE_SIZE - offs); + p = folio_address(folio) + offset_in_folio(folio, offs); + memcpy(val, p, to_read); + offs = 0; + bytes_left -= to_read; + val += to_read; + folio_put(folio); + } + +err_release_bdev: + fput(bdev_file); + + return ret; +} + +static int blk_nvmem_register(struct device *dev) +{ + struct device_node *np = dev_of_node(dev); + struct block_device *bdev = dev_to_bdev(dev); + struct nvmem_config config = {}; + struct blk_nvmem *bnv; + + /* skip devices which do not have a device tree node */ + if (!np) + return 0; + + /* skip devices without an nvmem layout defined */ + if (!of_get_child_by_name(np, "nvmem-layout")) + return 0; + + /* + * skip devices which don't have GENHD_FL_NVMEM set + * + * This flag is used for mtdblock and ubiblock devices because + * both, MTD and UBI already implement their own NVMEM provider. + * To avoid registering multiple NVMEM providers for the same + * device node, don't register the block NVMEM provider for them. + */ + if (!(bdev->bd_disk->flags & GENHD_FL_NVMEM)) + return 0; + + /* + * skip block device too large to be represented as NVMEM devices + * which are using an 'int' as address + */ + if (bdev_nr_bytes(bdev) > INT_MAX) + return -EFBIG; + + bnv = kzalloc(sizeof(struct blk_nvmem), GFP_KERNEL); + if (!bnv) + return -ENOMEM; + + config.id = NVMEM_DEVID_NONE; + config.dev = &bdev->bd_device; + config.name = dev_name(&bdev->bd_device); + config.owner = THIS_MODULE; + config.priv = bnv; + config.reg_read = blk_nvmem_reg_read; + config.size = bdev_nr_bytes(bdev); + config.word_size = 1; + config.stride = 1; + config.read_only = true; + config.root_only = true; + config.ignore_wp = true; + config.of_node = to_of_node(dev->fwnode); + + bnv->dev = &bdev->bd_device; + bnv->nvmem = nvmem_register(&config); + if (IS_ERR(bnv->nvmem)) { + dev_err_probe(&bdev->bd_device, PTR_ERR(bnv->nvmem), + "Failed to register NVMEM device\n"); + + kfree(bnv); + return PTR_ERR(bnv->nvmem); + } + + mutex_lock(&devices_mutex); + list_add_tail(&bnv->list, &nvmem_devices); + mutex_unlock(&devices_mutex); + + return 0; +} + +static void blk_nvmem_unregister(struct device *dev) +{ + struct blk_nvmem *bnv_c, *bnv = NULL; + + mutex_lock(&devices_mutex); + list_for_each_entry(bnv_c, &nvmem_devices, list) { + if (bnv_c->dev == dev) { + bnv = bnv_c; + break; + } + } + + if (!bnv) { + mutex_unlock(&devices_mutex); + return; + } + + list_del(&bnv->list); + mutex_unlock(&devices_mutex); + nvmem_unregister(bnv->nvmem); + kfree(bnv); +} + +static struct class_interface blk_nvmem_bus_interface __refdata = { + .class = &block_class, + .add_dev = &blk_nvmem_register, + .remove_dev = &blk_nvmem_unregister, +}; + +static int __init blk_nvmem_init(void) +{ + return class_interface_register(&blk_nvmem_bus_interface); +} +device_initcall(blk_nvmem_init); -- 2.44.0