root/drivers/acpi/nfit/core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. to_nfit_uuid
  2. to_acpi_dev
  3. xlat_bus_status
  4. xlat_nvdimm_status
  5. xlat_status
  6. pkg_to_buf
  7. int_to_buf
  8. acpi_label_write
  9. acpi_label_read
  10. acpi_label_info
  11. nfit_dsm_revid
  12. payload_dumpable
  13. cmd_to_func
  14. acpi_nfit_ctl
  15. spa_type_name
  16. nfit_spa_type
  17. add_spa
  18. add_memdev
  19. nfit_get_smbios_id
  20. sizeof_dcr
  21. add_dcr
  22. add_bdw
  23. sizeof_idt
  24. add_idt
  25. sizeof_flush
  26. add_flush
  27. add_platform_cap
  28. add_table
  29. nfit_mem_find_spa_bdw
  30. nfit_mem_init_bdw
  31. __nfit_mem_init
  32. nfit_mem_cmp
  33. nfit_mem_init
  34. bus_dsm_mask_show
  35. revision_show
  36. hw_error_scrub_show
  37. hw_error_scrub_store
  38. scrub_show
  39. scrub_store
  40. ars_supported
  41. nfit_visible
  42. to_nfit_memdev
  43. to_nfit_dcr
  44. handle_show
  45. phys_id_show
  46. vendor_show
  47. rev_id_show
  48. device_show
  49. subsystem_vendor_show
  50. subsystem_rev_id_show
  51. subsystem_device_show
  52. num_nvdimm_formats
  53. format_show
  54. format1_show
  55. formats_show
  56. serial_show
  57. family_show
  58. dsm_mask_show
  59. flags_show
  60. id_show
  61. dirty_shutdown_show
  62. acpi_nfit_dimm_attr_visible
  63. acpi_nfit_dimm_by_handle
  64. __acpi_nvdimm_notify
  65. acpi_nvdimm_notify
  66. acpi_nvdimm_has_method
  67. nfit_intel_shutdown_status
  68. populate_shutdown_status
  69. acpi_nfit_add_dimm
  70. shutdown_dimm_notify
  71. acpi_nfit_get_security_ops
  72. acpi_nfit_register_dimms
  73. acpi_nfit_init_dsms
  74. range_index_show
  75. sizeof_nfit_set_info
  76. sizeof_nfit_set_info2
  77. cmp_map_compat
  78. cmp_map
  79. cmp_map2
  80. memdev_from_spa
  81. acpi_nfit_init_interleave_set
  82. to_interleave_offset
  83. read_blk_stat
  84. write_blk_ctl
  85. acpi_nfit_blk_single_io
  86. acpi_nfit_blk_region_do_io
  87. nfit_blk_init_interleave
  88. acpi_nfit_blk_get_flags
  89. acpi_nfit_blk_region_enable
  90. ars_get_cap
  91. ars_start
  92. ars_continue
  93. ars_get_status
  94. ars_complete
  95. ars_status_process_records
  96. acpi_nfit_remove_resource
  97. acpi_nfit_insert_resource
  98. acpi_nfit_init_mapping
  99. nfit_spa_is_virtual
  100. nfit_spa_is_volatile
  101. acpi_nfit_register_region
  102. ars_status_alloc
  103. acpi_nfit_query_poison
  104. ars_register
  105. ars_complete_all
  106. __acpi_nfit_scrub
  107. __sched_ars
  108. sched_ars
  109. notify_ars_done
  110. acpi_nfit_scrub
  111. acpi_nfit_init_ars
  112. acpi_nfit_register_regions
  113. acpi_nfit_check_deletions
  114. acpi_nfit_desc_init_scrub_attr
  115. acpi_nfit_unregister
  116. acpi_nfit_init
  117. acpi_nfit_flush_probe
  118. __acpi_nfit_clear_to_send
  119. acpi_nfit_clear_to_send
  120. acpi_nfit_ars_rescan
  121. acpi_nfit_desc_init
  122. acpi_nfit_put_table
  123. acpi_nfit_shutdown
  124. acpi_nfit_add
  125. acpi_nfit_remove
  126. acpi_nfit_update_notify
  127. acpi_nfit_uc_error_notify
  128. __acpi_nfit_notify
  129. acpi_nfit_notify
  130. nfit_init
  131. nfit_exit

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

/* [<][>][^][v][top][bottom][index][help] */