Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 */
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6#include <linux/moduleparam.h>
7#include <linux/vmalloc.h>
8#include <linux/device.h>
9#include <linux/ndctl.h>
10#include <linux/slab.h>
11#include <linux/io.h>
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include "nd-core.h"
15#include "label.h"
16#include "pmem.h"
17#include "nd.h"
18
19static DEFINE_IDA(dimm_ida);
20
21static bool noblk;
22module_param(noblk, bool, 0444);
23MODULE_PARM_DESC(noblk, "force disable BLK / local alias support");
24
25/*
26 * Retrieve bus and dimm handle and return if this bus supports
27 * get_config_data commands
28 */
29int nvdimm_check_config_data(struct device *dev)
30{
31 struct nvdimm *nvdimm = to_nvdimm(dev);
32
33 if (!nvdimm->cmd_mask ||
34 !test_bit(ND_CMD_GET_CONFIG_DATA, &nvdimm->cmd_mask)) {
35 if (test_bit(NDD_LABELING, &nvdimm->flags))
36 return -ENXIO;
37 else
38 return -ENOTTY;
39 }
40
41 return 0;
42}
43
44static int validate_dimm(struct nvdimm_drvdata *ndd)
45{
46 int rc;
47
48 if (!ndd)
49 return -EINVAL;
50
51 rc = nvdimm_check_config_data(ndd->dev);
52 if (rc)
53 dev_dbg(ndd->dev, "%ps: %s error: %d\n",
54 __builtin_return_address(0), __func__, rc);
55 return rc;
56}
57
58/**
59 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
60 * @nvdimm: dimm to initialize
61 */
62int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
63{
64 struct nd_cmd_get_config_size *cmd = &ndd->nsarea;
65 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
66 struct nvdimm_bus_descriptor *nd_desc;
67 int rc = validate_dimm(ndd);
68 int cmd_rc = 0;
69
70 if (rc)
71 return rc;
72
73 if (cmd->config_size)
74 return 0; /* already valid */
75
76 memset(cmd, 0, sizeof(*cmd));
77 nd_desc = nvdimm_bus->nd_desc;
78 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
79 ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd), &cmd_rc);
80 if (rc < 0)
81 return rc;
82 return cmd_rc;
83}
84
85int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
86 size_t offset, size_t len)
87{
88 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
89 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
90 int rc = validate_dimm(ndd), cmd_rc = 0;
91 struct nd_cmd_get_config_data_hdr *cmd;
92 size_t max_cmd_size, buf_offset;
93
94 if (rc)
95 return rc;
96
97 if (offset + len > ndd->nsarea.config_size)
98 return -ENXIO;
99
100 max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
101 cmd = kvzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
102 if (!cmd)
103 return -ENOMEM;
104
105 for (buf_offset = 0; len;
106 len -= cmd->in_length, buf_offset += cmd->in_length) {
107 size_t cmd_size;
108
109 cmd->in_offset = offset + buf_offset;
110 cmd->in_length = min(max_cmd_size, len);
111
112 cmd_size = sizeof(*cmd) + cmd->in_length;
113
114 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
115 ND_CMD_GET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
116 if (rc < 0)
117 break;
118 if (cmd_rc < 0) {
119 rc = cmd_rc;
120 break;
121 }
122
123 /* out_buf should be valid, copy it into our output buffer */
124 memcpy(buf + buf_offset, cmd->out_buf, cmd->in_length);
125 }
126 kvfree(cmd);
127
128 return rc;
129}
130
131int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
132 void *buf, size_t len)
133{
134 size_t max_cmd_size, buf_offset;
135 struct nd_cmd_set_config_hdr *cmd;
136 int rc = validate_dimm(ndd), cmd_rc = 0;
137 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
138 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
139
140 if (rc)
141 return rc;
142
143 if (offset + len > ndd->nsarea.config_size)
144 return -ENXIO;
145
146 max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
147 cmd = kvzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL);
148 if (!cmd)
149 return -ENOMEM;
150
151 for (buf_offset = 0; len; len -= cmd->in_length,
152 buf_offset += cmd->in_length) {
153 size_t cmd_size;
154
155 cmd->in_offset = offset + buf_offset;
156 cmd->in_length = min(max_cmd_size, len);
157 memcpy(cmd->in_buf, buf + buf_offset, cmd->in_length);
158
159 /* status is output in the last 4-bytes of the command buffer */
160 cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32);
161
162 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
163 ND_CMD_SET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
164 if (rc < 0)
165 break;
166 if (cmd_rc < 0) {
167 rc = cmd_rc;
168 break;
169 }
170 }
171 kvfree(cmd);
172
173 return rc;
174}
175
176void nvdimm_set_labeling(struct device *dev)
177{
178 struct nvdimm *nvdimm = to_nvdimm(dev);
179
180 set_bit(NDD_LABELING, &nvdimm->flags);
181}
182
183void nvdimm_set_locked(struct device *dev)
184{
185 struct nvdimm *nvdimm = to_nvdimm(dev);
186
187 set_bit(NDD_LOCKED, &nvdimm->flags);
188}
189
190void nvdimm_clear_locked(struct device *dev)
191{
192 struct nvdimm *nvdimm = to_nvdimm(dev);
193
194 clear_bit(NDD_LOCKED, &nvdimm->flags);
195}
196
197static void nvdimm_release(struct device *dev)
198{
199 struct nvdimm *nvdimm = to_nvdimm(dev);
200
201 ida_simple_remove(&dimm_ida, nvdimm->id);
202 kfree(nvdimm);
203}
204
205struct nvdimm *to_nvdimm(struct device *dev)
206{
207 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
208
209 WARN_ON(!is_nvdimm(dev));
210 return nvdimm;
211}
212EXPORT_SYMBOL_GPL(to_nvdimm);
213
214struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
215{
216 struct nd_region *nd_region = &ndbr->nd_region;
217 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
218
219 return nd_mapping->nvdimm;
220}
221EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
222
223unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr)
224{
225 /* pmem mapping properties are private to libnvdimm */
226 return ARCH_MEMREMAP_PMEM;
227}
228EXPORT_SYMBOL_GPL(nd_blk_memremap_flags);
229
230struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
231{
232 struct nvdimm *nvdimm = nd_mapping->nvdimm;
233
234 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
235
236 return dev_get_drvdata(&nvdimm->dev);
237}
238EXPORT_SYMBOL(to_ndd);
239
240void nvdimm_drvdata_release(struct kref *kref)
241{
242 struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
243 struct device *dev = ndd->dev;
244 struct resource *res, *_r;
245
246 dev_dbg(dev, "trace\n");
247 nvdimm_bus_lock(dev);
248 for_each_dpa_resource_safe(ndd, res, _r)
249 nvdimm_free_dpa(ndd, res);
250 nvdimm_bus_unlock(dev);
251
252 kvfree(ndd->data);
253 kfree(ndd);
254 put_device(dev);
255}
256
257void get_ndd(struct nvdimm_drvdata *ndd)
258{
259 kref_get(&ndd->kref);
260}
261
262void put_ndd(struct nvdimm_drvdata *ndd)
263{
264 if (ndd)
265 kref_put(&ndd->kref, nvdimm_drvdata_release);
266}
267
268const char *nvdimm_name(struct nvdimm *nvdimm)
269{
270 return dev_name(&nvdimm->dev);
271}
272EXPORT_SYMBOL_GPL(nvdimm_name);
273
274struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
275{
276 return &nvdimm->dev.kobj;
277}
278EXPORT_SYMBOL_GPL(nvdimm_kobj);
279
280unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
281{
282 return nvdimm->cmd_mask;
283}
284EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
285
286void *nvdimm_provider_data(struct nvdimm *nvdimm)
287{
288 if (nvdimm)
289 return nvdimm->provider_data;
290 return NULL;
291}
292EXPORT_SYMBOL_GPL(nvdimm_provider_data);
293
294static ssize_t commands_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
297 struct nvdimm *nvdimm = to_nvdimm(dev);
298 int cmd, len = 0;
299
300 if (!nvdimm->cmd_mask)
301 return sprintf(buf, "\n");
302
303 for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
304 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
305 len += sprintf(buf + len, "\n");
306 return len;
307}
308static DEVICE_ATTR_RO(commands);
309
310static ssize_t flags_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
312{
313 struct nvdimm *nvdimm = to_nvdimm(dev);
314
315 return sprintf(buf, "%s%s%s\n",
316 test_bit(NDD_ALIASING, &nvdimm->flags) ? "alias " : "",
317 test_bit(NDD_LABELING, &nvdimm->flags) ? "label " : "",
318 test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : "");
319}
320static DEVICE_ATTR_RO(flags);
321
322static ssize_t state_show(struct device *dev, struct device_attribute *attr,
323 char *buf)
324{
325 struct nvdimm *nvdimm = to_nvdimm(dev);
326
327 /*
328 * The state may be in the process of changing, userspace should
329 * quiesce probing if it wants a static answer
330 */
331 nvdimm_bus_lock(dev);
332 nvdimm_bus_unlock(dev);
333 return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
334 ? "active" : "idle");
335}
336static DEVICE_ATTR_RO(state);
337
338static ssize_t available_slots_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
340{
341 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
342 ssize_t rc;
343 u32 nfree;
344
345 if (!ndd)
346 return -ENXIO;
347
348 nvdimm_bus_lock(dev);
349 nfree = nd_label_nfree(ndd);
350 if (nfree - 1 > nfree) {
351 dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
352 nfree = 0;
353 } else
354 nfree--;
355 rc = sprintf(buf, "%d\n", nfree);
356 nvdimm_bus_unlock(dev);
357 return rc;
358}
359static DEVICE_ATTR_RO(available_slots);
360
361__weak ssize_t security_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct nvdimm *nvdimm = to_nvdimm(dev);
365
366 if (test_bit(NVDIMM_SECURITY_OVERWRITE, &nvdimm->sec.flags))
367 return sprintf(buf, "overwrite\n");
368 if (test_bit(NVDIMM_SECURITY_DISABLED, &nvdimm->sec.flags))
369 return sprintf(buf, "disabled\n");
370 if (test_bit(NVDIMM_SECURITY_UNLOCKED, &nvdimm->sec.flags))
371 return sprintf(buf, "unlocked\n");
372 if (test_bit(NVDIMM_SECURITY_LOCKED, &nvdimm->sec.flags))
373 return sprintf(buf, "locked\n");
374 return -ENOTTY;
375}
376
377static ssize_t frozen_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
379{
380 struct nvdimm *nvdimm = to_nvdimm(dev);
381
382 return sprintf(buf, "%d\n", test_bit(NVDIMM_SECURITY_FROZEN,
383 &nvdimm->sec.flags));
384}
385static DEVICE_ATTR_RO(frozen);
386
387static ssize_t security_store(struct device *dev,
388 struct device_attribute *attr, const char *buf, size_t len)
389
390{
391 ssize_t rc;
392
393 /*
394 * Require all userspace triggered security management to be
395 * done while probing is idle and the DIMM is not in active use
396 * in any region.
397 */
398 nd_device_lock(dev);
399 nvdimm_bus_lock(dev);
400 wait_nvdimm_bus_probe_idle(dev);
401 rc = nvdimm_security_store(dev, buf, len);
402 nvdimm_bus_unlock(dev);
403 nd_device_unlock(dev);
404
405 return rc;
406}
407static DEVICE_ATTR_RW(security);
408
409static struct attribute *nvdimm_attributes[] = {
410 &dev_attr_state.attr,
411 &dev_attr_flags.attr,
412 &dev_attr_commands.attr,
413 &dev_attr_available_slots.attr,
414 &dev_attr_security.attr,
415 &dev_attr_frozen.attr,
416 NULL,
417};
418
419static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n)
420{
421 struct device *dev = container_of(kobj, typeof(*dev), kobj);
422 struct nvdimm *nvdimm = to_nvdimm(dev);
423
424 if (a != &dev_attr_security.attr && a != &dev_attr_frozen.attr)
425 return a->mode;
426 if (!nvdimm->sec.flags)
427 return 0;
428
429 if (a == &dev_attr_security.attr) {
430 /* Are there any state mutation ops (make writable)? */
431 if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable
432 || nvdimm->sec.ops->change_key
433 || nvdimm->sec.ops->erase
434 || nvdimm->sec.ops->overwrite)
435 return a->mode;
436 return 0444;
437 }
438
439 if (nvdimm->sec.ops->freeze)
440 return a->mode;
441 return 0;
442}
443
444static const struct attribute_group nvdimm_attribute_group = {
445 .attrs = nvdimm_attributes,
446 .is_visible = nvdimm_visible,
447};
448
449static ssize_t result_show(struct device *dev, struct device_attribute *attr, char *buf)
450{
451 struct nvdimm *nvdimm = to_nvdimm(dev);
452 enum nvdimm_fwa_result result;
453
454 if (!nvdimm->fw_ops)
455 return -EOPNOTSUPP;
456
457 nvdimm_bus_lock(dev);
458 result = nvdimm->fw_ops->activate_result(nvdimm);
459 nvdimm_bus_unlock(dev);
460
461 switch (result) {
462 case NVDIMM_FWA_RESULT_NONE:
463 return sprintf(buf, "none\n");
464 case NVDIMM_FWA_RESULT_SUCCESS:
465 return sprintf(buf, "success\n");
466 case NVDIMM_FWA_RESULT_FAIL:
467 return sprintf(buf, "fail\n");
468 case NVDIMM_FWA_RESULT_NOTSTAGED:
469 return sprintf(buf, "not_staged\n");
470 case NVDIMM_FWA_RESULT_NEEDRESET:
471 return sprintf(buf, "need_reset\n");
472 default:
473 return -ENXIO;
474 }
475}
476static DEVICE_ATTR_ADMIN_RO(result);
477
478static ssize_t activate_show(struct device *dev, struct device_attribute *attr, char *buf)
479{
480 struct nvdimm *nvdimm = to_nvdimm(dev);
481 enum nvdimm_fwa_state state;
482
483 if (!nvdimm->fw_ops)
484 return -EOPNOTSUPP;
485
486 nvdimm_bus_lock(dev);
487 state = nvdimm->fw_ops->activate_state(nvdimm);
488 nvdimm_bus_unlock(dev);
489
490 switch (state) {
491 case NVDIMM_FWA_IDLE:
492 return sprintf(buf, "idle\n");
493 case NVDIMM_FWA_BUSY:
494 return sprintf(buf, "busy\n");
495 case NVDIMM_FWA_ARMED:
496 return sprintf(buf, "armed\n");
497 default:
498 return -ENXIO;
499 }
500}
501
502static ssize_t activate_store(struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t len)
504{
505 struct nvdimm *nvdimm = to_nvdimm(dev);
506 enum nvdimm_fwa_trigger arg;
507 int rc;
508
509 if (!nvdimm->fw_ops)
510 return -EOPNOTSUPP;
511
512 if (sysfs_streq(buf, "arm"))
513 arg = NVDIMM_FWA_ARM;
514 else if (sysfs_streq(buf, "disarm"))
515 arg = NVDIMM_FWA_DISARM;
516 else
517 return -EINVAL;
518
519 nvdimm_bus_lock(dev);
520 rc = nvdimm->fw_ops->arm(nvdimm, arg);
521 nvdimm_bus_unlock(dev);
522
523 if (rc < 0)
524 return rc;
525 return len;
526}
527static DEVICE_ATTR_ADMIN_RW(activate);
528
529static struct attribute *nvdimm_firmware_attributes[] = {
530 &dev_attr_activate.attr,
531 &dev_attr_result.attr,
532 NULL,
533};
534
535static umode_t nvdimm_firmware_visible(struct kobject *kobj, struct attribute *a, int n)
536{
537 struct device *dev = container_of(kobj, typeof(*dev), kobj);
538 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
539 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
540 struct nvdimm *nvdimm = to_nvdimm(dev);
541 enum nvdimm_fwa_capability cap;
542
543 if (!nd_desc->fw_ops)
544 return 0;
545 if (!nvdimm->fw_ops)
546 return 0;
547
548 nvdimm_bus_lock(dev);
549 cap = nd_desc->fw_ops->capability(nd_desc);
550 nvdimm_bus_unlock(dev);
551
552 if (cap < NVDIMM_FWA_CAP_QUIESCE)
553 return 0;
554
555 return a->mode;
556}
557
558static const struct attribute_group nvdimm_firmware_attribute_group = {
559 .name = "firmware",
560 .attrs = nvdimm_firmware_attributes,
561 .is_visible = nvdimm_firmware_visible,
562};
563
564static const struct attribute_group *nvdimm_attribute_groups[] = {
565 &nd_device_attribute_group,
566 &nvdimm_attribute_group,
567 &nvdimm_firmware_attribute_group,
568 NULL,
569};
570
571static const struct device_type nvdimm_device_type = {
572 .name = "nvdimm",
573 .release = nvdimm_release,
574 .groups = nvdimm_attribute_groups,
575};
576
577bool is_nvdimm(struct device *dev)
578{
579 return dev->type == &nvdimm_device_type;
580}
581
582struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
583 void *provider_data, const struct attribute_group **groups,
584 unsigned long flags, unsigned long cmd_mask, int num_flush,
585 struct resource *flush_wpq, const char *dimm_id,
586 const struct nvdimm_security_ops *sec_ops,
587 const struct nvdimm_fw_ops *fw_ops)
588{
589 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
590 struct device *dev;
591
592 if (!nvdimm)
593 return NULL;
594
595 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
596 if (nvdimm->id < 0) {
597 kfree(nvdimm);
598 return NULL;
599 }
600
601 nvdimm->dimm_id = dimm_id;
602 nvdimm->provider_data = provider_data;
603 if (noblk)
604 flags |= 1 << NDD_NOBLK;
605 nvdimm->flags = flags;
606 nvdimm->cmd_mask = cmd_mask;
607 nvdimm->num_flush = num_flush;
608 nvdimm->flush_wpq = flush_wpq;
609 atomic_set(&nvdimm->busy, 0);
610 dev = &nvdimm->dev;
611 dev_set_name(dev, "nmem%d", nvdimm->id);
612 dev->parent = &nvdimm_bus->dev;
613 dev->type = &nvdimm_device_type;
614 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
615 dev->groups = groups;
616 nvdimm->sec.ops = sec_ops;
617 nvdimm->fw_ops = fw_ops;
618 nvdimm->sec.overwrite_tmo = 0;
619 INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query);
620 /*
621 * Security state must be initialized before device_add() for
622 * attribute visibility.
623 */
624 /* get security state and extended (master) state */
625 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
626 nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
627 nd_device_register(dev);
628
629 return nvdimm;
630}
631EXPORT_SYMBOL_GPL(__nvdimm_create);
632
633static void shutdown_security_notify(void *data)
634{
635 struct nvdimm *nvdimm = data;
636
637 sysfs_put(nvdimm->sec.overwrite_state);
638}
639
640int nvdimm_security_setup_events(struct device *dev)
641{
642 struct nvdimm *nvdimm = to_nvdimm(dev);
643
644 if (!nvdimm->sec.flags || !nvdimm->sec.ops
645 || !nvdimm->sec.ops->overwrite)
646 return 0;
647 nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security");
648 if (!nvdimm->sec.overwrite_state)
649 return -ENOMEM;
650
651 return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm);
652}
653EXPORT_SYMBOL_GPL(nvdimm_security_setup_events);
654
655int nvdimm_in_overwrite(struct nvdimm *nvdimm)
656{
657 return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags);
658}
659EXPORT_SYMBOL_GPL(nvdimm_in_overwrite);
660
661int nvdimm_security_freeze(struct nvdimm *nvdimm)
662{
663 int rc;
664
665 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
666
667 if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze)
668 return -EOPNOTSUPP;
669
670 if (!nvdimm->sec.flags)
671 return -EIO;
672
673 if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
674 dev_warn(&nvdimm->dev, "Overwrite operation in progress.\n");
675 return -EBUSY;
676 }
677
678 rc = nvdimm->sec.ops->freeze(nvdimm);
679 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
680
681 return rc;
682}
683
684static unsigned long dpa_align(struct nd_region *nd_region)
685{
686 struct device *dev = &nd_region->dev;
687
688 if (dev_WARN_ONCE(dev, !is_nvdimm_bus_locked(dev),
689 "bus lock required for capacity provision\n"))
690 return 0;
691 if (dev_WARN_ONCE(dev, !nd_region->ndr_mappings || nd_region->align
692 % nd_region->ndr_mappings,
693 "invalid region align %#lx mappings: %d\n",
694 nd_region->align, nd_region->ndr_mappings))
695 return 0;
696 return nd_region->align / nd_region->ndr_mappings;
697}
698
699int alias_dpa_busy(struct device *dev, void *data)
700{
701 resource_size_t map_end, blk_start, new;
702 struct blk_alloc_info *info = data;
703 struct nd_mapping *nd_mapping;
704 struct nd_region *nd_region;
705 struct nvdimm_drvdata *ndd;
706 struct resource *res;
707 unsigned long align;
708 int i;
709
710 if (!is_memory(dev))
711 return 0;
712
713 nd_region = to_nd_region(dev);
714 for (i = 0; i < nd_region->ndr_mappings; i++) {
715 nd_mapping = &nd_region->mapping[i];
716 if (nd_mapping->nvdimm == info->nd_mapping->nvdimm)
717 break;
718 }
719
720 if (i >= nd_region->ndr_mappings)
721 return 0;
722
723 ndd = to_ndd(nd_mapping);
724 map_end = nd_mapping->start + nd_mapping->size - 1;
725 blk_start = nd_mapping->start;
726
727 /*
728 * In the allocation case ->res is set to free space that we are
729 * looking to validate against PMEM aliasing collision rules
730 * (i.e. BLK is allocated after all aliased PMEM).
731 */
732 if (info->res) {
733 if (info->res->start >= nd_mapping->start
734 && info->res->start < map_end)
735 /* pass */;
736 else
737 return 0;
738 }
739
740 retry:
741 /*
742 * Find the free dpa from the end of the last pmem allocation to
743 * the end of the interleave-set mapping.
744 */
745 align = dpa_align(nd_region);
746 if (!align)
747 return 0;
748
749 for_each_dpa_resource(ndd, res) {
750 resource_size_t start, end;
751
752 if (strncmp(res->name, "pmem", 4) != 0)
753 continue;
754
755 start = ALIGN_DOWN(res->start, align);
756 end = ALIGN(res->end + 1, align) - 1;
757 if ((start >= blk_start && start < map_end)
758 || (end >= blk_start && end <= map_end)) {
759 new = max(blk_start, min(map_end, end) + 1);
760 if (new != blk_start) {
761 blk_start = new;
762 goto retry;
763 }
764 }
765 }
766
767 /* update the free space range with the probed blk_start */
768 if (info->res && blk_start > info->res->start) {
769 info->res->start = max(info->res->start, blk_start);
770 if (info->res->start > info->res->end)
771 info->res->end = info->res->start - 1;
772 return 1;
773 }
774
775 info->available -= blk_start - nd_mapping->start;
776
777 return 0;
778}
779
780/**
781 * nd_blk_available_dpa - account the unused dpa of BLK region
782 * @nd_mapping: container of dpa-resource-root + labels
783 *
784 * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
785 * we arrange for them to never start at an lower dpa than the last
786 * PMEM allocation in an aliased region.
787 */
788resource_size_t nd_blk_available_dpa(struct nd_region *nd_region)
789{
790 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
791 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
792 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
793 struct blk_alloc_info info = {
794 .nd_mapping = nd_mapping,
795 .available = nd_mapping->size,
796 .res = NULL,
797 };
798 struct resource *res;
799 unsigned long align;
800
801 if (!ndd)
802 return 0;
803
804 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
805
806 /* now account for busy blk allocations in unaliased dpa */
807 align = dpa_align(nd_region);
808 if (!align)
809 return 0;
810 for_each_dpa_resource(ndd, res) {
811 resource_size_t start, end, size;
812
813 if (strncmp(res->name, "blk", 3) != 0)
814 continue;
815 start = ALIGN_DOWN(res->start, align);
816 end = ALIGN(res->end + 1, align) - 1;
817 size = end - start + 1;
818 if (size >= info.available)
819 return 0;
820 info.available -= size;
821 }
822
823 return info.available;
824}
825
826/**
827 * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
828 * contiguous unallocated dpa range.
829 * @nd_region: constrain available space check to this reference region
830 * @nd_mapping: container of dpa-resource-root + labels
831 */
832resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region,
833 struct nd_mapping *nd_mapping)
834{
835 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
836 struct nvdimm_bus *nvdimm_bus;
837 resource_size_t max = 0;
838 struct resource *res;
839 unsigned long align;
840
841 /* if a dimm is disabled the available capacity is zero */
842 if (!ndd)
843 return 0;
844
845 align = dpa_align(nd_region);
846 if (!align)
847 return 0;
848
849 nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
850 if (__reserve_free_pmem(&nd_region->dev, nd_mapping->nvdimm))
851 return 0;
852 for_each_dpa_resource(ndd, res) {
853 resource_size_t start, end;
854
855 if (strcmp(res->name, "pmem-reserve") != 0)
856 continue;
857 /* trim free space relative to current alignment setting */
858 start = ALIGN(res->start, align);
859 end = ALIGN_DOWN(res->end + 1, align) - 1;
860 if (end < start)
861 continue;
862 if (end - start + 1 > max)
863 max = end - start + 1;
864 }
865 release_free_pmem(nvdimm_bus, nd_mapping);
866 return max;
867}
868
869/**
870 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
871 * @nd_mapping: container of dpa-resource-root + labels
872 * @nd_region: constrain available space check to this reference region
873 * @overlap: calculate available space assuming this level of overlap
874 *
875 * Validate that a PMEM label, if present, aligns with the start of an
876 * interleave set and truncate the available size at the lowest BLK
877 * overlap point.
878 *
879 * The expectation is that this routine is called multiple times as it
880 * probes for the largest BLK encroachment for any single member DIMM of
881 * the interleave set. Once that value is determined the PMEM-limit for
882 * the set can be established.
883 */
884resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
885 struct nd_mapping *nd_mapping, resource_size_t *overlap)
886{
887 resource_size_t map_start, map_end, busy = 0, available, blk_start;
888 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
889 struct resource *res;
890 const char *reason;
891 unsigned long align;
892
893 if (!ndd)
894 return 0;
895
896 align = dpa_align(nd_region);
897 if (!align)
898 return 0;
899
900 map_start = nd_mapping->start;
901 map_end = map_start + nd_mapping->size - 1;
902 blk_start = max(map_start, map_end + 1 - *overlap);
903 for_each_dpa_resource(ndd, res) {
904 resource_size_t start, end;
905
906 start = ALIGN_DOWN(res->start, align);
907 end = ALIGN(res->end + 1, align) - 1;
908 if (start >= map_start && start < map_end) {
909 if (strncmp(res->name, "blk", 3) == 0)
910 blk_start = min(blk_start,
911 max(map_start, start));
912 else if (end > map_end) {
913 reason = "misaligned to iset";
914 goto err;
915 } else
916 busy += end - start + 1;
917 } else if (end >= map_start && end <= map_end) {
918 if (strncmp(res->name, "blk", 3) == 0) {
919 /*
920 * If a BLK allocation overlaps the start of
921 * PMEM the entire interleave set may now only
922 * be used for BLK.
923 */
924 blk_start = map_start;
925 } else
926 busy += end - start + 1;
927 } else if (map_start > start && map_start < end) {
928 /* total eclipse of the mapping */
929 busy += nd_mapping->size;
930 blk_start = map_start;
931 }
932 }
933
934 *overlap = map_end + 1 - blk_start;
935 available = blk_start - map_start;
936 if (busy < available)
937 return ALIGN_DOWN(available - busy, align);
938 return 0;
939
940 err:
941 nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
942 return 0;
943}
944
945void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
946{
947 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
948 kfree(res->name);
949 __release_region(&ndd->dpa, res->start, resource_size(res));
950}
951
952struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
953 struct nd_label_id *label_id, resource_size_t start,
954 resource_size_t n)
955{
956 char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
957 struct resource *res;
958
959 if (!name)
960 return NULL;
961
962 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
963 res = __request_region(&ndd->dpa, start, n, name, 0);
964 if (!res)
965 kfree(name);
966 return res;
967}
968
969/**
970 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
971 * @nvdimm: container of dpa-resource-root + labels
972 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
973 */
974resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
975 struct nd_label_id *label_id)
976{
977 resource_size_t allocated = 0;
978 struct resource *res;
979
980 for_each_dpa_resource(ndd, res)
981 if (strcmp(res->name, label_id->id) == 0)
982 allocated += resource_size(res);
983
984 return allocated;
985}
986
987static int count_dimms(struct device *dev, void *c)
988{
989 int *count = c;
990
991 if (is_nvdimm(dev))
992 (*count)++;
993 return 0;
994}
995
996int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
997{
998 int count = 0;
999 /* Flush any possible dimm registration failures */
1000 nd_synchronize();
1001
1002 device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
1003 dev_dbg(&nvdimm_bus->dev, "count: %d\n", count);
1004 if (count != dimm_count)
1005 return -ENXIO;
1006 return 0;
1007}
1008EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
1009
1010void __exit nvdimm_devs_exit(void)
1011{
1012 ida_destroy(&dimm_ida);
1013}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 */
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6#include <linux/moduleparam.h>
7#include <linux/vmalloc.h>
8#include <linux/device.h>
9#include <linux/ndctl.h>
10#include <linux/slab.h>
11#include <linux/io.h>
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include "nd-core.h"
15#include "label.h"
16#include "pmem.h"
17#include "nd.h"
18
19static DEFINE_IDA(dimm_ida);
20
21/*
22 * Retrieve bus and dimm handle and return if this bus supports
23 * get_config_data commands
24 */
25int nvdimm_check_config_data(struct device *dev)
26{
27 struct nvdimm *nvdimm = to_nvdimm(dev);
28
29 if (!nvdimm->cmd_mask ||
30 !test_bit(ND_CMD_GET_CONFIG_DATA, &nvdimm->cmd_mask)) {
31 if (test_bit(NDD_LABELING, &nvdimm->flags))
32 return -ENXIO;
33 else
34 return -ENOTTY;
35 }
36
37 return 0;
38}
39
40static int validate_dimm(struct nvdimm_drvdata *ndd)
41{
42 int rc;
43
44 if (!ndd)
45 return -EINVAL;
46
47 rc = nvdimm_check_config_data(ndd->dev);
48 if (rc)
49 dev_dbg(ndd->dev, "%ps: %s error: %d\n",
50 __builtin_return_address(0), __func__, rc);
51 return rc;
52}
53
54/**
55 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
56 * @nvdimm: dimm to initialize
57 */
58int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
59{
60 struct nd_cmd_get_config_size *cmd = &ndd->nsarea;
61 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
62 struct nvdimm_bus_descriptor *nd_desc;
63 int rc = validate_dimm(ndd);
64 int cmd_rc = 0;
65
66 if (rc)
67 return rc;
68
69 if (cmd->config_size)
70 return 0; /* already valid */
71
72 memset(cmd, 0, sizeof(*cmd));
73 nd_desc = nvdimm_bus->nd_desc;
74 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
75 ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd), &cmd_rc);
76 if (rc < 0)
77 return rc;
78 return cmd_rc;
79}
80
81int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
82 size_t offset, size_t len)
83{
84 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
85 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
86 int rc = validate_dimm(ndd), cmd_rc = 0;
87 struct nd_cmd_get_config_data_hdr *cmd;
88 size_t max_cmd_size, buf_offset;
89
90 if (rc)
91 return rc;
92
93 if (offset + len > ndd->nsarea.config_size)
94 return -ENXIO;
95
96 max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
97 cmd = kvzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
98 if (!cmd)
99 return -ENOMEM;
100
101 for (buf_offset = 0; len;
102 len -= cmd->in_length, buf_offset += cmd->in_length) {
103 size_t cmd_size;
104
105 cmd->in_offset = offset + buf_offset;
106 cmd->in_length = min(max_cmd_size, len);
107
108 cmd_size = sizeof(*cmd) + cmd->in_length;
109
110 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
111 ND_CMD_GET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
112 if (rc < 0)
113 break;
114 if (cmd_rc < 0) {
115 rc = cmd_rc;
116 break;
117 }
118
119 /* out_buf should be valid, copy it into our output buffer */
120 memcpy(buf + buf_offset, cmd->out_buf, cmd->in_length);
121 }
122 kvfree(cmd);
123
124 return rc;
125}
126
127int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
128 void *buf, size_t len)
129{
130 size_t max_cmd_size, buf_offset;
131 struct nd_cmd_set_config_hdr *cmd;
132 int rc = validate_dimm(ndd), cmd_rc = 0;
133 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
134 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
135
136 if (rc)
137 return rc;
138
139 if (offset + len > ndd->nsarea.config_size)
140 return -ENXIO;
141
142 max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
143 cmd = kvzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL);
144 if (!cmd)
145 return -ENOMEM;
146
147 for (buf_offset = 0; len; len -= cmd->in_length,
148 buf_offset += cmd->in_length) {
149 size_t cmd_size;
150
151 cmd->in_offset = offset + buf_offset;
152 cmd->in_length = min(max_cmd_size, len);
153 memcpy(cmd->in_buf, buf + buf_offset, cmd->in_length);
154
155 /* status is output in the last 4-bytes of the command buffer */
156 cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32);
157
158 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
159 ND_CMD_SET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
160 if (rc < 0)
161 break;
162 if (cmd_rc < 0) {
163 rc = cmd_rc;
164 break;
165 }
166 }
167 kvfree(cmd);
168
169 return rc;
170}
171
172void nvdimm_set_labeling(struct device *dev)
173{
174 struct nvdimm *nvdimm = to_nvdimm(dev);
175
176 set_bit(NDD_LABELING, &nvdimm->flags);
177}
178
179void nvdimm_set_locked(struct device *dev)
180{
181 struct nvdimm *nvdimm = to_nvdimm(dev);
182
183 set_bit(NDD_LOCKED, &nvdimm->flags);
184}
185
186void nvdimm_clear_locked(struct device *dev)
187{
188 struct nvdimm *nvdimm = to_nvdimm(dev);
189
190 clear_bit(NDD_LOCKED, &nvdimm->flags);
191}
192
193static void nvdimm_release(struct device *dev)
194{
195 struct nvdimm *nvdimm = to_nvdimm(dev);
196
197 ida_simple_remove(&dimm_ida, nvdimm->id);
198 kfree(nvdimm);
199}
200
201struct nvdimm *to_nvdimm(struct device *dev)
202{
203 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
204
205 WARN_ON(!is_nvdimm(dev));
206 return nvdimm;
207}
208EXPORT_SYMBOL_GPL(to_nvdimm);
209
210struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
211{
212 struct nvdimm *nvdimm = nd_mapping->nvdimm;
213
214 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
215
216 return dev_get_drvdata(&nvdimm->dev);
217}
218EXPORT_SYMBOL(to_ndd);
219
220void nvdimm_drvdata_release(struct kref *kref)
221{
222 struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
223 struct device *dev = ndd->dev;
224 struct resource *res, *_r;
225
226 dev_dbg(dev, "trace\n");
227 nvdimm_bus_lock(dev);
228 for_each_dpa_resource_safe(ndd, res, _r)
229 nvdimm_free_dpa(ndd, res);
230 nvdimm_bus_unlock(dev);
231
232 kvfree(ndd->data);
233 kfree(ndd);
234 put_device(dev);
235}
236
237void get_ndd(struct nvdimm_drvdata *ndd)
238{
239 kref_get(&ndd->kref);
240}
241
242void put_ndd(struct nvdimm_drvdata *ndd)
243{
244 if (ndd)
245 kref_put(&ndd->kref, nvdimm_drvdata_release);
246}
247
248const char *nvdimm_name(struct nvdimm *nvdimm)
249{
250 return dev_name(&nvdimm->dev);
251}
252EXPORT_SYMBOL_GPL(nvdimm_name);
253
254struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
255{
256 return &nvdimm->dev.kobj;
257}
258EXPORT_SYMBOL_GPL(nvdimm_kobj);
259
260unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
261{
262 return nvdimm->cmd_mask;
263}
264EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
265
266void *nvdimm_provider_data(struct nvdimm *nvdimm)
267{
268 if (nvdimm)
269 return nvdimm->provider_data;
270 return NULL;
271}
272EXPORT_SYMBOL_GPL(nvdimm_provider_data);
273
274static ssize_t commands_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
276{
277 struct nvdimm *nvdimm = to_nvdimm(dev);
278 int cmd, len = 0;
279
280 if (!nvdimm->cmd_mask)
281 return sprintf(buf, "\n");
282
283 for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
284 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
285 len += sprintf(buf + len, "\n");
286 return len;
287}
288static DEVICE_ATTR_RO(commands);
289
290static ssize_t flags_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293 struct nvdimm *nvdimm = to_nvdimm(dev);
294
295 return sprintf(buf, "%s%s\n",
296 test_bit(NDD_LABELING, &nvdimm->flags) ? "label " : "",
297 test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : "");
298}
299static DEVICE_ATTR_RO(flags);
300
301static ssize_t state_show(struct device *dev, struct device_attribute *attr,
302 char *buf)
303{
304 struct nvdimm *nvdimm = to_nvdimm(dev);
305
306 /*
307 * The state may be in the process of changing, userspace should
308 * quiesce probing if it wants a static answer
309 */
310 nvdimm_bus_lock(dev);
311 nvdimm_bus_unlock(dev);
312 return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
313 ? "active" : "idle");
314}
315static DEVICE_ATTR_RO(state);
316
317static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf)
318{
319 struct device *dev;
320 ssize_t rc;
321 u32 nfree;
322
323 if (!ndd)
324 return -ENXIO;
325
326 dev = ndd->dev;
327 nvdimm_bus_lock(dev);
328 nfree = nd_label_nfree(ndd);
329 if (nfree - 1 > nfree) {
330 dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
331 nfree = 0;
332 } else
333 nfree--;
334 rc = sprintf(buf, "%d\n", nfree);
335 nvdimm_bus_unlock(dev);
336 return rc;
337}
338
339static ssize_t available_slots_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
341{
342 ssize_t rc;
343
344 device_lock(dev);
345 rc = __available_slots_show(dev_get_drvdata(dev), buf);
346 device_unlock(dev);
347
348 return rc;
349}
350static DEVICE_ATTR_RO(available_slots);
351
352ssize_t security_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
354{
355 struct nvdimm *nvdimm = to_nvdimm(dev);
356
357 /*
358 * For the test version we need to poll the "hardware" in order
359 * to get the updated status for unlock testing.
360 */
361 if (IS_ENABLED(CONFIG_NVDIMM_SECURITY_TEST))
362 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
363
364 if (test_bit(NVDIMM_SECURITY_OVERWRITE, &nvdimm->sec.flags))
365 return sprintf(buf, "overwrite\n");
366 if (test_bit(NVDIMM_SECURITY_DISABLED, &nvdimm->sec.flags))
367 return sprintf(buf, "disabled\n");
368 if (test_bit(NVDIMM_SECURITY_UNLOCKED, &nvdimm->sec.flags))
369 return sprintf(buf, "unlocked\n");
370 if (test_bit(NVDIMM_SECURITY_LOCKED, &nvdimm->sec.flags))
371 return sprintf(buf, "locked\n");
372 return -ENOTTY;
373}
374
375static ssize_t frozen_show(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
378 struct nvdimm *nvdimm = to_nvdimm(dev);
379
380 return sprintf(buf, "%d\n", test_bit(NVDIMM_SECURITY_FROZEN,
381 &nvdimm->sec.flags));
382}
383static DEVICE_ATTR_RO(frozen);
384
385static ssize_t security_store(struct device *dev,
386 struct device_attribute *attr, const char *buf, size_t len)
387
388{
389 ssize_t rc;
390
391 /*
392 * Require all userspace triggered security management to be
393 * done while probing is idle and the DIMM is not in active use
394 * in any region.
395 */
396 device_lock(dev);
397 nvdimm_bus_lock(dev);
398 wait_nvdimm_bus_probe_idle(dev);
399 rc = nvdimm_security_store(dev, buf, len);
400 nvdimm_bus_unlock(dev);
401 device_unlock(dev);
402
403 return rc;
404}
405static DEVICE_ATTR_RW(security);
406
407static struct attribute *nvdimm_attributes[] = {
408 &dev_attr_state.attr,
409 &dev_attr_flags.attr,
410 &dev_attr_commands.attr,
411 &dev_attr_available_slots.attr,
412 &dev_attr_security.attr,
413 &dev_attr_frozen.attr,
414 NULL,
415};
416
417static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n)
418{
419 struct device *dev = container_of(kobj, typeof(*dev), kobj);
420 struct nvdimm *nvdimm = to_nvdimm(dev);
421
422 if (a != &dev_attr_security.attr && a != &dev_attr_frozen.attr)
423 return a->mode;
424 if (!nvdimm->sec.flags)
425 return 0;
426
427 if (a == &dev_attr_security.attr) {
428 /* Are there any state mutation ops (make writable)? */
429 if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable
430 || nvdimm->sec.ops->change_key
431 || nvdimm->sec.ops->erase
432 || nvdimm->sec.ops->overwrite)
433 return a->mode;
434 return 0444;
435 }
436
437 if (nvdimm->sec.ops->freeze)
438 return a->mode;
439 return 0;
440}
441
442static const struct attribute_group nvdimm_attribute_group = {
443 .attrs = nvdimm_attributes,
444 .is_visible = nvdimm_visible,
445};
446
447static ssize_t result_show(struct device *dev, struct device_attribute *attr, char *buf)
448{
449 struct nvdimm *nvdimm = to_nvdimm(dev);
450 enum nvdimm_fwa_result result;
451
452 if (!nvdimm->fw_ops)
453 return -EOPNOTSUPP;
454
455 nvdimm_bus_lock(dev);
456 result = nvdimm->fw_ops->activate_result(nvdimm);
457 nvdimm_bus_unlock(dev);
458
459 switch (result) {
460 case NVDIMM_FWA_RESULT_NONE:
461 return sprintf(buf, "none\n");
462 case NVDIMM_FWA_RESULT_SUCCESS:
463 return sprintf(buf, "success\n");
464 case NVDIMM_FWA_RESULT_FAIL:
465 return sprintf(buf, "fail\n");
466 case NVDIMM_FWA_RESULT_NOTSTAGED:
467 return sprintf(buf, "not_staged\n");
468 case NVDIMM_FWA_RESULT_NEEDRESET:
469 return sprintf(buf, "need_reset\n");
470 default:
471 return -ENXIO;
472 }
473}
474static DEVICE_ATTR_ADMIN_RO(result);
475
476static ssize_t activate_show(struct device *dev, struct device_attribute *attr, char *buf)
477{
478 struct nvdimm *nvdimm = to_nvdimm(dev);
479 enum nvdimm_fwa_state state;
480
481 if (!nvdimm->fw_ops)
482 return -EOPNOTSUPP;
483
484 nvdimm_bus_lock(dev);
485 state = nvdimm->fw_ops->activate_state(nvdimm);
486 nvdimm_bus_unlock(dev);
487
488 switch (state) {
489 case NVDIMM_FWA_IDLE:
490 return sprintf(buf, "idle\n");
491 case NVDIMM_FWA_BUSY:
492 return sprintf(buf, "busy\n");
493 case NVDIMM_FWA_ARMED:
494 return sprintf(buf, "armed\n");
495 default:
496 return -ENXIO;
497 }
498}
499
500static ssize_t activate_store(struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t len)
502{
503 struct nvdimm *nvdimm = to_nvdimm(dev);
504 enum nvdimm_fwa_trigger arg;
505 int rc;
506
507 if (!nvdimm->fw_ops)
508 return -EOPNOTSUPP;
509
510 if (sysfs_streq(buf, "arm"))
511 arg = NVDIMM_FWA_ARM;
512 else if (sysfs_streq(buf, "disarm"))
513 arg = NVDIMM_FWA_DISARM;
514 else
515 return -EINVAL;
516
517 nvdimm_bus_lock(dev);
518 rc = nvdimm->fw_ops->arm(nvdimm, arg);
519 nvdimm_bus_unlock(dev);
520
521 if (rc < 0)
522 return rc;
523 return len;
524}
525static DEVICE_ATTR_ADMIN_RW(activate);
526
527static struct attribute *nvdimm_firmware_attributes[] = {
528 &dev_attr_activate.attr,
529 &dev_attr_result.attr,
530 NULL,
531};
532
533static umode_t nvdimm_firmware_visible(struct kobject *kobj, struct attribute *a, int n)
534{
535 struct device *dev = container_of(kobj, typeof(*dev), kobj);
536 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
537 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
538 struct nvdimm *nvdimm = to_nvdimm(dev);
539 enum nvdimm_fwa_capability cap;
540
541 if (!nd_desc->fw_ops)
542 return 0;
543 if (!nvdimm->fw_ops)
544 return 0;
545
546 nvdimm_bus_lock(dev);
547 cap = nd_desc->fw_ops->capability(nd_desc);
548 nvdimm_bus_unlock(dev);
549
550 if (cap < NVDIMM_FWA_CAP_QUIESCE)
551 return 0;
552
553 return a->mode;
554}
555
556static const struct attribute_group nvdimm_firmware_attribute_group = {
557 .name = "firmware",
558 .attrs = nvdimm_firmware_attributes,
559 .is_visible = nvdimm_firmware_visible,
560};
561
562static const struct attribute_group *nvdimm_attribute_groups[] = {
563 &nd_device_attribute_group,
564 &nvdimm_attribute_group,
565 &nvdimm_firmware_attribute_group,
566 NULL,
567};
568
569static const struct device_type nvdimm_device_type = {
570 .name = "nvdimm",
571 .release = nvdimm_release,
572 .groups = nvdimm_attribute_groups,
573};
574
575bool is_nvdimm(struct device *dev)
576{
577 return dev->type == &nvdimm_device_type;
578}
579
580static struct lock_class_key nvdimm_key;
581
582struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
583 void *provider_data, const struct attribute_group **groups,
584 unsigned long flags, unsigned long cmd_mask, int num_flush,
585 struct resource *flush_wpq, const char *dimm_id,
586 const struct nvdimm_security_ops *sec_ops,
587 const struct nvdimm_fw_ops *fw_ops)
588{
589 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
590 struct device *dev;
591
592 if (!nvdimm)
593 return NULL;
594
595 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
596 if (nvdimm->id < 0) {
597 kfree(nvdimm);
598 return NULL;
599 }
600
601 nvdimm->dimm_id = dimm_id;
602 nvdimm->provider_data = provider_data;
603 nvdimm->flags = flags;
604 nvdimm->cmd_mask = cmd_mask;
605 nvdimm->num_flush = num_flush;
606 nvdimm->flush_wpq = flush_wpq;
607 atomic_set(&nvdimm->busy, 0);
608 dev = &nvdimm->dev;
609 dev_set_name(dev, "nmem%d", nvdimm->id);
610 dev->parent = &nvdimm_bus->dev;
611 dev->type = &nvdimm_device_type;
612 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
613 dev->groups = groups;
614 nvdimm->sec.ops = sec_ops;
615 nvdimm->fw_ops = fw_ops;
616 nvdimm->sec.overwrite_tmo = 0;
617 INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query);
618 /*
619 * Security state must be initialized before device_add() for
620 * attribute visibility.
621 */
622 /* get security state and extended (master) state */
623 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
624 nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
625 device_initialize(dev);
626 lockdep_set_class(&dev->mutex, &nvdimm_key);
627 nd_device_register(dev);
628
629 return nvdimm;
630}
631EXPORT_SYMBOL_GPL(__nvdimm_create);
632
633void nvdimm_delete(struct nvdimm *nvdimm)
634{
635 struct device *dev = &nvdimm->dev;
636 bool dev_put = false;
637
638 /* We are shutting down. Make state frozen artificially. */
639 nvdimm_bus_lock(dev);
640 set_bit(NVDIMM_SECURITY_FROZEN, &nvdimm->sec.flags);
641 if (test_and_clear_bit(NDD_WORK_PENDING, &nvdimm->flags))
642 dev_put = true;
643 nvdimm_bus_unlock(dev);
644 cancel_delayed_work_sync(&nvdimm->dwork);
645 if (dev_put)
646 put_device(dev);
647 nd_device_unregister(dev, ND_SYNC);
648}
649EXPORT_SYMBOL_GPL(nvdimm_delete);
650
651static void shutdown_security_notify(void *data)
652{
653 struct nvdimm *nvdimm = data;
654
655 sysfs_put(nvdimm->sec.overwrite_state);
656}
657
658int nvdimm_security_setup_events(struct device *dev)
659{
660 struct nvdimm *nvdimm = to_nvdimm(dev);
661
662 if (!nvdimm->sec.flags || !nvdimm->sec.ops
663 || !nvdimm->sec.ops->overwrite)
664 return 0;
665 nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security");
666 if (!nvdimm->sec.overwrite_state)
667 return -ENOMEM;
668
669 return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm);
670}
671EXPORT_SYMBOL_GPL(nvdimm_security_setup_events);
672
673int nvdimm_in_overwrite(struct nvdimm *nvdimm)
674{
675 return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags);
676}
677EXPORT_SYMBOL_GPL(nvdimm_in_overwrite);
678
679int nvdimm_security_freeze(struct nvdimm *nvdimm)
680{
681 int rc;
682
683 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
684
685 if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze)
686 return -EOPNOTSUPP;
687
688 if (!nvdimm->sec.flags)
689 return -EIO;
690
691 if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
692 dev_warn(&nvdimm->dev, "Overwrite operation in progress.\n");
693 return -EBUSY;
694 }
695
696 rc = nvdimm->sec.ops->freeze(nvdimm);
697 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
698
699 return rc;
700}
701
702static unsigned long dpa_align(struct nd_region *nd_region)
703{
704 struct device *dev = &nd_region->dev;
705
706 if (dev_WARN_ONCE(dev, !is_nvdimm_bus_locked(dev),
707 "bus lock required for capacity provision\n"))
708 return 0;
709 if (dev_WARN_ONCE(dev, !nd_region->ndr_mappings || nd_region->align
710 % nd_region->ndr_mappings,
711 "invalid region align %#lx mappings: %d\n",
712 nd_region->align, nd_region->ndr_mappings))
713 return 0;
714 return nd_region->align / nd_region->ndr_mappings;
715}
716
717/**
718 * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
719 * contiguous unallocated dpa range.
720 * @nd_region: constrain available space check to this reference region
721 * @nd_mapping: container of dpa-resource-root + labels
722 */
723resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region,
724 struct nd_mapping *nd_mapping)
725{
726 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
727 struct nvdimm_bus *nvdimm_bus;
728 resource_size_t max = 0;
729 struct resource *res;
730 unsigned long align;
731
732 /* if a dimm is disabled the available capacity is zero */
733 if (!ndd)
734 return 0;
735
736 align = dpa_align(nd_region);
737 if (!align)
738 return 0;
739
740 nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
741 if (__reserve_free_pmem(&nd_region->dev, nd_mapping->nvdimm))
742 return 0;
743 for_each_dpa_resource(ndd, res) {
744 resource_size_t start, end;
745
746 if (strcmp(res->name, "pmem-reserve") != 0)
747 continue;
748 /* trim free space relative to current alignment setting */
749 start = ALIGN(res->start, align);
750 end = ALIGN_DOWN(res->end + 1, align) - 1;
751 if (end < start)
752 continue;
753 if (end - start + 1 > max)
754 max = end - start + 1;
755 }
756 release_free_pmem(nvdimm_bus, nd_mapping);
757 return max;
758}
759
760/**
761 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
762 * @nd_mapping: container of dpa-resource-root + labels
763 * @nd_region: constrain available space check to this reference region
764 *
765 * Validate that a PMEM label, if present, aligns with the start of an
766 * interleave set.
767 */
768resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
769 struct nd_mapping *nd_mapping)
770{
771 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
772 resource_size_t map_start, map_end, busy = 0;
773 struct resource *res;
774 unsigned long align;
775
776 if (!ndd)
777 return 0;
778
779 align = dpa_align(nd_region);
780 if (!align)
781 return 0;
782
783 map_start = nd_mapping->start;
784 map_end = map_start + nd_mapping->size - 1;
785 for_each_dpa_resource(ndd, res) {
786 resource_size_t start, end;
787
788 start = ALIGN_DOWN(res->start, align);
789 end = ALIGN(res->end + 1, align) - 1;
790 if (start >= map_start && start < map_end) {
791 if (end > map_end) {
792 nd_dbg_dpa(nd_region, ndd, res,
793 "misaligned to iset\n");
794 return 0;
795 }
796 busy += end - start + 1;
797 } else if (end >= map_start && end <= map_end) {
798 busy += end - start + 1;
799 } else if (map_start > start && map_start < end) {
800 /* total eclipse of the mapping */
801 busy += nd_mapping->size;
802 }
803 }
804
805 if (busy < nd_mapping->size)
806 return ALIGN_DOWN(nd_mapping->size - busy, align);
807 return 0;
808}
809
810void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
811{
812 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
813 kfree(res->name);
814 __release_region(&ndd->dpa, res->start, resource_size(res));
815}
816
817struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
818 struct nd_label_id *label_id, resource_size_t start,
819 resource_size_t n)
820{
821 char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
822 struct resource *res;
823
824 if (!name)
825 return NULL;
826
827 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
828 res = __request_region(&ndd->dpa, start, n, name, 0);
829 if (!res)
830 kfree(name);
831 return res;
832}
833
834/**
835 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
836 * @nvdimm: container of dpa-resource-root + labels
837 * @label_id: dpa resource name of the form pmem-<human readable uuid>
838 */
839resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
840 struct nd_label_id *label_id)
841{
842 resource_size_t allocated = 0;
843 struct resource *res;
844
845 for_each_dpa_resource(ndd, res)
846 if (strcmp(res->name, label_id->id) == 0)
847 allocated += resource_size(res);
848
849 return allocated;
850}
851
852static int count_dimms(struct device *dev, void *c)
853{
854 int *count = c;
855
856 if (is_nvdimm(dev))
857 (*count)++;
858 return 0;
859}
860
861int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
862{
863 int count = 0;
864 /* Flush any possible dimm registration failures */
865 nd_synchronize();
866
867 device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
868 dev_dbg(&nvdimm_bus->dev, "count: %d\n", count);
869 if (count != dimm_count)
870 return -ENXIO;
871 return 0;
872}
873EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
874
875void __exit nvdimm_devs_exit(void)
876{
877 ida_destroy(&dimm_ida);
878}