Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | */ |
| 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 15 | #include <linux/device.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 16 | #include <linux/ndctl.h> |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include "nd-core.h" |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 22 | #include "nd.h" |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 23 | |
| 24 | static DEFINE_IDA(dimm_ida); |
| 25 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 26 | /* |
| 27 | * Retrieve bus and dimm handle and return if this bus supports |
| 28 | * get_config_data commands |
| 29 | */ |
| 30 | static int __validate_dimm(struct nvdimm_drvdata *ndd) |
| 31 | { |
| 32 | struct nvdimm *nvdimm; |
| 33 | |
| 34 | if (!ndd) |
| 35 | return -EINVAL; |
| 36 | |
| 37 | nvdimm = to_nvdimm(ndd->dev); |
| 38 | |
| 39 | if (!nvdimm->dsm_mask) |
| 40 | return -ENXIO; |
| 41 | if (!test_bit(ND_CMD_GET_CONFIG_DATA, nvdimm->dsm_mask)) |
| 42 | return -ENXIO; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int validate_dimm(struct nvdimm_drvdata *ndd) |
| 48 | { |
| 49 | int rc = __validate_dimm(ndd); |
| 50 | |
| 51 | if (rc && ndd) |
| 52 | dev_dbg(ndd->dev, "%pf: %s error: %d\n", |
| 53 | __builtin_return_address(0), __func__, rc); |
| 54 | return rc; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area |
| 59 | * @nvdimm: dimm to initialize |
| 60 | */ |
| 61 | int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd) |
| 62 | { |
| 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); |
| 67 | |
| 68 | if (rc) |
| 69 | return rc; |
| 70 | |
| 71 | if (cmd->config_size) |
| 72 | return 0; /* already valid */ |
| 73 | |
| 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)); |
| 78 | } |
| 79 | |
| 80 | int nvdimm_init_config_data(struct nvdimm_drvdata *ndd) |
| 81 | { |
| 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; |
| 87 | size_t offset; |
| 88 | |
| 89 | if (rc) |
| 90 | return rc; |
| 91 | |
| 92 | if (ndd->data) |
| 93 | return 0; |
| 94 | |
| 95 | if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0) |
| 96 | return -ENXIO; |
| 97 | |
| 98 | ndd->data = kmalloc(ndd->nsarea.config_size, GFP_KERNEL); |
| 99 | if (!ndd->data) |
| 100 | ndd->data = vmalloc(ndd->nsarea.config_size); |
| 101 | |
| 102 | if (!ndd->data) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | max_cmd_size = min_t(u32, PAGE_SIZE, ndd->nsarea.max_xfer); |
| 106 | cmd = kzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL); |
| 107 | if (!cmd) |
| 108 | return -ENOMEM; |
| 109 | |
| 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) { |
| 120 | rc = -ENXIO; |
| 121 | break; |
| 122 | } |
| 123 | memcpy(ndd->data + offset, cmd->out_buf, cmd->in_length); |
| 124 | } |
| 125 | dev_dbg(ndd->dev, "%s: len: %zu rc: %d\n", __func__, offset, rc); |
| 126 | kfree(cmd); |
| 127 | |
| 128 | return rc; |
| 129 | } |
| 130 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 131 | static void nvdimm_release(struct device *dev) |
| 132 | { |
| 133 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 134 | |
| 135 | ida_simple_remove(&dimm_ida, nvdimm->id); |
| 136 | kfree(nvdimm); |
| 137 | } |
| 138 | |
| 139 | static struct device_type nvdimm_device_type = { |
| 140 | .name = "nvdimm", |
| 141 | .release = nvdimm_release, |
| 142 | }; |
| 143 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 144 | bool is_nvdimm(struct device *dev) |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 145 | { |
| 146 | return dev->type == &nvdimm_device_type; |
| 147 | } |
| 148 | |
| 149 | struct nvdimm *to_nvdimm(struct device *dev) |
| 150 | { |
| 151 | struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev); |
| 152 | |
| 153 | WARN_ON(!is_nvdimm(dev)); |
| 154 | return nvdimm; |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(to_nvdimm); |
| 157 | |
| 158 | const char *nvdimm_name(struct nvdimm *nvdimm) |
| 159 | { |
| 160 | return dev_name(&nvdimm->dev); |
| 161 | } |
| 162 | EXPORT_SYMBOL_GPL(nvdimm_name); |
| 163 | |
| 164 | void *nvdimm_provider_data(struct nvdimm *nvdimm) |
| 165 | { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 166 | if (nvdimm) |
| 167 | return nvdimm->provider_data; |
| 168 | return NULL; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 169 | } |
| 170 | EXPORT_SYMBOL_GPL(nvdimm_provider_data); |
| 171 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 172 | static ssize_t commands_show(struct device *dev, |
| 173 | struct device_attribute *attr, char *buf) |
| 174 | { |
| 175 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 176 | int cmd, len = 0; |
| 177 | |
| 178 | if (!nvdimm->dsm_mask) |
| 179 | return sprintf(buf, "\n"); |
| 180 | |
| 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"); |
| 184 | return len; |
| 185 | } |
| 186 | static DEVICE_ATTR_RO(commands); |
| 187 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame^] | 188 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 189 | char *buf) |
| 190 | { |
| 191 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 192 | |
| 193 | /* |
| 194 | * The state may be in the process of changing, userspace should |
| 195 | * quiesce probing if it wants a static answer |
| 196 | */ |
| 197 | nvdimm_bus_lock(dev); |
| 198 | nvdimm_bus_unlock(dev); |
| 199 | return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy) |
| 200 | ? "active" : "idle"); |
| 201 | } |
| 202 | static DEVICE_ATTR_RO(state); |
| 203 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 204 | static struct attribute *nvdimm_attributes[] = { |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame^] | 205 | &dev_attr_state.attr, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 206 | &dev_attr_commands.attr, |
| 207 | NULL, |
| 208 | }; |
| 209 | |
| 210 | struct attribute_group nvdimm_attribute_group = { |
| 211 | .attrs = nvdimm_attributes, |
| 212 | }; |
| 213 | EXPORT_SYMBOL_GPL(nvdimm_attribute_group); |
| 214 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 215 | struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 216 | const struct attribute_group **groups, unsigned long flags, |
| 217 | unsigned long *dsm_mask) |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 218 | { |
| 219 | struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL); |
| 220 | struct device *dev; |
| 221 | |
| 222 | if (!nvdimm) |
| 223 | return NULL; |
| 224 | |
| 225 | nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL); |
| 226 | if (nvdimm->id < 0) { |
| 227 | kfree(nvdimm); |
| 228 | return NULL; |
| 229 | } |
| 230 | nvdimm->provider_data = provider_data; |
| 231 | nvdimm->flags = flags; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 232 | nvdimm->dsm_mask = dsm_mask; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame^] | 233 | atomic_set(&nvdimm->busy, 0); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 234 | dev = &nvdimm->dev; |
| 235 | dev_set_name(dev, "nmem%d", nvdimm->id); |
| 236 | dev->parent = &nvdimm_bus->dev; |
| 237 | dev->type = &nvdimm_device_type; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 238 | dev->devt = MKDEV(nvdimm_major, nvdimm->id); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 239 | dev->groups = groups; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 240 | nd_device_register(dev); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 241 | |
| 242 | return nvdimm; |
| 243 | } |
| 244 | EXPORT_SYMBOL_GPL(nvdimm_create); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 245 | |
| 246 | static int count_dimms(struct device *dev, void *c) |
| 247 | { |
| 248 | int *count = c; |
| 249 | |
| 250 | if (is_nvdimm(dev)) |
| 251 | (*count)++; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count) |
| 256 | { |
| 257 | int count = 0; |
| 258 | /* Flush any possible dimm registration failures */ |
| 259 | nd_synchronize(); |
| 260 | |
| 261 | device_for_each_child(&nvdimm_bus->dev, &count, count_dimms); |
| 262 | dev_dbg(&nvdimm_bus->dev, "%s: count: %d\n", __func__, count); |
| 263 | if (count != dimm_count) |
| 264 | return -ENXIO; |
| 265 | return 0; |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count); |