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