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>
21 void __nd_detach_ndns(struct device
*dev
, struct nd_namespace_common
**_ndns
)
23 struct nd_namespace_common
*ndns
= *_ndns
;
24 struct nvdimm_bus
*nvdimm_bus
;
29 nvdimm_bus
= walk_to_nvdimm_bus(&ndns
->dev
);
30 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
31 dev_WARN_ONCE(dev
, ndns
->claim
!= dev
, "%s: invalid claim\n", __func__
);
34 put_device(&ndns
->dev
);
37 void nd_detach_ndns(struct device
*dev
,
38 struct nd_namespace_common
**_ndns
)
40 struct nd_namespace_common
*ndns
= *_ndns
;
44 get_device(&ndns
->dev
);
45 nvdimm_bus_lock(&ndns
->dev
);
46 __nd_detach_ndns(dev
, _ndns
);
47 nvdimm_bus_unlock(&ndns
->dev
);
48 put_device(&ndns
->dev
);
51 bool __nd_attach_ndns(struct device
*dev
, struct nd_namespace_common
*attach
,
52 struct nd_namespace_common
**_ndns
)
54 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&attach
->dev
);
58 lockdep_assert_held(&nvdimm_bus
->reconfig_mutex
);
59 dev_WARN_ONCE(dev
, *_ndns
, "%s: invalid claim\n", __func__
);
62 get_device(&attach
->dev
);
66 bool nd_attach_ndns(struct device
*dev
, struct nd_namespace_common
*attach
,
67 struct nd_namespace_common
**_ndns
)
71 nvdimm_bus_lock(&attach
->dev
);
72 claimed
= __nd_attach_ndns(dev
, attach
, _ndns
);
73 nvdimm_bus_unlock(&attach
->dev
);
77 static int namespace_match(struct device
*dev
, void *data
)
81 return strcmp(name
, dev_name(dev
)) == 0;
84 static bool is_idle(struct device
*dev
, struct nd_namespace_common
*ndns
)
86 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
87 struct device
*seed
= NULL
;
90 seed
= nd_region
->btt_seed
;
91 else if (is_nd_pfn(dev
))
92 seed
= nd_region
->pfn_seed
;
93 else if (is_nd_dax(dev
))
94 seed
= nd_region
->dax_seed
;
96 if (seed
== dev
|| ndns
|| dev
->driver
)
101 struct nd_pfn
*to_nd_pfn_safe(struct device
*dev
)
104 * pfn device attributes are re-used by dax device instances, so we
105 * need to be careful to correct device-to-nd_pfn conversion.
108 return to_nd_pfn(dev
);
110 if (is_nd_dax(dev
)) {
111 struct nd_dax
*nd_dax
= to_nd_dax(dev
);
113 return &nd_dax
->nd_pfn
;
120 static void nd_detach_and_reset(struct device
*dev
,
121 struct nd_namespace_common
**_ndns
)
123 /* detach the namespace and destroy / reset the device */
124 __nd_detach_ndns(dev
, _ndns
);
125 if (is_idle(dev
, *_ndns
)) {
126 nd_device_unregister(dev
, ND_ASYNC
);
127 } else if (is_nd_btt(dev
)) {
128 struct nd_btt
*nd_btt
= to_nd_btt(dev
);
133 } else if (is_nd_pfn(dev
) || is_nd_dax(dev
)) {
134 struct nd_pfn
*nd_pfn
= to_nd_pfn_safe(dev
);
138 nd_pfn
->mode
= PFN_MODE_NONE
;
142 ssize_t
nd_namespace_store(struct device
*dev
,
143 struct nd_namespace_common
**_ndns
, const char *buf
,
146 struct nd_namespace_common
*ndns
;
147 struct device
*found
;
151 dev_dbg(dev
, "namespace already active\n");
155 name
= kstrndup(buf
, len
, GFP_KERNEL
);
160 if (strncmp(name
, "namespace", 9) == 0 || strcmp(name
, "") == 0)
168 if (strcmp(name
, "") == 0) {
169 nd_detach_and_reset(dev
, _ndns
);
172 dev_dbg(dev
, "namespace already set to: %s\n",
173 dev_name(&ndns
->dev
));
178 found
= device_find_child(dev
->parent
, name
, namespace_match
);
180 dev_dbg(dev
, "'%s' not found under %s\n", name
,
181 dev_name(dev
->parent
));
186 ndns
= to_ndns(found
);
188 switch (ndns
->claim_class
) {
189 case NVDIMM_CCLASS_NONE
:
191 case NVDIMM_CCLASS_BTT
:
192 case NVDIMM_CCLASS_BTT2
:
193 if (!is_nd_btt(dev
)) {
198 case NVDIMM_CCLASS_PFN
:
199 if (!is_nd_pfn(dev
)) {
204 case NVDIMM_CCLASS_DAX
:
205 if (!is_nd_dax(dev
)) {
216 if (__nvdimm_namespace_capacity(ndns
) < SZ_16M
) {
217 dev_dbg(dev
, "%s too small to host\n", name
);
222 WARN_ON_ONCE(!is_nvdimm_bus_locked(dev
));
223 if (!__nd_attach_ndns(dev
, ndns
, _ndns
)) {
224 dev_dbg(dev
, "%s already claimed\n",
225 dev_name(&ndns
->dev
));
230 put_device(&ndns
->dev
); /* from device_find_child */
237 * nd_sb_checksum: compute checksum for a generic info block
239 * Returns a fletcher64 checksum of everything in the given info block
240 * except the last field (since that's where the checksum lives).
242 u64
nd_sb_checksum(struct nd_gen_sb
*nd_gen_sb
)
247 BUILD_BUG_ON(sizeof(struct btt_sb
) != SZ_4K
);
248 BUILD_BUG_ON(sizeof(struct nd_pfn_sb
) != SZ_4K
);
249 BUILD_BUG_ON(sizeof(struct nd_gen_sb
) != SZ_4K
);
251 sum_save
= nd_gen_sb
->checksum
;
252 nd_gen_sb
->checksum
= 0;
253 sum
= nd_fletcher64(nd_gen_sb
, sizeof(*nd_gen_sb
), 1);
254 nd_gen_sb
->checksum
= sum_save
;
257 EXPORT_SYMBOL(nd_sb_checksum
);
259 static int nsio_rw_bytes(struct nd_namespace_common
*ndns
,
260 resource_size_t offset
, void *buf
, size_t size
, int rw
,
263 struct nd_namespace_io
*nsio
= to_nd_namespace_io(&ndns
->dev
);
264 unsigned int sz_align
= ALIGN(size
+ (offset
& (512 - 1)), 512);
265 sector_t sector
= offset
>> 9;
271 if (unlikely(offset
+ size
> nsio
->size
)) {
272 dev_WARN_ONCE(&ndns
->dev
, 1, "request out of range\n");
277 if (unlikely(is_bad_pmem(&nsio
->bb
, sector
, sz_align
)))
279 if (memcpy_mcsafe(buf
, nsio
->addr
+ offset
, size
) != 0)
284 if (unlikely(is_bad_pmem(&nsio
->bb
, sector
, sz_align
))) {
285 if (IS_ALIGNED(offset
, 512) && IS_ALIGNED(size
, 512)
286 && !(flags
& NVDIMM_IO_ATOMIC
)) {
290 cleared
= nvdimm_clear_poison(&ndns
->dev
,
291 nsio
->res
.start
+ offset
, size
);
294 if (cleared
> 0 && cleared
/ 512) {
296 badblocks_clear(&nsio
->bb
, sector
, cleared
);
298 arch_invalidate_pmem(nsio
->addr
+ offset
, size
);
303 memcpy_flushcache(nsio
->addr
+ offset
, buf
, size
);
304 nvdimm_flush(to_nd_region(ndns
->dev
.parent
));
309 int devm_nsio_enable(struct device
*dev
, struct nd_namespace_io
*nsio
)
311 struct resource
*res
= &nsio
->res
;
312 struct nd_namespace_common
*ndns
= &nsio
->common
;
314 nsio
->size
= resource_size(res
);
315 if (!devm_request_mem_region(dev
, res
->start
, resource_size(res
),
316 dev_name(&ndns
->dev
))) {
317 dev_warn(dev
, "could not reserve region %pR\n", res
);
321 ndns
->rw_bytes
= nsio_rw_bytes
;
322 if (devm_init_badblocks(dev
, &nsio
->bb
))
324 nvdimm_badblocks_populate(to_nd_region(ndns
->dev
.parent
), &nsio
->bb
,
327 nsio
->addr
= devm_memremap(dev
, res
->start
, resource_size(res
),
330 return PTR_ERR_OR_ZERO(nsio
->addr
);
332 EXPORT_SYMBOL_GPL(devm_nsio_enable
);
334 void devm_nsio_disable(struct device
*dev
, struct nd_namespace_io
*nsio
)
336 struct resource
*res
= &nsio
->res
;
338 devm_memunmap(dev
, nsio
->addr
);
339 devm_exit_badblocks(dev
, &nsio
->bb
);
340 devm_release_mem_region(dev
, res
->start
, resource_size(res
));
342 EXPORT_SYMBOL_GPL(devm_nsio_disable
);