blob: 91d9163ee3038ba182248a41c41236bfa56e4ff6 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams4d88a972015-05-31 14:41:48 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williams4d88a972015-05-31 14:41:48 -04004 */
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>
11#include <linux/mm.h>
12#include <linux/nd.h>
Dan Williams4a826c82015-06-09 16:09:36 -040013#include "label.h"
Dan Williams4d88a972015-05-31 14:41:48 -040014#include "nd.h"
15
Dan Williams4d88a972015-05-31 14:41:48 -040016static int nvdimm_probe(struct device *dev)
17{
18 struct nvdimm_drvdata *ndd;
19 int rc;
20
Dan Williams1cd73862019-01-19 08:45:56 -080021 rc = nvdimm_security_setup_events(dev);
22 if (rc < 0) {
23 dev_err(dev, "security event setup failed: %d\n", rc);
24 return rc;
25 }
26
Toshi Kaniaee65982016-08-16 13:08:40 -060027 rc = nvdimm_check_config_data(dev);
28 if (rc) {
29 /* not required for non-aliased nvdimm, ex. NVDIMM-N */
30 if (rc == -ENOTTY)
31 rc = 0;
32 return rc;
33 }
34
Dave Jiang4c6926a2018-12-06 12:40:01 -080035 /*
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.
39 */
Dan Williams08e6b3c2018-06-13 09:08:36 -070040 nvdimm_clear_locked(dev);
41
Dan Williams4d88a972015-05-31 14:41:48 -040042 ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
43 if (!ndd)
44 return -ENOMEM;
45
46 dev_set_drvdata(dev, ndd);
Dan Williams4a826c82015-06-09 16:09:36 -040047 ndd->dpa.name = dev_name(dev);
48 ndd->ns_current = -1;
49 ndd->ns_next = -1;
50 ndd->dpa.start = 0;
51 ndd->dpa.end = -1;
Dan Williams4d88a972015-05-31 14:41:48 -040052 ndd->dev = dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -040053 get_device(dev);
54 kref_init(&ndd->kref);
Dan Williams4d88a972015-05-31 14:41:48 -040055
Dan Williams08e6b3c2018-06-13 09:08:36 -070056 /*
Dave Jiang4c6926a2018-12-06 12:40:01 -080057 * 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.
60 */
61 rc = nvdimm_security_unlock(dev);
62 if (rc < 0)
Dan Williams37379cf2018-12-22 11:35:41 -080063 dev_dbg(dev, "failed to unlock dimm: %d\n", rc);
Dave Jiang4c6926a2018-12-06 12:40:01 -080064
65
66 /*
Dan Williams08e6b3c2018-06-13 09:08:36 -070067 * 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.
70 */
Dan Williams4d88a972015-05-31 14:41:48 -040071 rc = nvdimm_init_nsarea(ndd);
Dan Williams08e6b3c2018-06-13 09:08:36 -070072 if (rc == -EACCES) {
73 /*
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.
77 */
Dan Williams9d62ed92017-05-04 11:47:22 -070078 nvdimm_set_locked(dev);
Dan Williams08e6b3c2018-06-13 09:08:36 -070079 rc = 0;
80 }
Dan Williams4d88a972015-05-31 14:41:48 -040081 if (rc)
82 goto err;
83
Dan Williams08e6b3c2018-06-13 09:08:36 -070084 /*
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.
89 */
Alexander Duyck2d657d12018-10-10 16:39:20 -070090 rc = nd_label_data_init(ndd);
Dan Williams4b27db72017-09-24 09:57:34 -070091 if (rc == -EACCES)
92 nvdimm_set_locked(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040093 if (rc)
94 goto err;
95
96 dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
97
Dan Williams4a826c82015-06-09 16:09:36 -040098 nvdimm_bus_lock(dev);
Dan Williamsc31898c2018-04-06 11:25:38 -070099 if (ndd->ns_current >= 0) {
100 rc = nd_label_reserve_dpa(ndd);
101 if (rc == 0)
Dan Williamsa0e37452020-01-30 12:06:18 -0800102 nvdimm_set_labeling(dev);
Dan Williamsc31898c2018-04-06 11:25:38 -0700103 }
Dan Williams4a826c82015-06-09 16:09:36 -0400104 nvdimm_bus_unlock(dev);
105
106 if (rc)
107 goto err;
108
Dan Williams4d88a972015-05-31 14:41:48 -0400109 return 0;
110
111 err:
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400112 put_ndd(ndd);
Dan Williams4d88a972015-05-31 14:41:48 -0400113 return rc;
114}
115
Uwe Kleine-König1f975072021-02-12 18:10:43 +0100116static void nvdimm_remove(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -0400117{
118 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
119
Dan Williams4a826c82015-06-09 16:09:36 -0400120 nvdimm_bus_lock(dev);
121 dev_set_drvdata(dev, NULL);
Dan Williams4a826c82015-06-09 16:09:36 -0400122 nvdimm_bus_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400123 put_ndd(ndd);
Dan Williams4d88a972015-05-31 14:41:48 -0400124}
125
126static struct nd_device_driver nvdimm_driver = {
127 .probe = nvdimm_probe,
128 .remove = nvdimm_remove,
129 .drv = {
130 .name = "nvdimm",
131 },
132 .type = ND_DRIVER_DIMM,
133};
134
135int __init nvdimm_init(void)
136{
137 return nd_driver_register(&nvdimm_driver);
138}
139
Dan Williams3d880022015-05-31 15:02:11 -0400140void nvdimm_exit(void)
Dan Williams4d88a972015-05-31 14:41:48 -0400141{
142 driver_unregister(&nvdimm_driver.drv);
143}
144
145MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);