2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/vmalloc.h>
15 #include <linux/device.h>
16 #include <linux/ndctl.h>
17 #include <linux/slab.h>
24 static DEFINE_IDA(dimm_ida
);
27 * Retrieve bus and dimm handle and return if this bus supports
28 * get_config_data commands
30 static int __validate_dimm(struct nvdimm_drvdata
*ndd
)
32 struct nvdimm
*nvdimm
;
37 nvdimm
= to_nvdimm(ndd
->dev
);
39 if (!nvdimm
->dsm_mask
)
41 if (!test_bit(ND_CMD_GET_CONFIG_DATA
, nvdimm
->dsm_mask
))
47 static int validate_dimm(struct nvdimm_drvdata
*ndd
)
49 int rc
= __validate_dimm(ndd
);
52 dev_dbg(ndd
->dev
, "%pf: %s error: %d\n",
53 __builtin_return_address(0), __func__
, rc
);
58 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
59 * @nvdimm: dimm to initialize
61 int nvdimm_init_nsarea(struct nvdimm_drvdata
*ndd
)
63 struct nd_cmd_get_config_size
*cmd
= &ndd
->nsarea
;
64 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
65 struct nvdimm_bus_descriptor
*nd_desc
;
66 int rc
= validate_dimm(ndd
);
72 return 0; /* already valid */
74 memset(cmd
, 0, sizeof(*cmd
));
75 nd_desc
= nvdimm_bus
->nd_desc
;
76 return nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
77 ND_CMD_GET_CONFIG_SIZE
, cmd
, sizeof(*cmd
));
80 int nvdimm_init_config_data(struct nvdimm_drvdata
*ndd
)
82 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
83 struct nd_cmd_get_config_data_hdr
*cmd
;
84 struct nvdimm_bus_descriptor
*nd_desc
;
85 int rc
= validate_dimm(ndd
);
86 u32 max_cmd_size
, config_size
;
95 if (ndd
->nsarea
.status
|| ndd
->nsarea
.max_xfer
== 0)
98 ndd
->data
= kmalloc(ndd
->nsarea
.config_size
, GFP_KERNEL
);
100 ndd
->data
= vmalloc(ndd
->nsarea
.config_size
);
105 max_cmd_size
= min_t(u32
, PAGE_SIZE
, ndd
->nsarea
.max_xfer
);
106 cmd
= kzalloc(max_cmd_size
+ sizeof(*cmd
), GFP_KERNEL
);
110 nd_desc
= nvdimm_bus
->nd_desc
;
111 for (config_size
= ndd
->nsarea
.config_size
, offset
= 0;
112 config_size
; config_size
-= cmd
->in_length
,
113 offset
+= cmd
->in_length
) {
114 cmd
->in_length
= min(config_size
, max_cmd_size
);
115 cmd
->in_offset
= offset
;
116 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
117 ND_CMD_GET_CONFIG_DATA
, cmd
,
118 cmd
->in_length
+ sizeof(*cmd
));
119 if (rc
|| cmd
->status
) {
123 memcpy(ndd
->data
+ offset
, cmd
->out_buf
, cmd
->in_length
);
125 dev_dbg(ndd
->dev
, "%s: len: %zu rc: %d\n", __func__
, offset
, rc
);
131 static void nvdimm_release(struct device
*dev
)
133 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
135 ida_simple_remove(&dimm_ida
, nvdimm
->id
);
139 static struct device_type nvdimm_device_type
= {
141 .release
= nvdimm_release
,
144 bool is_nvdimm(struct device
*dev
)
146 return dev
->type
== &nvdimm_device_type
;
149 struct nvdimm
*to_nvdimm(struct device
*dev
)
151 struct nvdimm
*nvdimm
= container_of(dev
, struct nvdimm
, dev
);
153 WARN_ON(!is_nvdimm(dev
));
156 EXPORT_SYMBOL_GPL(to_nvdimm
);
158 const char *nvdimm_name(struct nvdimm
*nvdimm
)
160 return dev_name(&nvdimm
->dev
);
162 EXPORT_SYMBOL_GPL(nvdimm_name
);
164 void *nvdimm_provider_data(struct nvdimm
*nvdimm
)
167 return nvdimm
->provider_data
;
170 EXPORT_SYMBOL_GPL(nvdimm_provider_data
);
172 static ssize_t
commands_show(struct device
*dev
,
173 struct device_attribute
*attr
, char *buf
)
175 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
178 if (!nvdimm
->dsm_mask
)
179 return sprintf(buf
, "\n");
181 for_each_set_bit(cmd
, nvdimm
->dsm_mask
, BITS_PER_LONG
)
182 len
+= sprintf(buf
+ len
, "%s ", nvdimm_cmd_name(cmd
));
183 len
+= sprintf(buf
+ len
, "\n");
186 static DEVICE_ATTR_RO(commands
);
188 static struct attribute
*nvdimm_attributes
[] = {
189 &dev_attr_commands
.attr
,
193 struct attribute_group nvdimm_attribute_group
= {
194 .attrs
= nvdimm_attributes
,
196 EXPORT_SYMBOL_GPL(nvdimm_attribute_group
);
198 struct nvdimm
*nvdimm_create(struct nvdimm_bus
*nvdimm_bus
, void *provider_data
,
199 const struct attribute_group
**groups
, unsigned long flags
,
200 unsigned long *dsm_mask
)
202 struct nvdimm
*nvdimm
= kzalloc(sizeof(*nvdimm
), GFP_KERNEL
);
208 nvdimm
->id
= ida_simple_get(&dimm_ida
, 0, 0, GFP_KERNEL
);
209 if (nvdimm
->id
< 0) {
213 nvdimm
->provider_data
= provider_data
;
214 nvdimm
->flags
= flags
;
215 nvdimm
->dsm_mask
= dsm_mask
;
218 dev_set_name(dev
, "nmem%d", nvdimm
->id
);
219 dev
->parent
= &nvdimm_bus
->dev
;
220 dev
->type
= &nvdimm_device_type
;
221 dev
->devt
= MKDEV(nvdimm_major
, nvdimm
->id
);
222 dev
->groups
= groups
;
223 nd_device_register(dev
);
227 EXPORT_SYMBOL_GPL(nvdimm_create
);
229 static int count_dimms(struct device
*dev
, void *c
)
238 int nvdimm_bus_check_dimm_count(struct nvdimm_bus
*nvdimm_bus
, int dimm_count
)
241 /* Flush any possible dimm registration failures */
244 device_for_each_child(&nvdimm_bus
->dev
, &count
, count_dimms
);
245 dev_dbg(&nvdimm_bus
->dev
, "%s: count: %d\n", __func__
, count
);
246 if (count
!= dimm_count
)
250 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count
);