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