Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/nd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "nd-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "pmem.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "pfn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "nd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) static void namespace_io_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 	struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 	kfree(nsio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) static void namespace_pmem_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	if (nspm->id >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 		ida_simple_remove(&nd_region->ns_ida, nspm->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	kfree(nspm->alt_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	kfree(nspm->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	kfree(nspm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) static void namespace_blk_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	if (nsblk->id >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 		ida_simple_remove(&nd_region->ns_ida, nsblk->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	kfree(nsblk->alt_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	kfree(nsblk->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	kfree(nsblk->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	kfree(nsblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static bool is_namespace_pmem(const struct device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) static bool is_namespace_blk(const struct device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) static bool is_namespace_io(const struct device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) static int is_uuid_busy(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	u8 *uuid1 = data, *uuid2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		uuid2 = nspm->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		uuid2 = nsblk->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	} else if (is_nd_btt(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		struct nd_btt *nd_btt = to_nd_btt(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		uuid2 = nd_btt->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	} else if (is_nd_pfn(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		struct nd_pfn *nd_pfn = to_nd_pfn(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		uuid2 = nd_pfn->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) static int is_namespace_uuid_busy(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	if (is_nd_region(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		return device_for_each_child(dev, data, is_uuid_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * nd_is_uuid_unique - verify that no other namespace has @uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * @dev: any device on a nvdimm_bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * @uuid: uuid to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	if (!nvdimm_bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	if (device_for_each_child(&nvdimm_bus->dev, uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 				is_namespace_uuid_busy) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) bool pmem_should_map_pages(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	struct nd_namespace_common *ndns = to_ndns(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct nd_namespace_io *nsio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	if (is_nd_pfn(dev) || is_nd_btt(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (ndns->force_raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	nsio = to_nd_namespace_io(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	if (region_intersects(nsio->res.start, resource_size(&nsio->res),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 				IORESOURCE_SYSTEM_RAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 				IORES_DESC_NONE) == REGION_MIXED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) EXPORT_SYMBOL(pmem_should_map_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	if (is_namespace_pmem(&ndns->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		struct nd_namespace_pmem *nspm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		nspm = to_nd_namespace_pmem(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		if (nspm->lbasize == 0 || nspm->lbasize == 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 			/* default */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		else if (nspm->lbasize == 4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 			return 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 			dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 					nspm->lbasize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	 * There is no namespace label (is_namespace_io()), or the label
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	 * indicates the default sector size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	return 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) EXPORT_SYMBOL(pmem_sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	const char *suffix = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	if (ndns->claim && is_nd_btt(ndns->claim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		suffix = "s";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		int nsidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		if (is_namespace_pmem(&ndns->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 			struct nd_namespace_pmem *nspm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			nspm = to_nd_namespace_pmem(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			nsidx = nspm->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		if (nsidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 					suffix ? suffix : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			sprintf(name, "pmem%d%s", nd_region->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 					suffix ? suffix : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	} else if (is_namespace_blk(&ndns->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		struct nd_namespace_blk *nsblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		nsblk = to_nd_namespace_blk(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 				suffix ? suffix : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) EXPORT_SYMBOL(nvdimm_namespace_disk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) const u8 *nd_dev_to_uuid(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	static const u8 null_uuid[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		return null_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		return nspm->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		return nsblk->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		return null_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) EXPORT_SYMBOL(nd_dev_to_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static ssize_t nstype_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) static DEVICE_ATTR_RO(nstype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) static ssize_t __alt_name_store(struct device *dev, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		const size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	char *input, *pos, *alt_name, **ns_altname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		ns_altname = &nspm->alt_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		ns_altname = &nsblk->alt_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (dev->driver || to_ndns(dev)->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	input = kstrndup(buf, len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	if (!input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	pos = strim(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (!alt_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	kfree(*ns_altname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	*ns_altname = alt_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	sprintf(*ns_altname, "%s", pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	rc = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	kfree(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	resource_size_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	if (!nsblk->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		if (strcmp(res->name, label_id.id) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			size += resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	int count, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	if (!nsblk->uuid || !nsblk->lbasize || !ndd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	for_each_dpa_resource(ndd, res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		if (strcmp(res->name, label_id.id) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		 * Resources with unacknowledged adjustments indicate a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		 * failure to update labels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		if (res->flags & DPA_RESOURCE_ADJUSTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	/* These values match after a successful label update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	if (count != nsblk->num_resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	for (i = 0; i < nsblk->num_resources; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		struct resource *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			if (res == nsblk->res[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 				found = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		/* stale resource */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	resource_size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	nvdimm_bus_lock(&nsblk->common.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	size = __nd_namespace_blk_validate(nsblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	nvdimm_bus_unlock(&nsblk->common.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) EXPORT_SYMBOL(nd_namespace_blk_validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) static int nd_namespace_label_update(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 			"namespace must be idle during label update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	if (dev->driver || to_ndns(dev)->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	 * Only allow label writes that will result in a valid namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	 * or deletion of an existing namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		resource_size_t size = resource_size(&nspm->nsio.res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		if (size == 0 && nspm->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			/* delete allocation */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		else if (!nspm->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		return nd_pmem_namespace_label_update(nd_region, nspm, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		resource_size_t size = nd_namespace_blk_size(nsblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		if (size == 0 && nsblk->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			/* delete allocation */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		else if (!nsblk->uuid || !nsblk->lbasize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		return nd_blk_namespace_label_update(nd_region, nsblk, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) static ssize_t alt_name_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	wait_nvdimm_bus_probe_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	rc = __alt_name_store(dev, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		rc = nd_namespace_label_update(nd_region, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	return rc < 0 ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) static ssize_t alt_name_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	char *ns_altname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		ns_altname = nspm->alt_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		ns_altname = nsblk->alt_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) static DEVICE_ATTR_RW(alt_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) static int scan_free(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		resource_size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		struct resource *res, *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		resource_size_t new_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			if (strcmp(res->name, label_id->id) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 				last = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		res = last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		if (n >= resource_size(res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			n -= resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 			nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			nvdimm_free_dpa(ndd, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			/* retry with last resource deleted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		 * Keep BLK allocations relegated to high DPA as much as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		 * possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		if (is_blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			new_start = res->start + n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			new_start = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		rc = adjust_resource(res, new_start, resource_size(res) - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			res->flags |= DPA_RESOURCE_ADJUSTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469)  * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470)  * @nd_region: the set of dimms to reclaim @n bytes from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471)  * @label_id: unique identifier for the namespace consuming this dpa range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472)  * @n: number of bytes per-dimm to release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474)  * Assumes resources are ordered.  Starting from the end try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475)  * adjust_resource() the allocation to @n, but if @n is larger than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476)  * allocation delete it and find the 'new' last allocation in the label
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477)  * set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static int shrink_dpa_allocation(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		struct nd_label_id *label_id, resource_size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		rc = scan_free(nd_region, nd_mapping, label_id, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		struct nd_region *nd_region, struct nd_mapping *nd_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		resource_size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	resource_size_t first_dpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	/* allocate blk from highest dpa first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	if (is_blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		first_dpa = nd_mapping->start + nd_mapping->size - n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		first_dpa = nd_mapping->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	/* first resource allocation for this label-id or dimm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	return rc ? n : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  * space_valid() - validate free dpa space against constraints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  * @nd_region: hosting region of the free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525)  * @ndd: dimm device data for debug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526)  * @label_id: namespace id to allocate space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527)  * @prev: potential allocation that precedes free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528)  * @next: allocation that follows the given free space range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529)  * @exist: first allocation with same id in the mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530)  * @n: range that must satisfied for pmem allocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531)  * @valid: free space range to validate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533)  * BLK-space is valid as long as it does not precede a PMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534)  * allocation in a given region. PMEM-space must be contiguous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535)  * and adjacent to an existing existing allocation (if one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536)  * exists).  If reserving PMEM any space is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		struct nd_label_id *label_id, struct resource *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		struct resource *next, struct resource *exist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		resource_size_t n, struct resource *valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	align = nd_region->align / nd_region->ndr_mappings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	valid->start = ALIGN(valid->start, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	valid->end = ALIGN_DOWN(valid->end + 1, align) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if (valid->start >= valid->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (is_reserve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (!is_pmem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		struct nd_mapping *nd_mapping = &nd_region->mapping[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		struct nvdimm_bus *nvdimm_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		struct blk_alloc_info info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			.nd_mapping = nd_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 			.available = nd_mapping->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			.res = valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		WARN_ON(!is_nd_blk(&nd_region->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	/* allocation needs to be contiguous, so this is all or nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	if (resource_size(valid) < n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	/* we've got all the space we need and no existing allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	if (!exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	/* allocation needs to be contiguous with the existing namespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (valid->start == exist->end + 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			|| valid->end == exist->start - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585)  invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	/* truncate @valid size to 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	valid->end = valid->start - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) enum alloc_loc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) static resource_size_t scan_allocate(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		resource_size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	struct resource *res, *exist = NULL, valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	const resource_size_t to_allocate = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	int first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		if (strcmp(label_id->id, res->name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 			exist = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	valid.start = nd_mapping->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	valid.end = mapping_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	valid.name = "free space";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	for_each_dpa_resource(ndd, res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		struct resource *next = res->sibling, *new_res = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		resource_size_t allocate, available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		enum alloc_loc loc = ALLOC_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		const char *action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		/* ignore resources outside this nd_mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		if (res->start > mapping_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		if (res->end < nd_mapping->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		/* space at the beginning of the mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		if (!first++ && res->start > nd_mapping->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			valid.start = nd_mapping->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 			valid.end = res->start - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 			space_valid(nd_region, ndd, label_id, NULL, next, exist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 					to_allocate, &valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			available = resource_size(&valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			if (available)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 				loc = ALLOC_BEFORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		/* space between allocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		if (!loc && next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			valid.start = res->start + resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			valid.end = min(mapping_end, next->start - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 			space_valid(nd_region, ndd, label_id, res, next, exist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 					to_allocate, &valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			available = resource_size(&valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			if (available)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 				loc = ALLOC_MID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		/* space at the end of the mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		if (!loc && !next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			valid.start = res->start + resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			valid.end = mapping_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			space_valid(nd_region, ndd, label_id, res, next, exist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 					to_allocate, &valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			available = resource_size(&valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			if (available)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 				loc = ALLOC_AFTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		if (!loc || !available)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		allocate = min(available, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		switch (loc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		case ALLOC_BEFORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			if (strcmp(res->name, label_id->id) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 				/* adjust current resource up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 				rc = adjust_resource(res, res->start - allocate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 						resource_size(res) + allocate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 				action = "cur grow up";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 				action = "allocate";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		case ALLOC_MID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			if (strcmp(next->name, label_id->id) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 				/* adjust next resource up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 				rc = adjust_resource(next, next->start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 						- allocate, resource_size(next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 						+ allocate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 				new_res = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 				action = "next grow up";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			} else if (strcmp(res->name, label_id->id) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				action = "grow down";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 				action = "allocate";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		case ALLOC_AFTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			if (strcmp(res->name, label_id->id) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 				action = "grow down";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 				action = "allocate";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		if (strcmp(action, "allocate") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			/* BLK allocate bottom up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			if (!is_pmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 				valid.start += available - allocate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			new_res = nvdimm_allocate_dpa(ndd, label_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 					valid.start, allocate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			if (!new_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 				rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		} else if (strcmp(action, "grow down") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			/* adjust current resource down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			rc = adjust_resource(res, res->start, resource_size(res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 					+ allocate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 				res->flags |= DPA_RESOURCE_ADJUSTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		if (!new_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 			new_res = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 				action, loc, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		n -= allocate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			 * Retry scan with newly inserted resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			 * For example, if we did an ALLOC_BEFORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			 * insertion there may also have been space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			 * available for an ALLOC_AFTER insertion, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			 * need to check this same resource again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	 * If we allocated nothing in the BLK case it may be because we are in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	 * an initial "pmem-reserve pass".  Only do an initial BLK allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	 * when none of the DPA space is reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) static int merge_dpa(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	if (strncmp("pmem", label_id->id, 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	for_each_dpa_resource(ndd, res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		struct resource *next = res->sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		resource_size_t end = res->start + resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		if (!next || strcmp(res->name, label_id->id) != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 				|| strcmp(next->name, label_id->id) != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 				|| end != next->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		end += resource_size(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		nvdimm_free_dpa(ndd, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		rc = adjust_resource(res, res->start, end - res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		res->flags |= DPA_RESOURCE_ADJUSTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) int __reserve_free_pmem(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	struct nvdimm *nvdimm = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	struct nd_region *nd_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (!is_memory(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	nd_region = to_nd_region(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (nd_region->ndr_mappings == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	memset(&label_id, 0, sizeof(label_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	strcat(label_id.id, "pmem-reserve");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		resource_size_t n, rem = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		if (nd_mapping->nvdimm != nvdimm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		dev_WARN_ONCE(&nd_region->dev, rem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 				"pmem reserve underrun: %#llx of %#llx bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 				(unsigned long long) n - rem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 				(unsigned long long) n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		return rem ? -ENXIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		struct nd_mapping *nd_mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	struct resource *res, *_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	for_each_dpa_resource_safe(ndd, res, _res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (strcmp(res->name, "pmem-reserve") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			nvdimm_free_dpa(ndd, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		struct nd_mapping *nd_mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct nvdimm *nvdimm = nd_mapping->nvdimm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			__reserve_free_pmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		release_free_pmem(nvdimm_bus, nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  * @nd_region: the set of dimms to allocate @n more bytes from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  * @label_id: unique identifier for the namespace consuming this dpa range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  * @n: number of bytes per-dimm to add to the existing allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  * Assumes resources are ordered.  For BLK regions, first consume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  * BLK-only available DPA free space, then consume PMEM-aliased DPA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  * space starting at the highest DPA.  For PMEM regions start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  * allocations from the start of an interleave set and end at the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * BLK allocation or the end of the interleave set, whichever comes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) static int grow_dpa_allocation(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		struct nd_label_id *label_id, resource_size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		resource_size_t rem = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		int rc, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		 * In the BLK case try once with all unallocated PMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		 * reserved, and once without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		for (j = is_pmem; j < 2; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			bool blk_only = j == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			if (blk_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 				rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			rem = scan_allocate(nd_region, nd_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 					label_id, rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			if (blk_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 				release_free_pmem(nvdimm_bus, nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			/* try again and allow encroachments into PMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			if (rem == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		dev_WARN_ONCE(&nd_region->dev, rem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 				"allocation underrun: %#llx of %#llx bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 				(unsigned long long) n - rem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 				(unsigned long long) n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		if (rem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		rc = merge_dpa(nd_region, nd_mapping, label_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		struct nd_namespace_pmem *nspm, resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	struct resource *res = &nspm->nsio.res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	resource_size_t offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (size && !nspm->uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	if (size && nspm->uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		struct nd_mapping *nd_mapping = &nd_region->mapping[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (!ndd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		nd_label_gen_id(&label_id, nspm->uuid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		/* calculate a spa offset from the dpa allocation offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			if (strcmp(res->name, label_id.id) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 				offset = (res->start - nd_mapping->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 					* nd_region->ndr_mappings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	res->start = nd_region->ndr_start + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	res->end = res->start + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (!uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		dev_dbg(dev, "%s: uuid not set\n", where);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) static ssize_t __size_store(struct device *dev, unsigned long long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	resource_size_t allocated = 0, available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	struct nd_namespace_common *ndns = to_ndns(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	struct nd_mapping *nd_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	struct nvdimm_drvdata *ndd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	u32 flags = 0, remainder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	int rc, i, id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	u8 *uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	if (dev->driver || ndns->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		uuid = nspm->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		id = nspm->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		uuid = nsblk->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		flags = NSLABEL_FLAG_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		id = nsblk->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	 * We need a uuid for the allocation-label and dimm(s) on which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	 * to store the label.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (uuid_not_set(uuid, dev, __func__))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	if (nd_region->ndr_mappings == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		dev_dbg(dev, "not associated with dimm(s)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	div_u64_rem(val, nd_region->align, &remainder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (remainder) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		dev_dbg(dev, "%llu is not %ldK aligned\n", val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 				nd_region->align / SZ_1K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	nd_label_gen_id(&label_id, uuid, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		 * All dimms in an interleave set, or the base dimm for a blk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		 * region, need to be enabled for the size to be changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		if (!ndd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		allocated += nvdimm_allocated_dpa(ndd, &label_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	available = nd_region_allocatable_dpa(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (val > available + allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (val == allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	val = div_u64(val, nd_region->ndr_mappings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	allocated = div_u64(allocated, nd_region->ndr_mappings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (val < allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		rc = shrink_dpa_allocation(nd_region, &label_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 				allocated - val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		nd_namespace_pmem_set_resource(nd_region, nspm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 				val * nd_region->ndr_mappings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	 * Try to delete the namespace if we deleted all of its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	 * allocation, this is not the seed or 0th device for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	 * region, and it is not actively claimed by a btt, pfn, or dax
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	 * instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		nd_device_unregister(dev, ND_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) static ssize_t size_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	unsigned long long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	u8 **uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	rc = kstrtoull(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	wait_nvdimm_bus_probe_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	rc = __size_store(dev, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		rc = nd_namespace_label_update(nd_region, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		uuid = &nspm->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		uuid = &nsblk->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	if (rc == 0 && val == 0 && uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		/* setting size zero == 'delete namespace' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		kfree(*uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		*uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	return rc < 0 ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	struct device *dev = &ndns->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		return resource_size(&nspm->nsio.res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		return nd_namespace_blk_size(to_nd_namespace_blk(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	} else if (is_namespace_io(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		return resource_size(&nsio->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		WARN_ONCE(1, "unknown namespace type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	resource_size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	nvdimm_bus_lock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	size = __nvdimm_namespace_capacity(ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	nvdimm_bus_unlock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) EXPORT_SYMBOL(nvdimm_namespace_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	bool locked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	struct device *dev = &ndns->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 			locked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	return locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) EXPORT_SYMBOL(nvdimm_namespace_locked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) static ssize_t size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	return sprintf(buf, "%llu\n", (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 			nvdimm_namespace_capacity(to_ndns(dev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) static DEVICE_ATTR(size, 0444, size_show, size_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) static u8 *namespace_to_uuid(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		return nspm->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		return nsblk->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		return ERR_PTR(-ENXIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static ssize_t uuid_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	u8 *uuid = namespace_to_uuid(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	if (IS_ERR(uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		return PTR_ERR(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	if (uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		return sprintf(buf, "%pUb\n", uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	return sprintf(buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)  * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)  * @nd_region: parent region so we can updates all dimms in the set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  * @dev: namespace type for generating label_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  * @new_uuid: incoming uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)  * @old_uuid: reference to the uuid storage location in the namespace object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) static int namespace_update_uuid(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		struct device *dev, u8 *new_uuid, u8 **old_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	struct nd_label_id old_label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	struct nd_label_id new_label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	if (!nd_is_uuid_unique(dev, new_uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (*old_uuid == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	 * If we've already written a label with this uuid, then it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	 * too late to rename because we can't reliably update the uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	 * without losing the old namespace.  Userspace must delete this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	 * namespace to abandon the old uuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		 * This check by itself is sufficient because old_uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		 * would be NULL above if this uuid did not exist in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		 * currently written set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		 * FIXME: can we delete uuid with zero dpa allocated?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		if (list_empty(&nd_mapping->labels))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	nd_label_gen_id(&old_label_id, *old_uuid, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	nd_label_gen_id(&new_label_id, new_uuid, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		struct nd_label_ent *label_ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			if (strcmp(res->name, old_label_id.id) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 				sprintf((void *) res->name, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 						new_label_id.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		mutex_lock(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		list_for_each_entry(label_ent, &nd_mapping->labels, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			struct nd_namespace_label *nd_label = label_ent->label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			if (!nd_label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			nd_label_gen_id(&label_id, nd_label->uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 					__le32_to_cpu(nd_label->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			if (strcmp(old_label_id.id, label_id.id) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 				set_bit(ND_LABEL_REAP, &label_ent->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		mutex_unlock(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	kfree(*old_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	*old_uuid = new_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) static ssize_t uuid_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	u8 *uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	u8 **ns_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		ns_uuid = &nspm->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		ns_uuid = &nsblk->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	wait_nvdimm_bus_probe_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if (to_ndns(dev)->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		rc = nd_uuid_store(dev, &uuid, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		rc = nd_namespace_label_update(nd_region, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		kfree(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			buf[len - 1] == '\n' ? "" : "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	return rc < 0 ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) static DEVICE_ATTR_RW(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) static ssize_t resource_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		res = &nspm->nsio.res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	} else if (is_namespace_io(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		res = &nsio->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	/* no address to convey if the namespace has no allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (resource_size(res) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) static DEVICE_ATTR_ADMIN_RO(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	4096, 4104, 4160, 4224, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) static ssize_t sector_size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		return nd_size_select_show(nsblk->lbasize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 				blk_lbasize_supported, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		return nd_size_select_show(nspm->lbasize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				pmem_lbasize_supported, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) static ssize_t sector_size_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	const unsigned long *supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	unsigned long *lbasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		lbasize = &nsblk->lbasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		supported = blk_lbasize_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	} else if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		lbasize = &nspm->lbasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		supported = pmem_lbasize_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	if (to_ndns(dev)->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		rc = nd_size_select_store(dev, buf, lbasize, supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		rc = nd_namespace_label_update(nd_region, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			buf, buf[len - 1] == '\n' ? "" : "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	return rc ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) static DEVICE_ATTR_RW(sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) static ssize_t dpa_extents_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	int count = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	u8 *uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	if (is_namespace_pmem(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		uuid = nspm->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	} else if (is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		uuid = nsblk->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		flags = NSLABEL_FLAG_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (!uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	nd_label_gen_id(&label_id, uuid, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			if (strcmp(res->name, label_id.id) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 				count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	return sprintf(buf, "%d\n", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) static DEVICE_ATTR_RO(dpa_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) static int btt_claim_class(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	int i, loop_bitmask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		struct nd_namespace_index *nsindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		 * If any of the DIMMs do not support labels the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		 * possible BTT format is v1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		if (!ndd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			loop_bitmask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		nsindex = to_namespace_index(ndd, ndd->ns_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		if (nsindex == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			loop_bitmask |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 			/* check whether existing labels are v1.1 or v1.2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			if (__le16_to_cpu(nsindex->major) == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 					&& __le16_to_cpu(nsindex->minor) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 				loop_bitmask |= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 				loop_bitmask |= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	 * block is found, a v1.1 label for any mapping will set bit 1, and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	 * v1.2 label will set bit 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	 * At the end of the loop, at most one of the three bits must be set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	 * If multiple bits were set, it means the different mappings disagree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	 * about their labels, and this must be cleaned up first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	 * If all the label index blocks are found to agree, nsindex of NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	 * implies labels haven't been initialized yet, and when they will,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	 * they will be of the 1.2 format, so we can assume BTT2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	 * found, we enforce BTT2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	switch (loop_bitmask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		return NVDIMM_CCLASS_BTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		return NVDIMM_CCLASS_BTT2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) static ssize_t holder_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	struct nd_namespace_common *ndns = to_ndns(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) static DEVICE_ATTR_RO(holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) static int __holder_class_store(struct device *dev, const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	struct nd_namespace_common *ndns = to_ndns(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	if (dev->driver || ndns->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	if (sysfs_streq(buf, "btt")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		int rc = btt_claim_class(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		if (rc < NVDIMM_CCLASS_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		ndns->claim_class = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	} else if (sysfs_streq(buf, "pfn"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		ndns->claim_class = NVDIMM_CCLASS_PFN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	else if (sysfs_streq(buf, "dax"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		ndns->claim_class = NVDIMM_CCLASS_DAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	else if (sysfs_streq(buf, ""))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		ndns->claim_class = NVDIMM_CCLASS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) static ssize_t holder_class_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	wait_nvdimm_bus_probe_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	rc = __holder_class_store(dev, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		rc = nd_namespace_label_update(nd_region, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	return rc < 0 ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) static ssize_t holder_class_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	struct nd_namespace_common *ndns = to_ndns(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	if (ndns->claim_class == NVDIMM_CCLASS_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		rc = sprintf(buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			(ndns->claim_class == NVDIMM_CCLASS_BTT2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		rc = sprintf(buf, "btt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		rc = sprintf(buf, "pfn\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		rc = sprintf(buf, "dax\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		rc = sprintf(buf, "<unknown>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) static DEVICE_ATTR_RW(holder_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) static ssize_t mode_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	struct nd_namespace_common *ndns = to_ndns(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	struct device *claim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	char *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	claim = ndns->claim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	if (claim && is_nd_btt(claim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		mode = "safe";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	else if (claim && is_nd_pfn(claim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		mode = "memory";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	else if (claim && is_nd_dax(claim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		mode = "dax";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	else if (!claim && pmem_should_map_pages(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		mode = "memory";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		mode = "raw";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	rc = sprintf(buf, "%s\n", mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) static DEVICE_ATTR_RO(mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) static ssize_t force_raw_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	bool force_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	int rc = strtobool(buf, &force_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	to_ndns(dev)->force_raw = force_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) static ssize_t force_raw_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) static DEVICE_ATTR_RW(force_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) static struct attribute *nd_namespace_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	&dev_attr_nstype.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	&dev_attr_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	&dev_attr_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	&dev_attr_uuid.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	&dev_attr_holder.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	&dev_attr_resource.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	&dev_attr_alt_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	&dev_attr_force_raw.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	&dev_attr_sector_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	&dev_attr_dpa_extents.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	&dev_attr_holder_class.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) static umode_t namespace_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		struct attribute *a, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	struct device *dev = container_of(kobj, struct device, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	if (a == &dev_attr_resource.attr && is_namespace_blk(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		if (a == &dev_attr_size.attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 			return 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		return a->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	/* base is_namespace_io() attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	    a == &dev_attr_holder.attr || a == &dev_attr_holder_class.attr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	    a == &dev_attr_force_raw.attr || a == &dev_attr_mode.attr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	    a == &dev_attr_resource.attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		return a->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) static struct attribute_group nd_namespace_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	.attrs = nd_namespace_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	.is_visible = namespace_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) static const struct attribute_group *nd_namespace_attribute_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	&nd_device_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	&nd_namespace_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	&nd_numa_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) static const struct device_type namespace_io_device_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	.name = "nd_namespace_io",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	.release = namespace_io_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	.groups = nd_namespace_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) static const struct device_type namespace_pmem_device_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	.name = "nd_namespace_pmem",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	.release = namespace_pmem_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	.groups = nd_namespace_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) static const struct device_type namespace_blk_device_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	.name = "nd_namespace_blk",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	.release = namespace_blk_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	.groups = nd_namespace_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) static bool is_namespace_pmem(const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	return dev ? dev->type == &namespace_pmem_device_type : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) static bool is_namespace_blk(const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	return dev ? dev->type == &namespace_blk_device_type : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) static bool is_namespace_io(const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	return dev ? dev->type == &namespace_io_device_type : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	struct nd_namespace_common *ndns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	resource_size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	if (nd_btt || nd_pfn || nd_dax) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		if (nd_btt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 			ndns = nd_btt->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		else if (nd_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 			ndns = nd_pfn->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 		else if (nd_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 			ndns = nd_dax->nd_pfn.ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		if (!ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 			return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		 * Flush any in-progess probes / removals in the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		 * for the raw personality of this namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		nd_device_lock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		nd_device_unlock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		if (ndns->dev.driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			dev_dbg(&ndns->dev, "is active, can't bind %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 					dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 			return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 					"host (%s) vs claim (%s) mismatch\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 					dev_name(dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 					dev_name(ndns->claim)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 			return ERR_PTR(-ENXIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		ndns = to_ndns(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		if (ndns->claim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 			dev_dbg(dev, "claimed by %s, failing probe\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 				dev_name(ndns->claim));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 			return ERR_PTR(-ENXIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	if (nvdimm_namespace_locked(ndns))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		return ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	size = nvdimm_namespace_capacity(ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	if (size < ND_MIN_NAMESPACE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 				&size, ND_MIN_NAMESPACE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	 * Note, alignment validation for fsdax and devdax mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	 * namespaces happens in nd_pfn_validate() where infoblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	 * padding parameters can be applied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	if (pmem_should_map_pages(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		struct resource *res = &nsio->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		if (!IS_ALIGNED(res->start | (res->end + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 					memremap_compat_align())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	if (is_namespace_pmem(&ndns->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 		struct nd_namespace_pmem *nspm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		nspm = to_nd_namespace_pmem(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 			return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	} else if (is_namespace_blk(&ndns->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		struct nd_namespace_blk *nsblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		nsblk = to_nd_namespace_blk(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 			return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 		if (!nsblk->lbasize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 			dev_dbg(&ndns->dev, "sector size not set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 			return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		if (!nd_namespace_blk_validate(nsblk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 			return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	return ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) EXPORT_SYMBOL(nvdimm_namespace_common_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	if (is_namespace_blk(&ndns->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) EXPORT_SYMBOL_GPL(devm_namespace_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	if (is_namespace_blk(&ndns->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) EXPORT_SYMBOL_GPL(devm_namespace_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) static struct device **create_namespace_io(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	struct nd_namespace_io *nsio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	struct device *dev, **devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	if (!nsio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	if (!devs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		kfree(nsio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	dev = &nsio->common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	dev->type = &namespace_io_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	dev->parent = &nd_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	res = &nsio->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	res->name = dev_name(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	res->flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	res->start = nd_region->ndr_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	res->end = res->start + nd_region->ndr_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	devs[0] = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	return devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		u64 cookie, u16 pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	struct nd_namespace_label *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		struct nd_interleave_set *nd_set = nd_region->nd_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		struct nd_label_ent *label_ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		bool found_uuid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		list_for_each_entry(label_ent, &nd_mapping->labels, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 			struct nd_namespace_label *nd_label = label_ent->label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 			u16 position, nlabel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 			u64 isetcookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 			if (!nd_label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 			isetcookie = __le64_to_cpu(nd_label->isetcookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 			position = __le16_to_cpu(nd_label->position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 			nlabel = __le16_to_cpu(nd_label->nlabel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 			if (isetcookie != cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 			if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			if (namespace_label_has(ndd, type_guid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 					&& !guid_equal(&nd_set->type_guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 						&nd_label->type_guid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 				dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 						&nd_set->type_guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 						&nd_label->type_guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 			if (found_uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 				dev_dbg(ndd->dev, "duplicate entry for uuid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 			found_uuid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 			if (nlabel != nd_region->ndr_mappings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 			if (position != pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 			found = nd_label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	return found != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	if (!pmem_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		struct nd_namespace_label *nd_label = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		u64 hw_start, hw_end, pmem_start, pmem_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		struct nd_label_ent *label_ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		lockdep_assert_held(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		list_for_each_entry(label_ent, &nd_mapping->labels, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 			nd_label = label_ent->label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 			if (!nd_label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 			if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 			nd_label = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		if (!nd_label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		 * Check that this label is compliant with the dpa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		 * range published in NFIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		hw_start = nd_mapping->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 		hw_end = hw_start + nd_mapping->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		pmem_start = __le64_to_cpu(nd_label->dpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		if (pmem_start >= hw_start && pmem_start < hw_end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 				&& pmem_end <= hw_end && pmem_end > hw_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 			/* pass */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 			dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 					dev_name(ndd->dev), nd_label->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		/* move recently validated label to the front of the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		list_move(&label_ent->list, &nd_mapping->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)  * create_namespace_pmem - validate interleave set labelling, retrieve label0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)  * @nd_region: region with mappings to validate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)  * @nspm: target namespace to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)  * @nd_label: target pmem namespace label to evaluate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) static struct device *create_namespace_pmem(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		struct nd_namespace_index *nsindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		struct nd_namespace_label *nd_label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	struct nd_label_ent *label_ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	struct nd_namespace_pmem *nspm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	struct nd_mapping *nd_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	resource_size_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	u16 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	if (cookie == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 		return ERR_PTR(-ENXIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 				nd_label->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 			return ERR_PTR(-EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 				nd_label->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	if (!nspm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	nspm->id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	dev = &nspm->nsio.common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	dev->type = &namespace_pmem_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	dev->parent = &nd_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	res = &nspm->nsio.res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	res->name = dev_name(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	res->flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	if (i < nd_region->ndr_mappings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		 * Give up if we don't find an instance of a uuid at each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		 * position (from 0 to nd_region->ndr_mappings - 1), or if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		 * find a dimm with two instances of the same uuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		dev_err(&nd_region->dev, "%s missing label for %pUb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 				nvdimm_name(nvdimm), nd_label->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	 * Fix up each mapping's 'labels' to have the validated pmem label for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	 * that position at labels[0], and NULL at labels[1].  In the process,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	 * check that the namespace aligns with interleave-set.  We know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	 * that it does not overlap with any blk namespaces by virtue of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	 * the dimm being enabled (i.e. nd_label_reserve_dpa()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	 * succeeded).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	rc = select_pmem_id(nd_region, nd_label->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	/* Calculate total size and populate namespace properties from label0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 		struct nd_namespace_label *label0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		struct nvdimm_drvdata *ndd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		label_ent = list_first_entry_or_null(&nd_mapping->labels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 				typeof(*label_ent), list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		label0 = label_ent ? label_ent->label : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		if (!label0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		size += __le64_to_cpu(label0->rawsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		if (__le16_to_cpu(label0->position) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		WARN_ON(nspm->alt_name || nspm->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		nspm->alt_name = kmemdup((void __force *) label0->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 				NSLABEL_NAME_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		nspm->uuid = kmemdup((void __force *) label0->uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 				NSLABEL_UUID_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		nspm->lbasize = __le64_to_cpu(label0->lbasize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 		ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		if (namespace_label_has(ndd, abstraction_guid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 			nspm->nsio.common.claim_class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 				= to_nvdimm_cclass(&label0->abstraction_guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	if (!nspm->alt_name || !nspm->uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	nd_namespace_pmem_set_resource(nd_region, nspm, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	namespace_pmem_release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	switch (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	case -EINVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		dev_dbg(&nd_region->dev, "invalid label(s)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	case -ENODEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		dev_dbg(&nd_region->dev, "label not found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 		dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) struct resource *nsblk_add_resource(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 		resource_size_t start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	struct nd_label_id label_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	res = krealloc(nsblk->res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			sizeof(void *) * (nsblk->num_resources + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	nsblk->res = (struct resource **) res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	for_each_dpa_resource(ndd, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 		if (strcmp(res->name, label_id.id) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 				&& res->start == start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 			nsblk->res[nsblk->num_resources++] = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 			return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	struct nd_namespace_blk *nsblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	if (!is_nd_blk(&nd_region->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	if (!nsblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	dev = &nsblk->common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	dev->type = &namespace_blk_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	if (nsblk->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		kfree(nsblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	dev->parent = &nd_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	return &nsblk->common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	struct nd_namespace_pmem *nspm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	if (!is_memory(&nd_region->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	if (!nspm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	dev = &nspm->nsio.common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	dev->type = &namespace_pmem_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	dev->parent = &nd_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	res = &nspm->nsio.res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	res->name = dev_name(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	res->flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	if (nspm->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		kfree(nspm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	nd_namespace_pmem_set_resource(nd_region, nspm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) void nd_region_create_ns_seed(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	if (is_nd_blk(&nd_region->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 		nd_region->ns_seed = nd_namespace_blk_create(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 		nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	 * Seed creation failures are not fatal, provisioning is simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	 * disabled until memory becomes available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	if (!nd_region->ns_seed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 		dev_err(&nd_region->dev, "failed to create %s namespace\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 				is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 		nd_device_register(nd_region->ns_seed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) void nd_region_create_dax_seed(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	nd_region->dax_seed = nd_dax_create(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	 * Seed creation failures are not fatal, provisioning is simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	 * disabled until memory becomes available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	if (!nd_region->dax_seed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		dev_err(&nd_region->dev, "failed to create dax namespace\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) void nd_region_create_pfn_seed(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	nd_region->pfn_seed = nd_pfn_create(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	 * Seed creation failures are not fatal, provisioning is simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	 * disabled until memory becomes available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	if (!nd_region->pfn_seed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		dev_err(&nd_region->dev, "failed to create pfn namespace\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) void nd_region_create_btt_seed(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	nd_region->btt_seed = nd_btt_create(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	 * Seed creation failures are not fatal, provisioning is simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	 * disabled until memory becomes available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	if (!nd_region->btt_seed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		dev_err(&nd_region->dev, "failed to create btt namespace\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) static int add_namespace_resource(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		struct nd_namespace_label *nd_label, struct device **devs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 		u8 *uuid = namespace_to_uuid(devs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		if (IS_ERR_OR_NULL(uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 		if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 		if (is_namespace_blk(devs[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 			res = nsblk_add_resource(nd_region, ndd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 					to_nd_namespace_blk(devs[i]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 					__le64_to_cpu(nd_label->dpa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 			if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 				return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 			nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 			dev_err(&nd_region->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 					"error: conflicting extents for uuid: %pUb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 					nd_label->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) static struct device *create_namespace_blk(struct nd_region *nd_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 		struct nd_namespace_label *nd_label, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 	struct nd_interleave_set *nd_set = nd_region->nd_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	struct nd_namespace_blk *nsblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	char name[NSLABEL_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	struct device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	if (namespace_label_has(ndd, type_guid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 			dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 					&nd_set->type_guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 					&nd_label->type_guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 			return ERR_PTR(-EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 			dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 					nd_set->cookie2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 					__le64_to_cpu(nd_label->isetcookie));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 			return ERR_PTR(-EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	if (!nsblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	dev = &nsblk->common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	dev->type = &namespace_blk_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	dev->parent = &nd_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	nsblk->id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	if (namespace_label_has(ndd, abstraction_guid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 		nsblk->common.claim_class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 			= to_nvdimm_cclass(&nd_label->abstraction_guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	if (!nsblk->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 		goto blk_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	if (name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 		nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 		if (!nsblk->alt_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 			goto blk_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	res = nsblk_add_resource(nd_region, ndd, nsblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 			__le64_to_cpu(nd_label->dpa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 		goto blk_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312)  blk_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	namespace_blk_release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	return ERR_PTR(-ENXIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) static int cmp_dpa(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	const struct device *dev_a = *(const struct device **) a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	const struct device *dev_b = *(const struct device **) b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	struct nd_namespace_blk *nsblk_a, *nsblk_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	struct nd_namespace_pmem *nspm_a, *nspm_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	if (is_namespace_io(dev_a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	if (is_namespace_blk(dev_a)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 		nsblk_a = to_nd_namespace_blk(dev_a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		nsblk_b = to_nd_namespace_blk(dev_b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 		return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 				sizeof(resource_size_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	nspm_a = to_nd_namespace_pmem(dev_a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	nspm_b = to_nd_namespace_pmem(dev_b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 			sizeof(resource_size_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) static struct device **scan_labels(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	int i, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	struct device *dev, **devs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	struct nd_label_ent *label_ent, *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	/* "safe" because create_namespace_pmem() might list_move() label_ent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		struct nd_namespace_label *nd_label = label_ent->label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		struct device **__devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		if (!nd_label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		flags = __le32_to_cpu(nd_label->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		if (is_nd_blk(&nd_region->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 				== !!(flags & NSLABEL_FLAG_LOCAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 			/* pass, region matches label type */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		/* skip labels that describe extents outside of the region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 		if (__le64_to_cpu(nd_label->dpa) < nd_mapping->start ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		    __le64_to_cpu(nd_label->dpa) > map_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 		i = add_namespace_resource(nd_region, nd_label, devs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 		if (i < count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		__devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		if (!__devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		memcpy(__devs, devs, sizeof(dev) * count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 		kfree(devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 		devs = __devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		if (is_nd_blk(&nd_region->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 			dev = create_namespace_blk(nd_region, nd_label, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 			struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 			struct nd_namespace_index *nsindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 			nsindex = to_namespace_index(ndd, ndd->ns_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 			dev = create_namespace_pmem(nd_region, nsindex, nd_label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		if (IS_ERR(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 			switch (PTR_ERR(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 			case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 				/* skip invalid labels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 			case -ENODEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 				/* fallthrough to seed creation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 			devs[count++] = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 			count, is_nd_blk(&nd_region->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 			? "blk" : "pmem", count == 1 ? "" : "s");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	if (count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		/* Publish a zero-sized namespace for userspace to configure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 		nd_mapping_free_labels(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 		if (!devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 		if (is_nd_blk(&nd_region->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 			struct nd_namespace_blk *nsblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 			nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 			if (!nsblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 			dev = &nsblk->common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 			dev->type = &namespace_blk_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 			struct nd_namespace_pmem *nspm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 			nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 			if (!nspm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 			dev = &nspm->nsio.common.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 			dev->type = &namespace_pmem_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 			nd_namespace_pmem_set_resource(nd_region, nspm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 		dev->parent = &nd_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 		devs[count++] = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	} else if (is_memory(&nd_region->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 		/* clean unselected labels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 		for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 			struct list_head *l, *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 			LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 			int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 			nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 			if (list_empty(&nd_mapping->labels)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 				WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 			j = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 			list_for_each_safe(l, e, &nd_mapping->labels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 				if (!j--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 				list_move_tail(l, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 			nd_mapping_free_labels(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 			list_splice_init(&list, &nd_mapping->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	if (count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 		sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	return devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	if (devs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		for (i = 0; devs[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 			if (is_nd_blk(&nd_region->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 				namespace_blk_release(devs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 				namespace_pmem_release(devs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		kfree(devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) static struct device **create_namespaces(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	struct nd_mapping *nd_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	struct device **devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	if (nd_region->ndr_mappings == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	/* lock down all mappings while we scan labels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 		nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 		mutex_lock_nested(&nd_mapping->lock, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	devs = scan_labels(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		int reverse = nd_region->ndr_mappings - 1 - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		nd_mapping = &nd_region->mapping[reverse];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		mutex_unlock(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	return devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) static void deactivate_labels(void *region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	struct nd_region *nd_region = region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 		struct nvdimm_drvdata *ndd = nd_mapping->ndd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		mutex_lock(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 		nd_mapping_free_labels(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		mutex_unlock(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 		put_ndd(ndd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 		nd_mapping->ndd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 		if (ndd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 			atomic_dec(&nvdimm->busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) static int init_active_labels(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	int i, rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	for (i = 0; i < nd_region->ndr_mappings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 		struct nd_label_ent *label_ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 		int count, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 		 * If the dimm is disabled then we may need to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 		 * the region from being activated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 		if (!ndd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 			if (test_bit(NDD_LOCKED, &nvdimm->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 				/* fail, label data may be unreadable */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 			else if (test_bit(NDD_LABELING, &nvdimm->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 				/* fail, labels needed to disambiguate dpa */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 			dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 					dev_name(&nd_mapping->nvdimm->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 					test_bit(NDD_LOCKED, &nvdimm->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 					? "locked" : "disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 			rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 		nd_mapping->ndd = ndd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 		atomic_inc(&nvdimm->busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 		get_ndd(ndd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 		count = nd_label_active_count(ndd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 		dev_dbg(ndd->dev, "count: %d\n", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 		if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 		for (j = 0; j < count; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 			struct nd_namespace_label *label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 			label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 			if (!label_ent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 			label = nd_label_active(ndd, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 			if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 				u32 flags = __le32_to_cpu(label->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 				flags &= ~NSLABEL_FLAG_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 				label->flags = __cpu_to_le32(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 			label_ent->label = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 			mutex_lock(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 			list_add_tail(&label_ent->list, &nd_mapping->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 			mutex_unlock(&nd_mapping->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 		if (j < count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	if (i < nd_region->ndr_mappings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 		deactivate_labels(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 					nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	struct device **devs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	int i, rc = 0, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	*err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	nvdimm_bus_lock(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	rc = init_active_labels(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 		nvdimm_bus_unlock(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 	type = nd_region_to_nstype(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	case ND_DEVICE_NAMESPACE_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		devs = create_namespace_io(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	case ND_DEVICE_NAMESPACE_PMEM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 	case ND_DEVICE_NAMESPACE_BLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 		devs = create_namespaces(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	nvdimm_bus_unlock(&nd_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	if (!devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	for (i = 0; devs[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		struct device *dev = devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		if (type == ND_DEVICE_NAMESPACE_BLK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 			struct nd_namespace_blk *nsblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 			nsblk = to_nd_namespace_blk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 			id = ida_simple_get(&nd_region->ns_ida, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 			nsblk->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		} else if (type == ND_DEVICE_NAMESPACE_PMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 			struct nd_namespace_pmem *nspm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 			nspm = to_nd_namespace_pmem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 			id = ida_simple_get(&nd_region->ns_ida, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 			nspm->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 			id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		if (id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 		dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		nd_device_register(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 		nd_region->ns_seed = devs[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	if (devs[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 		int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		for (j = i; devs[j]; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 			struct device *dev = devs[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 			device_initialize(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 			put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 		*err = j - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 		 * All of the namespaces we tried to register failed, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 		 * fail region activation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 		if (*err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 			rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	kfree(devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	if (rc == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) }