2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/device.h>
14 #include <linux/sizes.h>
20 void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns)
22 struct nd_namespace_common *ndns = *_ndns;
24 dev_WARN_ONCE(dev, !mutex_is_locked(&ndns->dev.mutex)
25 || ndns->claim != dev,
26 "%s: invalid claim\n", __func__);
29 put_device(&ndns->dev);
32 void nd_detach_ndns(struct device *dev,
33 struct nd_namespace_common **_ndns)
35 struct nd_namespace_common *ndns = *_ndns;
39 get_device(&ndns->dev);
40 device_lock(&ndns->dev);
41 __nd_detach_ndns(dev, _ndns);
42 device_unlock(&ndns->dev);
43 put_device(&ndns->dev);
46 bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
47 struct nd_namespace_common **_ndns)
51 dev_WARN_ONCE(dev, !mutex_is_locked(&attach->dev.mutex)
53 "%s: invalid claim\n", __func__);
56 get_device(&attach->dev);
60 bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
61 struct nd_namespace_common **_ndns)
65 device_lock(&attach->dev);
66 claimed = __nd_attach_ndns(dev, attach, _ndns);
67 device_unlock(&attach->dev);
71 static int namespace_match(struct device *dev, void *data)
75 return strcmp(name, dev_name(dev)) == 0;
78 static bool is_idle(struct device *dev, struct nd_namespace_common *ndns)
80 struct nd_region *nd_region = to_nd_region(dev->parent);
81 struct device *seed = NULL;
84 seed = nd_region->btt_seed;
85 else if (is_nd_pfn(dev))
86 seed = nd_region->pfn_seed;
88 if (seed == dev || ndns || dev->driver)
93 static void nd_detach_and_reset(struct device *dev,
94 struct nd_namespace_common **_ndns)
96 /* detach the namespace and destroy / reset the device */
97 nd_detach_ndns(dev, _ndns);
98 if (is_idle(dev, *_ndns)) {
99 nd_device_unregister(dev, ND_ASYNC);
100 } else if (is_nd_btt(dev)) {
101 struct nd_btt *nd_btt = to_nd_btt(dev);
106 } else if (is_nd_pfn(dev)) {
107 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
111 nd_pfn->mode = PFN_MODE_NONE;
115 ssize_t nd_namespace_store(struct device *dev,
116 struct nd_namespace_common **_ndns, const char *buf,
119 struct nd_namespace_common *ndns;
120 struct device *found;
124 dev_dbg(dev, "%s: -EBUSY\n", __func__);
128 name = kstrndup(buf, len, GFP_KERNEL);
133 if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
141 if (strcmp(name, "") == 0) {
142 nd_detach_and_reset(dev, _ndns);
145 dev_dbg(dev, "namespace already set to: %s\n",
146 dev_name(&ndns->dev));
151 found = device_find_child(dev->parent, name, namespace_match);
153 dev_dbg(dev, "'%s' not found under %s\n", name,
154 dev_name(dev->parent));
159 ndns = to_ndns(found);
160 if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
161 dev_dbg(dev, "%s too small to host\n", name);
166 WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
167 if (!nd_attach_ndns(dev, ndns, _ndns)) {
168 dev_dbg(dev, "%s already claimed\n",
169 dev_name(&ndns->dev));
174 put_device(&ndns->dev); /* from device_find_child */
181 * nd_sb_checksum: compute checksum for a generic info block
183 * Returns a fletcher64 checksum of everything in the given info block
184 * except the last field (since that's where the checksum lives).
186 u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
191 BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
192 BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
193 BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
195 sum_save = nd_gen_sb->checksum;
196 nd_gen_sb->checksum = 0;
197 sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
198 nd_gen_sb->checksum = sum_save;
201 EXPORT_SYMBOL(nd_sb_checksum);