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