Blame view

drivers/nvdimm/dimm_devs.c 22 KB
5b497af42   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
2
3
  /*
   * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
4
5
   */
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
d5d30d5a5   Dan Williams   libnvdimm/dimm: A...
6
  #include <linux/moduleparam.h>
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
7
  #include <linux/vmalloc.h>
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
8
  #include <linux/device.h>
62232e45f   Dan Williams   libnvdimm: contro...
9
  #include <linux/ndctl.h>
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
10
11
12
13
14
  #include <linux/slab.h>
  #include <linux/io.h>
  #include <linux/fs.h>
  #include <linux/mm.h>
  #include "nd-core.h"
0ba1c6348   Dan Williams   libnvdimm: write ...
15
  #include "label.h"
ca6a4657e   Dan Williams   x86, libnvdimm, p...
16
  #include "pmem.h"
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
17
  #include "nd.h"
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
18
19
  
  static DEFINE_IDA(dimm_ida);
d5d30d5a5   Dan Williams   libnvdimm/dimm: A...
20
21
22
  static bool noblk;
  module_param(noblk, bool, 0444);
  MODULE_PARM_DESC(noblk, "force disable BLK / local alias support");
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
23
24
25
26
  /*
   * Retrieve bus and dimm handle and return if this bus supports
   * get_config_data commands
   */
aee659874   Toshi Kani   libnvdimm: Fix nv...
27
  int nvdimm_check_config_data(struct device *dev)
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
28
  {
aee659874   Toshi Kani   libnvdimm: Fix nv...
29
  	struct nvdimm *nvdimm = to_nvdimm(dev);
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
30

aee659874   Toshi Kani   libnvdimm: Fix nv...
31
32
  	if (!nvdimm->cmd_mask ||
  	    !test_bit(ND_CMD_GET_CONFIG_DATA, &nvdimm->cmd_mask)) {
a0e374525   Dan Williams   libnvdimm/region:...
33
  		if (test_bit(NDD_LABELING, &nvdimm->flags))
aee659874   Toshi Kani   libnvdimm: Fix nv...
34
35
36
37
  			return -ENXIO;
  		else
  			return -ENOTTY;
  	}
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
38
39
40
41
42
43
  
  	return 0;
  }
  
  static int validate_dimm(struct nvdimm_drvdata *ndd)
  {
aee659874   Toshi Kani   libnvdimm: Fix nv...
44
  	int rc;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
45

aee659874   Toshi Kani   libnvdimm: Fix nv...
46
47
48
49
50
  	if (!ndd)
  		return -EINVAL;
  
  	rc = nvdimm_check_config_data(ndd->dev);
  	if (rc)
d75f773c8   Sakari Ailus   treewide: Switch ...
51
52
  		dev_dbg(ndd->dev, "%ps: %s error: %d
  ",
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  				__builtin_return_address(0), __func__, rc);
  	return rc;
  }
  
  /**
   * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
   * @nvdimm: dimm to initialize
   */
  int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
  {
  	struct nd_cmd_get_config_size *cmd = &ndd->nsarea;
  	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  	struct nvdimm_bus_descriptor *nd_desc;
  	int rc = validate_dimm(ndd);
9d62ed965   Dan Williams   libnvdimm: handle...
67
  	int cmd_rc = 0;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
68
69
70
71
72
73
74
75
76
  
  	if (rc)
  		return rc;
  
  	if (cmd->config_size)
  		return 0; /* already valid */
  
  	memset(cmd, 0, sizeof(*cmd));
  	nd_desc = nvdimm_bus->nd_desc;
9d62ed965   Dan Williams   libnvdimm: handle...
77
78
79
80
81
  	rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
  			ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd), &cmd_rc);
  	if (rc < 0)
  		return rc;
  	return cmd_rc;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
82
  }
2d657d17f   Alexander Duyck   nvdimm: Split lab...
83
84
  int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
  			   size_t offset, size_t len)
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
85
86
  {
  	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
2d657d17f   Alexander Duyck   nvdimm: Split lab...
87
  	struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
e7c5a571a   Dan Williams   libnvdimm, dimm: ...
88
  	int rc = validate_dimm(ndd), cmd_rc = 0;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
89
  	struct nd_cmd_get_config_data_hdr *cmd;
2d657d17f   Alexander Duyck   nvdimm: Split lab...
90
  	size_t max_cmd_size, buf_offset;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
91
92
93
  
  	if (rc)
  		return rc;
2d657d17f   Alexander Duyck   nvdimm: Split lab...
94
  	if (offset + len > ndd->nsarea.config_size)
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
95
  		return -ENXIO;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
96

2d657d17f   Alexander Duyck   nvdimm: Split lab...
97
  	max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
d11cf4a73   Dan Williams   libnvdimm, dimm: ...
98
  	cmd = kvzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
99
100
  	if (!cmd)
  		return -ENOMEM;
2d657d17f   Alexander Duyck   nvdimm: Split lab...
101
102
103
104
105
106
107
108
  	for (buf_offset = 0; len;
  	     len -= cmd->in_length, buf_offset += cmd->in_length) {
  		size_t cmd_size;
  
  		cmd->in_offset = offset + buf_offset;
  		cmd->in_length = min(max_cmd_size, len);
  
  		cmd_size = sizeof(*cmd) + cmd->in_length;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
109
  		rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
2d657d17f   Alexander Duyck   nvdimm: Split lab...
110
  				ND_CMD_GET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
e7c5a571a   Dan Williams   libnvdimm, dimm: ...
111
112
113
114
  		if (rc < 0)
  			break;
  		if (cmd_rc < 0) {
  			rc = cmd_rc;
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
115
116
  			break;
  		}
2d657d17f   Alexander Duyck   nvdimm: Split lab...
117
118
119
  
  		/* out_buf should be valid, copy it into our output buffer */
  		memcpy(buf + buf_offset, cmd->out_buf, cmd->in_length);
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
120
  	}
d11cf4a73   Dan Williams   libnvdimm, dimm: ...
121
  	kvfree(cmd);
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
122
123
124
  
  	return rc;
  }
f524bf271   Dan Williams   libnvdimm: write ...
125
126
127
  int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
  		void *buf, size_t len)
  {
f524bf271   Dan Williams   libnvdimm: write ...
128
129
  	size_t max_cmd_size, buf_offset;
  	struct nd_cmd_set_config_hdr *cmd;
e7c5a571a   Dan Williams   libnvdimm, dimm: ...
130
  	int rc = validate_dimm(ndd), cmd_rc = 0;
f524bf271   Dan Williams   libnvdimm: write ...
131
132
133
134
135
  	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  	struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  
  	if (rc)
  		return rc;
f524bf271   Dan Williams   libnvdimm: write ...
136
137
  	if (offset + len > ndd->nsarea.config_size)
  		return -ENXIO;
d11cf4a73   Dan Williams   libnvdimm, dimm: ...
138
139
  	max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
  	cmd = kvzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL);
f524bf271   Dan Williams   libnvdimm: write ...
140
141
142
143
144
145
  	if (!cmd)
  		return -ENOMEM;
  
  	for (buf_offset = 0; len; len -= cmd->in_length,
  			buf_offset += cmd->in_length) {
  		size_t cmd_size;
f524bf271   Dan Williams   libnvdimm: write ...
146
147
148
149
150
151
152
  
  		cmd->in_offset = offset + buf_offset;
  		cmd->in_length = min(max_cmd_size, len);
  		memcpy(cmd->in_buf, buf + buf_offset, cmd->in_length);
  
  		/* status is output in the last 4-bytes of the command buffer */
  		cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32);
f524bf271   Dan Williams   libnvdimm: write ...
153
154
  
  		rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
e7c5a571a   Dan Williams   libnvdimm, dimm: ...
155
156
157
158
159
  				ND_CMD_SET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
  		if (rc < 0)
  			break;
  		if (cmd_rc < 0) {
  			rc = cmd_rc;
f524bf271   Dan Williams   libnvdimm: write ...
160
161
162
  			break;
  		}
  	}
d11cf4a73   Dan Williams   libnvdimm, dimm: ...
163
  	kvfree(cmd);
f524bf271   Dan Williams   libnvdimm: write ...
164
165
166
  
  	return rc;
  }
a0e374525   Dan Williams   libnvdimm/region:...
167
  void nvdimm_set_labeling(struct device *dev)
42237e393   Dan Williams   libnvdimm: allow ...
168
169
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
a0e374525   Dan Williams   libnvdimm/region:...
170
  	set_bit(NDD_LABELING, &nvdimm->flags);
8f078b38d   Dan Williams   libnvdimm: conver...
171
172
173
174
175
176
177
  }
  
  void nvdimm_set_locked(struct device *dev)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
  
  	set_bit(NDD_LOCKED, &nvdimm->flags);
42237e393   Dan Williams   libnvdimm: allow ...
178
  }
d34cb8084   Dan Williams   libnvdimm, dimm: ...
179
180
181
182
183
184
  void nvdimm_clear_locked(struct device *dev)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
  
  	clear_bit(NDD_LOCKED, &nvdimm->flags);
  }
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
185
186
187
188
189
190
191
  static void nvdimm_release(struct device *dev)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
  
  	ida_simple_remove(&dimm_ida, nvdimm->id);
  	kfree(nvdimm);
  }
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
192
193
194
195
196
197
198
199
  struct nvdimm *to_nvdimm(struct device *dev)
  {
  	struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
  
  	WARN_ON(!is_nvdimm(dev));
  	return nvdimm;
  }
  EXPORT_SYMBOL_GPL(to_nvdimm);
047fc8a1f   Ross Zwisler   libnvdimm, nfit, ...
200
201
202
203
204
205
206
207
  struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
  {
  	struct nd_region *nd_region = &ndbr->nd_region;
  	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  
  	return nd_mapping->nvdimm;
  }
  EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
ca6a4657e   Dan Williams   x86, libnvdimm, p...
208
209
210
211
212
213
  unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr)
  {
  	/* pmem mapping properties are private to libnvdimm */
  	return ARCH_MEMREMAP_PMEM;
  }
  EXPORT_SYMBOL_GPL(nd_blk_memremap_flags);
bf9bccc14   Dan Williams   libnvdimm: pmem l...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
  {
  	struct nvdimm *nvdimm = nd_mapping->nvdimm;
  
  	WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
  
  	return dev_get_drvdata(&nvdimm->dev);
  }
  EXPORT_SYMBOL(to_ndd);
  
  void nvdimm_drvdata_release(struct kref *kref)
  {
  	struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
  	struct device *dev = ndd->dev;
  	struct resource *res, *_r;
426824d63   Dan Williams   libnvdimm: remove...
229
230
  	dev_dbg(dev, "trace
  ");
bf9bccc14   Dan Williams   libnvdimm: pmem l...
231
232
233
234
  	nvdimm_bus_lock(dev);
  	for_each_dpa_resource_safe(ndd, res, _r)
  		nvdimm_free_dpa(ndd, res);
  	nvdimm_bus_unlock(dev);
a06a75765   yalin wang   nvdimm: change to...
235
  	kvfree(ndd->data);
bf9bccc14   Dan Williams   libnvdimm: pmem l...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  	kfree(ndd);
  	put_device(dev);
  }
  
  void get_ndd(struct nvdimm_drvdata *ndd)
  {
  	kref_get(&ndd->kref);
  }
  
  void put_ndd(struct nvdimm_drvdata *ndd)
  {
  	if (ndd)
  		kref_put(&ndd->kref, nvdimm_drvdata_release);
  }
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
250
251
252
253
254
  const char *nvdimm_name(struct nvdimm *nvdimm)
  {
  	return dev_name(&nvdimm->dev);
  }
  EXPORT_SYMBOL_GPL(nvdimm_name);
ba9c8dd3c   Dan Williams   acpi, nfit: add d...
255
256
257
258
259
  struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
  {
  	return &nvdimm->dev.kobj;
  }
  EXPORT_SYMBOL_GPL(nvdimm_kobj);
e3654eca7   Dan Williams   nfit, libnvdimm: ...
260
261
262
263
264
  unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
  {
  	return nvdimm->cmd_mask;
  }
  EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
265
266
  void *nvdimm_provider_data(struct nvdimm *nvdimm)
  {
62232e45f   Dan Williams   libnvdimm: contro...
267
268
269
  	if (nvdimm)
  		return nvdimm->provider_data;
  	return NULL;
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
270
271
  }
  EXPORT_SYMBOL_GPL(nvdimm_provider_data);
62232e45f   Dan Williams   libnvdimm: contro...
272
273
274
275
276
  static ssize_t commands_show(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
  	int cmd, len = 0;
e3654eca7   Dan Williams   nfit, libnvdimm: ...
277
  	if (!nvdimm->cmd_mask)
62232e45f   Dan Williams   libnvdimm: contro...
278
279
  		return sprintf(buf, "
  ");
e3654eca7   Dan Williams   nfit, libnvdimm: ...
280
  	for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
62232e45f   Dan Williams   libnvdimm: contro...
281
282
283
284
285
286
  		len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
  	len += sprintf(buf + len, "
  ");
  	return len;
  }
  static DEVICE_ATTR_RO(commands);
efbf6f50a   Dan Williams   libnvdimm: introd...
287
288
289
290
  static ssize_t flags_show(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
a0e374525   Dan Williams   libnvdimm/region:...
291
292
  	return sprintf(buf, "%s%s%s
  ",
efbf6f50a   Dan Williams   libnvdimm: introd...
293
  			test_bit(NDD_ALIASING, &nvdimm->flags) ? "alias " : "",
a0e374525   Dan Williams   libnvdimm/region:...
294
  			test_bit(NDD_LABELING, &nvdimm->flags) ? "label " : "",
efbf6f50a   Dan Williams   libnvdimm: introd...
295
296
297
  			test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : "");
  }
  static DEVICE_ATTR_RO(flags);
eaf961536   Dan Williams   libnvdimm, nfit: ...
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  		char *buf)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
  
  	/*
  	 * The state may be in the process of changing, userspace should
  	 * quiesce probing if it wants a static answer
  	 */
  	nvdimm_bus_lock(dev);
  	nvdimm_bus_unlock(dev);
  	return sprintf(buf, "%s
  ", atomic_read(&nvdimm->busy)
  			? "active" : "idle");
  }
  static DEVICE_ATTR_RO(state);
0ba1c6348   Dan Williams   libnvdimm: write ...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  static ssize_t available_slots_show(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
  	ssize_t rc;
  	u32 nfree;
  
  	if (!ndd)
  		return -ENXIO;
  
  	nvdimm_bus_lock(dev);
  	nfree = nd_label_nfree(ndd);
  	if (nfree - 1 > nfree) {
  		dev_WARN_ONCE(dev, 1, "we ate our last label?
  ");
  		nfree = 0;
  	} else
  		nfree--;
  	rc = sprintf(buf, "%d
  ", nfree);
  	nvdimm_bus_unlock(dev);
  	return rc;
  }
  static DEVICE_ATTR_RO(available_slots);
3c13e2ac7   Dave Jiang   tools/testing/nvd...
338
  __weak ssize_t security_show(struct device *dev,
f29893965   Dave Jiang   acpi/nfit, libnvd...
339
340
341
  		struct device_attribute *attr, char *buf)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
d78c620a2   Dan Williams   libnvdimm/securit...
342
  	if (test_bit(NVDIMM_SECURITY_DISABLED, &nvdimm->sec.flags))
f29893965   Dave Jiang   acpi/nfit, libnvd...
343
344
  		return sprintf(buf, "disabled
  ");
d78c620a2   Dan Williams   libnvdimm/securit...
345
  	if (test_bit(NVDIMM_SECURITY_UNLOCKED, &nvdimm->sec.flags))
f29893965   Dave Jiang   acpi/nfit, libnvd...
346
347
  		return sprintf(buf, "unlocked
  ");
d78c620a2   Dan Williams   libnvdimm/securit...
348
  	if (test_bit(NVDIMM_SECURITY_LOCKED, &nvdimm->sec.flags))
f29893965   Dave Jiang   acpi/nfit, libnvd...
349
350
  		return sprintf(buf, "locked
  ");
d78c620a2   Dan Williams   libnvdimm/securit...
351
  	if (test_bit(NVDIMM_SECURITY_OVERWRITE, &nvdimm->sec.flags))
f29893965   Dave Jiang   acpi/nfit, libnvd...
352
353
  		return sprintf(buf, "overwrite
  ");
f29893965   Dave Jiang   acpi/nfit, libnvd...
354
355
  	return -ENOTTY;
  }
37833fb79   Dave Jiang   acpi/nfit, libnvd...
356

d78c620a2   Dan Williams   libnvdimm/securit...
357
358
359
360
361
362
363
364
365
366
  static ssize_t frozen_show(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
  
  	return sprintf(buf, "%d
  ", test_bit(NVDIMM_SECURITY_FROZEN,
  				&nvdimm->sec.flags));
  }
  static DEVICE_ATTR_RO(frozen);
37833fb79   Dave Jiang   acpi/nfit, libnvd...
367
368
369
370
371
372
373
374
375
376
377
  static ssize_t security_store(struct device *dev,
  		struct device_attribute *attr, const char *buf, size_t len)
  
  {
  	ssize_t rc;
  
  	/*
  	 * Require all userspace triggered security management to be
  	 * done while probing is idle and the DIMM is not in active use
  	 * in any region.
  	 */
87a30e1f0   Dan Williams   driver-core, libn...
378
  	nd_device_lock(dev);
37833fb79   Dave Jiang   acpi/nfit, libnvd...
379
380
  	nvdimm_bus_lock(dev);
  	wait_nvdimm_bus_probe_idle(dev);
7b60422cb   Dan Williams   libnvdimm/securit...
381
  	rc = nvdimm_security_store(dev, buf, len);
37833fb79   Dave Jiang   acpi/nfit, libnvd...
382
  	nvdimm_bus_unlock(dev);
87a30e1f0   Dan Williams   driver-core, libn...
383
  	nd_device_unlock(dev);
37833fb79   Dave Jiang   acpi/nfit, libnvd...
384
385
386
387
  
  	return rc;
  }
  static DEVICE_ATTR_RW(security);
f29893965   Dave Jiang   acpi/nfit, libnvd...
388

62232e45f   Dan Williams   libnvdimm: contro...
389
  static struct attribute *nvdimm_attributes[] = {
eaf961536   Dan Williams   libnvdimm, nfit: ...
390
  	&dev_attr_state.attr,
efbf6f50a   Dan Williams   libnvdimm: introd...
391
  	&dev_attr_flags.attr,
62232e45f   Dan Williams   libnvdimm: contro...
392
  	&dev_attr_commands.attr,
0ba1c6348   Dan Williams   libnvdimm: write ...
393
  	&dev_attr_available_slots.attr,
f29893965   Dave Jiang   acpi/nfit, libnvd...
394
  	&dev_attr_security.attr,
d78c620a2   Dan Williams   libnvdimm/securit...
395
  	&dev_attr_frozen.attr,
62232e45f   Dan Williams   libnvdimm: contro...
396
397
  	NULL,
  };
f29893965   Dave Jiang   acpi/nfit, libnvd...
398
399
400
401
  static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n)
  {
  	struct device *dev = container_of(kobj, typeof(*dev), kobj);
  	struct nvdimm *nvdimm = to_nvdimm(dev);
d78c620a2   Dan Williams   libnvdimm/securit...
402
  	if (a != &dev_attr_security.attr && a != &dev_attr_frozen.attr)
f29893965   Dave Jiang   acpi/nfit, libnvd...
403
  		return a->mode;
d78c620a2   Dan Williams   libnvdimm/securit...
404
  	if (!nvdimm->sec.flags)
f29893965   Dave Jiang   acpi/nfit, libnvd...
405
  		return 0;
d78c620a2   Dan Williams   libnvdimm/securit...
406
407
408
409
410
411
412
413
414
415
416
417
  
  	if (a == &dev_attr_security.attr) {
  		/* Are there any state mutation ops (make writable)? */
  		if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable
  				|| nvdimm->sec.ops->change_key
  				|| nvdimm->sec.ops->erase
  				|| nvdimm->sec.ops->overwrite)
  			return a->mode;
  		return 0444;
  	}
  
  	if (nvdimm->sec.ops->freeze)
37833fb79   Dave Jiang   acpi/nfit, libnvd...
418
  		return a->mode;
d78c620a2   Dan Williams   libnvdimm/securit...
419
  	return 0;
f29893965   Dave Jiang   acpi/nfit, libnvd...
420
  }
360eba7eb   Dan Williams   libnvdimm: Move n...
421
  static const struct attribute_group nvdimm_attribute_group = {
62232e45f   Dan Williams   libnvdimm: contro...
422
  	.attrs = nvdimm_attributes,
f29893965   Dave Jiang   acpi/nfit, libnvd...
423
  	.is_visible = nvdimm_visible,
62232e45f   Dan Williams   libnvdimm: contro...
424
  };
360eba7eb   Dan Williams   libnvdimm: Move n...
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  
  static const struct attribute_group *nvdimm_attribute_groups[] = {
  	&nd_device_attribute_group,
  	&nvdimm_attribute_group,
  	NULL,
  };
  
  static const struct device_type nvdimm_device_type = {
  	.name = "nvdimm",
  	.release = nvdimm_release,
  	.groups = nvdimm_attribute_groups,
  };
  
  bool is_nvdimm(struct device *dev)
  {
  	return dev->type == &nvdimm_device_type;
  }
62232e45f   Dan Williams   libnvdimm: contro...
442

d6548ae4d   Dave Jiang   acpi/nfit, libnvd...
443
444
445
  struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
  		void *provider_data, const struct attribute_group **groups,
  		unsigned long flags, unsigned long cmd_mask, int num_flush,
f29893965   Dave Jiang   acpi/nfit, libnvd...
446
447
  		struct resource *flush_wpq, const char *dimm_id,
  		const struct nvdimm_security_ops *sec_ops)
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
448
449
450
451
452
453
454
455
456
457
458
459
  {
  	struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
  	struct device *dev;
  
  	if (!nvdimm)
  		return NULL;
  
  	nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
  	if (nvdimm->id < 0) {
  		kfree(nvdimm);
  		return NULL;
  	}
d6548ae4d   Dave Jiang   acpi/nfit, libnvd...
460
461
  
  	nvdimm->dimm_id = dimm_id;
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
462
  	nvdimm->provider_data = provider_data;
d5d30d5a5   Dan Williams   libnvdimm/dimm: A...
463
464
  	if (noblk)
  		flags |= 1 << NDD_NOBLK;
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
465
  	nvdimm->flags = flags;
e3654eca7   Dan Williams   nfit, libnvdimm: ...
466
  	nvdimm->cmd_mask = cmd_mask;
e5ae3b252   Dan Williams   libnvdimm, nfit: ...
467
468
  	nvdimm->num_flush = num_flush;
  	nvdimm->flush_wpq = flush_wpq;
eaf961536   Dan Williams   libnvdimm, nfit: ...
469
  	atomic_set(&nvdimm->busy, 0);
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
470
471
472
473
  	dev = &nvdimm->dev;
  	dev_set_name(dev, "nmem%d", nvdimm->id);
  	dev->parent = &nvdimm_bus->dev;
  	dev->type = &nvdimm_device_type;
62232e45f   Dan Williams   libnvdimm: contro...
474
  	dev->devt = MKDEV(nvdimm_major, nvdimm->id);
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
475
  	dev->groups = groups;
f29893965   Dave Jiang   acpi/nfit, libnvd...
476
  	nvdimm->sec.ops = sec_ops;
7d988097c   Dave Jiang   acpi/nfit, libnvd...
477
478
  	nvdimm->sec.overwrite_tmo = 0;
  	INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query);
f29893965   Dave Jiang   acpi/nfit, libnvd...
479
480
481
482
  	/*
  	 * Security state must be initialized before device_add() for
  	 * attribute visibility.
  	 */
89fa9d8ea   Dave Jiang   acpi/nfit, libnvd...
483
  	/* get security state and extended (master) state */
d78c620a2   Dan Williams   libnvdimm/securit...
484
485
  	nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
  	nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
486
  	nd_device_register(dev);
e6dfb2de4   Dan Williams   libnvdimm, nfit: ...
487
488
489
  
  	return nvdimm;
  }
d6548ae4d   Dave Jiang   acpi/nfit, libnvd...
490
  EXPORT_SYMBOL_GPL(__nvdimm_create);
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
491

1cd738654   Dan Williams   libnvdimm/securit...
492
  static void shutdown_security_notify(void *data)
7d988097c   Dave Jiang   acpi/nfit, libnvd...
493
  {
1cd738654   Dan Williams   libnvdimm/securit...
494
495
496
497
498
499
500
501
  	struct nvdimm *nvdimm = data;
  
  	sysfs_put(nvdimm->sec.overwrite_state);
  }
  
  int nvdimm_security_setup_events(struct device *dev)
  {
  	struct nvdimm *nvdimm = to_nvdimm(dev);
d78c620a2   Dan Williams   libnvdimm/securit...
502
  	if (!nvdimm->sec.flags || !nvdimm->sec.ops
1cd738654   Dan Williams   libnvdimm/securit...
503
504
505
  			|| !nvdimm->sec.ops->overwrite)
  		return 0;
  	nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security");
7d988097c   Dave Jiang   acpi/nfit, libnvd...
506
  	if (!nvdimm->sec.overwrite_state)
1cd738654   Dan Williams   libnvdimm/securit...
507
508
509
  		return -ENOMEM;
  
  	return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm);
7d988097c   Dave Jiang   acpi/nfit, libnvd...
510
511
512
513
514
515
516
517
  }
  EXPORT_SYMBOL_GPL(nvdimm_security_setup_events);
  
  int nvdimm_in_overwrite(struct nvdimm *nvdimm)
  {
  	return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags);
  }
  EXPORT_SYMBOL_GPL(nvdimm_in_overwrite);
37833fb79   Dave Jiang   acpi/nfit, libnvd...
518
519
520
521
522
523
524
525
  int nvdimm_security_freeze(struct nvdimm *nvdimm)
  {
  	int rc;
  
  	WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
  
  	if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze)
  		return -EOPNOTSUPP;
d78c620a2   Dan Williams   libnvdimm/securit...
526
  	if (!nvdimm->sec.flags)
37833fb79   Dave Jiang   acpi/nfit, libnvd...
527
  		return -EIO;
7d988097c   Dave Jiang   acpi/nfit, libnvd...
528
529
530
531
532
  	if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
  		dev_warn(&nvdimm->dev, "Overwrite operation in progress.
  ");
  		return -EBUSY;
  	}
37833fb79   Dave Jiang   acpi/nfit, libnvd...
533
  	rc = nvdimm->sec.ops->freeze(nvdimm);
d78c620a2   Dan Williams   libnvdimm/securit...
534
  	nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
37833fb79   Dave Jiang   acpi/nfit, libnvd...
535
536
537
  
  	return rc;
  }
2522afb86   Dan Williams   libnvdimm/region:...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  static unsigned long dpa_align(struct nd_region *nd_region)
  {
  	struct device *dev = &nd_region->dev;
  
  	if (dev_WARN_ONCE(dev, !is_nvdimm_bus_locked(dev),
  				"bus lock required for capacity provision
  "))
  		return 0;
  	if (dev_WARN_ONCE(dev, !nd_region->ndr_mappings || nd_region->align
  				% nd_region->ndr_mappings,
  				"invalid region align %#lx mappings: %d
  ",
  				nd_region->align, nd_region->ndr_mappings))
  		return 0;
  	return nd_region->align / nd_region->ndr_mappings;
  }
762d067db   Dan Williams   libnvdimm, namesp...
554
  int alias_dpa_busy(struct device *dev, void *data)
a1f3e4d6a   Dan Williams   libnvdimm, region...
555
  {
fe514739d   Dan Williams   libnvdimm: fix bl...
556
  	resource_size_t map_end, blk_start, new;
a1f3e4d6a   Dan Williams   libnvdimm, region...
557
558
559
560
561
  	struct blk_alloc_info *info = data;
  	struct nd_mapping *nd_mapping;
  	struct nd_region *nd_region;
  	struct nvdimm_drvdata *ndd;
  	struct resource *res;
2522afb86   Dan Williams   libnvdimm/region:...
562
  	unsigned long align;
a1f3e4d6a   Dan Williams   libnvdimm, region...
563
  	int i;
c9e582aa6   Dan Williams   libnvdimm, nfit: ...
564
  	if (!is_memory(dev))
a1f3e4d6a   Dan Williams   libnvdimm, region...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
  		return 0;
  
  	nd_region = to_nd_region(dev);
  	for (i = 0; i < nd_region->ndr_mappings; i++) {
  		nd_mapping  = &nd_region->mapping[i];
  		if (nd_mapping->nvdimm == info->nd_mapping->nvdimm)
  			break;
  	}
  
  	if (i >= nd_region->ndr_mappings)
  		return 0;
  
  	ndd = to_ndd(nd_mapping);
  	map_end = nd_mapping->start + nd_mapping->size - 1;
  	blk_start = nd_mapping->start;
762d067db   Dan Williams   libnvdimm, namesp...
580
581
582
583
584
585
586
587
588
589
590
591
592
  
  	/*
  	 * In the allocation case ->res is set to free space that we are
  	 * looking to validate against PMEM aliasing collision rules
  	 * (i.e. BLK is allocated after all aliased PMEM).
  	 */
  	if (info->res) {
  		if (info->res->start >= nd_mapping->start
  				&& info->res->start < map_end)
  			/* pass */;
  		else
  			return 0;
  	}
a1f3e4d6a   Dan Williams   libnvdimm, region...
593
594
595
   retry:
  	/*
  	 * Find the free dpa from the end of the last pmem allocation to
fe514739d   Dan Williams   libnvdimm: fix bl...
596
  	 * the end of the interleave-set mapping.
a1f3e4d6a   Dan Williams   libnvdimm, region...
597
  	 */
2522afb86   Dan Williams   libnvdimm/region:...
598
599
600
  	align = dpa_align(nd_region);
  	if (!align)
  		return 0;
a1f3e4d6a   Dan Williams   libnvdimm, region...
601
  	for_each_dpa_resource(ndd, res) {
2522afb86   Dan Williams   libnvdimm/region:...
602
  		resource_size_t start, end;
fe514739d   Dan Williams   libnvdimm: fix bl...
603
604
  		if (strncmp(res->name, "pmem", 4) != 0)
  			continue;
2522afb86   Dan Williams   libnvdimm/region:...
605
606
607
608
609
610
  
  		start = ALIGN_DOWN(res->start, align);
  		end = ALIGN(res->end + 1, align) - 1;
  		if ((start >= blk_start && start < map_end)
  				|| (end >= blk_start && end <= map_end)) {
  			new = max(blk_start, min(map_end, end) + 1);
fe514739d   Dan Williams   libnvdimm: fix bl...
611
612
613
614
  			if (new != blk_start) {
  				blk_start = new;
  				goto retry;
  			}
a1f3e4d6a   Dan Williams   libnvdimm, region...
615
616
  		}
  	}
762d067db   Dan Williams   libnvdimm, namesp...
617
618
619
620
621
622
623
  	/* update the free space range with the probed blk_start */
  	if (info->res && blk_start > info->res->start) {
  		info->res->start = max(info->res->start, blk_start);
  		if (info->res->start > info->res->end)
  			info->res->end = info->res->start - 1;
  		return 1;
  	}
fe514739d   Dan Williams   libnvdimm: fix bl...
624
  	info->available -= blk_start - nd_mapping->start;
762d067db   Dan Williams   libnvdimm, namesp...
625

a1f3e4d6a   Dan Williams   libnvdimm, region...
626
627
  	return 0;
  }
bf9bccc14   Dan Williams   libnvdimm: pmem l...
628
  /**
1b40e09a1   Dan Williams   libnvdimm: blk la...
629
630
631
   * nd_blk_available_dpa - account the unused dpa of BLK region
   * @nd_mapping: container of dpa-resource-root + labels
   *
a1f3e4d6a   Dan Williams   libnvdimm, region...
632
633
634
   * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
   * we arrange for them to never start at an lower dpa than the last
   * PMEM allocation in an aliased region.
1b40e09a1   Dan Williams   libnvdimm: blk la...
635
   */
a1f3e4d6a   Dan Williams   libnvdimm, region...
636
  resource_size_t nd_blk_available_dpa(struct nd_region *nd_region)
1b40e09a1   Dan Williams   libnvdimm: blk la...
637
  {
a1f3e4d6a   Dan Williams   libnvdimm, region...
638
639
  	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  	struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1b40e09a1   Dan Williams   libnvdimm: blk la...
640
  	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
a1f3e4d6a   Dan Williams   libnvdimm, region...
641
642
643
  	struct blk_alloc_info info = {
  		.nd_mapping = nd_mapping,
  		.available = nd_mapping->size,
762d067db   Dan Williams   libnvdimm, namesp...
644
  		.res = NULL,
a1f3e4d6a   Dan Williams   libnvdimm, region...
645
  	};
1b40e09a1   Dan Williams   libnvdimm: blk la...
646
  	struct resource *res;
2522afb86   Dan Williams   libnvdimm/region:...
647
  	unsigned long align;
1b40e09a1   Dan Williams   libnvdimm: blk la...
648
649
650
  
  	if (!ndd)
  		return 0;
a1f3e4d6a   Dan Williams   libnvdimm, region...
651
  	device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
1b40e09a1   Dan Williams   libnvdimm: blk la...
652

a1f3e4d6a   Dan Williams   libnvdimm, region...
653
  	/* now account for busy blk allocations in unaliased dpa */
2522afb86   Dan Williams   libnvdimm/region:...
654
655
656
  	align = dpa_align(nd_region);
  	if (!align)
  		return 0;
a1f3e4d6a   Dan Williams   libnvdimm, region...
657
  	for_each_dpa_resource(ndd, res) {
2522afb86   Dan Williams   libnvdimm/region:...
658
  		resource_size_t start, end, size;
a1f3e4d6a   Dan Williams   libnvdimm, region...
659
660
  		if (strncmp(res->name, "blk", 3) != 0)
  			continue;
2522afb86   Dan Williams   libnvdimm/region:...
661
662
663
664
665
666
  		start = ALIGN_DOWN(res->start, align);
  		end = ALIGN(res->end + 1, align) - 1;
  		size = end - start + 1;
  		if (size >= info.available)
  			return 0;
  		info.available -= size;
a1f3e4d6a   Dan Williams   libnvdimm, region...
667
668
669
  	}
  
  	return info.available;
1b40e09a1   Dan Williams   libnvdimm: blk la...
670
671
672
  }
  
  /**
12e3129e2   Keith Busch   libnvdimm: Use ma...
673
674
675
676
677
678
679
680
681
682
683
684
   * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
   *			   contiguous unallocated dpa range.
   * @nd_region: constrain available space check to this reference region
   * @nd_mapping: container of dpa-resource-root + labels
   */
  resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region,
  					   struct nd_mapping *nd_mapping)
  {
  	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  	struct nvdimm_bus *nvdimm_bus;
  	resource_size_t max = 0;
  	struct resource *res;
2522afb86   Dan Williams   libnvdimm/region:...
685
  	unsigned long align;
12e3129e2   Keith Busch   libnvdimm: Use ma...
686
687
688
689
  
  	/* if a dimm is disabled the available capacity is zero */
  	if (!ndd)
  		return 0;
2522afb86   Dan Williams   libnvdimm/region:...
690
691
692
  	align = dpa_align(nd_region);
  	if (!align)
  		return 0;
12e3129e2   Keith Busch   libnvdimm: Use ma...
693
694
695
696
  	nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  	if (__reserve_free_pmem(&nd_region->dev, nd_mapping->nvdimm))
  		return 0;
  	for_each_dpa_resource(ndd, res) {
2522afb86   Dan Williams   libnvdimm/region:...
697
  		resource_size_t start, end;
12e3129e2   Keith Busch   libnvdimm: Use ma...
698
699
  		if (strcmp(res->name, "pmem-reserve") != 0)
  			continue;
2522afb86   Dan Williams   libnvdimm/region:...
700
701
702
703
704
705
706
  		/* trim free space relative to current alignment setting */
  		start = ALIGN(res->start, align);
  		end = ALIGN_DOWN(res->end + 1, align) - 1;
  		if (end < start)
  			continue;
  		if (end - start + 1 > max)
  			max = end - start + 1;
12e3129e2   Keith Busch   libnvdimm: Use ma...
707
708
709
710
711
712
  	}
  	release_free_pmem(nvdimm_bus, nd_mapping);
  	return max;
  }
  
  /**
bf9bccc14   Dan Williams   libnvdimm: pmem l...
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
   * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
   * @nd_mapping: container of dpa-resource-root + labels
   * @nd_region: constrain available space check to this reference region
   * @overlap: calculate available space assuming this level of overlap
   *
   * Validate that a PMEM label, if present, aligns with the start of an
   * interleave set and truncate the available size at the lowest BLK
   * overlap point.
   *
   * The expectation is that this routine is called multiple times as it
   * probes for the largest BLK encroachment for any single member DIMM of
   * the interleave set.  Once that value is determined the PMEM-limit for
   * the set can be established.
   */
  resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
  		struct nd_mapping *nd_mapping, resource_size_t *overlap)
  {
  	resource_size_t map_start, map_end, busy = 0, available, blk_start;
  	struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  	struct resource *res;
  	const char *reason;
2522afb86   Dan Williams   libnvdimm/region:...
734
  	unsigned long align;
bf9bccc14   Dan Williams   libnvdimm: pmem l...
735
736
737
  
  	if (!ndd)
  		return 0;
2522afb86   Dan Williams   libnvdimm/region:...
738
739
740
  	align = dpa_align(nd_region);
  	if (!align)
  		return 0;
bf9bccc14   Dan Williams   libnvdimm: pmem l...
741
742
743
  	map_start = nd_mapping->start;
  	map_end = map_start + nd_mapping->size - 1;
  	blk_start = max(map_start, map_end + 1 - *overlap);
a1f3e4d6a   Dan Williams   libnvdimm, region...
744
  	for_each_dpa_resource(ndd, res) {
2522afb86   Dan Williams   libnvdimm/region:...
745
746
747
748
749
  		resource_size_t start, end;
  
  		start = ALIGN_DOWN(res->start, align);
  		end = ALIGN(res->end + 1, align) - 1;
  		if (start >= map_start && start < map_end) {
bf9bccc14   Dan Williams   libnvdimm: pmem l...
750
  			if (strncmp(res->name, "blk", 3) == 0)
a1f3e4d6a   Dan Williams   libnvdimm, region...
751
  				blk_start = min(blk_start,
2522afb86   Dan Williams   libnvdimm/region:...
752
753
  						max(map_start, start));
  			else if (end > map_end) {
bf9bccc14   Dan Williams   libnvdimm: pmem l...
754
755
  				reason = "misaligned to iset";
  				goto err;
a1f3e4d6a   Dan Williams   libnvdimm, region...
756
  			} else
2522afb86   Dan Williams   libnvdimm/region:...
757
758
  				busy += end - start + 1;
  		} else if (end >= map_start && end <= map_end) {
bf9bccc14   Dan Williams   libnvdimm: pmem l...
759
760
761
762
763
764
765
  			if (strncmp(res->name, "blk", 3) == 0) {
  				/*
  				 * If a BLK allocation overlaps the start of
  				 * PMEM the entire interleave set may now only
  				 * be used for BLK.
  				 */
  				blk_start = map_start;
a1f3e4d6a   Dan Williams   libnvdimm, region...
766
  			} else
2522afb86   Dan Williams   libnvdimm/region:...
767
768
  				busy += end - start + 1;
  		} else if (map_start > start && map_start < end) {
bf9bccc14   Dan Williams   libnvdimm: pmem l...
769
770
771
772
  			/* total eclipse of the mapping */
  			busy += nd_mapping->size;
  			blk_start = map_start;
  		}
a1f3e4d6a   Dan Williams   libnvdimm, region...
773
  	}
bf9bccc14   Dan Williams   libnvdimm: pmem l...
774
775
776
777
  
  	*overlap = map_end + 1 - blk_start;
  	available = blk_start - map_start;
  	if (busy < available)
2522afb86   Dan Williams   libnvdimm/region:...
778
  		return ALIGN_DOWN(available - busy, align);
bf9bccc14   Dan Williams   libnvdimm: pmem l...
779
780
781
  	return 0;
  
   err:
bf9bccc14   Dan Williams   libnvdimm: pmem l...
782
783
784
785
  	nd_dbg_dpa(nd_region, ndd, res, "%s
  ", reason);
  	return 0;
  }
4a826c83d   Dan Williams   libnvdimm: namesp...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
  void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
  {
  	WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  	kfree(res->name);
  	__release_region(&ndd->dpa, res->start, resource_size(res));
  }
  
  struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
  		struct nd_label_id *label_id, resource_size_t start,
  		resource_size_t n)
  {
  	char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
  	struct resource *res;
  
  	if (!name)
  		return NULL;
  
  	WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  	res = __request_region(&ndd->dpa, start, n, name, 0);
  	if (!res)
  		kfree(name);
  	return res;
  }
bf9bccc14   Dan Williams   libnvdimm: pmem l...
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
  /**
   * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
   * @nvdimm: container of dpa-resource-root + labels
   * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
   */
  resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
  		struct nd_label_id *label_id)
  {
  	resource_size_t allocated = 0;
  	struct resource *res;
  
  	for_each_dpa_resource(ndd, res)
  		if (strcmp(res->name, label_id->id) == 0)
  			allocated += resource_size(res);
  
  	return allocated;
  }
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
  static int count_dimms(struct device *dev, void *c)
  {
  	int *count = c;
  
  	if (is_nvdimm(dev))
  		(*count)++;
  	return 0;
  }
  
  int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
  {
  	int count = 0;
  	/* Flush any possible dimm registration failures */
  	nd_synchronize();
  
  	device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
426824d63   Dan Williams   libnvdimm: remove...
842
843
  	dev_dbg(&nvdimm_bus->dev, "count: %d
  ", count);
4d88a97aa   Dan Williams   libnvdimm, nvdimm...
844
845
846
847
848
  	if (count != dimm_count)
  		return -ENXIO;
  	return 0;
  }
  EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
b354aba01   Dan Williams   libnvdimm: releas...
849
850
851
852
853
  
  void __exit nvdimm_devs_exit(void)
  {
  	ida_destroy(&dimm_ida);
  }