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