Dan Williams | b94d523 | 2015-05-19 22:54:31 -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 | #include <linux/list_sort.h> |
| 14 | #include <linux/libnvdimm.h> |
| 15 | #include <linux/module.h> |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 17 | #include <linux/ndctl.h> |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 18 | #include <linux/sysfs.h> |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 19 | #include <linux/delay.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 20 | #include <linux/list.h> |
| 21 | #include <linux/acpi.h> |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 22 | #include <linux/sort.h> |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 23 | #include <linux/pmem.h> |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 24 | #include <linux/io.h> |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 25 | #include <linux/nd.h> |
Dan Williams | 96601ad | 2015-08-24 18:29:38 -0400 | [diff] [blame] | 26 | #include <asm/cacheflush.h> |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 27 | #include "nfit.h" |
| 28 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 29 | /* |
| 30 | * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is |
| 31 | * irrelevant. |
| 32 | */ |
Christoph Hellwig | 2f8e2c8 | 2015-08-28 09:27:14 +0200 | [diff] [blame] | 33 | #include <linux/io-64-nonatomic-hi-lo.h> |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 34 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 35 | static bool force_enable_dimms; |
| 36 | module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR); |
| 37 | MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status"); |
| 38 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 39 | static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT; |
| 40 | module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR); |
| 41 | MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds"); |
| 42 | |
| 43 | /* after three payloads of overflow, it's dead jim */ |
| 44 | static unsigned int scrub_overflow_abort = 3; |
| 45 | module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR); |
| 46 | MODULE_PARM_DESC(scrub_overflow_abort, |
| 47 | "Number of times we overflow ARS results before abort"); |
| 48 | |
Dan Williams | 8755409 | 2016-04-28 18:01:20 -0700 | [diff] [blame] | 49 | static bool disable_vendor_specific; |
| 50 | module_param(disable_vendor_specific, bool, S_IRUGO); |
| 51 | MODULE_PARM_DESC(disable_vendor_specific, |
| 52 | "Limit commands to the publicly specified set\n"); |
| 53 | |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 54 | LIST_HEAD(acpi_descs); |
| 55 | DEFINE_MUTEX(acpi_desc_lock); |
| 56 | |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 57 | static struct workqueue_struct *nfit_wq; |
| 58 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 59 | struct nfit_table_prev { |
| 60 | struct list_head spas; |
| 61 | struct list_head memdevs; |
| 62 | struct list_head dcrs; |
| 63 | struct list_head bdws; |
| 64 | struct list_head idts; |
| 65 | struct list_head flushes; |
| 66 | }; |
| 67 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 68 | static u8 nfit_uuid[NFIT_UUID_MAX][16]; |
| 69 | |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 70 | const u8 *to_nfit_uuid(enum nfit_uuids id) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 71 | { |
| 72 | return nfit_uuid[id]; |
| 73 | } |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 74 | EXPORT_SYMBOL(to_nfit_uuid); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 75 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 76 | static struct acpi_nfit_desc *to_acpi_nfit_desc( |
| 77 | struct nvdimm_bus_descriptor *nd_desc) |
| 78 | { |
| 79 | return container_of(nd_desc, struct acpi_nfit_desc, nd_desc); |
| 80 | } |
| 81 | |
| 82 | static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc) |
| 83 | { |
| 84 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; |
| 85 | |
| 86 | /* |
| 87 | * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct |
| 88 | * acpi_device. |
| 89 | */ |
| 90 | if (!nd_desc->provider_name |
| 91 | || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0) |
| 92 | return NULL; |
| 93 | |
| 94 | return to_acpi_device(acpi_desc->dev); |
| 95 | } |
| 96 | |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 97 | static int xlat_status(void *buf, unsigned int cmd) |
| 98 | { |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 99 | struct nd_cmd_clear_error *clear_err; |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 100 | struct nd_cmd_ars_status *ars_status; |
| 101 | struct nd_cmd_ars_start *ars_start; |
| 102 | struct nd_cmd_ars_cap *ars_cap; |
| 103 | u16 flags; |
| 104 | |
| 105 | switch (cmd) { |
| 106 | case ND_CMD_ARS_CAP: |
| 107 | ars_cap = buf; |
| 108 | if ((ars_cap->status & 0xffff) == NFIT_ARS_CAP_NONE) |
| 109 | return -ENOTTY; |
| 110 | |
| 111 | /* Command failed */ |
| 112 | if (ars_cap->status & 0xffff) |
| 113 | return -EIO; |
| 114 | |
| 115 | /* No supported scan types for this range */ |
| 116 | flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE; |
| 117 | if ((ars_cap->status >> 16 & flags) == 0) |
| 118 | return -ENOTTY; |
| 119 | break; |
| 120 | case ND_CMD_ARS_START: |
| 121 | ars_start = buf; |
| 122 | /* ARS is in progress */ |
| 123 | if ((ars_start->status & 0xffff) == NFIT_ARS_START_BUSY) |
| 124 | return -EBUSY; |
| 125 | |
| 126 | /* Command failed */ |
| 127 | if (ars_start->status & 0xffff) |
| 128 | return -EIO; |
| 129 | break; |
| 130 | case ND_CMD_ARS_STATUS: |
| 131 | ars_status = buf; |
| 132 | /* Command failed */ |
| 133 | if (ars_status->status & 0xffff) |
| 134 | return -EIO; |
| 135 | /* Check extended status (Upper two bytes) */ |
| 136 | if (ars_status->status == NFIT_ARS_STATUS_DONE) |
| 137 | return 0; |
| 138 | |
| 139 | /* ARS is in progress */ |
| 140 | if (ars_status->status == NFIT_ARS_STATUS_BUSY) |
| 141 | return -EBUSY; |
| 142 | |
| 143 | /* No ARS performed for the current boot */ |
| 144 | if (ars_status->status == NFIT_ARS_STATUS_NONE) |
| 145 | return -EAGAIN; |
| 146 | |
| 147 | /* |
| 148 | * ARS interrupted, either we overflowed or some other |
| 149 | * agent wants the scan to stop. If we didn't overflow |
| 150 | * then just continue with the returned results. |
| 151 | */ |
| 152 | if (ars_status->status == NFIT_ARS_STATUS_INTR) { |
| 153 | if (ars_status->flags & NFIT_ARS_F_OVERFLOW) |
| 154 | return -ENOSPC; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* Unknown status */ |
| 159 | if (ars_status->status >> 16) |
| 160 | return -EIO; |
| 161 | break; |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 162 | case ND_CMD_CLEAR_ERROR: |
| 163 | clear_err = buf; |
| 164 | if (clear_err->status & 0xffff) |
| 165 | return -EIO; |
| 166 | if (!clear_err->cleared) |
| 167 | return -EIO; |
| 168 | if (clear_err->length > clear_err->cleared) |
| 169 | return clear_err->cleared; |
| 170 | break; |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 178 | static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, |
| 179 | struct nvdimm *nvdimm, unsigned int cmd, void *buf, |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 180 | unsigned int buf_len, int *cmd_rc) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 181 | { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 182 | struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 183 | union acpi_object in_obj, in_buf, *out_obj; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 184 | const struct nd_cmd_desc *desc = NULL; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 185 | struct device *dev = acpi_desc->dev; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 186 | struct nd_cmd_pkg *call_pkg = NULL; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 187 | const char *cmd_name, *dimm_name; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 188 | unsigned long cmd_mask, dsm_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 189 | acpi_handle handle; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 190 | unsigned int func; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 191 | const u8 *uuid; |
| 192 | u32 offset; |
| 193 | int rc, i; |
| 194 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 195 | func = cmd; |
| 196 | if (cmd == ND_CMD_CALL) { |
| 197 | call_pkg = buf; |
| 198 | func = call_pkg->nd_command; |
| 199 | } |
| 200 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 201 | if (nvdimm) { |
| 202 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); |
| 203 | struct acpi_device *adev = nfit_mem->adev; |
| 204 | |
| 205 | if (!adev) |
| 206 | return -ENOTTY; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 207 | if (call_pkg && nfit_mem->family != call_pkg->nd_family) |
| 208 | return -ENOTTY; |
| 209 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 210 | dimm_name = nvdimm_name(nvdimm); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 211 | cmd_name = nvdimm_cmd_name(cmd); |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 212 | cmd_mask = nvdimm_cmd_mask(nvdimm); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 213 | dsm_mask = nfit_mem->dsm_mask; |
| 214 | desc = nd_cmd_dimm_desc(cmd); |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 215 | uuid = to_nfit_uuid(nfit_mem->family); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 216 | handle = adev->handle; |
| 217 | } else { |
| 218 | struct acpi_device *adev = to_acpi_dev(acpi_desc); |
| 219 | |
| 220 | cmd_name = nvdimm_bus_cmd_name(cmd); |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 221 | cmd_mask = nd_desc->cmd_mask; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 222 | dsm_mask = cmd_mask; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 223 | desc = nd_cmd_bus_desc(cmd); |
| 224 | uuid = to_nfit_uuid(NFIT_DEV_BUS); |
| 225 | handle = adev->handle; |
| 226 | dimm_name = "bus"; |
| 227 | } |
| 228 | |
| 229 | if (!desc || (cmd && (desc->out_num + desc->in_num == 0))) |
| 230 | return -ENOTTY; |
| 231 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 232 | if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask)) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 233 | return -ENOTTY; |
| 234 | |
| 235 | in_obj.type = ACPI_TYPE_PACKAGE; |
| 236 | in_obj.package.count = 1; |
| 237 | in_obj.package.elements = &in_buf; |
| 238 | in_buf.type = ACPI_TYPE_BUFFER; |
| 239 | in_buf.buffer.pointer = buf; |
| 240 | in_buf.buffer.length = 0; |
| 241 | |
| 242 | /* libnvdimm has already validated the input envelope */ |
| 243 | for (i = 0; i < desc->in_num; i++) |
| 244 | in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc, |
| 245 | i, buf); |
| 246 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 247 | if (call_pkg) { |
| 248 | /* skip over package wrapper */ |
| 249 | in_buf.buffer.pointer = (void *) &call_pkg->nd_payload; |
| 250 | in_buf.buffer.length = call_pkg->nd_size_in; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 253 | if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) { |
| 254 | dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n", |
| 255 | __func__, dimm_name, cmd, func, |
| 256 | in_buf.buffer.length); |
| 257 | print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4, |
| 258 | in_buf.buffer.pointer, |
| 259 | min_t(u32, 256, in_buf.buffer.length), true); |
| 260 | } |
| 261 | |
| 262 | out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 263 | if (!out_obj) { |
| 264 | dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name, |
| 265 | cmd_name); |
| 266 | return -EINVAL; |
| 267 | } |
| 268 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 269 | if (call_pkg) { |
| 270 | call_pkg->nd_fw_size = out_obj->buffer.length; |
| 271 | memcpy(call_pkg->nd_payload + call_pkg->nd_size_in, |
| 272 | out_obj->buffer.pointer, |
| 273 | min(call_pkg->nd_fw_size, call_pkg->nd_size_out)); |
| 274 | |
| 275 | ACPI_FREE(out_obj); |
| 276 | /* |
| 277 | * Need to support FW function w/o known size in advance. |
| 278 | * Caller can determine required size based upon nd_fw_size. |
| 279 | * If we return an error (like elsewhere) then caller wouldn't |
| 280 | * be able to rely upon data returned to make calculation. |
| 281 | */ |
| 282 | return 0; |
| 283 | } |
| 284 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 285 | if (out_obj->package.type != ACPI_TYPE_BUFFER) { |
| 286 | dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n", |
| 287 | __func__, dimm_name, cmd_name, out_obj->type); |
| 288 | rc = -EINVAL; |
| 289 | goto out; |
| 290 | } |
| 291 | |
| 292 | if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) { |
| 293 | dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, |
| 294 | dimm_name, cmd_name, out_obj->buffer.length); |
| 295 | print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, |
| 296 | 4, out_obj->buffer.pointer, min_t(u32, 128, |
| 297 | out_obj->buffer.length), true); |
| 298 | } |
| 299 | |
| 300 | for (i = 0, offset = 0; i < desc->out_num; i++) { |
| 301 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf, |
| 302 | (u32 *) out_obj->buffer.pointer); |
| 303 | |
| 304 | if (offset + out_size > out_obj->buffer.length) { |
| 305 | dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n", |
| 306 | __func__, dimm_name, cmd_name, i); |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | if (in_buf.buffer.length + offset + out_size > buf_len) { |
| 311 | dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n", |
| 312 | __func__, dimm_name, cmd_name, i); |
| 313 | rc = -ENXIO; |
| 314 | goto out; |
| 315 | } |
| 316 | memcpy(buf + in_buf.buffer.length + offset, |
| 317 | out_obj->buffer.pointer + offset, out_size); |
| 318 | offset += out_size; |
| 319 | } |
| 320 | if (offset + in_buf.buffer.length < buf_len) { |
| 321 | if (i >= 1) { |
| 322 | /* |
| 323 | * status valid, return the number of bytes left |
| 324 | * unfilled in the output buffer |
| 325 | */ |
| 326 | rc = buf_len - offset - in_buf.buffer.length; |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 327 | if (cmd_rc) |
| 328 | *cmd_rc = xlat_status(buf, cmd); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 329 | } else { |
| 330 | dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n", |
| 331 | __func__, dimm_name, cmd_name, buf_len, |
| 332 | offset); |
| 333 | rc = -ENXIO; |
| 334 | } |
Dan Williams | 2eea658 | 2016-05-02 09:11:53 -0700 | [diff] [blame] | 335 | } else { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 336 | rc = 0; |
Dan Williams | 2eea658 | 2016-05-02 09:11:53 -0700 | [diff] [blame] | 337 | if (cmd_rc) |
| 338 | *cmd_rc = xlat_status(buf, cmd); |
| 339 | } |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 340 | |
| 341 | out: |
| 342 | ACPI_FREE(out_obj); |
| 343 | |
| 344 | return rc; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static const char *spa_type_name(u16 type) |
| 348 | { |
| 349 | static const char *to_name[] = { |
| 350 | [NFIT_SPA_VOLATILE] = "volatile", |
| 351 | [NFIT_SPA_PM] = "pmem", |
| 352 | [NFIT_SPA_DCR] = "dimm-control-region", |
| 353 | [NFIT_SPA_BDW] = "block-data-window", |
| 354 | [NFIT_SPA_VDISK] = "volatile-disk", |
| 355 | [NFIT_SPA_VCD] = "volatile-cd", |
| 356 | [NFIT_SPA_PDISK] = "persistent-disk", |
| 357 | [NFIT_SPA_PCD] = "persistent-cd", |
| 358 | |
| 359 | }; |
| 360 | |
| 361 | if (type > NFIT_SPA_PCD) |
| 362 | return "unknown"; |
| 363 | |
| 364 | return to_name[type]; |
| 365 | } |
| 366 | |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 367 | int nfit_spa_type(struct acpi_nfit_system_address *spa) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 368 | { |
| 369 | int i; |
| 370 | |
| 371 | for (i = 0; i < NFIT_UUID_MAX; i++) |
| 372 | if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0) |
| 373 | return i; |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | static bool add_spa(struct acpi_nfit_desc *acpi_desc, |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 378 | struct nfit_table_prev *prev, |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 379 | struct acpi_nfit_system_address *spa) |
| 380 | { |
| 381 | struct device *dev = acpi_desc->dev; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 382 | struct nfit_spa *nfit_spa; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 383 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 384 | if (spa->header.length != sizeof(*spa)) |
| 385 | return false; |
| 386 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 387 | list_for_each_entry(nfit_spa, &prev->spas, list) { |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 388 | if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 389 | list_move_tail(&nfit_spa->list, &acpi_desc->spas); |
| 390 | return true; |
| 391 | } |
| 392 | } |
| 393 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 394 | nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa), |
| 395 | GFP_KERNEL); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 396 | if (!nfit_spa) |
| 397 | return false; |
| 398 | INIT_LIST_HEAD(&nfit_spa->list); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 399 | memcpy(nfit_spa->spa, spa, sizeof(*spa)); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 400 | list_add_tail(&nfit_spa->list, &acpi_desc->spas); |
| 401 | dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__, |
| 402 | spa->range_index, |
| 403 | spa_type_name(nfit_spa_type(spa))); |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | static bool add_memdev(struct acpi_nfit_desc *acpi_desc, |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 408 | struct nfit_table_prev *prev, |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 409 | struct acpi_nfit_memory_map *memdev) |
| 410 | { |
| 411 | struct device *dev = acpi_desc->dev; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 412 | struct nfit_memdev *nfit_memdev; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 413 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 414 | if (memdev->header.length != sizeof(*memdev)) |
| 415 | return false; |
| 416 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 417 | list_for_each_entry(nfit_memdev, &prev->memdevs, list) |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 418 | if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 419 | list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs); |
| 420 | return true; |
| 421 | } |
| 422 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 423 | nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev), |
| 424 | GFP_KERNEL); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 425 | if (!nfit_memdev) |
| 426 | return false; |
| 427 | INIT_LIST_HEAD(&nfit_memdev->list); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 428 | memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev)); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 429 | list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs); |
| 430 | dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n", |
| 431 | __func__, memdev->device_handle, memdev->range_index, |
| 432 | memdev->region_index); |
| 433 | return true; |
| 434 | } |
| 435 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 436 | /* |
| 437 | * An implementation may provide a truncated control region if no block windows |
| 438 | * are defined. |
| 439 | */ |
| 440 | static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr) |
| 441 | { |
| 442 | if (dcr->header.length < offsetof(struct acpi_nfit_control_region, |
| 443 | window_size)) |
| 444 | return 0; |
| 445 | if (dcr->windows) |
| 446 | return sizeof(*dcr); |
| 447 | return offsetof(struct acpi_nfit_control_region, window_size); |
| 448 | } |
| 449 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 450 | static bool add_dcr(struct acpi_nfit_desc *acpi_desc, |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 451 | struct nfit_table_prev *prev, |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 452 | struct acpi_nfit_control_region *dcr) |
| 453 | { |
| 454 | struct device *dev = acpi_desc->dev; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 455 | struct nfit_dcr *nfit_dcr; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 456 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 457 | if (!sizeof_dcr(dcr)) |
| 458 | return false; |
| 459 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 460 | list_for_each_entry(nfit_dcr, &prev->dcrs, list) |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 461 | if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 462 | list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs); |
| 463 | return true; |
| 464 | } |
| 465 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 466 | nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr), |
| 467 | GFP_KERNEL); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 468 | if (!nfit_dcr) |
| 469 | return false; |
| 470 | INIT_LIST_HEAD(&nfit_dcr->list); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 471 | memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 472 | list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs); |
| 473 | dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__, |
| 474 | dcr->region_index, dcr->windows); |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | static bool add_bdw(struct acpi_nfit_desc *acpi_desc, |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 479 | struct nfit_table_prev *prev, |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 480 | struct acpi_nfit_data_region *bdw) |
| 481 | { |
| 482 | struct device *dev = acpi_desc->dev; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 483 | struct nfit_bdw *nfit_bdw; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 484 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 485 | if (bdw->header.length != sizeof(*bdw)) |
| 486 | return false; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 487 | list_for_each_entry(nfit_bdw, &prev->bdws, list) |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 488 | if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 489 | list_move_tail(&nfit_bdw->list, &acpi_desc->bdws); |
| 490 | return true; |
| 491 | } |
| 492 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 493 | nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw), |
| 494 | GFP_KERNEL); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 495 | if (!nfit_bdw) |
| 496 | return false; |
| 497 | INIT_LIST_HEAD(&nfit_bdw->list); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 498 | memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw)); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 499 | list_add_tail(&nfit_bdw->list, &acpi_desc->bdws); |
| 500 | dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__, |
| 501 | bdw->region_index, bdw->windows); |
| 502 | return true; |
| 503 | } |
| 504 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 505 | static size_t sizeof_idt(struct acpi_nfit_interleave *idt) |
| 506 | { |
| 507 | if (idt->header.length < sizeof(*idt)) |
| 508 | return 0; |
| 509 | return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1); |
| 510 | } |
| 511 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 512 | static bool add_idt(struct acpi_nfit_desc *acpi_desc, |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 513 | struct nfit_table_prev *prev, |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 514 | struct acpi_nfit_interleave *idt) |
| 515 | { |
| 516 | struct device *dev = acpi_desc->dev; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 517 | struct nfit_idt *nfit_idt; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 518 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 519 | if (!sizeof_idt(idt)) |
| 520 | return false; |
| 521 | |
| 522 | list_for_each_entry(nfit_idt, &prev->idts, list) { |
| 523 | if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt)) |
| 524 | continue; |
| 525 | |
| 526 | if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 527 | list_move_tail(&nfit_idt->list, &acpi_desc->idts); |
| 528 | return true; |
| 529 | } |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 530 | } |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 531 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 532 | nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt), |
| 533 | GFP_KERNEL); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 534 | if (!nfit_idt) |
| 535 | return false; |
| 536 | INIT_LIST_HEAD(&nfit_idt->list); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 537 | memcpy(nfit_idt->idt, idt, sizeof_idt(idt)); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 538 | list_add_tail(&nfit_idt->list, &acpi_desc->idts); |
| 539 | dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__, |
| 540 | idt->interleave_index, idt->line_count); |
| 541 | return true; |
| 542 | } |
| 543 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 544 | static size_t sizeof_flush(struct acpi_nfit_flush_address *flush) |
| 545 | { |
| 546 | if (flush->header.length < sizeof(*flush)) |
| 547 | return 0; |
| 548 | return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1); |
| 549 | } |
| 550 | |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 551 | static bool add_flush(struct acpi_nfit_desc *acpi_desc, |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 552 | struct nfit_table_prev *prev, |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 553 | struct acpi_nfit_flush_address *flush) |
| 554 | { |
| 555 | struct device *dev = acpi_desc->dev; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 556 | struct nfit_flush *nfit_flush; |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 557 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 558 | if (!sizeof_flush(flush)) |
| 559 | return false; |
| 560 | |
| 561 | list_for_each_entry(nfit_flush, &prev->flushes, list) { |
| 562 | if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush)) |
| 563 | continue; |
| 564 | |
| 565 | if (memcmp(nfit_flush->flush, flush, |
| 566 | sizeof_flush(flush)) == 0) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 567 | list_move_tail(&nfit_flush->list, &acpi_desc->flushes); |
| 568 | return true; |
| 569 | } |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 570 | } |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 571 | |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 572 | nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush) |
| 573 | + sizeof_flush(flush), GFP_KERNEL); |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 574 | if (!nfit_flush) |
| 575 | return false; |
| 576 | INIT_LIST_HEAD(&nfit_flush->list); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 577 | memcpy(nfit_flush->flush, flush, sizeof_flush(flush)); |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 578 | list_add_tail(&nfit_flush->list, &acpi_desc->flushes); |
| 579 | dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__, |
| 580 | flush->device_handle, flush->hint_count); |
| 581 | return true; |
| 582 | } |
| 583 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 584 | static void *add_table(struct acpi_nfit_desc *acpi_desc, |
| 585 | struct nfit_table_prev *prev, void *table, const void *end) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 586 | { |
| 587 | struct device *dev = acpi_desc->dev; |
| 588 | struct acpi_nfit_header *hdr; |
| 589 | void *err = ERR_PTR(-ENOMEM); |
| 590 | |
| 591 | if (table >= end) |
| 592 | return NULL; |
| 593 | |
| 594 | hdr = table; |
Vishal Verma | 564d501 | 2015-10-27 16:58:26 -0600 | [diff] [blame] | 595 | if (!hdr->length) { |
| 596 | dev_warn(dev, "found a zero length table '%d' parsing nfit\n", |
| 597 | hdr->type); |
| 598 | return NULL; |
| 599 | } |
| 600 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 601 | switch (hdr->type) { |
| 602 | case ACPI_NFIT_TYPE_SYSTEM_ADDRESS: |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 603 | if (!add_spa(acpi_desc, prev, table)) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 604 | return err; |
| 605 | break; |
| 606 | case ACPI_NFIT_TYPE_MEMORY_MAP: |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 607 | if (!add_memdev(acpi_desc, prev, table)) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 608 | return err; |
| 609 | break; |
| 610 | case ACPI_NFIT_TYPE_CONTROL_REGION: |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 611 | if (!add_dcr(acpi_desc, prev, table)) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 612 | return err; |
| 613 | break; |
| 614 | case ACPI_NFIT_TYPE_DATA_REGION: |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 615 | if (!add_bdw(acpi_desc, prev, table)) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 616 | return err; |
| 617 | break; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 618 | case ACPI_NFIT_TYPE_INTERLEAVE: |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 619 | if (!add_idt(acpi_desc, prev, table)) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 620 | return err; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 621 | break; |
| 622 | case ACPI_NFIT_TYPE_FLUSH_ADDRESS: |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 623 | if (!add_flush(acpi_desc, prev, table)) |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 624 | return err; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 625 | break; |
| 626 | case ACPI_NFIT_TYPE_SMBIOS: |
| 627 | dev_dbg(dev, "%s: smbios\n", __func__); |
| 628 | break; |
| 629 | default: |
| 630 | dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type); |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | return table + hdr->length; |
| 635 | } |
| 636 | |
| 637 | static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc, |
| 638 | struct nfit_mem *nfit_mem) |
| 639 | { |
| 640 | u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle; |
| 641 | u16 dcr = nfit_mem->dcr->region_index; |
| 642 | struct nfit_spa *nfit_spa; |
| 643 | |
| 644 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { |
| 645 | u16 range_index = nfit_spa->spa->range_index; |
| 646 | int type = nfit_spa_type(nfit_spa->spa); |
| 647 | struct nfit_memdev *nfit_memdev; |
| 648 | |
| 649 | if (type != NFIT_SPA_BDW) |
| 650 | continue; |
| 651 | |
| 652 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { |
| 653 | if (nfit_memdev->memdev->range_index != range_index) |
| 654 | continue; |
| 655 | if (nfit_memdev->memdev->device_handle != device_handle) |
| 656 | continue; |
| 657 | if (nfit_memdev->memdev->region_index != dcr) |
| 658 | continue; |
| 659 | |
| 660 | nfit_mem->spa_bdw = nfit_spa->spa; |
| 661 | return; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n", |
| 666 | nfit_mem->spa_dcr->range_index); |
| 667 | nfit_mem->bdw = NULL; |
| 668 | } |
| 669 | |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 670 | static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc, |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 671 | struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa) |
| 672 | { |
| 673 | u16 dcr = __to_nfit_memdev(nfit_mem)->region_index; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 674 | struct nfit_memdev *nfit_memdev; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 675 | struct nfit_bdw *nfit_bdw; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 676 | struct nfit_idt *nfit_idt; |
| 677 | u16 idt_idx, range_index; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 678 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 679 | list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) { |
| 680 | if (nfit_bdw->bdw->region_index != dcr) |
| 681 | continue; |
| 682 | nfit_mem->bdw = nfit_bdw->bdw; |
| 683 | break; |
| 684 | } |
| 685 | |
| 686 | if (!nfit_mem->bdw) |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 687 | return; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 688 | |
| 689 | nfit_mem_find_spa_bdw(acpi_desc, nfit_mem); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 690 | |
| 691 | if (!nfit_mem->spa_bdw) |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 692 | return; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 693 | |
| 694 | range_index = nfit_mem->spa_bdw->range_index; |
| 695 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { |
| 696 | if (nfit_memdev->memdev->range_index != range_index || |
| 697 | nfit_memdev->memdev->region_index != dcr) |
| 698 | continue; |
| 699 | nfit_mem->memdev_bdw = nfit_memdev->memdev; |
| 700 | idt_idx = nfit_memdev->memdev->interleave_index; |
| 701 | list_for_each_entry(nfit_idt, &acpi_desc->idts, list) { |
| 702 | if (nfit_idt->idt->interleave_index != idt_idx) |
| 703 | continue; |
| 704 | nfit_mem->idt_bdw = nfit_idt->idt; |
| 705 | break; |
| 706 | } |
| 707 | break; |
| 708 | } |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc, |
| 712 | struct acpi_nfit_system_address *spa) |
| 713 | { |
| 714 | struct nfit_mem *nfit_mem, *found; |
| 715 | struct nfit_memdev *nfit_memdev; |
| 716 | int type = nfit_spa_type(spa); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 717 | |
| 718 | switch (type) { |
| 719 | case NFIT_SPA_DCR: |
| 720 | case NFIT_SPA_PM: |
| 721 | break; |
| 722 | default: |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { |
Dan Williams | ad9ac5e | 2016-05-26 11:38:08 -0700 | [diff] [blame] | 727 | struct nfit_flush *nfit_flush; |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 728 | struct nfit_dcr *nfit_dcr; |
| 729 | u32 device_handle; |
| 730 | u16 dcr; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 731 | |
| 732 | if (nfit_memdev->memdev->range_index != spa->range_index) |
| 733 | continue; |
| 734 | found = NULL; |
| 735 | dcr = nfit_memdev->memdev->region_index; |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 736 | device_handle = nfit_memdev->memdev->device_handle; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 737 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 738 | if (__to_nfit_memdev(nfit_mem)->device_handle |
| 739 | == device_handle) { |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 740 | found = nfit_mem; |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | if (found) |
| 745 | nfit_mem = found; |
| 746 | else { |
| 747 | nfit_mem = devm_kzalloc(acpi_desc->dev, |
| 748 | sizeof(*nfit_mem), GFP_KERNEL); |
| 749 | if (!nfit_mem) |
| 750 | return -ENOMEM; |
| 751 | INIT_LIST_HEAD(&nfit_mem->list); |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 752 | nfit_mem->acpi_desc = acpi_desc; |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 753 | list_add(&nfit_mem->list, &acpi_desc->dimms); |
| 754 | } |
| 755 | |
| 756 | list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) { |
| 757 | if (nfit_dcr->dcr->region_index != dcr) |
| 758 | continue; |
| 759 | /* |
| 760 | * Record the control region for the dimm. For |
| 761 | * the ACPI 6.1 case, where there are separate |
| 762 | * control regions for the pmem vs blk |
| 763 | * interfaces, be sure to record the extended |
| 764 | * blk details. |
| 765 | */ |
| 766 | if (!nfit_mem->dcr) |
| 767 | nfit_mem->dcr = nfit_dcr->dcr; |
| 768 | else if (nfit_mem->dcr->windows == 0 |
| 769 | && nfit_dcr->dcr->windows) |
| 770 | nfit_mem->dcr = nfit_dcr->dcr; |
| 771 | break; |
| 772 | } |
| 773 | |
Dan Williams | ad9ac5e | 2016-05-26 11:38:08 -0700 | [diff] [blame] | 774 | list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) { |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 775 | struct acpi_nfit_flush_address *flush; |
| 776 | u16 i; |
| 777 | |
Dan Williams | ad9ac5e | 2016-05-26 11:38:08 -0700 | [diff] [blame] | 778 | if (nfit_flush->flush->device_handle != device_handle) |
| 779 | continue; |
| 780 | nfit_mem->nfit_flush = nfit_flush; |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 781 | flush = nfit_flush->flush; |
| 782 | nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev, |
| 783 | flush->hint_count |
| 784 | * sizeof(struct resource), GFP_KERNEL); |
| 785 | if (!nfit_mem->flush_wpq) |
| 786 | return -ENOMEM; |
| 787 | for (i = 0; i < flush->hint_count; i++) { |
| 788 | struct resource *res = &nfit_mem->flush_wpq[i]; |
| 789 | |
| 790 | res->start = flush->hint_address[i]; |
| 791 | res->end = res->start + 8 - 1; |
| 792 | } |
Dan Williams | ad9ac5e | 2016-05-26 11:38:08 -0700 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 796 | if (dcr && !nfit_mem->dcr) { |
| 797 | dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n", |
| 798 | spa->range_index, dcr); |
| 799 | return -ENODEV; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | if (type == NFIT_SPA_DCR) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 803 | struct nfit_idt *nfit_idt; |
| 804 | u16 idt_idx; |
| 805 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 806 | /* multiple dimms may share a SPA when interleaved */ |
| 807 | nfit_mem->spa_dcr = spa; |
| 808 | nfit_mem->memdev_dcr = nfit_memdev->memdev; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 809 | idt_idx = nfit_memdev->memdev->interleave_index; |
| 810 | list_for_each_entry(nfit_idt, &acpi_desc->idts, list) { |
| 811 | if (nfit_idt->idt->interleave_index != idt_idx) |
| 812 | continue; |
| 813 | nfit_mem->idt_dcr = nfit_idt->idt; |
| 814 | break; |
| 815 | } |
Dan Williams | 6697b2c | 2016-02-04 16:51:00 -0800 | [diff] [blame] | 816 | nfit_mem_init_bdw(acpi_desc, nfit_mem, spa); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 817 | } else { |
| 818 | /* |
| 819 | * A single dimm may belong to multiple SPA-PM |
| 820 | * ranges, record at least one in addition to |
| 821 | * any SPA-DCR range. |
| 822 | */ |
| 823 | nfit_mem->memdev_pmem = nfit_memdev->memdev; |
| 824 | } |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b) |
| 831 | { |
| 832 | struct nfit_mem *a = container_of(_a, typeof(*a), list); |
| 833 | struct nfit_mem *b = container_of(_b, typeof(*b), list); |
| 834 | u32 handleA, handleB; |
| 835 | |
| 836 | handleA = __to_nfit_memdev(a)->device_handle; |
| 837 | handleB = __to_nfit_memdev(b)->device_handle; |
| 838 | if (handleA < handleB) |
| 839 | return -1; |
| 840 | else if (handleA > handleB) |
| 841 | return 1; |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc) |
| 846 | { |
| 847 | struct nfit_spa *nfit_spa; |
| 848 | |
| 849 | /* |
| 850 | * For each SPA-DCR or SPA-PMEM address range find its |
| 851 | * corresponding MEMDEV(s). From each MEMDEV find the |
| 852 | * corresponding DCR. Then, if we're operating on a SPA-DCR, |
| 853 | * try to find a SPA-BDW and a corresponding BDW that references |
| 854 | * the DCR. Throw it all into an nfit_mem object. Note, that |
| 855 | * BDWs are optional. |
| 856 | */ |
| 857 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { |
| 858 | int rc; |
| 859 | |
| 860 | rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa); |
| 861 | if (rc) |
| 862 | return rc; |
| 863 | } |
| 864 | |
| 865 | list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp); |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 870 | static ssize_t revision_show(struct device *dev, |
| 871 | struct device_attribute *attr, char *buf) |
| 872 | { |
| 873 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 874 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); |
| 875 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); |
| 876 | |
Linda Knippers | 6b577c9 | 2015-11-20 19:05:49 -0500 | [diff] [blame] | 877 | return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision); |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 878 | } |
| 879 | static DEVICE_ATTR_RO(revision); |
| 880 | |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 881 | /* |
| 882 | * This shows the number of full Address Range Scrubs that have been |
| 883 | * completed since driver load time. Userspace can wait on this using |
| 884 | * select/poll etc. A '+' at the end indicates an ARS is in progress |
| 885 | */ |
| 886 | static ssize_t scrub_show(struct device *dev, |
| 887 | struct device_attribute *attr, char *buf) |
| 888 | { |
| 889 | struct nvdimm_bus_descriptor *nd_desc; |
| 890 | ssize_t rc = -ENXIO; |
| 891 | |
| 892 | device_lock(dev); |
| 893 | nd_desc = dev_get_drvdata(dev); |
| 894 | if (nd_desc) { |
| 895 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); |
| 896 | |
| 897 | rc = sprintf(buf, "%d%s", acpi_desc->scrub_count, |
| 898 | (work_busy(&acpi_desc->work)) ? "+\n" : "\n"); |
| 899 | } |
| 900 | device_unlock(dev); |
| 901 | return rc; |
| 902 | } |
| 903 | |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 904 | static ssize_t scrub_store(struct device *dev, |
| 905 | struct device_attribute *attr, const char *buf, size_t size) |
| 906 | { |
| 907 | struct nvdimm_bus_descriptor *nd_desc; |
| 908 | ssize_t rc; |
| 909 | long val; |
| 910 | |
| 911 | rc = kstrtol(buf, 0, &val); |
| 912 | if (rc) |
| 913 | return rc; |
| 914 | if (val != 1) |
| 915 | return -EINVAL; |
| 916 | |
| 917 | device_lock(dev); |
| 918 | nd_desc = dev_get_drvdata(dev); |
| 919 | if (nd_desc) { |
| 920 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); |
| 921 | |
| 922 | rc = acpi_nfit_ars_rescan(acpi_desc); |
| 923 | } |
| 924 | device_unlock(dev); |
| 925 | if (rc) |
| 926 | return rc; |
| 927 | return size; |
| 928 | } |
| 929 | static DEVICE_ATTR_RW(scrub); |
| 930 | |
| 931 | static bool ars_supported(struct nvdimm_bus *nvdimm_bus) |
| 932 | { |
| 933 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); |
| 934 | const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START |
| 935 | | 1 << ND_CMD_ARS_STATUS; |
| 936 | |
| 937 | return (nd_desc->cmd_mask & mask) == mask; |
| 938 | } |
| 939 | |
| 940 | static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n) |
| 941 | { |
| 942 | struct device *dev = container_of(kobj, struct device, kobj); |
| 943 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); |
| 944 | |
| 945 | if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus)) |
| 946 | return 0; |
| 947 | return a->mode; |
| 948 | } |
| 949 | |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 950 | static struct attribute *acpi_nfit_attributes[] = { |
| 951 | &dev_attr_revision.attr, |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 952 | &dev_attr_scrub.attr, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 953 | NULL, |
| 954 | }; |
| 955 | |
| 956 | static struct attribute_group acpi_nfit_attribute_group = { |
| 957 | .name = "nfit", |
| 958 | .attrs = acpi_nfit_attributes, |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 959 | .is_visible = nfit_visible, |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 960 | }; |
| 961 | |
Dan Williams | a61fe6f | 2016-02-19 12:29:32 -0800 | [diff] [blame] | 962 | static const struct attribute_group *acpi_nfit_attribute_groups[] = { |
Dan Williams | 45def22 | 2015-04-26 19:26:48 -0400 | [diff] [blame] | 963 | &nvdimm_bus_attribute_group, |
| 964 | &acpi_nfit_attribute_group, |
| 965 | NULL, |
| 966 | }; |
| 967 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 968 | static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev) |
| 969 | { |
| 970 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 971 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); |
| 972 | |
| 973 | return __to_nfit_memdev(nfit_mem); |
| 974 | } |
| 975 | |
| 976 | static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev) |
| 977 | { |
| 978 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 979 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); |
| 980 | |
| 981 | return nfit_mem->dcr; |
| 982 | } |
| 983 | |
| 984 | static ssize_t handle_show(struct device *dev, |
| 985 | struct device_attribute *attr, char *buf) |
| 986 | { |
| 987 | struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev); |
| 988 | |
| 989 | return sprintf(buf, "%#x\n", memdev->device_handle); |
| 990 | } |
| 991 | static DEVICE_ATTR_RO(handle); |
| 992 | |
| 993 | static ssize_t phys_id_show(struct device *dev, |
| 994 | struct device_attribute *attr, char *buf) |
| 995 | { |
| 996 | struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev); |
| 997 | |
| 998 | return sprintf(buf, "%#x\n", memdev->physical_id); |
| 999 | } |
| 1000 | static DEVICE_ATTR_RO(phys_id); |
| 1001 | |
| 1002 | static ssize_t vendor_show(struct device *dev, |
| 1003 | struct device_attribute *attr, char *buf) |
| 1004 | { |
| 1005 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1006 | |
Toshi Kani | 5ad9a7f | 2016-04-25 15:34:58 -0600 | [diff] [blame] | 1007 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id)); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1008 | } |
| 1009 | static DEVICE_ATTR_RO(vendor); |
| 1010 | |
| 1011 | static ssize_t rev_id_show(struct device *dev, |
| 1012 | struct device_attribute *attr, char *buf) |
| 1013 | { |
| 1014 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1015 | |
Toshi Kani | 5ad9a7f | 2016-04-25 15:34:58 -0600 | [diff] [blame] | 1016 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id)); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1017 | } |
| 1018 | static DEVICE_ATTR_RO(rev_id); |
| 1019 | |
| 1020 | static ssize_t device_show(struct device *dev, |
| 1021 | struct device_attribute *attr, char *buf) |
| 1022 | { |
| 1023 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1024 | |
Toshi Kani | 5ad9a7f | 2016-04-25 15:34:58 -0600 | [diff] [blame] | 1025 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id)); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1026 | } |
| 1027 | static DEVICE_ATTR_RO(device); |
| 1028 | |
Dan Williams | 6ca7208 | 2016-04-29 10:33:23 -0700 | [diff] [blame] | 1029 | static ssize_t subsystem_vendor_show(struct device *dev, |
| 1030 | struct device_attribute *attr, char *buf) |
| 1031 | { |
| 1032 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1033 | |
| 1034 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id)); |
| 1035 | } |
| 1036 | static DEVICE_ATTR_RO(subsystem_vendor); |
| 1037 | |
| 1038 | static ssize_t subsystem_rev_id_show(struct device *dev, |
| 1039 | struct device_attribute *attr, char *buf) |
| 1040 | { |
| 1041 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1042 | |
| 1043 | return sprintf(buf, "0x%04x\n", |
| 1044 | be16_to_cpu(dcr->subsystem_revision_id)); |
| 1045 | } |
| 1046 | static DEVICE_ATTR_RO(subsystem_rev_id); |
| 1047 | |
| 1048 | static ssize_t subsystem_device_show(struct device *dev, |
| 1049 | struct device_attribute *attr, char *buf) |
| 1050 | { |
| 1051 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1052 | |
| 1053 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id)); |
| 1054 | } |
| 1055 | static DEVICE_ATTR_RO(subsystem_device); |
| 1056 | |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 1057 | static int num_nvdimm_formats(struct nvdimm *nvdimm) |
| 1058 | { |
| 1059 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); |
| 1060 | int formats = 0; |
| 1061 | |
| 1062 | if (nfit_mem->memdev_pmem) |
| 1063 | formats++; |
| 1064 | if (nfit_mem->memdev_bdw) |
| 1065 | formats++; |
| 1066 | return formats; |
| 1067 | } |
| 1068 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1069 | static ssize_t format_show(struct device *dev, |
| 1070 | struct device_attribute *attr, char *buf) |
| 1071 | { |
| 1072 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1073 | |
Dan Williams | 1bcbf42 | 2016-06-29 11:19:32 -0700 | [diff] [blame] | 1074 | return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code)); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1075 | } |
| 1076 | static DEVICE_ATTR_RO(format); |
| 1077 | |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 1078 | static ssize_t format1_show(struct device *dev, |
| 1079 | struct device_attribute *attr, char *buf) |
| 1080 | { |
| 1081 | u32 handle; |
| 1082 | ssize_t rc = -ENXIO; |
| 1083 | struct nfit_mem *nfit_mem; |
| 1084 | struct nfit_memdev *nfit_memdev; |
| 1085 | struct acpi_nfit_desc *acpi_desc; |
| 1086 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 1087 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1088 | |
| 1089 | nfit_mem = nvdimm_provider_data(nvdimm); |
| 1090 | acpi_desc = nfit_mem->acpi_desc; |
| 1091 | handle = to_nfit_memdev(dev)->device_handle; |
| 1092 | |
| 1093 | /* assumes DIMMs have at most 2 published interface codes */ |
| 1094 | mutex_lock(&acpi_desc->init_mutex); |
| 1095 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { |
| 1096 | struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev; |
| 1097 | struct nfit_dcr *nfit_dcr; |
| 1098 | |
| 1099 | if (memdev->device_handle != handle) |
| 1100 | continue; |
| 1101 | |
| 1102 | list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) { |
| 1103 | if (nfit_dcr->dcr->region_index != memdev->region_index) |
| 1104 | continue; |
| 1105 | if (nfit_dcr->dcr->code == dcr->code) |
| 1106 | continue; |
Dan Williams | 1bcbf42 | 2016-06-29 11:19:32 -0700 | [diff] [blame] | 1107 | rc = sprintf(buf, "0x%04x\n", |
| 1108 | le16_to_cpu(nfit_dcr->dcr->code)); |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 1109 | break; |
| 1110 | } |
| 1111 | if (rc != ENXIO) |
| 1112 | break; |
| 1113 | } |
| 1114 | mutex_unlock(&acpi_desc->init_mutex); |
| 1115 | return rc; |
| 1116 | } |
| 1117 | static DEVICE_ATTR_RO(format1); |
| 1118 | |
| 1119 | static ssize_t formats_show(struct device *dev, |
| 1120 | struct device_attribute *attr, char *buf) |
| 1121 | { |
| 1122 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 1123 | |
| 1124 | return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm)); |
| 1125 | } |
| 1126 | static DEVICE_ATTR_RO(formats); |
| 1127 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1128 | static ssize_t serial_show(struct device *dev, |
| 1129 | struct device_attribute *attr, char *buf) |
| 1130 | { |
| 1131 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1132 | |
Toshi Kani | 5ad9a7f | 2016-04-25 15:34:58 -0600 | [diff] [blame] | 1133 | return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number)); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1134 | } |
| 1135 | static DEVICE_ATTR_RO(serial); |
| 1136 | |
Dan Williams | a94e3fb | 2016-04-28 18:18:05 -0700 | [diff] [blame] | 1137 | static ssize_t family_show(struct device *dev, |
| 1138 | struct device_attribute *attr, char *buf) |
| 1139 | { |
| 1140 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 1141 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); |
| 1142 | |
| 1143 | if (nfit_mem->family < 0) |
| 1144 | return -ENXIO; |
| 1145 | return sprintf(buf, "%d\n", nfit_mem->family); |
| 1146 | } |
| 1147 | static DEVICE_ATTR_RO(family); |
| 1148 | |
| 1149 | static ssize_t dsm_mask_show(struct device *dev, |
| 1150 | struct device_attribute *attr, char *buf) |
| 1151 | { |
| 1152 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 1153 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); |
| 1154 | |
| 1155 | if (nfit_mem->family < 0) |
| 1156 | return -ENXIO; |
| 1157 | return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask); |
| 1158 | } |
| 1159 | static DEVICE_ATTR_RO(dsm_mask); |
| 1160 | |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1161 | static ssize_t flags_show(struct device *dev, |
| 1162 | struct device_attribute *attr, char *buf) |
| 1163 | { |
| 1164 | u16 flags = to_nfit_memdev(dev)->flags; |
| 1165 | |
| 1166 | return sprintf(buf, "%s%s%s%s%s\n", |
Toshi Kani | 402bae5 | 2015-08-26 10:20:23 -0600 | [diff] [blame] | 1167 | flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "", |
| 1168 | flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "", |
| 1169 | flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "", |
Bob Moore | ca321d1 | 2015-10-19 10:24:52 +0800 | [diff] [blame] | 1170 | flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "", |
Toshi Kani | 402bae5 | 2015-08-26 10:20:23 -0600 | [diff] [blame] | 1171 | flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : ""); |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1172 | } |
| 1173 | static DEVICE_ATTR_RO(flags); |
| 1174 | |
Toshi Kani | 38a879b | 2016-04-25 15:34:59 -0600 | [diff] [blame] | 1175 | static ssize_t id_show(struct device *dev, |
| 1176 | struct device_attribute *attr, char *buf) |
| 1177 | { |
| 1178 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); |
| 1179 | |
| 1180 | if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID) |
| 1181 | return sprintf(buf, "%04x-%02x-%04x-%08x\n", |
| 1182 | be16_to_cpu(dcr->vendor_id), |
| 1183 | dcr->manufacturing_location, |
| 1184 | be16_to_cpu(dcr->manufacturing_date), |
| 1185 | be32_to_cpu(dcr->serial_number)); |
| 1186 | else |
| 1187 | return sprintf(buf, "%04x-%08x\n", |
| 1188 | be16_to_cpu(dcr->vendor_id), |
| 1189 | be32_to_cpu(dcr->serial_number)); |
| 1190 | } |
| 1191 | static DEVICE_ATTR_RO(id); |
| 1192 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1193 | static struct attribute *acpi_nfit_dimm_attributes[] = { |
| 1194 | &dev_attr_handle.attr, |
| 1195 | &dev_attr_phys_id.attr, |
| 1196 | &dev_attr_vendor.attr, |
| 1197 | &dev_attr_device.attr, |
Dan Williams | 6ca7208 | 2016-04-29 10:33:23 -0700 | [diff] [blame] | 1198 | &dev_attr_rev_id.attr, |
| 1199 | &dev_attr_subsystem_vendor.attr, |
| 1200 | &dev_attr_subsystem_device.attr, |
| 1201 | &dev_attr_subsystem_rev_id.attr, |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1202 | &dev_attr_format.attr, |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 1203 | &dev_attr_formats.attr, |
| 1204 | &dev_attr_format1.attr, |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1205 | &dev_attr_serial.attr, |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1206 | &dev_attr_flags.attr, |
Toshi Kani | 38a879b | 2016-04-25 15:34:59 -0600 | [diff] [blame] | 1207 | &dev_attr_id.attr, |
Dan Williams | a94e3fb | 2016-04-28 18:18:05 -0700 | [diff] [blame] | 1208 | &dev_attr_family.attr, |
| 1209 | &dev_attr_dsm_mask.attr, |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1210 | NULL, |
| 1211 | }; |
| 1212 | |
| 1213 | static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj, |
| 1214 | struct attribute *a, int n) |
| 1215 | { |
| 1216 | struct device *dev = container_of(kobj, struct device, kobj); |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 1217 | struct nvdimm *nvdimm = to_nvdimm(dev); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1218 | |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 1219 | if (!to_nfit_dcr(dev)) |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1220 | return 0; |
Dan Williams | 8cc6ddf | 2016-04-05 15:26:50 -0700 | [diff] [blame] | 1221 | if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1) |
| 1222 | return 0; |
| 1223 | return a->mode; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | static struct attribute_group acpi_nfit_dimm_attribute_group = { |
| 1227 | .name = "nfit", |
| 1228 | .attrs = acpi_nfit_dimm_attributes, |
| 1229 | .is_visible = acpi_nfit_dimm_attr_visible, |
| 1230 | }; |
| 1231 | |
| 1232 | static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = { |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1233 | &nvdimm_attribute_group, |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 1234 | &nd_device_attribute_group, |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1235 | &acpi_nfit_dimm_attribute_group, |
| 1236 | NULL, |
| 1237 | }; |
| 1238 | |
| 1239 | static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc, |
| 1240 | u32 device_handle) |
| 1241 | { |
| 1242 | struct nfit_mem *nfit_mem; |
| 1243 | |
| 1244 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) |
| 1245 | if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle) |
| 1246 | return nfit_mem->nvdimm; |
| 1247 | |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | |
Dan Williams | 231bf11 | 2016-08-22 19:23:25 -0700 | [diff] [blame^] | 1251 | void __acpi_nvdimm_notify(struct device *dev, u32 event) |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1252 | { |
| 1253 | struct nfit_mem *nfit_mem; |
| 1254 | struct acpi_nfit_desc *acpi_desc; |
| 1255 | |
| 1256 | dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__, |
| 1257 | event); |
| 1258 | |
| 1259 | if (event != NFIT_NOTIFY_DIMM_HEALTH) { |
| 1260 | dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev), |
| 1261 | event); |
| 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | acpi_desc = dev_get_drvdata(dev->parent); |
| 1266 | if (!acpi_desc) |
| 1267 | return; |
| 1268 | |
| 1269 | /* |
| 1270 | * If we successfully retrieved acpi_desc, then we know nfit_mem data |
| 1271 | * is still valid. |
| 1272 | */ |
| 1273 | nfit_mem = dev_get_drvdata(dev); |
| 1274 | if (nfit_mem && nfit_mem->flags_attr) |
| 1275 | sysfs_notify_dirent(nfit_mem->flags_attr); |
| 1276 | } |
Dan Williams | 231bf11 | 2016-08-22 19:23:25 -0700 | [diff] [blame^] | 1277 | EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify); |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1278 | |
| 1279 | static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data) |
| 1280 | { |
| 1281 | struct acpi_device *adev = data; |
| 1282 | struct device *dev = &adev->dev; |
| 1283 | |
| 1284 | device_lock(dev->parent); |
| 1285 | __acpi_nvdimm_notify(dev, event); |
| 1286 | device_unlock(dev->parent); |
| 1287 | } |
| 1288 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1289 | static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc, |
| 1290 | struct nfit_mem *nfit_mem, u32 device_handle) |
| 1291 | { |
| 1292 | struct acpi_device *adev, *adev_dimm; |
| 1293 | struct device *dev = acpi_desc->dev; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1294 | unsigned long dsm_mask; |
| 1295 | const u8 *uuid; |
Linda Knippers | 60e95f4 | 2015-07-22 16:17:22 -0400 | [diff] [blame] | 1296 | int i; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1297 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 1298 | /* nfit test assumes 1:1 relationship between commands and dsms */ |
| 1299 | nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1300 | nfit_mem->family = NVDIMM_FAMILY_INTEL; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1301 | adev = to_acpi_dev(acpi_desc); |
| 1302 | if (!adev) |
| 1303 | return 0; |
| 1304 | |
| 1305 | adev_dimm = acpi_find_child_device(adev, device_handle, false); |
| 1306 | nfit_mem->adev = adev_dimm; |
| 1307 | if (!adev_dimm) { |
| 1308 | dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n", |
| 1309 | device_handle); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 1310 | return force_enable_dimms ? 0 : -ENODEV; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1311 | } |
| 1312 | |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1313 | if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle, |
| 1314 | ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) { |
| 1315 | dev_err(dev, "%s: notification registration failed\n", |
| 1316 | dev_name(&adev_dimm->dev)); |
| 1317 | return -ENXIO; |
| 1318 | } |
| 1319 | |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1320 | /* |
stuart hayes | e02fb72 | 2016-05-26 11:38:41 -0500 | [diff] [blame] | 1321 | * Until standardization materializes we need to consider 4 |
Dan Williams | a722559 | 2016-07-19 12:32:39 -0700 | [diff] [blame] | 1322 | * different command sets. Note, that checking for function0 (bit0) |
| 1323 | * tells us if any commands are reachable through this uuid. |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1324 | */ |
stuart hayes | e02fb72 | 2016-05-26 11:38:41 -0500 | [diff] [blame] | 1325 | for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++) |
Dan Williams | a722559 | 2016-07-19 12:32:39 -0700 | [diff] [blame] | 1326 | if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1)) |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1327 | break; |
| 1328 | |
| 1329 | /* limit the supported commands to those that are publicly documented */ |
| 1330 | nfit_mem->family = i; |
Dan Williams | 8755409 | 2016-04-28 18:01:20 -0700 | [diff] [blame] | 1331 | if (nfit_mem->family == NVDIMM_FAMILY_INTEL) { |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1332 | dsm_mask = 0x3fe; |
Dan Williams | 8755409 | 2016-04-28 18:01:20 -0700 | [diff] [blame] | 1333 | if (disable_vendor_specific) |
| 1334 | dsm_mask &= ~(1 << ND_CMD_VENDOR); |
stuart hayes | e02fb72 | 2016-05-26 11:38:41 -0500 | [diff] [blame] | 1335 | } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) { |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1336 | dsm_mask = 0x1c3c76; |
stuart hayes | e02fb72 | 2016-05-26 11:38:41 -0500 | [diff] [blame] | 1337 | } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) { |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1338 | dsm_mask = 0x1fe; |
Dan Williams | 8755409 | 2016-04-28 18:01:20 -0700 | [diff] [blame] | 1339 | if (disable_vendor_specific) |
| 1340 | dsm_mask &= ~(1 << 8); |
stuart hayes | e02fb72 | 2016-05-26 11:38:41 -0500 | [diff] [blame] | 1341 | } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) { |
| 1342 | dsm_mask = 0xffffffff; |
Dan Williams | 8755409 | 2016-04-28 18:01:20 -0700 | [diff] [blame] | 1343 | } else { |
Dan Williams | a722559 | 2016-07-19 12:32:39 -0700 | [diff] [blame] | 1344 | dev_dbg(dev, "unknown dimm command family\n"); |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1345 | nfit_mem->family = -1; |
Dan Williams | a722559 | 2016-07-19 12:32:39 -0700 | [diff] [blame] | 1346 | /* DSMs are optional, continue loading the driver... */ |
| 1347 | return 0; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | uuid = to_nfit_uuid(nfit_mem->family); |
| 1351 | for_each_set_bit(i, &dsm_mask, BITS_PER_LONG) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1352 | if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i)) |
| 1353 | set_bit(i, &nfit_mem->dsm_mask); |
| 1354 | |
Linda Knippers | 60e95f4 | 2015-07-22 16:17:22 -0400 | [diff] [blame] | 1355 | return 0; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1356 | } |
| 1357 | |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1358 | static void shutdown_dimm_notify(void *data) |
| 1359 | { |
| 1360 | struct acpi_nfit_desc *acpi_desc = data; |
| 1361 | struct nfit_mem *nfit_mem; |
| 1362 | |
| 1363 | mutex_lock(&acpi_desc->init_mutex); |
| 1364 | /* |
| 1365 | * Clear out the nfit_mem->flags_attr and shut down dimm event |
| 1366 | * notifications. |
| 1367 | */ |
| 1368 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) { |
Dan Williams | 231bf11 | 2016-08-22 19:23:25 -0700 | [diff] [blame^] | 1369 | struct acpi_device *adev_dimm = nfit_mem->adev; |
| 1370 | |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1371 | if (nfit_mem->flags_attr) { |
| 1372 | sysfs_put(nfit_mem->flags_attr); |
| 1373 | nfit_mem->flags_attr = NULL; |
| 1374 | } |
Dan Williams | 231bf11 | 2016-08-22 19:23:25 -0700 | [diff] [blame^] | 1375 | if (adev_dimm) |
| 1376 | acpi_remove_notify_handler(adev_dimm->handle, |
| 1377 | ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify); |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1378 | } |
| 1379 | mutex_unlock(&acpi_desc->init_mutex); |
| 1380 | } |
| 1381 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1382 | static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc) |
| 1383 | { |
| 1384 | struct nfit_mem *nfit_mem; |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1385 | int dimm_count = 0, rc; |
| 1386 | struct nvdimm *nvdimm; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1387 | |
| 1388 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) { |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 1389 | struct acpi_nfit_flush_address *flush; |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1390 | unsigned long flags = 0, cmd_mask; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1391 | u32 device_handle; |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1392 | u16 mem_flags; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1393 | |
| 1394 | device_handle = __to_nfit_memdev(nfit_mem)->device_handle; |
| 1395 | nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle); |
| 1396 | if (nvdimm) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 1397 | dimm_count++; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1398 | continue; |
| 1399 | } |
| 1400 | |
| 1401 | if (nfit_mem->bdw && nfit_mem->memdev_pmem) |
| 1402 | flags |= NDD_ALIASING; |
| 1403 | |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1404 | mem_flags = __to_nfit_memdev(nfit_mem)->flags; |
Bob Moore | ca321d1 | 2015-10-19 10:24:52 +0800 | [diff] [blame] | 1405 | if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED) |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1406 | flags |= NDD_UNARMED; |
| 1407 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1408 | rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle); |
| 1409 | if (rc) |
| 1410 | continue; |
| 1411 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 1412 | /* |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1413 | * TODO: provide translation for non-NVDIMM_FAMILY_INTEL |
| 1414 | * devices (i.e. from nd_cmd to acpi_dsm) to standardize the |
| 1415 | * userspace interface. |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 1416 | */ |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 1417 | cmd_mask = 1UL << ND_CMD_CALL; |
| 1418 | if (nfit_mem->family == NVDIMM_FAMILY_INTEL) |
| 1419 | cmd_mask |= nfit_mem->dsm_mask; |
| 1420 | |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 1421 | flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush |
| 1422 | : NULL; |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1423 | nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem, |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1424 | acpi_nfit_dimm_attribute_groups, |
Dan Williams | e5ae3b2 | 2016-06-07 17:00:04 -0700 | [diff] [blame] | 1425 | flags, cmd_mask, flush ? flush->hint_count : 0, |
| 1426 | nfit_mem->flush_wpq); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1427 | if (!nvdimm) |
| 1428 | return -ENOMEM; |
| 1429 | |
| 1430 | nfit_mem->nvdimm = nvdimm; |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 1431 | dimm_count++; |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1432 | |
| 1433 | if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0) |
| 1434 | continue; |
| 1435 | |
Toshi Kani | 402bae5 | 2015-08-26 10:20:23 -0600 | [diff] [blame] | 1436 | dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n", |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1437 | nvdimm_name(nvdimm), |
Toshi Kani | 402bae5 | 2015-08-26 10:20:23 -0600 | [diff] [blame] | 1438 | mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "", |
| 1439 | mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"", |
| 1440 | mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "", |
Bob Moore | ca321d1 | 2015-10-19 10:24:52 +0800 | [diff] [blame] | 1441 | mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : ""); |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 1442 | |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1443 | } |
| 1444 | |
Dan Williams | ba9c8dd | 2016-08-22 19:28:37 -0700 | [diff] [blame] | 1445 | rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count); |
| 1446 | if (rc) |
| 1447 | return rc; |
| 1448 | |
| 1449 | /* |
| 1450 | * Now that dimms are successfully registered, and async registration |
| 1451 | * is flushed, attempt to enable event notification. |
| 1452 | */ |
| 1453 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) { |
| 1454 | struct kernfs_node *nfit_kernfs; |
| 1455 | |
| 1456 | nvdimm = nfit_mem->nvdimm; |
| 1457 | nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit"); |
| 1458 | if (nfit_kernfs) |
| 1459 | nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs, |
| 1460 | "flags"); |
| 1461 | sysfs_put(nfit_kernfs); |
| 1462 | if (!nfit_mem->flags_attr) |
| 1463 | dev_warn(acpi_desc->dev, "%s: notifications disabled\n", |
| 1464 | nvdimm_name(nvdimm)); |
| 1465 | } |
| 1466 | |
| 1467 | return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify, |
| 1468 | acpi_desc); |
Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1471 | static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc) |
| 1472 | { |
| 1473 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; |
| 1474 | const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS); |
| 1475 | struct acpi_device *adev; |
| 1476 | int i; |
| 1477 | |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 1478 | nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en; |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1479 | adev = to_acpi_dev(acpi_desc); |
| 1480 | if (!adev) |
| 1481 | return; |
| 1482 | |
Dan Williams | d4f3236 | 2016-03-03 16:08:54 -0800 | [diff] [blame] | 1483 | for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++) |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1484 | if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i)) |
Dan Williams | e3654ec | 2016-04-28 16:17:07 -0700 | [diff] [blame] | 1485 | set_bit(i, &nd_desc->cmd_mask); |
Dan Williams | 62232e45 | 2015-06-08 14:27:06 -0400 | [diff] [blame] | 1486 | } |
| 1487 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 1488 | static ssize_t range_index_show(struct device *dev, |
| 1489 | struct device_attribute *attr, char *buf) |
| 1490 | { |
| 1491 | struct nd_region *nd_region = to_nd_region(dev); |
| 1492 | struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region); |
| 1493 | |
| 1494 | return sprintf(buf, "%d\n", nfit_spa->spa->range_index); |
| 1495 | } |
| 1496 | static DEVICE_ATTR_RO(range_index); |
| 1497 | |
| 1498 | static struct attribute *acpi_nfit_region_attributes[] = { |
| 1499 | &dev_attr_range_index.attr, |
| 1500 | NULL, |
| 1501 | }; |
| 1502 | |
| 1503 | static struct attribute_group acpi_nfit_region_attribute_group = { |
| 1504 | .name = "nfit", |
| 1505 | .attrs = acpi_nfit_region_attributes, |
| 1506 | }; |
| 1507 | |
| 1508 | static const struct attribute_group *acpi_nfit_region_attribute_groups[] = { |
| 1509 | &nd_region_attribute_group, |
| 1510 | &nd_mapping_attribute_group, |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 1511 | &nd_device_attribute_group, |
Toshi Kani | 74ae66c | 2015-06-19 12:18:34 -0600 | [diff] [blame] | 1512 | &nd_numa_attribute_group, |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 1513 | &acpi_nfit_region_attribute_group, |
| 1514 | NULL, |
| 1515 | }; |
| 1516 | |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 1517 | /* enough info to uniquely specify an interleave set */ |
| 1518 | struct nfit_set_info { |
| 1519 | struct nfit_set_info_map { |
| 1520 | u64 region_offset; |
| 1521 | u32 serial_number; |
| 1522 | u32 pad; |
| 1523 | } mapping[0]; |
| 1524 | }; |
| 1525 | |
| 1526 | static size_t sizeof_nfit_set_info(int num_mappings) |
| 1527 | { |
| 1528 | return sizeof(struct nfit_set_info) |
| 1529 | + num_mappings * sizeof(struct nfit_set_info_map); |
| 1530 | } |
| 1531 | |
| 1532 | static int cmp_map(const void *m0, const void *m1) |
| 1533 | { |
| 1534 | const struct nfit_set_info_map *map0 = m0; |
| 1535 | const struct nfit_set_info_map *map1 = m1; |
| 1536 | |
| 1537 | return memcmp(&map0->region_offset, &map1->region_offset, |
| 1538 | sizeof(u64)); |
| 1539 | } |
| 1540 | |
| 1541 | /* Retrieve the nth entry referencing this spa */ |
| 1542 | static struct acpi_nfit_memory_map *memdev_from_spa( |
| 1543 | struct acpi_nfit_desc *acpi_desc, u16 range_index, int n) |
| 1544 | { |
| 1545 | struct nfit_memdev *nfit_memdev; |
| 1546 | |
| 1547 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) |
| 1548 | if (nfit_memdev->memdev->range_index == range_index) |
| 1549 | if (n-- == 0) |
| 1550 | return nfit_memdev->memdev; |
| 1551 | return NULL; |
| 1552 | } |
| 1553 | |
| 1554 | static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc, |
| 1555 | struct nd_region_desc *ndr_desc, |
| 1556 | struct acpi_nfit_system_address *spa) |
| 1557 | { |
| 1558 | int i, spa_type = nfit_spa_type(spa); |
| 1559 | struct device *dev = acpi_desc->dev; |
| 1560 | struct nd_interleave_set *nd_set; |
| 1561 | u16 nr = ndr_desc->num_mappings; |
| 1562 | struct nfit_set_info *info; |
| 1563 | |
| 1564 | if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE) |
| 1565 | /* pass */; |
| 1566 | else |
| 1567 | return 0; |
| 1568 | |
| 1569 | nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL); |
| 1570 | if (!nd_set) |
| 1571 | return -ENOMEM; |
| 1572 | |
| 1573 | info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL); |
| 1574 | if (!info) |
| 1575 | return -ENOMEM; |
| 1576 | for (i = 0; i < nr; i++) { |
| 1577 | struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i]; |
| 1578 | struct nfit_set_info_map *map = &info->mapping[i]; |
| 1579 | struct nvdimm *nvdimm = nd_mapping->nvdimm; |
| 1580 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); |
| 1581 | struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc, |
| 1582 | spa->range_index, i); |
| 1583 | |
| 1584 | if (!memdev || !nfit_mem->dcr) { |
| 1585 | dev_err(dev, "%s: failed to find DCR\n", __func__); |
| 1586 | return -ENODEV; |
| 1587 | } |
| 1588 | |
| 1589 | map->region_offset = memdev->region_offset; |
| 1590 | map->serial_number = nfit_mem->dcr->serial_number; |
| 1591 | } |
| 1592 | |
| 1593 | sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map), |
| 1594 | cmp_map, NULL); |
| 1595 | nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0); |
| 1596 | ndr_desc->nd_set = nd_set; |
| 1597 | devm_kfree(dev, info); |
| 1598 | |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1602 | static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio) |
| 1603 | { |
| 1604 | struct acpi_nfit_interleave *idt = mmio->idt; |
| 1605 | u32 sub_line_offset, line_index, line_offset; |
| 1606 | u64 line_no, table_skip_count, table_offset; |
| 1607 | |
| 1608 | line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset); |
| 1609 | table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index); |
| 1610 | line_offset = idt->line_offset[line_index] |
| 1611 | * mmio->line_size; |
| 1612 | table_offset = table_skip_count * mmio->table_size; |
| 1613 | |
| 1614 | return mmio->base_offset + line_offset + table_offset + sub_line_offset; |
| 1615 | } |
| 1616 | |
Ross Zwisler | de4a196 | 2015-08-20 16:27:38 -0600 | [diff] [blame] | 1617 | static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1618 | { |
| 1619 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR]; |
| 1620 | u64 offset = nfit_blk->stat_offset + mmio->size * bw; |
Ross Zwisler | 68202c9 | 2016-07-29 14:59:12 -0600 | [diff] [blame] | 1621 | const u32 STATUS_MASK = 0x80000037; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1622 | |
| 1623 | if (mmio->num_lines) |
| 1624 | offset = to_interleave_offset(offset, mmio); |
| 1625 | |
Ross Zwisler | 68202c9 | 2016-07-29 14:59:12 -0600 | [diff] [blame] | 1626 | return readl(mmio->addr.base + offset) & STATUS_MASK; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw, |
| 1630 | resource_size_t dpa, unsigned int len, unsigned int write) |
| 1631 | { |
| 1632 | u64 cmd, offset; |
| 1633 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR]; |
| 1634 | |
| 1635 | enum { |
| 1636 | BCW_OFFSET_MASK = (1ULL << 48)-1, |
| 1637 | BCW_LEN_SHIFT = 48, |
| 1638 | BCW_LEN_MASK = (1ULL << 8) - 1, |
| 1639 | BCW_CMD_SHIFT = 56, |
| 1640 | }; |
| 1641 | |
| 1642 | cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK; |
| 1643 | len = len >> L1_CACHE_SHIFT; |
| 1644 | cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT; |
| 1645 | cmd |= ((u64) write) << BCW_CMD_SHIFT; |
| 1646 | |
| 1647 | offset = nfit_blk->cmd_offset + mmio->size * bw; |
| 1648 | if (mmio->num_lines) |
| 1649 | offset = to_interleave_offset(offset, mmio); |
| 1650 | |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1651 | writeq(cmd, mmio->addr.base + offset); |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 1652 | nvdimm_flush(nfit_blk->nd_region); |
Ross Zwisler | f0f2c07 | 2015-07-10 11:06:14 -0600 | [diff] [blame] | 1653 | |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1654 | if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH) |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1655 | readq(mmio->addr.base + offset); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk, |
| 1659 | resource_size_t dpa, void *iobuf, size_t len, int rw, |
| 1660 | unsigned int lane) |
| 1661 | { |
| 1662 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW]; |
| 1663 | unsigned int copied = 0; |
| 1664 | u64 base_offset; |
| 1665 | int rc; |
| 1666 | |
| 1667 | base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES |
| 1668 | + lane * mmio->size; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1669 | write_blk_ctl(nfit_blk, lane, dpa, len, rw); |
| 1670 | while (len) { |
| 1671 | unsigned int c; |
| 1672 | u64 offset; |
| 1673 | |
| 1674 | if (mmio->num_lines) { |
| 1675 | u32 line_offset; |
| 1676 | |
| 1677 | offset = to_interleave_offset(base_offset + copied, |
| 1678 | mmio); |
| 1679 | div_u64_rem(offset, mmio->line_size, &line_offset); |
| 1680 | c = min_t(size_t, len, mmio->line_size - line_offset); |
| 1681 | } else { |
| 1682 | offset = base_offset + nfit_blk->bdw_offset; |
| 1683 | c = len; |
| 1684 | } |
| 1685 | |
| 1686 | if (rw) |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1687 | memcpy_to_pmem(mmio->addr.aperture + offset, |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 1688 | iobuf + copied, c); |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1689 | else { |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1690 | if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH) |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1691 | mmio_flush_range((void __force *) |
| 1692 | mmio->addr.aperture + offset, c); |
| 1693 | |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 1694 | memcpy_from_pmem(iobuf + copied, |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1695 | mmio->addr.aperture + offset, c); |
| 1696 | } |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1697 | |
| 1698 | copied += c; |
| 1699 | len -= c; |
| 1700 | } |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 1701 | |
| 1702 | if (rw) |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 1703 | nvdimm_flush(nfit_blk->nd_region); |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 1704 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1705 | rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0; |
| 1706 | return rc; |
| 1707 | } |
| 1708 | |
| 1709 | static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr, |
| 1710 | resource_size_t dpa, void *iobuf, u64 len, int rw) |
| 1711 | { |
| 1712 | struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr); |
| 1713 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW]; |
| 1714 | struct nd_region *nd_region = nfit_blk->nd_region; |
| 1715 | unsigned int lane, copied = 0; |
| 1716 | int rc = 0; |
| 1717 | |
| 1718 | lane = nd_region_acquire_lane(nd_region); |
| 1719 | while (len) { |
| 1720 | u64 c = min(len, mmio->size); |
| 1721 | |
| 1722 | rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied, |
| 1723 | iobuf + copied, c, rw, lane); |
| 1724 | if (rc) |
| 1725 | break; |
| 1726 | |
| 1727 | copied += c; |
| 1728 | len -= c; |
| 1729 | } |
| 1730 | nd_region_release_lane(nd_region, lane); |
| 1731 | |
| 1732 | return rc; |
| 1733 | } |
| 1734 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1735 | static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio, |
| 1736 | struct acpi_nfit_interleave *idt, u16 interleave_ways) |
| 1737 | { |
| 1738 | if (idt) { |
| 1739 | mmio->num_lines = idt->line_count; |
| 1740 | mmio->line_size = idt->line_size; |
| 1741 | if (interleave_ways == 0) |
| 1742 | return -ENXIO; |
| 1743 | mmio->table_size = mmio->num_lines * interleave_ways |
| 1744 | * mmio->line_size; |
| 1745 | } |
| 1746 | |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
Ross Zwisler | f0f2c07 | 2015-07-10 11:06:14 -0600 | [diff] [blame] | 1750 | static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc, |
| 1751 | struct nvdimm *nvdimm, struct nfit_blk *nfit_blk) |
| 1752 | { |
| 1753 | struct nd_cmd_dimm_flags flags; |
| 1754 | int rc; |
| 1755 | |
| 1756 | memset(&flags, 0, sizeof(flags)); |
| 1757 | rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags, |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1758 | sizeof(flags), NULL); |
Ross Zwisler | f0f2c07 | 2015-07-10 11:06:14 -0600 | [diff] [blame] | 1759 | |
| 1760 | if (rc >= 0 && flags.status == 0) |
| 1761 | nfit_blk->dimm_flags = flags.flags; |
| 1762 | else if (rc == -ENOTTY) { |
| 1763 | /* fall back to a conservative default */ |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1764 | nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH; |
Ross Zwisler | f0f2c07 | 2015-07-10 11:06:14 -0600 | [diff] [blame] | 1765 | rc = 0; |
| 1766 | } else |
| 1767 | rc = -ENXIO; |
| 1768 | |
| 1769 | return rc; |
| 1770 | } |
| 1771 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1772 | static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus, |
| 1773 | struct device *dev) |
| 1774 | { |
| 1775 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1776 | struct nd_blk_region *ndbr = to_nd_blk_region(dev); |
| 1777 | struct nfit_blk_mmio *mmio; |
| 1778 | struct nfit_blk *nfit_blk; |
| 1779 | struct nfit_mem *nfit_mem; |
| 1780 | struct nvdimm *nvdimm; |
| 1781 | int rc; |
| 1782 | |
| 1783 | nvdimm = nd_blk_region_to_dimm(ndbr); |
| 1784 | nfit_mem = nvdimm_provider_data(nvdimm); |
| 1785 | if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) { |
| 1786 | dev_dbg(dev, "%s: missing%s%s%s\n", __func__, |
| 1787 | nfit_mem ? "" : " nfit_mem", |
Dan Williams | 193ccca | 2015-06-30 16:09:39 -0400 | [diff] [blame] | 1788 | (nfit_mem && nfit_mem->dcr) ? "" : " dcr", |
| 1789 | (nfit_mem && nfit_mem->bdw) ? "" : " bdw"); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1790 | return -ENXIO; |
| 1791 | } |
| 1792 | |
| 1793 | nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL); |
| 1794 | if (!nfit_blk) |
| 1795 | return -ENOMEM; |
| 1796 | nd_blk_region_set_provider_data(ndbr, nfit_blk); |
| 1797 | nfit_blk->nd_region = to_nd_region(dev); |
| 1798 | |
| 1799 | /* map block aperture memory */ |
| 1800 | nfit_blk->bdw_offset = nfit_mem->bdw->offset; |
| 1801 | mmio = &nfit_blk->mmio[BDW]; |
Dan Williams | 29b9aa0 | 2016-06-06 17:42:38 -0700 | [diff] [blame] | 1802 | mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address, |
| 1803 | nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM); |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1804 | if (!mmio->addr.base) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1805 | dev_dbg(dev, "%s: %s failed to map bdw\n", __func__, |
| 1806 | nvdimm_name(nvdimm)); |
| 1807 | return -ENOMEM; |
| 1808 | } |
| 1809 | mmio->size = nfit_mem->bdw->size; |
| 1810 | mmio->base_offset = nfit_mem->memdev_bdw->region_offset; |
| 1811 | mmio->idt = nfit_mem->idt_bdw; |
| 1812 | mmio->spa = nfit_mem->spa_bdw; |
| 1813 | rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw, |
| 1814 | nfit_mem->memdev_bdw->interleave_ways); |
| 1815 | if (rc) { |
| 1816 | dev_dbg(dev, "%s: %s failed to init bdw interleave\n", |
| 1817 | __func__, nvdimm_name(nvdimm)); |
| 1818 | return rc; |
| 1819 | } |
| 1820 | |
| 1821 | /* map block control memory */ |
| 1822 | nfit_blk->cmd_offset = nfit_mem->dcr->command_offset; |
| 1823 | nfit_blk->stat_offset = nfit_mem->dcr->status_offset; |
| 1824 | mmio = &nfit_blk->mmio[DCR]; |
Dan Williams | 29b9aa0 | 2016-06-06 17:42:38 -0700 | [diff] [blame] | 1825 | mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address, |
| 1826 | nfit_mem->spa_dcr->length); |
Ross Zwisler | 67a3e8f | 2015-08-27 13:14:20 -0600 | [diff] [blame] | 1827 | if (!mmio->addr.base) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1828 | dev_dbg(dev, "%s: %s failed to map dcr\n", __func__, |
| 1829 | nvdimm_name(nvdimm)); |
| 1830 | return -ENOMEM; |
| 1831 | } |
| 1832 | mmio->size = nfit_mem->dcr->window_size; |
| 1833 | mmio->base_offset = nfit_mem->memdev_dcr->region_offset; |
| 1834 | mmio->idt = nfit_mem->idt_dcr; |
| 1835 | mmio->spa = nfit_mem->spa_dcr; |
| 1836 | rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr, |
| 1837 | nfit_mem->memdev_dcr->interleave_ways); |
| 1838 | if (rc) { |
| 1839 | dev_dbg(dev, "%s: %s failed to init dcr interleave\n", |
| 1840 | __func__, nvdimm_name(nvdimm)); |
| 1841 | return rc; |
| 1842 | } |
| 1843 | |
Ross Zwisler | f0f2c07 | 2015-07-10 11:06:14 -0600 | [diff] [blame] | 1844 | rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk); |
| 1845 | if (rc < 0) { |
| 1846 | dev_dbg(dev, "%s: %s failed get DIMM flags\n", |
| 1847 | __func__, nvdimm_name(nvdimm)); |
| 1848 | return rc; |
| 1849 | } |
| 1850 | |
Dan Williams | f284a4f | 2016-07-07 19:44:50 -0700 | [diff] [blame] | 1851 | if (nvdimm_has_flush(nfit_blk->nd_region) < 0) |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 1852 | dev_warn(dev, "unable to guarantee persistence of writes\n"); |
| 1853 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 1854 | if (mmio->line_size == 0) |
| 1855 | return 0; |
| 1856 | |
| 1857 | if ((u32) nfit_blk->cmd_offset % mmio->line_size |
| 1858 | + 8 > mmio->line_size) { |
| 1859 | dev_dbg(dev, "cmd_offset crosses interleave boundary\n"); |
| 1860 | return -ENXIO; |
| 1861 | } else if ((u32) nfit_blk->stat_offset % mmio->line_size |
| 1862 | + 8 > mmio->line_size) { |
| 1863 | dev_dbg(dev, "stat_offset crosses interleave boundary\n"); |
| 1864 | return -ENXIO; |
| 1865 | } |
| 1866 | |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1870 | static int ars_get_cap(struct acpi_nfit_desc *acpi_desc, |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1871 | struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1872 | { |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1873 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1874 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1875 | int cmd_rc, rc; |
| 1876 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1877 | cmd->address = spa->address; |
| 1878 | cmd->length = spa->length; |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1879 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd, |
| 1880 | sizeof(*cmd), &cmd_rc); |
| 1881 | if (rc < 0) |
| 1882 | return rc; |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1883 | return cmd_rc; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1886 | static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1887 | { |
| 1888 | int rc; |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1889 | int cmd_rc; |
| 1890 | struct nd_cmd_ars_start ars_start; |
| 1891 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
| 1892 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1893 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1894 | memset(&ars_start, 0, sizeof(ars_start)); |
| 1895 | ars_start.address = spa->address; |
| 1896 | ars_start.length = spa->length; |
| 1897 | if (nfit_spa_type(spa) == NFIT_SPA_PM) |
| 1898 | ars_start.type = ND_ARS_PERSISTENT; |
| 1899 | else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) |
| 1900 | ars_start.type = ND_ARS_VOLATILE; |
| 1901 | else |
| 1902 | return -ENOTTY; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1903 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1904 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start, |
| 1905 | sizeof(ars_start), &cmd_rc); |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1906 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1907 | if (rc < 0) |
| 1908 | return rc; |
| 1909 | return cmd_rc; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1912 | static int ars_continue(struct acpi_nfit_desc *acpi_desc) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1913 | { |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1914 | int rc, cmd_rc; |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1915 | struct nd_cmd_ars_start ars_start; |
| 1916 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; |
| 1917 | struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1918 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1919 | memset(&ars_start, 0, sizeof(ars_start)); |
| 1920 | ars_start.address = ars_status->restart_address; |
| 1921 | ars_start.length = ars_status->restart_length; |
| 1922 | ars_start.type = ars_status->type; |
| 1923 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start, |
| 1924 | sizeof(ars_start), &cmd_rc); |
| 1925 | if (rc < 0) |
| 1926 | return rc; |
| 1927 | return cmd_rc; |
| 1928 | } |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1929 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1930 | static int ars_get_status(struct acpi_nfit_desc *acpi_desc) |
| 1931 | { |
| 1932 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; |
| 1933 | struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status; |
| 1934 | int rc, cmd_rc; |
Dan Williams | aef2533 | 2016-02-12 17:01:11 -0800 | [diff] [blame] | 1935 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1936 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status, |
| 1937 | acpi_desc->ars_status_size, &cmd_rc); |
| 1938 | if (rc < 0) |
| 1939 | return rc; |
| 1940 | return cmd_rc; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus, |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 1944 | struct nd_cmd_ars_status *ars_status) |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1945 | { |
| 1946 | int rc; |
| 1947 | u32 i; |
| 1948 | |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 1949 | for (i = 0; i < ars_status->num_records; i++) { |
| 1950 | rc = nvdimm_bus_add_poison(nvdimm_bus, |
| 1951 | ars_status->records[i].err_address, |
| 1952 | ars_status->records[i].length); |
| 1953 | if (rc) |
| 1954 | return rc; |
| 1955 | } |
| 1956 | |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
Toshi Kani | af1996e | 2016-03-09 12:47:06 -0700 | [diff] [blame] | 1960 | static void acpi_nfit_remove_resource(void *data) |
| 1961 | { |
| 1962 | struct resource *res = data; |
| 1963 | |
| 1964 | remove_resource(res); |
| 1965 | } |
| 1966 | |
| 1967 | static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc, |
| 1968 | struct nd_region_desc *ndr_desc) |
| 1969 | { |
| 1970 | struct resource *res, *nd_res = ndr_desc->res; |
| 1971 | int is_pmem, ret; |
| 1972 | |
| 1973 | /* No operation if the region is already registered as PMEM */ |
| 1974 | is_pmem = region_intersects(nd_res->start, resource_size(nd_res), |
| 1975 | IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY); |
| 1976 | if (is_pmem == REGION_INTERSECTS) |
| 1977 | return 0; |
| 1978 | |
| 1979 | res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL); |
| 1980 | if (!res) |
| 1981 | return -ENOMEM; |
| 1982 | |
| 1983 | res->name = "Persistent Memory"; |
| 1984 | res->start = nd_res->start; |
| 1985 | res->end = nd_res->end; |
| 1986 | res->flags = IORESOURCE_MEM; |
| 1987 | res->desc = IORES_DESC_PERSISTENT_MEMORY; |
| 1988 | |
| 1989 | ret = insert_resource(&iomem_resource, res); |
| 1990 | if (ret) |
| 1991 | return ret; |
| 1992 | |
Sajjan, Vikas C | d932dd2 | 2016-07-04 10:02:51 +0530 | [diff] [blame] | 1993 | ret = devm_add_action_or_reset(acpi_desc->dev, |
| 1994 | acpi_nfit_remove_resource, |
| 1995 | res); |
| 1996 | if (ret) |
Toshi Kani | af1996e | 2016-03-09 12:47:06 -0700 | [diff] [blame] | 1997 | return ret; |
Toshi Kani | af1996e | 2016-03-09 12:47:06 -0700 | [diff] [blame] | 1998 | |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2002 | static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc, |
| 2003 | struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc, |
| 2004 | struct acpi_nfit_memory_map *memdev, |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2005 | struct nfit_spa *nfit_spa) |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2006 | { |
| 2007 | struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, |
| 2008 | memdev->device_handle); |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2009 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2010 | struct nd_blk_region_desc *ndbr_desc; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2011 | struct nfit_mem *nfit_mem; |
| 2012 | int blk_valid = 0; |
| 2013 | |
| 2014 | if (!nvdimm) { |
| 2015 | dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n", |
| 2016 | spa->range_index, memdev->device_handle); |
| 2017 | return -ENODEV; |
| 2018 | } |
| 2019 | |
| 2020 | nd_mapping->nvdimm = nvdimm; |
| 2021 | switch (nfit_spa_type(spa)) { |
| 2022 | case NFIT_SPA_PM: |
| 2023 | case NFIT_SPA_VOLATILE: |
| 2024 | nd_mapping->start = memdev->address; |
| 2025 | nd_mapping->size = memdev->region_size; |
| 2026 | break; |
| 2027 | case NFIT_SPA_DCR: |
| 2028 | nfit_mem = nvdimm_provider_data(nvdimm); |
| 2029 | if (!nfit_mem || !nfit_mem->bdw) { |
| 2030 | dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n", |
| 2031 | spa->range_index, nvdimm_name(nvdimm)); |
| 2032 | } else { |
| 2033 | nd_mapping->size = nfit_mem->bdw->capacity; |
| 2034 | nd_mapping->start = nfit_mem->bdw->start_address; |
Vishal Verma | 5212e11 | 2015-06-25 04:20:32 -0400 | [diff] [blame] | 2035 | ndr_desc->num_lanes = nfit_mem->bdw->windows; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2036 | blk_valid = 1; |
| 2037 | } |
| 2038 | |
| 2039 | ndr_desc->nd_mapping = nd_mapping; |
| 2040 | ndr_desc->num_mappings = blk_valid; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2041 | ndbr_desc = to_blk_region_desc(ndr_desc); |
| 2042 | ndbr_desc->enable = acpi_nfit_blk_region_enable; |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 2043 | ndbr_desc->do_io = acpi_desc->blk_do_io; |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2044 | nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus, |
| 2045 | ndr_desc); |
| 2046 | if (!nfit_spa->nd_region) |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2047 | return -ENOMEM; |
| 2048 | break; |
| 2049 | } |
| 2050 | |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
Lee, Chun-Yi | c2f32ac | 2016-07-15 12:05:35 +0800 | [diff] [blame] | 2054 | static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa) |
| 2055 | { |
| 2056 | return (nfit_spa_type(spa) == NFIT_SPA_VDISK || |
| 2057 | nfit_spa_type(spa) == NFIT_SPA_VCD || |
| 2058 | nfit_spa_type(spa) == NFIT_SPA_PDISK || |
| 2059 | nfit_spa_type(spa) == NFIT_SPA_PCD); |
| 2060 | } |
| 2061 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2062 | static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc, |
| 2063 | struct nfit_spa *nfit_spa) |
| 2064 | { |
| 2065 | static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS]; |
| 2066 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2067 | struct nd_blk_region_desc ndbr_desc; |
| 2068 | struct nd_region_desc *ndr_desc; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2069 | struct nfit_memdev *nfit_memdev; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2070 | struct nvdimm_bus *nvdimm_bus; |
| 2071 | struct resource res; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 2072 | int count = 0, rc; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2073 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2074 | if (nfit_spa->nd_region) |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2075 | return 0; |
| 2076 | |
Lee, Chun-Yi | c2f32ac | 2016-07-15 12:05:35 +0800 | [diff] [blame] | 2077 | if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) { |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2078 | dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n", |
| 2079 | __func__); |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
| 2083 | memset(&res, 0, sizeof(res)); |
| 2084 | memset(&nd_mappings, 0, sizeof(nd_mappings)); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2085 | memset(&ndbr_desc, 0, sizeof(ndbr_desc)); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2086 | res.start = spa->address; |
| 2087 | res.end = res.start + spa->length - 1; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2088 | ndr_desc = &ndbr_desc.ndr_desc; |
| 2089 | ndr_desc->res = &res; |
| 2090 | ndr_desc->provider_data = nfit_spa; |
| 2091 | ndr_desc->attr_groups = acpi_nfit_region_attribute_groups; |
Toshi Kani | 41d7a6d | 2015-06-19 12:18:33 -0600 | [diff] [blame] | 2092 | if (spa->flags & ACPI_NFIT_PROXIMITY_VALID) |
| 2093 | ndr_desc->numa_node = acpi_map_pxm_to_online_node( |
| 2094 | spa->proximity_domain); |
| 2095 | else |
| 2096 | ndr_desc->numa_node = NUMA_NO_NODE; |
| 2097 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2098 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { |
| 2099 | struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev; |
| 2100 | struct nd_mapping *nd_mapping; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2101 | |
| 2102 | if (memdev->range_index != spa->range_index) |
| 2103 | continue; |
| 2104 | if (count >= ND_MAX_MAPPINGS) { |
| 2105 | dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n", |
| 2106 | spa->range_index, ND_MAX_MAPPINGS); |
| 2107 | return -ENXIO; |
| 2108 | } |
| 2109 | nd_mapping = &nd_mappings[count++]; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2110 | rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc, |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2111 | memdev, nfit_spa); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2112 | if (rc) |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2113 | goto out; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2114 | } |
| 2115 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2116 | ndr_desc->nd_mapping = nd_mappings; |
| 2117 | ndr_desc->num_mappings = count; |
| 2118 | rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa); |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 2119 | if (rc) |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2120 | goto out; |
Dan Williams | eaf9615 | 2015-05-01 13:11:27 -0400 | [diff] [blame] | 2121 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2122 | nvdimm_bus = acpi_desc->nvdimm_bus; |
| 2123 | if (nfit_spa_type(spa) == NFIT_SPA_PM) { |
Toshi Kani | af1996e | 2016-03-09 12:47:06 -0700 | [diff] [blame] | 2124 | rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc); |
Dan Williams | 4890116 | 2016-03-09 17:15:43 -0800 | [diff] [blame] | 2125 | if (rc) { |
Toshi Kani | af1996e | 2016-03-09 12:47:06 -0700 | [diff] [blame] | 2126 | dev_warn(acpi_desc->dev, |
| 2127 | "failed to insert pmem resource to iomem: %d\n", |
| 2128 | rc); |
Dan Williams | 4890116 | 2016-03-09 17:15:43 -0800 | [diff] [blame] | 2129 | goto out; |
Vishal Verma | 0caeef6 | 2015-12-24 19:21:43 -0700 | [diff] [blame] | 2130 | } |
Dan Williams | 4890116 | 2016-03-09 17:15:43 -0800 | [diff] [blame] | 2131 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2132 | nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus, |
| 2133 | ndr_desc); |
| 2134 | if (!nfit_spa->nd_region) |
| 2135 | rc = -ENOMEM; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2136 | } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) { |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2137 | nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus, |
| 2138 | ndr_desc); |
| 2139 | if (!nfit_spa->nd_region) |
| 2140 | rc = -ENOMEM; |
Lee, Chun-Yi | c2f32ac | 2016-07-15 12:05:35 +0800 | [diff] [blame] | 2141 | } else if (nfit_spa_is_virtual(spa)) { |
| 2142 | nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus, |
| 2143 | ndr_desc); |
| 2144 | if (!nfit_spa->nd_region) |
| 2145 | rc = -ENOMEM; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2146 | } |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2147 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2148 | out: |
| 2149 | if (rc) |
| 2150 | dev_err(acpi_desc->dev, "failed to register spa range %d\n", |
| 2151 | nfit_spa->spa->range_index); |
| 2152 | return rc; |
| 2153 | } |
| 2154 | |
| 2155 | static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc, |
| 2156 | u32 max_ars) |
| 2157 | { |
| 2158 | struct device *dev = acpi_desc->dev; |
| 2159 | struct nd_cmd_ars_status *ars_status; |
| 2160 | |
| 2161 | if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) { |
| 2162 | memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size); |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | if (acpi_desc->ars_status) |
| 2167 | devm_kfree(dev, acpi_desc->ars_status); |
| 2168 | acpi_desc->ars_status = NULL; |
| 2169 | ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL); |
| 2170 | if (!ars_status) |
| 2171 | return -ENOMEM; |
| 2172 | acpi_desc->ars_status = ars_status; |
| 2173 | acpi_desc->ars_status_size = max_ars; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2174 | return 0; |
| 2175 | } |
| 2176 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2177 | static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc, |
| 2178 | struct nfit_spa *nfit_spa) |
| 2179 | { |
| 2180 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
| 2181 | int rc; |
| 2182 | |
| 2183 | if (!nfit_spa->max_ars) { |
| 2184 | struct nd_cmd_ars_cap ars_cap; |
| 2185 | |
| 2186 | memset(&ars_cap, 0, sizeof(ars_cap)); |
| 2187 | rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa); |
| 2188 | if (rc < 0) |
| 2189 | return rc; |
| 2190 | nfit_spa->max_ars = ars_cap.max_ars_out; |
| 2191 | nfit_spa->clear_err_unit = ars_cap.clear_err_unit; |
| 2192 | /* check that the supported scrub types match the spa type */ |
| 2193 | if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE && |
| 2194 | ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0) |
| 2195 | return -ENOTTY; |
| 2196 | else if (nfit_spa_type(spa) == NFIT_SPA_PM && |
| 2197 | ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0) |
| 2198 | return -ENOTTY; |
| 2199 | } |
| 2200 | |
| 2201 | if (ars_status_alloc(acpi_desc, nfit_spa->max_ars)) |
| 2202 | return -ENOMEM; |
| 2203 | |
| 2204 | rc = ars_get_status(acpi_desc); |
| 2205 | if (rc < 0 && rc != -ENOSPC) |
| 2206 | return rc; |
| 2207 | |
| 2208 | if (ars_status_process_records(acpi_desc->nvdimm_bus, |
| 2209 | acpi_desc->ars_status)) |
| 2210 | return -ENOMEM; |
| 2211 | |
| 2212 | return 0; |
| 2213 | } |
| 2214 | |
| 2215 | static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc, |
| 2216 | struct nfit_spa *nfit_spa) |
| 2217 | { |
| 2218 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
| 2219 | unsigned int overflow_retry = scrub_overflow_abort; |
| 2220 | u64 init_ars_start = 0, init_ars_len = 0; |
| 2221 | struct device *dev = acpi_desc->dev; |
| 2222 | unsigned int tmo = scrub_timeout; |
| 2223 | int rc; |
| 2224 | |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2225 | if (!nfit_spa->ars_required || !nfit_spa->nd_region) |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2226 | return; |
| 2227 | |
| 2228 | rc = ars_start(acpi_desc, nfit_spa); |
| 2229 | /* |
| 2230 | * If we timed out the initial scan we'll still be busy here, |
| 2231 | * and will wait another timeout before giving up permanently. |
| 2232 | */ |
| 2233 | if (rc < 0 && rc != -EBUSY) |
| 2234 | return; |
| 2235 | |
| 2236 | do { |
| 2237 | u64 ars_start, ars_len; |
| 2238 | |
| 2239 | if (acpi_desc->cancel) |
| 2240 | break; |
| 2241 | rc = acpi_nfit_query_poison(acpi_desc, nfit_spa); |
| 2242 | if (rc == -ENOTTY) |
| 2243 | break; |
| 2244 | if (rc == -EBUSY && !tmo) { |
| 2245 | dev_warn(dev, "range %d ars timeout, aborting\n", |
| 2246 | spa->range_index); |
| 2247 | break; |
| 2248 | } |
| 2249 | |
| 2250 | if (rc == -EBUSY) { |
| 2251 | /* |
| 2252 | * Note, entries may be appended to the list |
| 2253 | * while the lock is dropped, but the workqueue |
| 2254 | * being active prevents entries being deleted / |
| 2255 | * freed. |
| 2256 | */ |
| 2257 | mutex_unlock(&acpi_desc->init_mutex); |
| 2258 | ssleep(1); |
| 2259 | tmo--; |
| 2260 | mutex_lock(&acpi_desc->init_mutex); |
| 2261 | continue; |
| 2262 | } |
| 2263 | |
| 2264 | /* we got some results, but there are more pending... */ |
| 2265 | if (rc == -ENOSPC && overflow_retry--) { |
| 2266 | if (!init_ars_len) { |
| 2267 | init_ars_len = acpi_desc->ars_status->length; |
| 2268 | init_ars_start = acpi_desc->ars_status->address; |
| 2269 | } |
| 2270 | rc = ars_continue(acpi_desc); |
| 2271 | } |
| 2272 | |
| 2273 | if (rc < 0) { |
| 2274 | dev_warn(dev, "range %d ars continuation failed\n", |
| 2275 | spa->range_index); |
| 2276 | break; |
| 2277 | } |
| 2278 | |
| 2279 | if (init_ars_len) { |
| 2280 | ars_start = init_ars_start; |
| 2281 | ars_len = init_ars_len; |
| 2282 | } else { |
| 2283 | ars_start = acpi_desc->ars_status->address; |
| 2284 | ars_len = acpi_desc->ars_status->length; |
| 2285 | } |
| 2286 | dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n", |
| 2287 | spa->range_index, ars_start, ars_len); |
| 2288 | /* notify the region about new poison entries */ |
| 2289 | nvdimm_region_notify(nfit_spa->nd_region, |
| 2290 | NVDIMM_REVALIDATE_POISON); |
| 2291 | break; |
| 2292 | } while (1); |
| 2293 | } |
| 2294 | |
| 2295 | static void acpi_nfit_scrub(struct work_struct *work) |
| 2296 | { |
| 2297 | struct device *dev; |
| 2298 | u64 init_scrub_length = 0; |
| 2299 | struct nfit_spa *nfit_spa; |
| 2300 | u64 init_scrub_address = 0; |
| 2301 | bool init_ars_done = false; |
| 2302 | struct acpi_nfit_desc *acpi_desc; |
| 2303 | unsigned int tmo = scrub_timeout; |
| 2304 | unsigned int overflow_retry = scrub_overflow_abort; |
| 2305 | |
| 2306 | acpi_desc = container_of(work, typeof(*acpi_desc), work); |
| 2307 | dev = acpi_desc->dev; |
| 2308 | |
| 2309 | /* |
| 2310 | * We scrub in 2 phases. The first phase waits for any platform |
| 2311 | * firmware initiated scrubs to complete and then we go search for the |
| 2312 | * affected spa regions to mark them scanned. In the second phase we |
| 2313 | * initiate a directed scrub for every range that was not scrubbed in |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2314 | * phase 1. If we're called for a 'rescan', we harmlessly pass through |
| 2315 | * the first phase, but really only care about running phase 2, where |
| 2316 | * regions can be notified of new poison. |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2317 | */ |
| 2318 | |
| 2319 | /* process platform firmware initiated scrubs */ |
| 2320 | retry: |
| 2321 | mutex_lock(&acpi_desc->init_mutex); |
| 2322 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { |
| 2323 | struct nd_cmd_ars_status *ars_status; |
| 2324 | struct acpi_nfit_system_address *spa; |
| 2325 | u64 ars_start, ars_len; |
| 2326 | int rc; |
| 2327 | |
| 2328 | if (acpi_desc->cancel) |
| 2329 | break; |
| 2330 | |
| 2331 | if (nfit_spa->nd_region) |
| 2332 | continue; |
| 2333 | |
| 2334 | if (init_ars_done) { |
| 2335 | /* |
| 2336 | * No need to re-query, we're now just |
| 2337 | * reconciling all the ranges covered by the |
| 2338 | * initial scrub |
| 2339 | */ |
| 2340 | rc = 0; |
| 2341 | } else |
| 2342 | rc = acpi_nfit_query_poison(acpi_desc, nfit_spa); |
| 2343 | |
| 2344 | if (rc == -ENOTTY) { |
| 2345 | /* no ars capability, just register spa and move on */ |
| 2346 | acpi_nfit_register_region(acpi_desc, nfit_spa); |
| 2347 | continue; |
| 2348 | } |
| 2349 | |
| 2350 | if (rc == -EBUSY && !tmo) { |
| 2351 | /* fallthrough to directed scrub in phase 2 */ |
| 2352 | dev_warn(dev, "timeout awaiting ars results, continuing...\n"); |
| 2353 | break; |
| 2354 | } else if (rc == -EBUSY) { |
| 2355 | mutex_unlock(&acpi_desc->init_mutex); |
| 2356 | ssleep(1); |
| 2357 | tmo--; |
| 2358 | goto retry; |
| 2359 | } |
| 2360 | |
| 2361 | /* we got some results, but there are more pending... */ |
| 2362 | if (rc == -ENOSPC && overflow_retry--) { |
| 2363 | ars_status = acpi_desc->ars_status; |
| 2364 | /* |
| 2365 | * Record the original scrub range, so that we |
| 2366 | * can recall all the ranges impacted by the |
| 2367 | * initial scrub. |
| 2368 | */ |
| 2369 | if (!init_scrub_length) { |
| 2370 | init_scrub_length = ars_status->length; |
| 2371 | init_scrub_address = ars_status->address; |
| 2372 | } |
| 2373 | rc = ars_continue(acpi_desc); |
| 2374 | if (rc == 0) { |
| 2375 | mutex_unlock(&acpi_desc->init_mutex); |
| 2376 | goto retry; |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | if (rc < 0) { |
| 2381 | /* |
| 2382 | * Initial scrub failed, we'll give it one more |
| 2383 | * try below... |
| 2384 | */ |
| 2385 | break; |
| 2386 | } |
| 2387 | |
| 2388 | /* We got some final results, record completed ranges */ |
| 2389 | ars_status = acpi_desc->ars_status; |
| 2390 | if (init_scrub_length) { |
| 2391 | ars_start = init_scrub_address; |
| 2392 | ars_len = ars_start + init_scrub_length; |
| 2393 | } else { |
| 2394 | ars_start = ars_status->address; |
| 2395 | ars_len = ars_status->length; |
| 2396 | } |
| 2397 | spa = nfit_spa->spa; |
| 2398 | |
| 2399 | if (!init_ars_done) { |
| 2400 | init_ars_done = true; |
| 2401 | dev_dbg(dev, "init scrub %#llx + %#llx complete\n", |
| 2402 | ars_start, ars_len); |
| 2403 | } |
| 2404 | if (ars_start <= spa->address && ars_start + ars_len |
| 2405 | >= spa->address + spa->length) |
| 2406 | acpi_nfit_register_region(acpi_desc, nfit_spa); |
| 2407 | } |
| 2408 | |
| 2409 | /* |
| 2410 | * For all the ranges not covered by an initial scrub we still |
| 2411 | * want to see if there are errors, but it's ok to discover them |
| 2412 | * asynchronously. |
| 2413 | */ |
| 2414 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { |
| 2415 | /* |
| 2416 | * Flag all the ranges that still need scrubbing, but |
| 2417 | * register them now to make data available. |
| 2418 | */ |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2419 | if (!nfit_spa->nd_region) { |
| 2420 | nfit_spa->ars_required = 1; |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2421 | acpi_nfit_register_region(acpi_desc, nfit_spa); |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2422 | } |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) |
| 2426 | acpi_nfit_async_scrub(acpi_desc, nfit_spa); |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2427 | acpi_desc->scrub_count++; |
| 2428 | if (acpi_desc->scrub_count_state) |
| 2429 | sysfs_notify_dirent(acpi_desc->scrub_count_state); |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2430 | mutex_unlock(&acpi_desc->init_mutex); |
| 2431 | } |
| 2432 | |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2433 | static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc) |
| 2434 | { |
| 2435 | struct nfit_spa *nfit_spa; |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2436 | int rc; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2437 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2438 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) |
| 2439 | if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) { |
| 2440 | /* BLK regions don't need to wait for ars results */ |
| 2441 | rc = acpi_nfit_register_region(acpi_desc, nfit_spa); |
| 2442 | if (rc) |
| 2443 | return rc; |
| 2444 | } |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2445 | |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2446 | queue_work(nfit_wq, &acpi_desc->work); |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2447 | return 0; |
| 2448 | } |
| 2449 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2450 | static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc, |
| 2451 | struct nfit_table_prev *prev) |
| 2452 | { |
| 2453 | struct device *dev = acpi_desc->dev; |
| 2454 | |
| 2455 | if (!list_empty(&prev->spas) || |
| 2456 | !list_empty(&prev->memdevs) || |
| 2457 | !list_empty(&prev->dcrs) || |
| 2458 | !list_empty(&prev->bdws) || |
| 2459 | !list_empty(&prev->idts) || |
| 2460 | !list_empty(&prev->flushes)) { |
| 2461 | dev_err(dev, "new nfit deletes entries (unsupported)\n"); |
| 2462 | return -ENXIO; |
| 2463 | } |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2467 | static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc) |
| 2468 | { |
| 2469 | struct device *dev = acpi_desc->dev; |
| 2470 | struct kernfs_node *nfit; |
| 2471 | struct device *bus_dev; |
| 2472 | |
| 2473 | if (!ars_supported(acpi_desc->nvdimm_bus)) |
| 2474 | return 0; |
| 2475 | |
| 2476 | bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus); |
| 2477 | nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit"); |
| 2478 | if (!nfit) { |
| 2479 | dev_err(dev, "sysfs_get_dirent 'nfit' failed\n"); |
| 2480 | return -ENODEV; |
| 2481 | } |
| 2482 | acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub"); |
| 2483 | sysfs_put(nfit); |
| 2484 | if (!acpi_desc->scrub_count_state) { |
| 2485 | dev_err(dev, "sysfs_get_dirent 'scrub' failed\n"); |
| 2486 | return -ENODEV; |
| 2487 | } |
| 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2492 | static void acpi_nfit_destruct(void *data) |
| 2493 | { |
| 2494 | struct acpi_nfit_desc *acpi_desc = data; |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2495 | struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus); |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2496 | |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2497 | /* |
| 2498 | * Destruct under acpi_desc_lock so that nfit_handle_mce does not |
| 2499 | * race teardown |
| 2500 | */ |
| 2501 | mutex_lock(&acpi_desc_lock); |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2502 | acpi_desc->cancel = 1; |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2503 | /* |
| 2504 | * Bounce the nvdimm bus lock to make sure any in-flight |
| 2505 | * acpi_nfit_ars_rescan() submissions have had a chance to |
| 2506 | * either submit or see ->cancel set. |
| 2507 | */ |
| 2508 | device_lock(bus_dev); |
| 2509 | device_unlock(bus_dev); |
| 2510 | |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2511 | flush_workqueue(nfit_wq); |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2512 | if (acpi_desc->scrub_count_state) |
| 2513 | sysfs_put(acpi_desc->scrub_count_state); |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2514 | nvdimm_bus_unregister(acpi_desc->nvdimm_bus); |
| 2515 | acpi_desc->nvdimm_bus = NULL; |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2516 | list_del(&acpi_desc->list); |
| 2517 | mutex_unlock(&acpi_desc_lock); |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2518 | } |
| 2519 | |
Dan Williams | e7a11b4 | 2016-07-14 16:19:55 -0700 | [diff] [blame] | 2520 | int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz) |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2521 | { |
| 2522 | struct device *dev = acpi_desc->dev; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2523 | struct nfit_table_prev prev; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2524 | const void *end; |
Dan Williams | 1f7df6f | 2015-06-09 20:13:14 -0400 | [diff] [blame] | 2525 | int rc; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2526 | |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2527 | if (!acpi_desc->nvdimm_bus) { |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2528 | acpi_nfit_init_dsms(acpi_desc); |
| 2529 | |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2530 | acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, |
| 2531 | &acpi_desc->nd_desc); |
| 2532 | if (!acpi_desc->nvdimm_bus) |
| 2533 | return -ENOMEM; |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2534 | |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2535 | rc = devm_add_action_or_reset(dev, acpi_nfit_destruct, |
| 2536 | acpi_desc); |
| 2537 | if (rc) |
| 2538 | return rc; |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2539 | |
| 2540 | rc = acpi_nfit_desc_init_scrub_attr(acpi_desc); |
| 2541 | if (rc) |
| 2542 | return rc; |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2543 | |
| 2544 | /* register this acpi_desc for mce notifications */ |
| 2545 | mutex_lock(&acpi_desc_lock); |
| 2546 | list_add_tail(&acpi_desc->list, &acpi_descs); |
| 2547 | mutex_unlock(&acpi_desc_lock); |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2550 | mutex_lock(&acpi_desc->init_mutex); |
| 2551 | |
| 2552 | INIT_LIST_HEAD(&prev.spas); |
| 2553 | INIT_LIST_HEAD(&prev.memdevs); |
| 2554 | INIT_LIST_HEAD(&prev.dcrs); |
| 2555 | INIT_LIST_HEAD(&prev.bdws); |
| 2556 | INIT_LIST_HEAD(&prev.idts); |
| 2557 | INIT_LIST_HEAD(&prev.flushes); |
| 2558 | |
| 2559 | list_cut_position(&prev.spas, &acpi_desc->spas, |
| 2560 | acpi_desc->spas.prev); |
| 2561 | list_cut_position(&prev.memdevs, &acpi_desc->memdevs, |
| 2562 | acpi_desc->memdevs.prev); |
| 2563 | list_cut_position(&prev.dcrs, &acpi_desc->dcrs, |
| 2564 | acpi_desc->dcrs.prev); |
| 2565 | list_cut_position(&prev.bdws, &acpi_desc->bdws, |
| 2566 | acpi_desc->bdws.prev); |
| 2567 | list_cut_position(&prev.idts, &acpi_desc->idts, |
| 2568 | acpi_desc->idts.prev); |
| 2569 | list_cut_position(&prev.flushes, &acpi_desc->flushes, |
| 2570 | acpi_desc->flushes.prev); |
| 2571 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2572 | end = data + sz; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2573 | while (!IS_ERR_OR_NULL(data)) |
| 2574 | data = add_table(acpi_desc, &prev, data, end); |
| 2575 | |
| 2576 | if (IS_ERR(data)) { |
| 2577 | dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__, |
| 2578 | PTR_ERR(data)); |
| 2579 | rc = PTR_ERR(data); |
| 2580 | goto out_unlock; |
| 2581 | } |
| 2582 | |
| 2583 | rc = acpi_nfit_check_deletions(acpi_desc, &prev); |
| 2584 | if (rc) |
| 2585 | goto out_unlock; |
| 2586 | |
Dan Williams | 81ed4e3 | 2016-06-10 18:20:53 -0700 | [diff] [blame] | 2587 | rc = nfit_mem_init(acpi_desc); |
| 2588 | if (rc) |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2589 | goto out_unlock; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2590 | |
| 2591 | rc = acpi_nfit_register_dimms(acpi_desc); |
| 2592 | if (rc) |
| 2593 | goto out_unlock; |
| 2594 | |
| 2595 | rc = acpi_nfit_register_regions(acpi_desc); |
| 2596 | |
| 2597 | out_unlock: |
| 2598 | mutex_unlock(&acpi_desc->init_mutex); |
| 2599 | return rc; |
| 2600 | } |
| 2601 | EXPORT_SYMBOL_GPL(acpi_nfit_init); |
| 2602 | |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 2603 | struct acpi_nfit_flush_work { |
| 2604 | struct work_struct work; |
| 2605 | struct completion cmp; |
| 2606 | }; |
| 2607 | |
| 2608 | static void flush_probe(struct work_struct *work) |
| 2609 | { |
| 2610 | struct acpi_nfit_flush_work *flush; |
| 2611 | |
| 2612 | flush = container_of(work, typeof(*flush), work); |
| 2613 | complete(&flush->cmp); |
| 2614 | } |
| 2615 | |
| 2616 | static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc) |
| 2617 | { |
| 2618 | struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc); |
| 2619 | struct device *dev = acpi_desc->dev; |
| 2620 | struct acpi_nfit_flush_work flush; |
| 2621 | |
| 2622 | /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */ |
| 2623 | device_lock(dev); |
| 2624 | device_unlock(dev); |
| 2625 | |
| 2626 | /* |
| 2627 | * Scrub work could take 10s of seconds, userspace may give up so we |
| 2628 | * need to be interruptible while waiting. |
| 2629 | */ |
| 2630 | INIT_WORK_ONSTACK(&flush.work, flush_probe); |
| 2631 | COMPLETION_INITIALIZER_ONSTACK(flush.cmp); |
| 2632 | queue_work(nfit_wq, &flush.work); |
| 2633 | return wait_for_completion_interruptible(&flush.cmp); |
| 2634 | } |
| 2635 | |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 2636 | static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc, |
| 2637 | struct nvdimm *nvdimm, unsigned int cmd) |
| 2638 | { |
| 2639 | struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc); |
| 2640 | |
| 2641 | if (nvdimm) |
| 2642 | return 0; |
| 2643 | if (cmd != ND_CMD_ARS_START) |
| 2644 | return 0; |
| 2645 | |
| 2646 | /* |
| 2647 | * The kernel and userspace may race to initiate a scrub, but |
| 2648 | * the scrub thread is prepared to lose that initial race. It |
| 2649 | * just needs guarantees that any ars it initiates are not |
| 2650 | * interrupted by any intervening start reqeusts from userspace. |
| 2651 | */ |
| 2652 | if (work_busy(&acpi_desc->work)) |
| 2653 | return -EBUSY; |
| 2654 | |
| 2655 | return 0; |
| 2656 | } |
| 2657 | |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2658 | int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc) |
Vishal Verma | 37b137f | 2016-07-23 21:51:42 -0700 | [diff] [blame] | 2659 | { |
| 2660 | struct device *dev = acpi_desc->dev; |
| 2661 | struct nfit_spa *nfit_spa; |
| 2662 | |
| 2663 | if (work_busy(&acpi_desc->work)) |
| 2664 | return -EBUSY; |
| 2665 | |
| 2666 | if (acpi_desc->cancel) |
| 2667 | return 0; |
| 2668 | |
| 2669 | mutex_lock(&acpi_desc->init_mutex); |
| 2670 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { |
| 2671 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
| 2672 | |
| 2673 | if (nfit_spa_type(spa) != NFIT_SPA_PM) |
| 2674 | continue; |
| 2675 | |
| 2676 | nfit_spa->ars_required = 1; |
| 2677 | } |
| 2678 | queue_work(nfit_wq, &acpi_desc->work); |
| 2679 | dev_dbg(dev, "%s: ars_scan triggered\n", __func__); |
| 2680 | mutex_unlock(&acpi_desc->init_mutex); |
| 2681 | |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
Dan Williams | a61fe6f | 2016-02-19 12:29:32 -0800 | [diff] [blame] | 2685 | void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev) |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2686 | { |
| 2687 | struct nvdimm_bus_descriptor *nd_desc; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2688 | |
| 2689 | dev_set_drvdata(dev, acpi_desc); |
| 2690 | acpi_desc->dev = dev; |
| 2691 | acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io; |
| 2692 | nd_desc = &acpi_desc->nd_desc; |
| 2693 | nd_desc->provider_name = "ACPI.NFIT"; |
Dan Williams | bc9775d | 2016-07-21 20:03:19 -0700 | [diff] [blame] | 2694 | nd_desc->module = THIS_MODULE; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2695 | nd_desc->ndctl = acpi_nfit_ctl; |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 2696 | nd_desc->flush_probe = acpi_nfit_flush_probe; |
Dan Williams | 87bf572 | 2016-02-22 21:50:31 -0800 | [diff] [blame] | 2697 | nd_desc->clear_to_send = acpi_nfit_clear_to_send; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2698 | nd_desc->attr_groups = acpi_nfit_attribute_groups; |
| 2699 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2700 | INIT_LIST_HEAD(&acpi_desc->spas); |
| 2701 | INIT_LIST_HEAD(&acpi_desc->dcrs); |
| 2702 | INIT_LIST_HEAD(&acpi_desc->bdws); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2703 | INIT_LIST_HEAD(&acpi_desc->idts); |
Ross Zwisler | c2ad295 | 2015-07-10 11:06:13 -0600 | [diff] [blame] | 2704 | INIT_LIST_HEAD(&acpi_desc->flushes); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2705 | INIT_LIST_HEAD(&acpi_desc->memdevs); |
| 2706 | INIT_LIST_HEAD(&acpi_desc->dimms); |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2707 | INIT_LIST_HEAD(&acpi_desc->list); |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2708 | mutex_init(&acpi_desc->init_mutex); |
Dan Williams | 1cf03c0 | 2016-02-17 13:01:23 -0800 | [diff] [blame] | 2709 | INIT_WORK(&acpi_desc->work, acpi_nfit_scrub); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2710 | } |
Dan Williams | a61fe6f | 2016-02-19 12:29:32 -0800 | [diff] [blame] | 2711 | EXPORT_SYMBOL_GPL(acpi_nfit_desc_init); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2712 | |
| 2713 | static int acpi_nfit_add(struct acpi_device *adev) |
| 2714 | { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2715 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2716 | struct acpi_nfit_desc *acpi_desc; |
| 2717 | struct device *dev = &adev->dev; |
| 2718 | struct acpi_table_header *tbl; |
| 2719 | acpi_status status = AE_OK; |
| 2720 | acpi_size sz; |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 2721 | int rc = 0; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2722 | |
Lee, Chun-Yi | 8259542 | 2016-01-21 20:32:10 +0800 | [diff] [blame] | 2723 | status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2724 | if (ACPI_FAILURE(status)) { |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2725 | /* This is ok, we could have an nvdimm hotplugged later */ |
| 2726 | dev_dbg(dev, "failed to find NFIT at startup\n"); |
| 2727 | return 0; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2728 | } |
| 2729 | |
Dan Williams | a61fe6f | 2016-02-19 12:29:32 -0800 | [diff] [blame] | 2730 | acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL); |
| 2731 | if (!acpi_desc) |
| 2732 | return -ENOMEM; |
| 2733 | acpi_nfit_desc_init(acpi_desc, &adev->dev); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2734 | |
Dan Williams | e7a11b4 | 2016-07-14 16:19:55 -0700 | [diff] [blame] | 2735 | /* Save the acpi header for exporting the revision via sysfs */ |
Linda Knippers | 6b577c9 | 2015-11-20 19:05:49 -0500 | [diff] [blame] | 2736 | acpi_desc->acpi_header = *tbl; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2737 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2738 | /* Evaluate _FIT and override with that if present */ |
| 2739 | status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf); |
| 2740 | if (ACPI_SUCCESS(status) && buf.length > 0) { |
Dan Williams | e7a11b4 | 2016-07-14 16:19:55 -0700 | [diff] [blame] | 2741 | union acpi_object *obj = buf.pointer; |
| 2742 | |
| 2743 | if (obj->type == ACPI_TYPE_BUFFER) |
| 2744 | rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer, |
| 2745 | obj->buffer.length); |
| 2746 | else |
Linda Knippers | 6b577c9 | 2015-11-20 19:05:49 -0500 | [diff] [blame] | 2747 | dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n", |
| 2748 | __func__, (int) obj->type); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 2749 | kfree(buf.pointer); |
| 2750 | } else |
Dan Williams | e7a11b4 | 2016-07-14 16:19:55 -0700 | [diff] [blame] | 2751 | /* skip over the lead-in header table */ |
| 2752 | rc = acpi_nfit_init(acpi_desc, (void *) tbl |
| 2753 | + sizeof(struct acpi_table_nfit), |
| 2754 | sz - sizeof(struct acpi_table_nfit)); |
Dan Williams | e7a11b4 | 2016-07-14 16:19:55 -0700 | [diff] [blame] | 2755 | return rc; |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | static int acpi_nfit_remove(struct acpi_device *adev) |
| 2759 | { |
Dan Williams | 58cd71b | 2016-07-21 18:05:36 -0700 | [diff] [blame] | 2760 | /* see acpi_nfit_destruct */ |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2761 | return 0; |
| 2762 | } |
| 2763 | |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2764 | void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event) |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2765 | { |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2766 | struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev); |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2767 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; |
Dan Williams | e7a11b4 | 2016-07-14 16:19:55 -0700 | [diff] [blame] | 2768 | union acpi_object *obj; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2769 | acpi_status status; |
| 2770 | int ret; |
| 2771 | |
| 2772 | dev_dbg(dev, "%s: event: %d\n", __func__, event); |
| 2773 | |
Vishal Verma | c09f121 | 2016-08-19 14:40:58 -0600 | [diff] [blame] | 2774 | if (event != NFIT_NOTIFY_UPDATE) |
| 2775 | return; |
| 2776 | |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2777 | if (!dev->driver) { |
| 2778 | /* dev->driver may be null if we're being removed */ |
| 2779 | dev_dbg(dev, "%s: no driver found for dev\n", __func__); |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2780 | return; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | if (!acpi_desc) { |
Dan Williams | a61fe6f | 2016-02-19 12:29:32 -0800 | [diff] [blame] | 2784 | acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL); |
| 2785 | if (!acpi_desc) |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2786 | return; |
| 2787 | acpi_nfit_desc_init(acpi_desc, dev); |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 2788 | } else { |
| 2789 | /* |
| 2790 | * Finish previous registration before considering new |
| 2791 | * regions. |
| 2792 | */ |
| 2793 | flush_workqueue(nfit_wq); |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | /* Evaluate _FIT */ |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2797 | status = acpi_evaluate_object(handle, "_FIT", NULL, &buf); |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2798 | if (ACPI_FAILURE(status)) { |
| 2799 | dev_err(dev, "failed to evaluate _FIT\n"); |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2800 | return; |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2801 | } |
| 2802 | |
Linda Knippers | 6b577c9 | 2015-11-20 19:05:49 -0500 | [diff] [blame] | 2803 | obj = buf.pointer; |
| 2804 | if (obj->type == ACPI_TYPE_BUFFER) { |
Dan Williams | e7a11b4 | 2016-07-14 16:19:55 -0700 | [diff] [blame] | 2805 | ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer, |
| 2806 | obj->buffer.length); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 2807 | if (ret) |
Linda Knippers | 6b577c9 | 2015-11-20 19:05:49 -0500 | [diff] [blame] | 2808 | dev_err(dev, "failed to merge updated NFIT\n"); |
Dan Williams | 3193204 | 2016-07-14 17:22:48 -0700 | [diff] [blame] | 2809 | } else |
Linda Knippers | 6b577c9 | 2015-11-20 19:05:49 -0500 | [diff] [blame] | 2810 | dev_err(dev, "Invalid _FIT\n"); |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2811 | kfree(buf.pointer); |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2812 | } |
| 2813 | EXPORT_SYMBOL_GPL(__acpi_nfit_notify); |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2814 | |
Dan Williams | c14a868 | 2016-08-18 22:15:04 -0700 | [diff] [blame] | 2815 | static void acpi_nfit_notify(struct acpi_device *adev, u32 event) |
| 2816 | { |
| 2817 | device_lock(&adev->dev); |
| 2818 | __acpi_nfit_notify(&adev->dev, adev->handle, event); |
| 2819 | device_unlock(&adev->dev); |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2820 | } |
| 2821 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2822 | static const struct acpi_device_id acpi_nfit_ids[] = { |
| 2823 | { "ACPI0012", 0 }, |
| 2824 | { "", 0 }, |
| 2825 | }; |
| 2826 | MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids); |
| 2827 | |
| 2828 | static struct acpi_driver acpi_nfit_driver = { |
| 2829 | .name = KBUILD_MODNAME, |
| 2830 | .ids = acpi_nfit_ids, |
| 2831 | .ops = { |
| 2832 | .add = acpi_nfit_add, |
| 2833 | .remove = acpi_nfit_remove, |
Vishal Verma | 2098516 | 2015-10-27 16:58:27 -0600 | [diff] [blame] | 2834 | .notify = acpi_nfit_notify, |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2835 | }, |
| 2836 | }; |
| 2837 | |
| 2838 | static __init int nfit_init(void) |
| 2839 | { |
| 2840 | BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40); |
| 2841 | BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56); |
| 2842 | BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48); |
| 2843 | BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20); |
| 2844 | BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9); |
| 2845 | BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80); |
| 2846 | BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40); |
| 2847 | |
| 2848 | acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]); |
| 2849 | acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]); |
| 2850 | acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]); |
| 2851 | acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]); |
| 2852 | acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]); |
| 2853 | acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]); |
| 2854 | acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]); |
| 2855 | acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]); |
| 2856 | acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]); |
| 2857 | acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]); |
Dan Williams | 31eca76 | 2016-04-28 16:23:43 -0700 | [diff] [blame] | 2858 | acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]); |
| 2859 | acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]); |
stuart hayes | e02fb72 | 2016-05-26 11:38:41 -0500 | [diff] [blame] | 2860 | acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2861 | |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 2862 | nfit_wq = create_singlethread_workqueue("nfit"); |
| 2863 | if (!nfit_wq) |
| 2864 | return -ENOMEM; |
| 2865 | |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2866 | nfit_mce_register(); |
| 2867 | |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2868 | return acpi_bus_register_driver(&acpi_nfit_driver); |
| 2869 | } |
| 2870 | |
| 2871 | static __exit void nfit_exit(void) |
| 2872 | { |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2873 | nfit_mce_unregister(); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2874 | acpi_bus_unregister_driver(&acpi_nfit_driver); |
Dan Williams | 7ae0fa43 | 2016-02-19 12:16:34 -0800 | [diff] [blame] | 2875 | destroy_workqueue(nfit_wq); |
Vishal Verma | 6839a6d | 2016-07-23 21:51:21 -0700 | [diff] [blame] | 2876 | WARN_ON(!list_empty(&acpi_descs)); |
Dan Williams | b94d523 | 2015-05-19 22:54:31 -0400 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | module_init(nfit_init); |
| 2880 | module_exit(nfit_exit); |
| 2881 | MODULE_LICENSE("GPL v2"); |
| 2882 | MODULE_AUTHOR("Intel Corporation"); |