1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/device.h>
6 #include <linux/sizes.h>
13 void __nd_detach_ndns(struct device
*dev
, struct nd_namespace_common
**_ndns
)
15 struct nd_namespace_common
*ndns
= *_ndns
;
16 struct nvdimm_bus
*nvdimm_bus
;
21 nvdimm_bus
= walk_to_nvdimm_bus(&ndns
->dev
);
22 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
23 dev_WARN_ONCE(dev
, ndns
->claim
!= dev
, "%s: invalid claim\n", __func__
);
26 put_device(&ndns
->dev
);
29 void nd_detach_ndns(struct device
*dev
,
30 struct nd_namespace_common
**_ndns
)
32 struct nd_namespace_common
*ndns
= *_ndns
;
36 get_device(&ndns
->dev
);
37 nvdimm_bus_lock(&ndns
->dev
);
38 __nd_detach_ndns(dev
, _ndns
);
39 nvdimm_bus_unlock(&ndns
->dev
);
40 put_device(&ndns
->dev
);
43 bool __nd_attach_ndns(struct device
*dev
, struct nd_namespace_common
*attach
,
44 struct nd_namespace_common
**_ndns
)
46 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&attach
->dev
);
50 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
51 dev_WARN_ONCE(dev
, *_ndns
, "%s: invalid claim\n", __func__
);
54 get_device(&attach
->dev
);
58 bool nd_attach_ndns(struct device
*dev
, struct nd_namespace_common
*attach
,
59 struct nd_namespace_common
**_ndns
)
63 nvdimm_bus_lock(&attach
->dev
);
64 claimed
= __nd_attach_ndns(dev
, attach
, _ndns
);
65 nvdimm_bus_unlock(&attach
->dev
);
69 static int namespace_match(struct device
*dev
, void *data
)
73 return strcmp(name
, dev_name(dev
)) == 0;
76 static bool is_idle(struct device
*dev
, struct nd_namespace_common
*ndns
)
78 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
79 struct device
*seed
= NULL
;
82 seed
= nd_region
->btt_seed
;
83 else if (is_nd_pfn(dev
))
84 seed
= nd_region
->pfn_seed
;
85 else if (is_nd_dax(dev
))
86 seed
= nd_region
->dax_seed
;
88 if (seed
== dev
|| ndns
|| dev
->driver
)
93 struct nd_pfn
*to_nd_pfn_safe(struct device
*dev
)
96 * pfn device attributes are re-used by dax device instances, so we
97 * need to be careful to correct device-to-nd_pfn conversion.
100 return to_nd_pfn(dev
);
102 if (is_nd_dax(dev
)) {
103 struct nd_dax
*nd_dax
= to_nd_dax(dev
);
105 return &nd_dax
->nd_pfn
;
112 static void nd_detach_and_reset(struct device
*dev
,
113 struct nd_namespace_common
**_ndns
)
115 /* detach the namespace and destroy / reset the device */
116 __nd_detach_ndns(dev
, _ndns
);
117 if (is_idle(dev
, *_ndns
)) {
118 nd_device_unregister(dev
, ND_ASYNC
);
119 } else if (is_nd_btt(dev
)) {
120 struct nd_btt
*nd_btt
= to_nd_btt(dev
);
125 } else if (is_nd_pfn(dev
) || is_nd_dax(dev
)) {
126 struct nd_pfn
*nd_pfn
= to_nd_pfn_safe(dev
);
130 nd_pfn
->mode
= PFN_MODE_NONE
;
134 ssize_t
nd_namespace_store(struct device
*dev
,
135 struct nd_namespace_common
**_ndns
, const char *buf
,
138 struct nd_namespace_common
*ndns
;
139 struct device
*found
;
143 dev_dbg(dev
, "namespace already active\n");
147 name
= kstrndup(buf
, len
, GFP_KERNEL
);
152 if (strncmp(name
, "namespace", 9) == 0 || strcmp(name
, "") == 0)
160 if (strcmp(name
, "") == 0) {
161 nd_detach_and_reset(dev
, _ndns
);
164 dev_dbg(dev
, "namespace already set to: %s\n",
165 dev_name(&ndns
->dev
));
170 found
= device_find_child(dev
->parent
, name
, namespace_match
);
172 dev_dbg(dev
, "'%s' not found under %s\n", name
,
173 dev_name(dev
->parent
));
178 ndns
= to_ndns(found
);
180 switch (ndns
->claim_class
) {
181 case NVDIMM_CCLASS_NONE
:
183 case NVDIMM_CCLASS_BTT
:
184 case NVDIMM_CCLASS_BTT2
:
185 if (!is_nd_btt(dev
)) {
190 case NVDIMM_CCLASS_PFN
:
191 if (!is_nd_pfn(dev
)) {
196 case NVDIMM_CCLASS_DAX
:
197 if (!is_nd_dax(dev
)) {
208 if (__nvdimm_namespace_capacity(ndns
) < SZ_16M
) {
209 dev_dbg(dev
, "%s too small to host\n", name
);
214 WARN_ON_ONCE(!is_nvdimm_bus_locked(dev
));
215 if (!__nd_attach_ndns(dev
, ndns
, _ndns
)) {
216 dev_dbg(dev
, "%s already claimed\n",
217 dev_name(&ndns
->dev
));
222 put_device(&ndns
->dev
); /* from device_find_child */
229 * nd_sb_checksum: compute checksum for a generic info block
231 * Returns a fletcher64 checksum of everything in the given info block
232 * except the last field (since that's where the checksum lives).
234 u64
nd_sb_checksum(struct nd_gen_sb
*nd_gen_sb
)
239 BUILD_BUG_ON(sizeof(struct btt_sb
) != SZ_4K
);
240 BUILD_BUG_ON(sizeof(struct nd_pfn_sb
) != SZ_4K
);
241 BUILD_BUG_ON(sizeof(struct nd_gen_sb
) != SZ_4K
);
243 sum_save
= nd_gen_sb
->checksum
;
244 nd_gen_sb
->checksum
= 0;
245 sum
= nd_fletcher64(nd_gen_sb
, sizeof(*nd_gen_sb
), 1);
246 nd_gen_sb
->checksum
= sum_save
;
249 EXPORT_SYMBOL(nd_sb_checksum
);
251 static int nsio_rw_bytes(struct nd_namespace_common
*ndns
,
252 resource_size_t offset
, void *buf
, size_t size
, int rw
,
255 struct nd_namespace_io
*nsio
= to_nd_namespace_io(&ndns
->dev
);
256 unsigned int sz_align
= ALIGN(size
+ (offset
& (512 - 1)), 512);
257 sector_t sector
= offset
>> 9;
263 if (unlikely(offset
+ size
> nsio
->size
)) {
264 dev_WARN_ONCE(&ndns
->dev
, 1, "request out of range\n");
269 if (unlikely(is_bad_pmem(&nsio
->bb
, sector
, sz_align
)))
271 if (memcpy_mcsafe(buf
, nsio
->addr
+ offset
, size
) != 0)
276 if (unlikely(is_bad_pmem(&nsio
->bb
, sector
, sz_align
))) {
277 if (IS_ALIGNED(offset
, 512) && IS_ALIGNED(size
, 512)
278 && !(flags
& NVDIMM_IO_ATOMIC
)) {
282 cleared
= nvdimm_clear_poison(&ndns
->dev
,
283 nsio
->res
.start
+ offset
, size
);
286 if (cleared
> 0 && cleared
/ 512) {
288 badblocks_clear(&nsio
->bb
, sector
, cleared
);
290 arch_invalidate_pmem(nsio
->addr
+ offset
, size
);
295 memcpy_flushcache(nsio
->addr
+ offset
, buf
, size
);
296 ret
= nvdimm_flush(to_nd_region(ndns
->dev
.parent
), NULL
);
303 int devm_nsio_enable(struct device
*dev
, struct nd_namespace_io
*nsio
,
304 resource_size_t size
)
306 struct resource
*res
= &nsio
->res
;
307 struct nd_namespace_common
*ndns
= &nsio
->common
;
310 if (!devm_request_mem_region(dev
, res
->start
, size
,
311 dev_name(&ndns
->dev
))) {
312 dev_warn(dev
, "could not reserve region %pR\n", res
);
316 ndns
->rw_bytes
= nsio_rw_bytes
;
317 if (devm_init_badblocks(dev
, &nsio
->bb
))
319 nvdimm_badblocks_populate(to_nd_region(ndns
->dev
.parent
), &nsio
->bb
,
322 nsio
->addr
= devm_memremap(dev
, res
->start
, size
, ARCH_MEMREMAP_PMEM
);
324 return PTR_ERR_OR_ZERO(nsio
->addr
);
327 void devm_nsio_disable(struct device
*dev
, struct nd_namespace_io
*nsio
)
329 struct resource
*res
= &nsio
->res
;
331 devm_memunmap(dev
, nsio
->addr
);
332 devm_exit_badblocks(dev
, &nsio
->bb
);
333 devm_release_mem_region(dev
, res
->start
, nsio
->size
);