1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/vmalloc.h>
6 #include <linux/module.h>
7 #include <linux/device.h>
8 #include <linux/sizes.h>
9 #include <linux/ndctl.h>
10 #include <linux/slab.h>
16 static int nvdimm_probe(struct device *dev)
18 struct nvdimm_drvdata *ndd;
21 rc = nvdimm_security_setup_events(dev);
23 dev_err(dev, "security event setup failed: %d\n", rc);
27 rc = nvdimm_check_config_data(dev);
29 /* not required for non-aliased nvdimm, ex. NVDIMM-N */
36 * The locked status bit reflects explicit status codes from the
37 * label reading commands, revalidate it each time the driver is
38 * activated and re-reads the label area.
40 nvdimm_clear_locked(dev);
42 ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
46 dev_set_drvdata(dev, ndd);
47 ndd->dpa.name = dev_name(dev);
54 kref_init(&ndd->kref);
57 * Attempt to unlock, if the DIMM supports security commands,
58 * otherwise the locked indication is determined by explicit
59 * status codes from the label reading commands.
61 rc = nvdimm_security_unlock(dev);
63 dev_dbg(dev, "failed to unlock dimm: %d\n", rc);
67 * EACCES failures reading the namespace label-area-properties
68 * are interpreted as the DIMM capacity being locked but the
69 * namespace labels themselves being accessible.
71 rc = nvdimm_init_nsarea(ndd);
74 * See nvdimm_namespace_common_probe() where we fail to
75 * allow namespaces to probe while the DIMM is locked,
76 * but we do allow for namespace enumeration.
78 nvdimm_set_locked(dev);
85 * EACCES failures reading the namespace label-data are
86 * interpreted as the label area being locked in addition to the
87 * DIMM capacity. We fail the dimm probe to prevent regions from
88 * attempting to parse the label area.
90 rc = nd_label_data_init(ndd);
92 nvdimm_set_locked(dev);
96 dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
99 if (ndd->ns_current >= 0) {
100 rc = nd_label_reserve_dpa(ndd);
102 nvdimm_set_labeling(dev);
104 nvdimm_bus_unlock(dev);
116 static void nvdimm_remove(struct device *dev)
118 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
120 nvdimm_bus_lock(dev);
121 dev_set_drvdata(dev, NULL);
122 nvdimm_bus_unlock(dev);
126 static struct nd_device_driver nvdimm_driver = {
127 .probe = nvdimm_probe,
128 .remove = nvdimm_remove,
132 .type = ND_DRIVER_DIMM,
135 int __init nvdimm_init(void)
137 return nd_driver_register(&nvdimm_driver);
140 void nvdimm_exit(void)
142 driver_unregister(&nvdimm_driver.drv);
145 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);