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