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>
15 #include <linux/pmem.h>
21 void __nd_detach_ndns(struct device
*dev
, struct nd_namespace_common
**_ndns
)
23 struct nd_namespace_common
*ndns
= *_ndns
;
25 dev_WARN_ONCE(dev
, !mutex_is_locked(&ndns
->dev
.mutex
)
26 || ndns
->claim
!= dev
,
27 "%s: invalid claim\n", __func__
);
30 put_device(&ndns
->dev
);
33 void nd_detach_ndns(struct device
*dev
,
34 struct nd_namespace_common
**_ndns
)
36 struct nd_namespace_common
*ndns
= *_ndns
;
40 get_device(&ndns
->dev
);
41 device_lock(&ndns
->dev
);
42 __nd_detach_ndns(dev
, _ndns
);
43 device_unlock(&ndns
->dev
);
44 put_device(&ndns
->dev
);
47 bool __nd_attach_ndns(struct device
*dev
, struct nd_namespace_common
*attach
,
48 struct nd_namespace_common
**_ndns
)
52 dev_WARN_ONCE(dev
, !mutex_is_locked(&attach
->dev
.mutex
)
54 "%s: invalid claim\n", __func__
);
57 get_device(&attach
->dev
);
61 bool nd_attach_ndns(struct device
*dev
, struct nd_namespace_common
*attach
,
62 struct nd_namespace_common
**_ndns
)
66 device_lock(&attach
->dev
);
67 claimed
= __nd_attach_ndns(dev
, attach
, _ndns
);
68 device_unlock(&attach
->dev
);
72 static int namespace_match(struct device
*dev
, void *data
)
76 return strcmp(name
, dev_name(dev
)) == 0;
79 static bool is_idle(struct device
*dev
, struct nd_namespace_common
*ndns
)
81 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
82 struct device
*seed
= NULL
;
85 seed
= nd_region
->btt_seed
;
86 else if (is_nd_pfn(dev
))
87 seed
= nd_region
->pfn_seed
;
88 else if (is_nd_dax(dev
))
89 seed
= nd_region
->dax_seed
;
91 if (seed
== dev
|| ndns
|| dev
->driver
)
96 static void nd_detach_and_reset(struct device
*dev
,
97 struct nd_namespace_common
**_ndns
)
99 /* detach the namespace and destroy / reset the device */
100 nd_detach_ndns(dev
, _ndns
);
101 if (is_idle(dev
, *_ndns
)) {
102 nd_device_unregister(dev
, ND_ASYNC
);
103 } else if (is_nd_btt(dev
)) {
104 struct nd_btt
*nd_btt
= to_nd_btt(dev
);
109 } else if (is_nd_pfn(dev
)) {
110 struct nd_pfn
*nd_pfn
= to_nd_pfn(dev
);
114 nd_pfn
->mode
= PFN_MODE_NONE
;
118 ssize_t
nd_namespace_store(struct device
*dev
,
119 struct nd_namespace_common
**_ndns
, const char *buf
,
122 struct nd_namespace_common
*ndns
;
123 struct device
*found
;
127 dev_dbg(dev
, "%s: -EBUSY\n", __func__
);
131 name
= kstrndup(buf
, len
, GFP_KERNEL
);
136 if (strncmp(name
, "namespace", 9) == 0 || strcmp(name
, "") == 0)
144 if (strcmp(name
, "") == 0) {
145 nd_detach_and_reset(dev
, _ndns
);
148 dev_dbg(dev
, "namespace already set to: %s\n",
149 dev_name(&ndns
->dev
));
154 found
= device_find_child(dev
->parent
, name
, namespace_match
);
156 dev_dbg(dev
, "'%s' not found under %s\n", name
,
157 dev_name(dev
->parent
));
162 ndns
= to_ndns(found
);
163 if (__nvdimm_namespace_capacity(ndns
) < SZ_16M
) {
164 dev_dbg(dev
, "%s too small to host\n", name
);
169 WARN_ON_ONCE(!is_nvdimm_bus_locked(dev
));
170 if (!nd_attach_ndns(dev
, ndns
, _ndns
)) {
171 dev_dbg(dev
, "%s already claimed\n",
172 dev_name(&ndns
->dev
));
177 put_device(&ndns
->dev
); /* from device_find_child */
184 * nd_sb_checksum: compute checksum for a generic info block
186 * Returns a fletcher64 checksum of everything in the given info block
187 * except the last field (since that's where the checksum lives).
189 u64
nd_sb_checksum(struct nd_gen_sb
*nd_gen_sb
)
194 BUILD_BUG_ON(sizeof(struct btt_sb
) != SZ_4K
);
195 BUILD_BUG_ON(sizeof(struct nd_pfn_sb
) != SZ_4K
);
196 BUILD_BUG_ON(sizeof(struct nd_gen_sb
) != SZ_4K
);
198 sum_save
= nd_gen_sb
->checksum
;
199 nd_gen_sb
->checksum
= 0;
200 sum
= nd_fletcher64(nd_gen_sb
, sizeof(*nd_gen_sb
), 1);
201 nd_gen_sb
->checksum
= sum_save
;
204 EXPORT_SYMBOL(nd_sb_checksum
);
206 static int nsio_rw_bytes(struct nd_namespace_common
*ndns
,
207 resource_size_t offset
, void *buf
, size_t size
, int rw
)
209 struct nd_namespace_io
*nsio
= to_nd_namespace_io(&ndns
->dev
);
211 if (unlikely(offset
+ size
> nsio
->size
)) {
212 dev_WARN_ONCE(&ndns
->dev
, 1, "request out of range\n");
217 unsigned int sz_align
= ALIGN(size
+ (offset
& (512 - 1)), 512);
219 if (unlikely(is_bad_pmem(&nsio
->bb
, offset
/ 512, sz_align
)))
221 return memcpy_from_pmem(buf
, nsio
->addr
+ offset
, size
);
223 memcpy_to_pmem(nsio
->addr
+ offset
, buf
, size
);
230 int devm_nsio_enable(struct device
*dev
, struct nd_namespace_io
*nsio
)
232 struct resource
*res
= &nsio
->res
;
233 struct nd_namespace_common
*ndns
= &nsio
->common
;
235 nsio
->size
= resource_size(res
);
236 if (!devm_request_mem_region(dev
, res
->start
, resource_size(res
),
238 dev_warn(dev
, "could not reserve region %pR\n", res
);
242 ndns
->rw_bytes
= nsio_rw_bytes
;
243 if (devm_init_badblocks(dev
, &nsio
->bb
))
245 nvdimm_badblocks_populate(to_nd_region(ndns
->dev
.parent
), &nsio
->bb
,
248 nsio
->addr
= devm_memremap(dev
, res
->start
, resource_size(res
),
250 if (IS_ERR(nsio
->addr
))
251 return PTR_ERR(nsio
->addr
);
254 EXPORT_SYMBOL_GPL(devm_nsio_enable
);
256 void devm_nsio_disable(struct device
*dev
, struct nd_namespace_io
*nsio
)
258 struct resource
*res
= &nsio
->res
;
260 devm_memunmap(dev
, nsio
->addr
);
261 devm_exit_badblocks(dev
, &nsio
->bb
);
262 devm_release_mem_region(dev
, res
->start
, resource_size(res
));
264 EXPORT_SYMBOL_GPL(devm_nsio_disable
);
This page took 0.052741 seconds and 6 git commands to generate.