^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-2016 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/memremap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/genhd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "nd-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "pfn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "nd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static void nd_pfn_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct nd_pfn *nd_pfn = to_nd_pfn(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) dev_dbg(dev, "trace\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) kfree(nd_pfn->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) kfree(nd_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct nd_pfn *to_nd_pfn(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) WARN_ON(!is_nd_pfn(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return nd_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) EXPORT_SYMBOL(to_nd_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static ssize_t mode_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) switch (nd_pfn->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) case PFN_MODE_RAM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return sprintf(buf, "ram\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) case PFN_MODE_PMEM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return sprintf(buf, "pmem\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return sprintf(buf, "none\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static ssize_t mode_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) size_t n = len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (strncmp(buf, "pmem\n", n) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) || strncmp(buf, "pmem", n) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) nd_pfn->mode = PFN_MODE_PMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) } else if (strncmp(buf, "ram\n", n) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) || strncmp(buf, "ram", n) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) nd_pfn->mode = PFN_MODE_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) else if (strncmp(buf, "none\n", n) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) || strncmp(buf, "none", n) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) nd_pfn->mode = PFN_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) buf[len - 1] == '\n' ? "" : "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return rc ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static DEVICE_ATTR_RW(mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static ssize_t align_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return sprintf(buf, "%ld\n", nd_pfn->align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static unsigned long *nd_pfn_supported_alignments(unsigned long *alignments)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) alignments[0] = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (has_transparent_hugepage()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) alignments[1] = HPAGE_PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) alignments[2] = HPAGE_PUD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return alignments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * Use pmd mapping if supported as default alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static unsigned long nd_pfn_default_alignment(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (has_transparent_hugepage())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return HPAGE_PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static ssize_t align_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) rc = nd_size_select_store(dev, buf, &nd_pfn->align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) nd_pfn_supported_alignments(aligns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) buf[len - 1] == '\n' ? "" : "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return rc ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static DEVICE_ATTR_RW(align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static ssize_t uuid_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (nd_pfn->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return sprintf(buf, "%pUb\n", nd_pfn->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return sprintf(buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static ssize_t uuid_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) buf[len - 1] == '\n' ? "" : "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return rc ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static DEVICE_ATTR_RW(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static ssize_t namespace_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) rc = sprintf(buf, "%s\n", nd_pfn->ndns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ? dev_name(&nd_pfn->ndns->dev) : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static ssize_t namespace_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct device_attribute *attr, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) nvdimm_bus_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) buf[len - 1] == '\n' ? "" : "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) nvdimm_bus_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static DEVICE_ATTR_RW(namespace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static ssize_t resource_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) u64 offset = __le64_to_cpu(pfn_sb->dataoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct nd_namespace_common *ndns = nd_pfn->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) + start_pad + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* no address to convey if the pfn instance is disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static DEVICE_ATTR_ADMIN_RO(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static ssize_t size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) nd_device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) u64 offset = __le64_to_cpu(pfn_sb->dataoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct nd_namespace_common *ndns = nd_pfn->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) rc = sprintf(buf, "%llu\n", (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) resource_size(&nsio->res) - start_pad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) - end_trunc - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* no size to convey if the pfn instance is disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) nd_device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static DEVICE_ATTR_RO(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static ssize_t supported_alignments_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return nd_size_select_show(0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) nd_pfn_supported_alignments(aligns), buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static DEVICE_ATTR_RO(supported_alignments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static struct attribute *nd_pfn_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) &dev_attr_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) &dev_attr_namespace.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) &dev_attr_uuid.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) &dev_attr_align.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) &dev_attr_resource.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) &dev_attr_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) &dev_attr_supported_alignments.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static struct attribute_group nd_pfn_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) .attrs = nd_pfn_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) const struct attribute_group *nd_pfn_attribute_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) &nd_pfn_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) &nd_device_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) &nd_numa_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static const struct device_type nd_pfn_device_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .name = "nd_pfn",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .release = nd_pfn_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .groups = nd_pfn_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) bool is_nd_pfn(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return dev ? dev->type == &nd_pfn_device_type : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) EXPORT_SYMBOL(is_nd_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!nd_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) nd_pfn->mode = PFN_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) nd_pfn->align = nd_pfn_default_alignment();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dev = &nd_pfn->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) device_initialize(&nd_pfn->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) dev_name(ndns->claim));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct nd_pfn *nd_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!nd_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (nd_pfn->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) kfree(nd_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) dev = &nd_pfn->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) dev->type = &nd_pfn_device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) dev->parent = &nd_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return nd_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct device *nd_pfn_create(struct nd_region *nd_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct nd_pfn *nd_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!is_memory(&nd_region->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) nd_pfn = nd_pfn_alloc(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) dev = nd_pfn_devinit(nd_pfn, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) __nd_device_register(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^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) * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * space associated with the namespace. If the memmap is set to DRAM, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * this is a no-op. Since the memmap area is freshly initialized during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * probe, we have an opportunity to clear any badblocks in this area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct nd_namespace_common *ndns = nd_pfn->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) void *zero_page = page_address(ZERO_PAGE(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int num_bad, meta_num, rc, bb_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) sector_t first_bad, meta_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct nd_namespace_io *nsio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (nd_pfn->mode != PFN_MODE_PMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * re-enable the namespace with correct size so that we can access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * the device memmap area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) devm_namespace_disable(&nd_pfn->dev, ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) rc = devm_namespace_enable(&nd_pfn->dev, ndns, le64_to_cpu(pfn_sb->dataoff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) unsigned long zero_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) u64 nsoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) bb_present = badblocks_check(&nd_region->bb, meta_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) meta_num, &first_bad, &num_bad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (bb_present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) num_bad, first_bad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) nsoff = ALIGN_DOWN((nd_region->ndr_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) + (first_bad << 9)) - nsio->res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) zero_len = ALIGN(num_bad << 9, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) while (zero_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) unsigned long chunk = min(zero_len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) rc = nvdimm_write_bytes(ndns, nsoff, zero_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) chunk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) zero_len -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) nsoff += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) dev_err(&nd_pfn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) "error clearing %x badblocks at %llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) num_bad, first_bad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) } while (bb_present);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static bool nd_supported_alignment(unsigned long align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) unsigned long supported[MAX_NVDIMM_ALIGN] = { [0] = 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (align == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) nd_pfn_supported_alignments(supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) for (i = 0; supported[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (align == supported[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * nd_pfn_validate - read and validate info-block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * @nd_pfn: fsdax namespace runtime state / properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * @sig: 'devdax' or 'fsdax' signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * Upon return the info-block buffer contents (->pfn_sb) are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * indeterminate when validation fails, and a coherent info-block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) u64 checksum, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) enum nd_pfn_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct nd_namespace_io *nsio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) unsigned long align, start_pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct nd_namespace_common *ndns = nd_pfn->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (!pfn_sb || !ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (!is_memory(nd_pfn->dev.parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) checksum = le64_to_cpu(pfn_sb->checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) pfn_sb->checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) pfn_sb->checksum = cpu_to_le64(checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) pfn_sb->start_pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) pfn_sb->end_trunc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (__le16_to_cpu(pfn_sb->version_minor) < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) pfn_sb->align = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (__le16_to_cpu(pfn_sb->version_minor) < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) pfn_sb->page_struct_size = cpu_to_le16(64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) switch (le32_to_cpu(pfn_sb->mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) case PFN_MODE_RAM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) case PFN_MODE_PMEM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) align = le32_to_cpu(pfn_sb->align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) offset = le64_to_cpu(pfn_sb->dataoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) start_pad = le32_to_cpu(pfn_sb->start_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (align == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) align = 1UL << ilog2(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) mode = le32_to_cpu(pfn_sb->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if ((le32_to_cpu(pfn_sb->page_size) > PAGE_SIZE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) (mode == PFN_MODE_PMEM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) dev_err(&nd_pfn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) "init failed, page size mismatch %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) le32_to_cpu(pfn_sb->page_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if ((le16_to_cpu(pfn_sb->page_struct_size) < sizeof(struct page)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) (mode == PFN_MODE_PMEM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) dev_err(&nd_pfn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) "init failed, struct page size mismatch %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) le16_to_cpu(pfn_sb->page_struct_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return -EOPNOTSUPP;
^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) * Check whether the we support the alignment. For Dax if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * superblock alignment is not matching, we won't initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (!nd_supported_alignment(align) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) !memcmp(pfn_sb->signature, DAX_SIG, PFN_SIG_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) dev_err(&nd_pfn->dev, "init failed, alignment mismatch: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) "%ld:%ld\n", nd_pfn->align, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!nd_pfn->uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * When probing a namepace via nd_pfn_probe() the uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * is NULL (see: nd_pfn_devinit()) we init settings from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * pfn_sb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (!nd_pfn->uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) nd_pfn->align = align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) nd_pfn->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * When probing a pfn / dax instance we validate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * live settings against the pfn_sb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * If the uuid validates, but other settings mismatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * return EINVAL because userspace has managed to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * the configuration without specifying new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * identification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (nd_pfn->align != align || nd_pfn->mode != mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) dev_err(&nd_pfn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) "init failed, settings mismatch\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) nd_pfn->align, align, nd_pfn->mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (align > nvdimm_namespace_capacity(ndns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) align, nvdimm_namespace_capacity(ndns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * These warnings are verbose because they can only trigger in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * the case where the physical address alignment of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * namespace has changed since the pfn superblock was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * established.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) res = &nsio->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (offset >= resource_size(res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) dev_name(&ndns->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if ((align && !IS_ALIGNED(res->start + offset + start_pad, align))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) || !IS_ALIGNED(offset, PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) dev_err(&nd_pfn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) "bad offset: %#llx dax disabled align: %#lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) offset, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (!IS_ALIGNED(res->start + le32_to_cpu(pfn_sb->start_pad),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) memremap_compat_align())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) dev_err(&nd_pfn->dev, "resource start misaligned\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (!IS_ALIGNED(res->end + 1 - le32_to_cpu(pfn_sb->end_trunc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) memremap_compat_align())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) dev_err(&nd_pfn->dev, "resource end misaligned\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) EXPORT_SYMBOL(nd_pfn_validate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct nd_pfn *nd_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) struct device *pfn_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) struct nd_pfn_sb *pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (ndns->force_raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) switch (ndns->claim_class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) case NVDIMM_CCLASS_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) case NVDIMM_CCLASS_PFN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) nvdimm_bus_lock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) nd_pfn = nd_pfn_alloc(nd_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) nvdimm_bus_unlock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (!pfn_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) nd_pfn = to_nd_pfn(pfn_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) nd_pfn->pfn_sb = pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) rc = nd_pfn_validate(nd_pfn, PFN_SIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) put_device(pfn_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) __nd_device_register(pfn_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) EXPORT_SYMBOL(nd_pfn_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * We hotplug memory at sub-section granularity, pad the reserved area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * from the previous section base to the namespace base address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static unsigned long init_altmap_base(resource_size_t base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) unsigned long base_pfn = PHYS_PFN(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return SUBSECTION_ALIGN_DOWN(base_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) static unsigned long init_altmap_reserve(resource_size_t base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) unsigned long reserve = nd_info_block_reserve() >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) unsigned long base_pfn = PHYS_PFN(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) reserve += base_pfn - SUBSECTION_ALIGN_DOWN(base_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) struct range *range = &pgmap->range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct vmem_altmap *altmap = &pgmap->altmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) u64 offset = le64_to_cpu(pfn_sb->dataoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) u32 reserve = nd_info_block_reserve();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) struct nd_namespace_common *ndns = nd_pfn->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) resource_size_t base = nsio->res.start + start_pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) resource_size_t end = nsio->res.end - end_trunc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct vmem_altmap __altmap = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) .base_pfn = init_altmap_base(base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) .reserve = init_altmap_reserve(base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) .end_pfn = PHYS_PFN(end),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) *range = (struct range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) .start = nsio->res.start + start_pad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) .end = nsio->res.end - end_trunc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) pgmap->nr_range = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (nd_pfn->mode == PFN_MODE_RAM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (offset < reserve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) } else if (nd_pfn->mode == PFN_MODE_PMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) nd_pfn->npfns = PHYS_PFN((range_len(range) - offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) dev_info(&nd_pfn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) "number of pfns truncated from %lld to %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) le64_to_cpu(nd_pfn->pfn_sb->npfns),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) nd_pfn->npfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) memcpy(altmap, &__altmap, sizeof(*altmap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) altmap->free = PHYS_PFN(offset - reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) altmap->alloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) pgmap->flags |= PGMAP_ALTMAP_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) static int nd_pfn_init(struct nd_pfn *nd_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) struct nd_namespace_common *ndns = nd_pfn->ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) resource_size_t start, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) struct nd_region *nd_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) unsigned long npfns, align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) u32 end_trunc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) struct nd_pfn_sb *pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) phys_addr_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) const char *sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) u64 checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (!pfn_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) nd_pfn->pfn_sb = pfn_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (is_nd_dax(&nd_pfn->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) sig = DAX_SIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) sig = PFN_SIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) rc = nd_pfn_validate(nd_pfn, sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return nd_pfn_clear_memmap_errors(nd_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (rc != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /* no info block, do init */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) memset(pfn_sb, 0, sizeof(*pfn_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) nd_region = to_nd_region(nd_pfn->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (nd_region->ro) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) dev_info(&nd_pfn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) "%s is read-only, unable to init metadata\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) dev_name(&nd_region->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * Note, we use 64 here for the standard size of struct page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * debugging options may cause it to be larger in which case the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * implementation will limit the pfns advertised through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * ->direct_access() to those that are included in the memmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) start = nsio->res.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) size = resource_size(&nsio->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) npfns = PHYS_PFN(size - SZ_8K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) align = max(nd_pfn->align, memremap_compat_align());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * When @start is misaligned fail namespace creation. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * the 'struct nd_pfn_sb' commentary on why ->start_pad is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * an option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (!IS_ALIGNED(start, memremap_compat_align())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) dev_err(&nd_pfn->dev, "%s: start %pa misaligned to %#lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) dev_name(&ndns->dev), &start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) memremap_compat_align());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) end_trunc = start + size - ALIGN_DOWN(start + size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (nd_pfn->mode == PFN_MODE_PMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * The altmap should be padded out to the block size used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * when populating the vmemmap. This *should* be equal to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * PMD_SIZE for most architectures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * Also make sure size of struct page is less than 64. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * want to make sure we use large enough size here so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * we don't have a dynamic reserve space depending on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * struct page size. But we also want to make sure we notice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * when we end up adding new elements to struct page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) BUILD_BUG_ON(sizeof(struct page) > MAX_STRUCT_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) offset = ALIGN(start + SZ_8K + MAX_STRUCT_PAGE_SIZE * npfns, align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) } else if (nd_pfn->mode == PFN_MODE_RAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) offset = ALIGN(start + SZ_8K, align) - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (offset >= size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) dev_name(&ndns->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) npfns = PHYS_PFN(size - offset - end_trunc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) pfn_sb->dataoff = cpu_to_le64(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) pfn_sb->npfns = cpu_to_le64(npfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) pfn_sb->version_major = cpu_to_le16(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) pfn_sb->version_minor = cpu_to_le16(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) pfn_sb->end_trunc = cpu_to_le32(end_trunc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) pfn_sb->align = cpu_to_le32(nd_pfn->align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) pfn_sb->page_struct_size = cpu_to_le16(MAX_STRUCT_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) pfn_sb->checksum = cpu_to_le64(checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) rc = nd_pfn_clear_memmap_errors(nd_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * Determine the effective resource range and vmem_altmap from an nd_pfn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (!nd_pfn->uuid || !nd_pfn->ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) rc = nd_pfn_init(nd_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) /* we need a valid pfn_sb before we can init a dev_pagemap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return __nvdimm_setup_pfn(nd_pfn, pgmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);