^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/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "nd-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "pmem.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "pfn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "btt.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "nd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct nd_namespace_common *ndns = *_ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct nvdimm_bus *nvdimm_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (!ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) nvdimm_bus = walk_to_nvdimm_bus(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) dev_WARN_ONCE(dev, ndns->claim != dev, "%s: invalid claim\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) ndns->claim = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *_ndns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) put_device(&ndns->dev);
^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) void nd_detach_ndns(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct nd_namespace_common **_ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct nd_namespace_common *ndns = *_ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (!ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) get_device(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) nvdimm_bus_lock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __nd_detach_ndns(dev, _ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) nvdimm_bus_unlock(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) put_device(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct nd_namespace_common **_ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&attach->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (attach->claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dev_WARN_ONCE(dev, *_ndns, "%s: invalid claim\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) attach->claim = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *_ndns = attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) get_device(&attach->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct nd_namespace_common **_ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) bool claimed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) nvdimm_bus_lock(&attach->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) claimed = __nd_attach_ndns(dev, attach, _ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) nvdimm_bus_unlock(&attach->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return claimed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int namespace_match(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) char *name = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return strcmp(name, dev_name(dev)) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static bool is_idle(struct device *dev, struct nd_namespace_common *ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct nd_region *nd_region = to_nd_region(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct device *seed = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (is_nd_btt(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) seed = nd_region->btt_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) else if (is_nd_pfn(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) seed = nd_region->pfn_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) else if (is_nd_dax(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) seed = nd_region->dax_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (seed == dev || ndns || dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct nd_pfn *to_nd_pfn_safe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * pfn device attributes are re-used by dax device instances, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * need to be careful to correct device-to-nd_pfn conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (is_nd_pfn(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return to_nd_pfn(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (is_nd_dax(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct nd_dax *nd_dax = to_nd_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return &nd_dax->nd_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void nd_detach_and_reset(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct nd_namespace_common **_ndns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* detach the namespace and destroy / reset the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) __nd_detach_ndns(dev, _ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (is_idle(dev, *_ndns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) nd_device_unregister(dev, ND_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } else if (is_nd_btt(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct nd_btt *nd_btt = to_nd_btt(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) nd_btt->lbasize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) kfree(nd_btt->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) nd_btt->uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else if (is_nd_pfn(dev) || is_nd_dax(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) kfree(nd_pfn->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) nd_pfn->uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) nd_pfn->mode = PFN_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ssize_t nd_namespace_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct nd_namespace_common **_ndns, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct nd_namespace_common *ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct device *found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) dev_dbg(dev, "namespace already active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) name = kstrndup(buf, len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) strim(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* pass */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) len = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ndns = *_ndns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (strcmp(name, "") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) nd_detach_and_reset(dev, _ndns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) } else if (ndns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) dev_dbg(dev, "namespace already set to: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev_name(&ndns->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) len = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) found = device_find_child(dev->parent, name, namespace_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) dev_dbg(dev, "'%s' not found under %s\n", name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dev_name(dev->parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) len = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ndns = to_ndns(found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) switch (ndns->claim_class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) case NVDIMM_CCLASS_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) case NVDIMM_CCLASS_BTT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) case NVDIMM_CCLASS_BTT2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!is_nd_btt(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) len = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) goto out_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) case NVDIMM_CCLASS_PFN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!is_nd_pfn(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) len = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) goto out_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) case NVDIMM_CCLASS_DAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (!is_nd_dax(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) len = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) goto out_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) len = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) goto out_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dev_dbg(dev, "%s too small to host\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) len = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) goto out_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!__nd_attach_ndns(dev, ndns, _ndns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dev_dbg(dev, "%s already claimed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) dev_name(&ndns->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) len = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) out_attach:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) put_device(&ndns->dev); /* from device_find_child */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * nd_sb_checksum: compute checksum for a generic info block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * Returns a fletcher64 checksum of everything in the given info block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * except the last field (since that's where the checksum lives).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) u64 sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) __le64 sum_save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) sum_save = nd_gen_sb->checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) nd_gen_sb->checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) nd_gen_sb->checksum = sum_save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) EXPORT_SYMBOL(nd_sb_checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int nsio_rw_bytes(struct nd_namespace_common *ndns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) resource_size_t offset, void *buf, size_t size, int rw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) sector_t sector = offset >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int rc = 0, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (unlikely(!size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (unlikely(offset + size > nsio->size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (rw == READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (copy_mc_to_kernel(buf, nsio->addr + offset, size) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 0;
^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) if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (IS_ALIGNED(offset, 512) && IS_ALIGNED(size, 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) && !(flags & NVDIMM_IO_ATOMIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) long cleared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) cleared = nvdimm_clear_poison(&ndns->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) nsio->res.start + offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (cleared < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (cleared > 0 && cleared / 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) cleared /= 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) badblocks_clear(&nsio->bb, sector, cleared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) arch_invalidate_pmem(nsio->addr + offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) memcpy_flushcache(nsio->addr + offset, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ret = nvdimm_flush(to_nd_region(ndns->dev.parent), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) rc = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct nd_namespace_common *ndns = &nsio->common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct range range = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .start = nsio->res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .end = nsio->res.end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) nsio->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (!devm_request_mem_region(dev, range.start, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) dev_name(&ndns->dev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ndns->rw_bytes = nsio_rw_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (devm_init_badblocks(dev, &nsio->bb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) nsio->addr = devm_memremap(dev, range.start, size, ARCH_MEMREMAP_PMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return PTR_ERR_OR_ZERO(nsio->addr);
^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) void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct resource *res = &nsio->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) devm_memunmap(dev, nsio->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) devm_exit_badblocks(dev, &nsio->bb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) devm_release_mem_region(dev, res->start, nsio->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }