Blame view

drivers/nvdimm/claim.c 7.62 KB
5b497af42   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
e1455744b   Dan Williams   libnvdimm, pfn: '...
2
3
  /*
   * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
e1455744b   Dan Williams   libnvdimm, pfn: '...
4
5
6
7
   */
  #include <linux/device.h>
  #include <linux/sizes.h>
  #include "nd-core.h"
f2b612578   Dan Williams   x86, libnvdimm, p...
8
  #include "pmem.h"
e1455744b   Dan Williams   libnvdimm, pfn: '...
9
10
11
12
13
14
15
  #include "pfn.h"
  #include "btt.h"
  #include "nd.h"
  
  void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns)
  {
  	struct nd_namespace_common *ndns = *_ndns;
452bae0ae   Dan Williams   libnvdimm: fix nv...
16
  	struct nvdimm_bus *nvdimm_bus;
e1455744b   Dan Williams   libnvdimm, pfn: '...
17

452bae0ae   Dan Williams   libnvdimm: fix nv...
18
19
20
21
22
  	if (!ndns)
  		return;
  
  	nvdimm_bus = walk_to_nvdimm_bus(&ndns->dev);
  	lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
9cf8bd529   Dan Williams   libnvdimm: replac...
23
24
  	dev_WARN_ONCE(dev, ndns->claim != dev, "%s: invalid claim
  ", __func__);
e1455744b   Dan Williams   libnvdimm, pfn: '...
25
26
27
28
29
30
31
32
33
34
35
36
37
  	ndns->claim = NULL;
  	*_ndns = NULL;
  	put_device(&ndns->dev);
  }
  
  void nd_detach_ndns(struct device *dev,
  		struct nd_namespace_common **_ndns)
  {
  	struct nd_namespace_common *ndns = *_ndns;
  
  	if (!ndns)
  		return;
  	get_device(&ndns->dev);
452bae0ae   Dan Williams   libnvdimm: fix nv...
38
  	nvdimm_bus_lock(&ndns->dev);
e1455744b   Dan Williams   libnvdimm, pfn: '...
39
  	__nd_detach_ndns(dev, _ndns);
452bae0ae   Dan Williams   libnvdimm: fix nv...
40
  	nvdimm_bus_unlock(&ndns->dev);
e1455744b   Dan Williams   libnvdimm, pfn: '...
41
42
43
44
45
46
  	put_device(&ndns->dev);
  }
  
  bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
  		struct nd_namespace_common **_ndns)
  {
452bae0ae   Dan Williams   libnvdimm: fix nv...
47
  	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&attach->dev);
e1455744b   Dan Williams   libnvdimm, pfn: '...
48
49
  	if (attach->claim)
  		return false;
452bae0ae   Dan Williams   libnvdimm: fix nv...
50
  	lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
9cf8bd529   Dan Williams   libnvdimm: replac...
51
52
  	dev_WARN_ONCE(dev, *_ndns, "%s: invalid claim
  ", __func__);
e1455744b   Dan Williams   libnvdimm, pfn: '...
53
54
55
56
57
58
59
60
61
62
  	attach->claim = dev;
  	*_ndns = attach;
  	get_device(&attach->dev);
  	return true;
  }
  
  bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
  		struct nd_namespace_common **_ndns)
  {
  	bool claimed;
452bae0ae   Dan Williams   libnvdimm: fix nv...
63
  	nvdimm_bus_lock(&attach->dev);
e1455744b   Dan Williams   libnvdimm, pfn: '...
64
  	claimed = __nd_attach_ndns(dev, attach, _ndns);
452bae0ae   Dan Williams   libnvdimm: fix nv...
65
  	nvdimm_bus_unlock(&attach->dev);
e1455744b   Dan Williams   libnvdimm, pfn: '...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  	return claimed;
  }
  
  static int namespace_match(struct device *dev, void *data)
  {
  	char *name = data;
  
  	return strcmp(name, dev_name(dev)) == 0;
  }
  
  static bool is_idle(struct device *dev, struct nd_namespace_common *ndns)
  {
  	struct nd_region *nd_region = to_nd_region(dev->parent);
  	struct device *seed = NULL;
  
  	if (is_nd_btt(dev))
  		seed = nd_region->btt_seed;
  	else if (is_nd_pfn(dev))
  		seed = nd_region->pfn_seed;
cd03412a5   Dan Williams   libnvdimm, dax: i...
85
86
  	else if (is_nd_dax(dev))
  		seed = nd_region->dax_seed;
e1455744b   Dan Williams   libnvdimm, pfn: '...
87
88
89
90
91
  
  	if (seed == dev || ndns || dev->driver)
  		return false;
  	return true;
  }
03dca343a   Dan Williams   libnvdimm, dax: f...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  struct nd_pfn *to_nd_pfn_safe(struct device *dev)
  {
  	/*
  	 * pfn device attributes are re-used by dax device instances, so we
  	 * need to be careful to correct device-to-nd_pfn conversion.
  	 */
  	if (is_nd_pfn(dev))
  		return to_nd_pfn(dev);
  
  	if (is_nd_dax(dev)) {
  		struct nd_dax *nd_dax = to_nd_dax(dev);
  
  		return &nd_dax->nd_pfn;
  	}
  
  	WARN_ON(1);
  	return NULL;
  }
e1455744b   Dan Williams   libnvdimm, pfn: '...
110
111
112
113
  static void nd_detach_and_reset(struct device *dev,
  		struct nd_namespace_common **_ndns)
  {
  	/* detach the namespace and destroy / reset the device */
452bae0ae   Dan Williams   libnvdimm: fix nv...
114
  	__nd_detach_ndns(dev, _ndns);
e1455744b   Dan Williams   libnvdimm, pfn: '...
115
116
117
118
119
120
121
122
  	if (is_idle(dev, *_ndns)) {
  		nd_device_unregister(dev, ND_ASYNC);
  	} else if (is_nd_btt(dev)) {
  		struct nd_btt *nd_btt = to_nd_btt(dev);
  
  		nd_btt->lbasize = 0;
  		kfree(nd_btt->uuid);
  		nd_btt->uuid = NULL;
03dca343a   Dan Williams   libnvdimm, dax: f...
123
124
  	} else if (is_nd_pfn(dev) || is_nd_dax(dev)) {
  		struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
e1455744b   Dan Williams   libnvdimm, pfn: '...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  
  		kfree(nd_pfn->uuid);
  		nd_pfn->uuid = NULL;
  		nd_pfn->mode = PFN_MODE_NONE;
  	}
  }
  
  ssize_t nd_namespace_store(struct device *dev,
  		struct nd_namespace_common **_ndns, const char *buf,
  		size_t len)
  {
  	struct nd_namespace_common *ndns;
  	struct device *found;
  	char *name;
  
  	if (dev->driver) {
426824d63   Dan Williams   libnvdimm: remove...
141
142
  		dev_dbg(dev, "namespace already active
  ");
e1455744b   Dan Williams   libnvdimm, pfn: '...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  		return -EBUSY;
  	}
  
  	name = kstrndup(buf, len, GFP_KERNEL);
  	if (!name)
  		return -ENOMEM;
  	strim(name);
  
  	if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
  		/* pass */;
  	else {
  		len = -EINVAL;
  		goto out;
  	}
  
  	ndns = *_ndns;
  	if (strcmp(name, "") == 0) {
  		nd_detach_and_reset(dev, _ndns);
  		goto out;
  	} else if (ndns) {
  		dev_dbg(dev, "namespace already set to: %s
  ",
  				dev_name(&ndns->dev));
  		len = -EBUSY;
  		goto out;
  	}
  
  	found = device_find_child(dev->parent, name, namespace_match);
  	if (!found) {
  		dev_dbg(dev, "'%s' not found under %s
  ", name,
  				dev_name(dev->parent));
  		len = -ENODEV;
  		goto out;
  	}
  
  	ndns = to_ndns(found);
b3fde74ea   Dan Williams   libnvdimm, label:...
180
181
182
183
184
  
  	switch (ndns->claim_class) {
  	case NVDIMM_CCLASS_NONE:
  		break;
  	case NVDIMM_CCLASS_BTT:
14e494542   Vishal Verma   libnvdimm, btt: B...
185
  	case NVDIMM_CCLASS_BTT2:
b3fde74ea   Dan Williams   libnvdimm, label:...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  		if (!is_nd_btt(dev)) {
  			len = -EBUSY;
  			goto out_attach;
  		}
  		break;
  	case NVDIMM_CCLASS_PFN:
  		if (!is_nd_pfn(dev)) {
  			len = -EBUSY;
  			goto out_attach;
  		}
  		break;
  	case NVDIMM_CCLASS_DAX:
  		if (!is_nd_dax(dev)) {
  			len = -EBUSY;
  			goto out_attach;
  		}
  		break;
  	default:
  		len = -EBUSY;
  		goto out_attach;
  		break;
  	}
e1455744b   Dan Williams   libnvdimm, pfn: '...
208
209
210
211
212
213
214
215
  	if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
  		dev_dbg(dev, "%s too small to host
  ", name);
  		len = -ENXIO;
  		goto out_attach;
  	}
  
  	WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
452bae0ae   Dan Williams   libnvdimm: fix nv...
216
  	if (!__nd_attach_ndns(dev, ndns, _ndns)) {
e1455744b   Dan Williams   libnvdimm, pfn: '...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  		dev_dbg(dev, "%s already claimed
  ",
  				dev_name(&ndns->dev));
  		len = -EBUSY;
  	}
  
   out_attach:
  	put_device(&ndns->dev); /* from device_find_child */
   out:
  	kfree(name);
  	return len;
  }
  
  /*
   * nd_sb_checksum: compute checksum for a generic info block
   *
   * Returns a fletcher64 checksum of everything in the given info block
   * except the last field (since that's where the checksum lives).
   */
  u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
  {
  	u64 sum;
  	__le64 sum_save;
  
  	BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
  	BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
  	BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
  
  	sum_save = nd_gen_sb->checksum;
  	nd_gen_sb->checksum = 0;
  	sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
  	nd_gen_sb->checksum = sum_save;
  	return sum;
  }
  EXPORT_SYMBOL(nd_sb_checksum);
200c79da8   Dan Williams   libnvdimm, pmem, ...
252
253
  
  static int nsio_rw_bytes(struct nd_namespace_common *ndns,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
254
255
  		resource_size_t offset, void *buf, size_t size, int rw,
  		unsigned long flags)
200c79da8   Dan Williams   libnvdimm, pmem, ...
256
257
  {
  	struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
82bf1037f   Dave Jiang   libnvdimm: check ...
258
259
  	unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
  	sector_t sector = offset >> 9;
c5d4355d1   Pankaj Gupta   libnvdimm: nd_reg...
260
  	int rc = 0, ret = 0;
82bf1037f   Dave Jiang   libnvdimm: check ...
261
262
263
  
  	if (unlikely(!size))
  		return 0;
200c79da8   Dan Williams   libnvdimm, pmem, ...
264
265
266
267
268
269
270
271
  
  	if (unlikely(offset + size > nsio->size)) {
  		dev_WARN_ONCE(&ndns->dev, 1, "request out of range
  ");
  		return -EFAULT;
  	}
  
  	if (rw == READ) {
82bf1037f   Dave Jiang   libnvdimm: check ...
272
  		if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align)))
200c79da8   Dan Williams   libnvdimm, pmem, ...
273
  			return -EIO;
ec6347bb4   Dan Williams   x86, powerpc: Ren...
274
  		if (copy_mc_to_kernel(buf, nsio->addr + offset, size) != 0)
60622d682   Dan Williams   x86/asm/memcpy_mc...
275
  			return -EIO;
b62cc6fdd   Dan Williams   libnvdimm, pmem: ...
276
  		return 0;
d37806dc3   Fabian Frederick   libnvdimm: remove...
277
278
279
  	}
  
  	if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) {
a3e9af95f   Dan Williams   libnvdimm: restor...
280
  		if (IS_ALIGNED(offset, 512) && IS_ALIGNED(size, 512)
7e5a21dfe   Vishal Verma   libnvdimm: fix th...
281
  				&& !(flags & NVDIMM_IO_ATOMIC)) {
d37806dc3   Fabian Frederick   libnvdimm: remove...
282
  			long cleared;
1db1f3cea   Vishal Verma   libnvdimm, btt: f...
283
  			might_sleep();
97681f9b0   Toshi Kani   libnvdimm: fix ph...
284
285
  			cleared = nvdimm_clear_poison(&ndns->dev,
  					nsio->res.start + offset, size);
868f036fe   Dan Williams   libnvdimm: fix mi...
286
  			if (cleared < size)
82bf1037f   Dave Jiang   libnvdimm: check ...
287
  				rc = -EIO;
868f036fe   Dan Williams   libnvdimm: fix mi...
288
289
290
  			if (cleared > 0 && cleared / 512) {
  				cleared /= 512;
  				badblocks_clear(&nsio->bb, sector, cleared);
d37806dc3   Fabian Frederick   libnvdimm: remove...
291
  			}
f2b612578   Dan Williams   x86, libnvdimm, p...
292
  			arch_invalidate_pmem(nsio->addr + offset, size);
d37806dc3   Fabian Frederick   libnvdimm: remove...
293
294
  		} else
  			rc = -EIO;
200c79da8   Dan Williams   libnvdimm, pmem, ...
295
  	}
0aed55af8   Dan Williams   x86, uaccess: int...
296
  	memcpy_flushcache(nsio->addr + offset, buf, size);
c5d4355d1   Pankaj Gupta   libnvdimm: nd_reg...
297
298
299
  	ret = nvdimm_flush(to_nd_region(ndns->dev.parent), NULL);
  	if (ret)
  		rc = ret;
d37806dc3   Fabian Frederick   libnvdimm: remove...
300

82bf1037f   Dave Jiang   libnvdimm: check ...
301
  	return rc;
200c79da8   Dan Williams   libnvdimm, pmem, ...
302
  }
8f4b01fcd   Aneesh Kumar K.V   libnvdimm/namespa...
303
304
  int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio,
  		resource_size_t size)
200c79da8   Dan Williams   libnvdimm, pmem, ...
305
  {
200c79da8   Dan Williams   libnvdimm, pmem, ...
306
  	struct nd_namespace_common *ndns = &nsio->common;
a4574f63e   Dan Williams   mm/memremap_pages...
307
308
309
310
  	struct range range = {
  		.start = nsio->res.start,
  		.end = nsio->res.end,
  	};
200c79da8   Dan Williams   libnvdimm, pmem, ...
311

8f4b01fcd   Aneesh Kumar K.V   libnvdimm/namespa...
312
  	nsio->size = size;
a4574f63e   Dan Williams   mm/memremap_pages...
313
  	if (!devm_request_mem_region(dev, range.start, size,
450c6633e   Dan Williams   libnvdimm: use co...
314
  				dev_name(&ndns->dev))) {
a4574f63e   Dan Williams   mm/memremap_pages...
315
316
  		dev_warn(dev, "could not reserve region %pR
  ", &nsio->res);
200c79da8   Dan Williams   libnvdimm, pmem, ...
317
318
319
320
321
322
323
  		return -EBUSY;
  	}
  
  	ndns->rw_bytes = nsio_rw_bytes;
  	if (devm_init_badblocks(dev, &nsio->bb))
  		return -ENOMEM;
  	nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
a4574f63e   Dan Williams   mm/memremap_pages...
324
  			&range);
200c79da8   Dan Williams   libnvdimm, pmem, ...
325

a4574f63e   Dan Williams   mm/memremap_pages...
326
  	nsio->addr = devm_memremap(dev, range.start, size, ARCH_MEMREMAP_PMEM);
425889581   Dan Williams   libnvdimm: IS_ERR...
327
328
  
  	return PTR_ERR_OR_ZERO(nsio->addr);
200c79da8   Dan Williams   libnvdimm, pmem, ...
329
  }
200c79da8   Dan Williams   libnvdimm, pmem, ...
330
331
332
333
334
335
336
  
  void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio)
  {
  	struct resource *res = &nsio->res;
  
  	devm_memunmap(dev, nsio->addr);
  	devm_exit_badblocks(dev, &nsio->bb);
8f4b01fcd   Aneesh Kumar K.V   libnvdimm/namespa...
337
  	devm_release_mem_region(dev, res->start, nsio->size);
200c79da8   Dan Williams   libnvdimm, pmem, ...
338
  }