nfit.c 88.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
16
#include <linux/workqueue.h>
17
#include <linux/libnvdimm.h>
18
#include <linux/genalloc.h>
19 20 21
#include <linux/vmalloc.h>
#include <linux/device.h>
#include <linux/module.h>
V
Vishal Verma 已提交
22
#include <linux/mutex.h>
23 24
#include <linux/ndctl.h>
#include <linux/sizes.h>
V
Vishal Verma 已提交
25
#include <linux/list.h>
26
#include <linux/slab.h>
27
#include <nd-core.h>
D
Dan Williams 已提交
28
#include <intel.h>
29 30 31
#include <nfit.h>
#include <nd.h>
#include "nfit_test.h"
32
#include "../watermark.h"
33

34 35
#include <asm/mcsafe_test.h>

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*
 * Generate an NFIT table to describe the following topology:
 *
 * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
 *
 *                     (a)                       (b)            DIMM   BLK-REGION
 *           +----------+--------------+----------+---------+
 * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
 * | imc0 +--+- - - - - region0 - - - -+----------+         +
 * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
 *    |      +----------+--------------v----------v         v
 * +--+---+                            |                    |
 * | cpu0 |                                    region1
 * +--+---+                            |                    |
 *    |      +-------------------------^----------^         ^
 * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
 * | imc1 +--+-------------------------+----------+         +
 * +------+  |                 blk5.0             |  pm1.0  |    3      region5
 *           +-------------------------+----------+-+-------+
 *
V
Vishal Verma 已提交
56 57 58 59 60 61 62 63 64
 * +--+---+
 * | cpu1 |
 * +--+---+                   (Hotplug DIMM)
 *    |      +----------------------------------------------+
 * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
 * | imc0 +--+----------------------------------------------+
 * +------+
 *
 *
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 * *) In this layout we have four dimms and two memory controllers in one
 *    socket.  Each unique interface (BLK or PMEM) to DPA space
 *    is identified by a region device with a dynamically assigned id.
 *
 * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
 *    A single PMEM namespace "pm0.0" is created using half of the
 *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
 *    allocate from from the bottom of a region.  The unallocated
 *    portion of REGION0 aliases with REGION2 and REGION3.  That
 *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
 *    "blk3.0") starting at the base of each DIMM to offset (a) in those
 *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
 *    names that can be assigned to a namespace.
 *
 * *) In the last portion of dimm0 and dimm1 we have an interleaved
 *    SPA range, REGION1, that spans those two dimms as well as dimm2
 *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
 *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
 *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
 *    "blk5.0".
 *
 * *) The portion of dimm2 and dimm3 that do not participate in the
 *    REGION1 interleaved SPA range (i.e. the DPA address below offset
 *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
 *    Note, that BLK namespaces need not be contiguous in DPA-space, and
 *    can consume aliased capacity from multiple interleave sets.
 *
 * BUS1: Legacy NVDIMM (single contiguous range)
 *
 *  region2
 * +---------------------+
 * |---------------------|
 * ||       pm2.0       ||
 * |---------------------|
 * +---------------------+
 *
 * *) A NFIT-table may describe a simple system-physical-address range
 *    with no BLK aliasing.  This type of region may optionally
 *    reference an NVDIMM.
 */
enum {
V
Vishal Verma 已提交
106 107
	NUM_PM  = 3,
	NUM_DCR = 5,
108
	NUM_HINTS = 8,
109 110
	NUM_BDW = NUM_DCR,
	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
111 112
	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
113 114
	DIMM_SIZE = SZ_32M,
	LABEL_SIZE = SZ_128K,
115
	SPA_VCD_SIZE = SZ_4M,
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	SPA0_SIZE = DIMM_SIZE,
	SPA1_SIZE = DIMM_SIZE*2,
	SPA2_SIZE = DIMM_SIZE,
	BDW_SIZE = 64 << 8,
	DCR_SIZE = 12,
	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
};

struct nfit_test_dcr {
	__le64 bdw_addr;
	__le32 bdw_status;
	__u8 aperature[BDW_SIZE];
};

#define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))

134
static u32 handle[] = {
135 136 137 138
	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
V
Vishal Verma 已提交
139
	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
140
	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
141
	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
142 143
};

144 145
static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
static int dimm_fail_cmd_code[ARRAY_SIZE(handle)];
146 147
struct nfit_test_sec {
	u8 state;
148
	u8 ext_state;
149
	u8 old_state;
150
	u8 passphrase[32];
151
	u8 master_passphrase[32];
152
	u64 overwrite_end_time;
153
} dimm_sec_info[NUM_DCR];
154

155 156 157 158 159 160
static const struct nd_intel_smart smart_def = {
	.flags = ND_INTEL_SMART_HEALTH_VALID
		| ND_INTEL_SMART_SPARES_VALID
		| ND_INTEL_SMART_ALARM_VALID
		| ND_INTEL_SMART_USED_VALID
		| ND_INTEL_SMART_SHUTDOWN_VALID
161
		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
162 163 164 165 166 167 168 169 170 171 172 173
		| ND_INTEL_SMART_MTEMP_VALID
		| ND_INTEL_SMART_CTEMP_VALID,
	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
	.media_temperature = 23 * 16,
	.ctrl_temperature = 25 * 16,
	.pmic_temperature = 40 * 16,
	.spares = 75,
	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
		| ND_INTEL_SMART_TEMP_TRIP,
	.ait_status = 1,
	.life_used = 5,
	.shutdown_state = 0,
174
	.shutdown_count = 42,
175 176 177
	.vendor_size = 0,
};

178 179 180 181 182 183 184 185
struct nfit_test_fw {
	enum intel_fw_update_state state;
	u32 context;
	u64 version;
	u32 size_received;
	u64 end_time;
};

186 187 188 189 190 191 192
struct nfit_test {
	struct acpi_nfit_desc acpi_desc;
	struct platform_device pdev;
	struct list_head resources;
	void *nfit_buf;
	dma_addr_t nfit_dma;
	size_t nfit_size;
193
	size_t nfit_filled;
194
	int dcr_idx;
195 196 197 198
	int num_dcr;
	int num_pm;
	void **dimm;
	dma_addr_t *dimm_dma;
199 200
	void **flush;
	dma_addr_t *flush_dma;
201 202 203 204 205 206 207 208
	void **label;
	dma_addr_t *label_dma;
	void **spa_set;
	dma_addr_t *spa_set_dma;
	struct nfit_test_dcr **dcr;
	dma_addr_t *dcr_dma;
	int (*alloc)(struct nfit_test *t);
	void (*setup)(struct nfit_test *t);
V
Vishal Verma 已提交
209
	int setup_hotplug;
210 211
	union acpi_object **_fit;
	dma_addr_t _fit_dma;
212 213 214 215 216
	struct ars_state {
		struct nd_cmd_ars_status *ars_status;
		unsigned long deadline;
		spinlock_t lock;
	} ars_state;
217
	struct device *dimm_dev[ARRAY_SIZE(handle)];
218 219
	struct nd_intel_smart *smart;
	struct nd_intel_smart_threshold *smart_threshold;
D
Dave Jiang 已提交
220 221
	struct badrange badrange;
	struct work_struct work;
222
	struct nfit_test_fw *fw;
223 224
};

D
Dave Jiang 已提交
225 226
static struct workqueue_struct *nfit_wq;

227 228
static struct gen_pool *nfit_pool;

229 230
static const char zero_key[NVDIMM_PASSPHRASE_LEN];

231 232 233 234 235 236 237
static struct nfit_test *to_nfit_test(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);

	return container_of(pdev, struct nfit_test, pdev);
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
static int nd_intel_test_get_fw_info(struct nfit_test *t,
		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
		int idx)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_fw *fw = &t->fw[idx];

	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
			__func__, t, nd_cmd, buf_len, idx);

	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;

	nd_cmd->status = 0;
	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
	nd_cmd->update_cap = 0;
	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
	nd_cmd->run_version = 0;
	nd_cmd->updated_version = fw->version;

	return 0;
}

static int nd_intel_test_start_update(struct nfit_test *t,
		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
		int idx)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_fw *fw = &t->fw[idx];

	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
			__func__, t, nd_cmd, buf_len, idx);

	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;

	if (fw->state != FW_STATE_NEW) {
		/* extended status, FW update in progress */
		nd_cmd->status = 0x10007;
		return 0;
	}

	fw->state = FW_STATE_IN_PROGRESS;
	fw->context++;
	fw->size_received = 0;
	nd_cmd->status = 0;
	nd_cmd->context = fw->context;

	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);

	return 0;
}

static int nd_intel_test_send_data(struct nfit_test *t,
		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
		int idx)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_fw *fw = &t->fw[idx];
	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];

	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
			__func__, t, nd_cmd, buf_len, idx);

	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;


	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
			nd_cmd->data[nd_cmd->length-1]);

	if (fw->state != FW_STATE_IN_PROGRESS) {
		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
		*status = 0x5;
		return 0;
	}

	if (nd_cmd->context != fw->context) {
		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
				__func__, nd_cmd->context, fw->context);
		*status = 0x10007;
		return 0;
	}

	/*
	 * check offset + len > size of fw storage
	 * check length is > max send length
	 */
	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
		*status = 0x3;
		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
		return 0;
	}

	fw->size_received += nd_cmd->length;
	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
			__func__, nd_cmd->length, fw->size_received);
	*status = 0;
	return 0;
}

static int nd_intel_test_finish_fw(struct nfit_test *t,
		struct nd_intel_fw_finish_update *nd_cmd,
		unsigned int buf_len, int idx)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_fw *fw = &t->fw[idx];

	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
			__func__, t, nd_cmd, buf_len, idx);

	if (fw->state == FW_STATE_UPDATED) {
		/* update already done, need cold boot */
		nd_cmd->status = 0x20007;
		return 0;
	}

	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
			__func__, nd_cmd->context, nd_cmd->ctrl_flags);

	switch (nd_cmd->ctrl_flags) {
	case 0: /* finish */
		if (nd_cmd->context != fw->context) {
			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
					__func__, nd_cmd->context,
					fw->context);
			nd_cmd->status = 0x10007;
			return 0;
		}
		nd_cmd->status = 0;
		fw->state = FW_STATE_VERIFY;
		/* set 1 second of time for firmware "update" */
		fw->end_time = jiffies + HZ;
		break;

	case 1: /* abort */
		fw->size_received = 0;
		/* successfully aborted status */
		nd_cmd->status = 0x40007;
		fw->state = FW_STATE_NEW;
		dev_dbg(dev, "%s: abort successful\n", __func__);
		break;

	default: /* bad control flag */
		dev_warn(dev, "%s: unknown control flag: %#x\n",
				__func__, nd_cmd->ctrl_flags);
		return -EINVAL;
	}

	return 0;
}

static int nd_intel_test_finish_query(struct nfit_test *t,
		struct nd_intel_fw_finish_query *nd_cmd,
		unsigned int buf_len, int idx)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_fw *fw = &t->fw[idx];

	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
			__func__, t, nd_cmd, buf_len, idx);

	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;

	if (nd_cmd->context != fw->context) {
		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
				__func__, nd_cmd->context, fw->context);
		nd_cmd->status = 0x10007;
		return 0;
	}

	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);

	switch (fw->state) {
	case FW_STATE_NEW:
		nd_cmd->updated_fw_rev = 0;
		nd_cmd->status = 0;
		dev_dbg(dev, "%s: new state\n", __func__);
		break;

	case FW_STATE_IN_PROGRESS:
		/* sequencing error */
		nd_cmd->status = 0x40007;
		nd_cmd->updated_fw_rev = 0;
		dev_dbg(dev, "%s: sequence error\n", __func__);
		break;

	case FW_STATE_VERIFY:
		if (time_is_after_jiffies64(fw->end_time)) {
			nd_cmd->updated_fw_rev = 0;
			nd_cmd->status = 0x20007;
			dev_dbg(dev, "%s: still verifying\n", __func__);
			break;
		}

		dev_dbg(dev, "%s: transition out verify\n", __func__);
		fw->state = FW_STATE_UPDATED;
		/* we are going to fall through if it's "done" */
	case FW_STATE_UPDATED:
		nd_cmd->status = 0;
		/* bogus test version */
		fw->version = nd_cmd->updated_fw_rev =
			INTEL_FW_FAKE_VERSION;
		dev_dbg(dev, "%s: updated\n", __func__);
		break;

	default: /* we should never get here */
		return -EINVAL;
	}

	return 0;
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
		unsigned int buf_len)
{
	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;

	nd_cmd->status = 0;
	nd_cmd->config_size = LABEL_SIZE;
	nd_cmd->max_xfer = SZ_4K;

	return 0;
}

static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
		*nd_cmd, unsigned int buf_len, void *label)
{
	unsigned int len, offset = nd_cmd->in_offset;
	int rc;

	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;
	if (offset >= LABEL_SIZE)
		return -EINVAL;
	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
		return -EINVAL;

	nd_cmd->status = 0;
	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
	memcpy(nd_cmd->out_buf, label + offset, len);
	rc = buf_len - sizeof(*nd_cmd) - len;

	return rc;
}

static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
		unsigned int buf_len, void *label)
{
	unsigned int len, offset = nd_cmd->in_offset;
	u32 *status;
	int rc;

	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;
	if (offset >= LABEL_SIZE)
		return -EINVAL;
	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
		return -EINVAL;

	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
	*status = 0;
	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
	memcpy(label + offset, nd_cmd->in_buf, len);
	rc = buf_len - sizeof(*nd_cmd) - (len + 4);

	return rc;
}

515
#define NFIT_TEST_CLEAR_ERR_UNIT 256
516

517 518 519
static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
		unsigned int buf_len)
{
D
Dave Jiang 已提交
520 521
	int ars_recs;

522 523 524
	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;

D
Dave Jiang 已提交
525 526 527
	/* for testing, only store up to n records that fit within 4k */
	ars_recs = SZ_4K / sizeof(struct nd_ars_record);

528
	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
D
Dave Jiang 已提交
529
		+ ars_recs * sizeof(struct nd_ars_record);
530
	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
531
	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
532 533 534 535

	return 0;
}

D
Dave Jiang 已提交
536 537
static void post_ars_status(struct ars_state *ars_state,
		struct badrange *badrange, u64 addr, u64 len)
538
{
539 540
	struct nd_cmd_ars_status *ars_status;
	struct nd_ars_record *ars_record;
D
Dave Jiang 已提交
541 542 543
	struct badrange_entry *be;
	u64 end = addr + len - 1;
	int i = 0;
544 545 546 547 548 549 550

	ars_state->deadline = jiffies + 1*HZ;
	ars_status = ars_state->ars_status;
	ars_status->status = 0;
	ars_status->address = addr;
	ars_status->length = len;
	ars_status->type = ND_ARS_PERSISTENT;
D
Dave Jiang 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

	spin_lock(&badrange->lock);
	list_for_each_entry(be, &badrange->list, list) {
		u64 be_end = be->start + be->length - 1;
		u64 rstart, rend;

		/* skip entries outside the range */
		if (be_end < addr || be->start > end)
			continue;

		rstart = (be->start < addr) ? addr : be->start;
		rend = (be_end < end) ? be_end : end;
		ars_record = &ars_status->records[i];
		ars_record->handle = 0;
		ars_record->err_address = rstart;
		ars_record->length = rend - rstart + 1;
		i++;
	}
	spin_unlock(&badrange->lock);
	ars_status->num_records = i;
	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
		+ i * sizeof(struct nd_ars_record);
573 574
}

D
Dave Jiang 已提交
575 576
static int nfit_test_cmd_ars_start(struct nfit_test *t,
		struct ars_state *ars_state,
577 578 579 580
		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
		int *cmd_rc)
{
	if (buf_len < sizeof(*ars_start))
581 582
		return -EINVAL;

583 584 585 586 587 588 589
	spin_lock(&ars_state->lock);
	if (time_before(jiffies, ars_state->deadline)) {
		ars_start->status = NFIT_ARS_START_BUSY;
		*cmd_rc = -EBUSY;
	} else {
		ars_start->status = 0;
		ars_start->scrub_time = 1;
D
Dave Jiang 已提交
590
		post_ars_status(ars_state, &t->badrange, ars_start->address,
591 592 593 594
				ars_start->length);
		*cmd_rc = 0;
	}
	spin_unlock(&ars_state->lock);
595 596 597 598

	return 0;
}

599 600 601
static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
		int *cmd_rc)
602
{
603
	if (buf_len < ars_state->ars_status->out_length)
604 605
		return -EINVAL;

606 607 608 609 610 611 612 613 614 615 616 617
	spin_lock(&ars_state->lock);
	if (time_before(jiffies, ars_state->deadline)) {
		memset(ars_status, 0, buf_len);
		ars_status->status = NFIT_ARS_STATUS_BUSY;
		ars_status->out_length = sizeof(*ars_status);
		*cmd_rc = -EBUSY;
	} else {
		memcpy(ars_status, ars_state->ars_status,
				ars_state->ars_status->out_length);
		*cmd_rc = 0;
	}
	spin_unlock(&ars_state->lock);
618 619 620
	return 0;
}

621 622
static int nfit_test_cmd_clear_error(struct nfit_test *t,
		struct nd_cmd_clear_error *clear_err,
623 624 625 626 627 628 629 630 631
		unsigned int buf_len, int *cmd_rc)
{
	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
	if (buf_len < sizeof(*clear_err))
		return -EINVAL;

	if ((clear_err->address & mask) || (clear_err->length & mask))
		return -EINVAL;

632
	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
633 634 635 636 637 638
	clear_err->status = 0;
	clear_err->cleared = clear_err->length;
	*cmd_rc = 0;
	return 0;
}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
struct region_search_spa {
	u64 addr;
	struct nd_region *region;
};

static int is_region_device(struct device *dev)
{
	return !strncmp(dev->kobj.name, "region", 6);
}

static int nfit_test_search_region_spa(struct device *dev, void *data)
{
	struct region_search_spa *ctx = data;
	struct nd_region *nd_region;
	resource_size_t ndr_end;

	if (!is_region_device(dev))
		return 0;

	nd_region = to_nd_region(dev);
	ndr_end = nd_region->ndr_start + nd_region->ndr_size;

	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
		ctx->region = nd_region;
		return 1;
	}

	return 0;
}

static int nfit_test_search_spa(struct nvdimm_bus *bus,
		struct nd_cmd_translate_spa *spa)
{
	int ret;
	struct nd_region *nd_region = NULL;
	struct nvdimm *nvdimm = NULL;
	struct nd_mapping *nd_mapping = NULL;
	struct region_search_spa ctx = {
		.addr = spa->spa,
		.region = NULL,
	};
	u64 dpa;

	ret = device_for_each_child(&bus->dev, &ctx,
				nfit_test_search_region_spa);

	if (!ret)
		return -ENODEV;

	nd_region = ctx.region;

	dpa = ctx.addr - nd_region->ndr_start;

	/*
	 * last dimm is selected for test
	 */
	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
	nvdimm = nd_mapping->nvdimm;

	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
	spa->num_nvdimms = 1;
	spa->devices[0].dpa = dpa;

	return 0;
}

static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
{
	if (buf_len < spa->translate_length)
		return -EINVAL;

	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
		spa->status = 2;

	return 0;
}

717 718
static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
		struct nd_intel_smart *smart_data)
719 720 721
{
	if (buf_len < sizeof(*smart))
		return -EINVAL;
722
	memcpy(smart, smart_data, sizeof(*smart));
723 724 725
	return 0;
}

726
static int nfit_test_cmd_smart_threshold(
727 728 729
		struct nd_intel_smart_threshold *out,
		unsigned int buf_len,
		struct nd_intel_smart_threshold *smart_t)
730 731 732
{
	if (buf_len < sizeof(*smart_t))
		return -EINVAL;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
	memcpy(out, smart_t, sizeof(*smart_t));
	return 0;
}

static void smart_notify(struct device *bus_dev,
		struct device *dimm_dev, struct nd_intel_smart *smart,
		struct nd_intel_smart_threshold *thresh)
{
	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
			__func__, thresh->alarm_control, thresh->spares,
			smart->spares, thresh->media_temperature,
			smart->media_temperature, thresh->ctrl_temperature,
			smart->ctrl_temperature);
	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
				&& smart->spares
				<= thresh->spares)
			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
				&& smart->media_temperature
				>= thresh->media_temperature)
			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
				&& smart->ctrl_temperature
754 755 756
				>= thresh->ctrl_temperature)
			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
			|| (smart->shutdown_state != 0)) {
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
		device_lock(bus_dev);
		__acpi_nvdimm_notify(dimm_dev, 0x81);
		device_unlock(bus_dev);
	}
}

static int nfit_test_cmd_smart_set_threshold(
		struct nd_intel_smart_set_threshold *in,
		unsigned int buf_len,
		struct nd_intel_smart_threshold *thresh,
		struct nd_intel_smart *smart,
		struct device *bus_dev, struct device *dimm_dev)
{
	unsigned int size;

	size = sizeof(*in) - 4;
	if (buf_len < size)
		return -EINVAL;
	memcpy(thresh->data, in, size);
	in->status = 0;
	smart_notify(bus_dev, dimm_dev, smart, thresh);

779 780 781
	return 0;
}

782 783 784 785 786 787 788 789 790 791
static int nfit_test_cmd_smart_inject(
		struct nd_intel_smart_inject *inj,
		unsigned int buf_len,
		struct nd_intel_smart_threshold *thresh,
		struct nd_intel_smart *smart,
		struct device *bus_dev, struct device *dimm_dev)
{
	if (buf_len != sizeof(*inj))
		return -EINVAL;

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
		if (inj->mtemp_enable)
			smart->media_temperature = inj->media_temperature;
		else
			smart->media_temperature = smart_def.media_temperature;
	}
	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
		if (inj->spare_enable)
			smart->spares = inj->spares;
		else
			smart->spares = smart_def.spares;
	}
	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
		if (inj->fatal_enable)
			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
		else
			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
	}
	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
		if (inj->unsafe_shutdown_enable) {
			smart->shutdown_state = 1;
			smart->shutdown_count++;
		} else
			smart->shutdown_state = 0;
816 817 818 819 820 821 822
	}
	inj->status = 0;
	smart_notify(bus_dev, dimm_dev, smart, thresh);

	return 0;
}

D
Dave Jiang 已提交
823 824 825 826 827 828 829 830 831 832 833 834
static void uc_error_notify(struct work_struct *work)
{
	struct nfit_test *t = container_of(work, typeof(*t), work);

	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
}

static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
{
	int rc;

835
	if (buf_len != sizeof(*err_inj)) {
D
Dave Jiang 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
		rc = -EINVAL;
		goto err;
	}

	if (err_inj->err_inj_spa_range_length <= 0) {
		rc = -EINVAL;
		goto err;
	}

	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
			err_inj->err_inj_spa_range_length);
	if (rc < 0)
		goto err;

	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
		queue_work(nfit_wq, &t->work);

	err_inj->status = 0;
	return 0;

err:
	err_inj->status = NFIT_ARS_INJECT_INVALID;
	return rc;
}

static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
{
	int rc;

866
	if (buf_len != sizeof(*err_clr)) {
D
Dave Jiang 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
		rc = -EINVAL;
		goto err;
	}

	if (err_clr->err_inj_clr_spa_range_length <= 0) {
		rc = -EINVAL;
		goto err;
	}

	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
			err_clr->err_inj_clr_spa_range_length);

	err_clr->status = 0;
	return 0;

err:
	err_clr->status = NFIT_ARS_INJECT_INVALID;
	return rc;
}

static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
		struct nd_cmd_ars_err_inj_stat *err_stat,
		unsigned int buf_len)
{
	struct badrange_entry *be;
	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
	int i = 0;

	err_stat->status = 0;
	spin_lock(&t->badrange.lock);
	list_for_each_entry(be, &t->badrange.list, list) {
		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
		i++;
		if (i > max)
			break;
	}
	spin_unlock(&t->badrange.lock);
	err_stat->inj_err_rec_count = i;

	return 0;
}

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
{
	struct device *dev = &t->pdev.dev;

	if (buf_len < sizeof(*nd_cmd))
		return -EINVAL;

	switch (nd_cmd->enable) {
	case 0:
		nd_cmd->status = 0;
		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
				__func__);
		break;
	case 1:
		nd_cmd->status = 0;
		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
				__func__);
		break;
	default:
		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
		nd_cmd->status = 0x3;
		break;
	}


	return 0;
}

939 940 941 942 943 944 945 946 947 948
static int override_return_code(int dimm, unsigned int func, int rc)
{
	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
		if (dimm_fail_cmd_code[dimm])
			return dimm_fail_cmd_code[dimm];
		return -EIO;
	}
	return rc;
}

949 950 951 952 953 954 955 956 957
static int nd_intel_test_cmd_security_status(struct nfit_test *t,
		struct nd_intel_get_security_state *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	nd_cmd->status = 0;
	nd_cmd->state = sec->state;
958
	nd_cmd->extended_state = sec->ext_state;
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);

	return 0;
}

static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
		struct nd_intel_unlock_unit *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
				sec->state);
	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
				ND_INTEL_PASSPHRASE_SIZE) != 0) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
		dev_dbg(dev, "unlock unit: invalid passphrase\n");
	} else {
		nd_cmd->status = 0;
		sec->state = ND_INTEL_SEC_STATE_ENABLED;
		dev_dbg(dev, "Unit unlocked\n");
	}

	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
	return 0;
}

static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
		struct nd_intel_set_passphrase *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
		dev_dbg(dev, "set passphrase: wrong security state\n");
	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
				ND_INTEL_PASSPHRASE_SIZE) != 0) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
		dev_dbg(dev, "set passphrase: wrong passphrase\n");
	} else {
		memcpy(sec->passphrase, nd_cmd->new_pass,
				ND_INTEL_PASSPHRASE_SIZE);
		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
		nd_cmd->status = 0;
		dev_dbg(dev, "passphrase updated\n");
	}

	return 0;
}

static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
		struct nd_intel_freeze_lock *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
		dev_dbg(dev, "freeze lock: wrong security state\n");
	} else {
		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
		nd_cmd->status = 0;
		dev_dbg(dev, "security frozen\n");
	}

	return 0;
}

static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
		struct nd_intel_disable_passphrase *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
		dev_dbg(dev, "disable passphrase: wrong security state\n");
	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
				ND_INTEL_PASSPHRASE_SIZE) != 0) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
	} else {
		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
		sec->state = 0;
		dev_dbg(dev, "disable passphrase: done\n");
	}

	return 0;
}

static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
		struct nd_intel_secure_erase *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

1065
	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
1066 1067 1068 1069 1070 1071 1072
		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
		dev_dbg(dev, "secure erase: wrong security state\n");
	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
				ND_INTEL_PASSPHRASE_SIZE) != 0) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
		dev_dbg(dev, "secure erase: wrong passphrase\n");
	} else {
1073 1074 1075 1076 1077 1078
		if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)
				&& (memcmp(nd_cmd->passphrase, zero_key,
					ND_INTEL_PASSPHRASE_SIZE) != 0)) {
			dev_dbg(dev, "invalid zero key\n");
			return 0;
		}
1079
		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1080
		memset(sec->master_passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1081
		sec->state = 0;
1082
		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1083 1084 1085 1086 1087 1088
		dev_dbg(dev, "secure erase: done\n");
	}

	return 0;
}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
static int nd_intel_test_cmd_overwrite(struct nfit_test *t,
		struct nd_intel_overwrite *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if ((sec->state & ND_INTEL_SEC_STATE_ENABLED) &&
			memcmp(nd_cmd->passphrase, sec->passphrase,
				ND_INTEL_PASSPHRASE_SIZE) != 0) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
		dev_dbg(dev, "overwrite: wrong passphrase\n");
		return 0;
	}

1104
	sec->old_state = sec->state;
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	sec->state = ND_INTEL_SEC_STATE_OVERWRITE;
	dev_dbg(dev, "overwrite progressing.\n");
	sec->overwrite_end_time = get_jiffies_64() + 5 * HZ;

	return 0;
}

static int nd_intel_test_cmd_query_overwrite(struct nfit_test *t,
		struct nd_intel_query_overwrite *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if (!(sec->state & ND_INTEL_SEC_STATE_OVERWRITE)) {
		nd_cmd->status = ND_INTEL_STATUS_OQUERY_SEQUENCE_ERR;
		return 0;
	}

	if (time_is_before_jiffies64(sec->overwrite_end_time)) {
		sec->overwrite_end_time = 0;
1126 1127
		sec->state = sec->old_state;
		sec->old_state = 0;
1128
		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1129 1130 1131 1132 1133 1134
		dev_dbg(dev, "overwrite is complete\n");
	} else
		nd_cmd->status = ND_INTEL_STATUS_OQUERY_INPROGRESS;
	return 0;
}

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
static int nd_intel_test_cmd_master_set_pass(struct nfit_test *t,
		struct nd_intel_set_master_passphrase *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
		dev_dbg(dev, "master set passphrase: in wrong state\n");
	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
		dev_dbg(dev, "master set passphrase: in wrong security state\n");
	} else if (memcmp(nd_cmd->old_pass, sec->master_passphrase,
				ND_INTEL_PASSPHRASE_SIZE) != 0) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
		dev_dbg(dev, "master set passphrase: wrong passphrase\n");
	} else {
		memcpy(sec->master_passphrase, nd_cmd->new_pass,
				ND_INTEL_PASSPHRASE_SIZE);
		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
		dev_dbg(dev, "master passphrase: updated\n");
	}

	return 0;
}

static int nd_intel_test_cmd_master_secure_erase(struct nfit_test *t,
		struct nd_intel_master_secure_erase *nd_cmd,
		unsigned int buf_len, int dimm)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
		dev_dbg(dev, "master secure erase: in wrong state\n");
	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
		dev_dbg(dev, "master secure erase: in wrong security state\n");
	} else if (memcmp(nd_cmd->passphrase, sec->master_passphrase,
				ND_INTEL_PASSPHRASE_SIZE) != 0) {
		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
		dev_dbg(dev, "master secure erase: wrong passphrase\n");
	} else {
		/* we do not erase master state passphrase ever */
		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
		sec->state = 0;
		dev_dbg(dev, "master secure erase: done\n");
	}

	return 0;
}


1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
{
	int i;

	/* lookup per-dimm data */
	for (i = 0; i < ARRAY_SIZE(handle); i++)
		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
			break;
	if (i >= ARRAY_SIZE(handle))
		return -ENXIO;
	return i;
}

1204 1205
static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1206
		unsigned int buf_len, int *cmd_rc)
1207 1208 1209
{
	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
1210
	unsigned int func = cmd;
1211 1212 1213 1214 1215
	int i, rc = 0, __cmd_rc;

	if (!cmd_rc)
		cmd_rc = &__cmd_rc;
	*cmd_rc = 0;
1216

1217 1218
	if (nvdimm) {
		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1219
		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
1220

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
		if (!nfit_mem)
			return -ENOTTY;

		if (cmd == ND_CMD_CALL) {
			struct nd_cmd_pkg *call_pkg = buf;

			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
			buf = (void *) call_pkg->nd_payload;
			func = call_pkg->nd_command;
			if (call_pkg->nd_family != nfit_mem->family)
				return -ENOTTY;
1232 1233 1234 1235 1236 1237

			i = get_dimm(nfit_mem, func);
			if (i < 0)
				return i;

			switch (func) {
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
			case NVDIMM_INTEL_GET_SECURITY_STATE:
				rc = nd_intel_test_cmd_security_status(t,
						buf, buf_len, i);
				break;
			case NVDIMM_INTEL_UNLOCK_UNIT:
				rc = nd_intel_test_cmd_unlock_unit(t,
						buf, buf_len, i);
				break;
			case NVDIMM_INTEL_SET_PASSPHRASE:
				rc = nd_intel_test_cmd_set_pass(t,
						buf, buf_len, i);
				break;
			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
				rc = nd_intel_test_cmd_disable_pass(t,
						buf, buf_len, i);
				break;
			case NVDIMM_INTEL_FREEZE_LOCK:
				rc = nd_intel_test_cmd_freeze_lock(t,
						buf, buf_len, i);
				break;
			case NVDIMM_INTEL_SECURE_ERASE:
				rc = nd_intel_test_cmd_secure_erase(t,
						buf, buf_len, i);
				break;
1262 1263 1264 1265 1266 1267 1268 1269
			case NVDIMM_INTEL_OVERWRITE:
				rc = nd_intel_test_cmd_overwrite(t,
						buf, buf_len, i - t->dcr_idx);
				break;
			case NVDIMM_INTEL_QUERY_OVERWRITE:
				rc = nd_intel_test_cmd_query_overwrite(t,
						buf, buf_len, i - t->dcr_idx);
				break;
1270 1271 1272 1273 1274 1275 1276 1277
			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
				rc = nd_intel_test_cmd_master_set_pass(t,
						buf, buf_len, i);
				break;
			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
				rc = nd_intel_test_cmd_master_secure_erase(t,
						buf, buf_len, i);
				break;
1278
			case ND_INTEL_ENABLE_LSS_STATUS:
1279
				rc = nd_intel_test_cmd_set_lss_status(t,
1280
						buf, buf_len);
1281
				break;
1282
			case ND_INTEL_FW_GET_INFO:
1283
				rc = nd_intel_test_get_fw_info(t, buf,
1284
						buf_len, i - t->dcr_idx);
1285
				break;
1286
			case ND_INTEL_FW_START_UPDATE:
1287
				rc = nd_intel_test_start_update(t, buf,
1288
						buf_len, i - t->dcr_idx);
1289
				break;
1290
			case ND_INTEL_FW_SEND_DATA:
1291
				rc = nd_intel_test_send_data(t, buf,
1292
						buf_len, i - t->dcr_idx);
1293
				break;
1294
			case ND_INTEL_FW_FINISH_UPDATE:
1295
				rc = nd_intel_test_finish_fw(t, buf,
1296
						buf_len, i - t->dcr_idx);
1297
				break;
1298
			case ND_INTEL_FW_FINISH_QUERY:
1299
				rc = nd_intel_test_finish_query(t, buf,
1300
						buf_len, i - t->dcr_idx);
1301
				break;
1302
			case ND_INTEL_SMART:
1303
				rc = nfit_test_cmd_smart(buf, buf_len,
1304
						&t->smart[i - t->dcr_idx]);
1305
				break;
1306
			case ND_INTEL_SMART_THRESHOLD:
1307
				rc = nfit_test_cmd_smart_threshold(buf,
1308 1309 1310
						buf_len,
						&t->smart_threshold[i -
							t->dcr_idx]);
1311
				break;
1312
			case ND_INTEL_SMART_SET_THRESHOLD:
1313
				rc = nfit_test_cmd_smart_set_threshold(buf,
1314 1315 1316 1317 1318
						buf_len,
						&t->smart_threshold[i -
							t->dcr_idx],
						&t->smart[i - t->dcr_idx],
						&t->pdev.dev, t->dimm_dev[i]);
1319
				break;
1320
			case ND_INTEL_SMART_INJECT:
1321
				rc = nfit_test_cmd_smart_inject(buf,
1322 1323 1324 1325 1326
						buf_len,
						&t->smart_threshold[i -
							t->dcr_idx],
						&t->smart[i - t->dcr_idx],
						&t->pdev.dev, t->dimm_dev[i]);
1327
				break;
1328 1329 1330
			default:
				return -ENOTTY;
			}
1331
			return override_return_code(i, func, rc);
1332 1333 1334 1335
		}

		if (!test_bit(cmd, &cmd_mask)
				|| !test_bit(func, &nfit_mem->dsm_mask))
1336 1337
			return -ENOTTY;

1338 1339 1340
		i = get_dimm(nfit_mem, func);
		if (i < 0)
			return i;
1341

1342
		switch (func) {
1343 1344
		case ND_CMD_GET_CONFIG_SIZE:
			rc = nfit_test_cmd_get_config_size(buf, buf_len);
1345
			break;
1346 1347
		case ND_CMD_GET_CONFIG_DATA:
			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1348
				t->label[i - t->dcr_idx]);
1349 1350 1351
			break;
		case ND_CMD_SET_CONFIG_DATA:
			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1352
				t->label[i - t->dcr_idx]);
1353 1354 1355 1356
			break;
		default:
			return -ENOTTY;
		}
1357
		return override_return_code(i, func, rc);
1358
	} else {
1359
		struct ars_state *ars_state = &t->ars_state;
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
		struct nd_cmd_pkg *call_pkg = buf;

		if (!nd_desc)
			return -ENOTTY;

		if (cmd == ND_CMD_CALL) {
			func = call_pkg->nd_command;

			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
			buf = (void *) call_pkg->nd_payload;

			switch (func) {
			case NFIT_CMD_TRANSLATE_SPA:
				rc = nfit_test_cmd_translate_spa(
					acpi_desc->nvdimm_bus, buf, buf_len);
				return rc;
D
Dave Jiang 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
			case NFIT_CMD_ARS_INJECT_SET:
				rc = nfit_test_cmd_ars_error_inject(t, buf,
					buf_len);
				return rc;
			case NFIT_CMD_ARS_INJECT_CLEAR:
				rc = nfit_test_cmd_ars_inject_clear(t, buf,
					buf_len);
				return rc;
			case NFIT_CMD_ARS_INJECT_GET:
				rc = nfit_test_cmd_ars_inject_status(t, buf,
					buf_len);
				return rc;
1388 1389 1390 1391
			default:
				return -ENOTTY;
			}
		}
1392

1393
		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
1394 1395
			return -ENOTTY;

1396
		switch (func) {
1397 1398 1399 1400
		case ND_CMD_ARS_CAP:
			rc = nfit_test_cmd_ars_cap(buf, buf_len);
			break;
		case ND_CMD_ARS_START:
D
Dave Jiang 已提交
1401 1402
			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
					buf_len, cmd_rc);
1403 1404
			break;
		case ND_CMD_ARS_STATUS:
1405 1406
			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
					cmd_rc);
1407
			break;
1408
		case ND_CMD_CLEAR_ERROR:
1409
			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1410
			break;
1411 1412 1413
		default:
			return -ENOTTY;
		}
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
	}

	return rc;
}

static DEFINE_SPINLOCK(nfit_test_lock);
static struct nfit_test *instances[NUM_NFITS];

static void release_nfit_res(void *data)
{
	struct nfit_test_resource *nfit_res = data;

	spin_lock(&nfit_test_lock);
	list_del(&nfit_res->list);
	spin_unlock(&nfit_test_lock);

1430 1431 1432
	if (resource_size(&nfit_res->res) >= DIMM_SIZE)
		gen_pool_free(nfit_pool, nfit_res->res.start,
				resource_size(&nfit_res->res));
1433
	vfree(nfit_res->buf);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	kfree(nfit_res);
}

static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
		void *buf)
{
	struct device *dev = &t->pdev.dev;
	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
			GFP_KERNEL);
	int rc;

1445
	if (!buf || !nfit_res || !*dma)
1446 1447 1448 1449 1450 1451 1452 1453
		goto err;
	rc = devm_add_action(dev, release_nfit_res, nfit_res);
	if (rc)
		goto err;
	INIT_LIST_HEAD(&nfit_res->list);
	memset(buf, 0, size);
	nfit_res->dev = dev;
	nfit_res->buf = buf;
1454 1455 1456 1457 1458
	nfit_res->res.start = *dma;
	nfit_res->res.end = *dma + size - 1;
	nfit_res->res.name = "NFIT";
	spin_lock_init(&nfit_res->lock);
	INIT_LIST_HEAD(&nfit_res->requests);
1459 1460 1461 1462 1463 1464
	spin_lock(&nfit_test_lock);
	list_add(&nfit_res->list, &t->resources);
	spin_unlock(&nfit_test_lock);

	return nfit_res->buf;
 err:
1465 1466
	if (*dma && size >= DIMM_SIZE)
		gen_pool_free(nfit_pool, *dma, size);
1467
	if (buf)
1468 1469 1470 1471 1472 1473 1474
		vfree(buf);
	kfree(nfit_res);
	return NULL;
}

static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
{
1475 1476 1477
	struct genpool_data_align data = {
		.align = SZ_128M,
	};
1478 1479
	void *buf = vmalloc(size);

1480 1481 1482 1483 1484
	if (size >= DIMM_SIZE)
		*dma = gen_pool_alloc_algo(nfit_pool, size,
				gen_pool_first_fit_align, &data);
	else
		*dma = (unsigned long) buf;
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	return __test_alloc(t, size, dma, buf);
}

static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(instances); i++) {
		struct nfit_test_resource *n, *nfit_res = NULL;
		struct nfit_test *t = instances[i];

		if (!t)
			continue;
		spin_lock(&nfit_test_lock);
		list_for_each_entry(n, &t->resources, list) {
1500 1501
			if (addr >= n->res.start && (addr < n->res.start
						+ resource_size(&n->res))) {
1502 1503 1504 1505
				nfit_res = n;
				break;
			} else if (addr >= (unsigned long) n->buf
					&& (addr < (unsigned long) n->buf
1506
						+ resource_size(&n->res))) {
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
				nfit_res = n;
				break;
			}
		}
		spin_unlock(&nfit_test_lock);
		if (nfit_res)
			return nfit_res;
	}

	return NULL;
}

1519 1520
static int ars_state_init(struct device *dev, struct ars_state *ars_state)
{
D
Dave Jiang 已提交
1521
	/* for testing, only store up to n records that fit within 4k */
1522
	ars_state->ars_status = devm_kzalloc(dev,
D
Dave Jiang 已提交
1523
			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1524 1525 1526 1527 1528 1529
	if (!ars_state->ars_status)
		return -ENOMEM;
	spin_lock_init(&ars_state->lock);
	return 0;
}

1530 1531
static void put_dimms(void *data)
{
1532
	struct nfit_test *t = data;
1533 1534
	int i;

1535 1536 1537
	for (i = 0; i < t->num_dcr; i++)
		if (t->dimm_dev[i])
			device_unregister(t->dimm_dev[i]);
1538 1539 1540 1541
}

static struct class *nfit_test_dimm;

1542 1543 1544 1545
static int dimm_name_to_id(struct device *dev)
{
	int dimm;

1546
	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
		return -ENXIO;
	return dimm;
}

static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	int dimm = dimm_name_to_id(dev);

	if (dimm < 0)
		return dimm;

1559
	return sprintf(buf, "%#x\n", handle[dimm]);
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
}
DEVICE_ATTR_RO(handle);

static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	int dimm = dimm_name_to_id(dev);

	if (dimm < 0)
		return dimm;

	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
}

static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t size)
{
	int dimm = dimm_name_to_id(dev);
	unsigned long val;
	ssize_t rc;

	if (dimm < 0)
		return dimm;

	rc = kstrtol(buf, 0, &val);
	if (rc)
		return rc;

	dimm_fail_cmd_flags[dimm] = val;
	return size;
}
static DEVICE_ATTR_RW(fail_cmd);

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	int dimm = dimm_name_to_id(dev);

	if (dimm < 0)
		return dimm;

	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
}

static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t size)
{
	int dimm = dimm_name_to_id(dev);
	unsigned long val;
	ssize_t rc;

	if (dimm < 0)
		return dimm;

	rc = kstrtol(buf, 0, &val);
	if (rc)
		return rc;

	dimm_fail_cmd_code[dimm] = val;
	return size;
}
static DEVICE_ATTR_RW(fail_cmd_code);

1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
static ssize_t lock_dimm_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	int dimm = dimm_name_to_id(dev);
	struct nfit_test_sec *sec = &dimm_sec_info[dimm];

	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
	return size;
}
static DEVICE_ATTR_WO(lock_dimm);

1634 1635
static struct attribute *nfit_test_dimm_attributes[] = {
	&dev_attr_fail_cmd.attr,
1636
	&dev_attr_fail_cmd_code.attr,
1637
	&dev_attr_handle.attr,
1638
	&dev_attr_lock_dimm.attr,
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	NULL,
};

static struct attribute_group nfit_test_dimm_attribute_group = {
	.attrs = nfit_test_dimm_attributes,
};

static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
	&nfit_test_dimm_attribute_group,
	NULL,
};

1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
static int nfit_test_dimm_init(struct nfit_test *t)
{
	int i;

	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
		return -ENOMEM;
	for (i = 0; i < t->num_dcr; i++) {
		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
				&t->pdev.dev, 0, NULL,
				nfit_test_dimm_attribute_groups,
				"test_dimm%d", i + t->dcr_idx);
		if (!t->dimm_dev[i])
			return -ENOMEM;
	}
	return 0;
}

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
static void security_init(struct nfit_test *t)
{
	int i;

	for (i = 0; i < t->num_dcr; i++) {
		struct nfit_test_sec *sec = &dimm_sec_info[i];

		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
	}
}

1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
static void smart_init(struct nfit_test *t)
{
	int i;
	const struct nd_intel_smart_threshold smart_t_data = {
		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
			| ND_INTEL_SMART_TEMP_TRIP,
		.media_temperature = 40 * 16,
		.ctrl_temperature = 30 * 16,
		.spares = 5,
	};

	for (i = 0; i < t->num_dcr; i++) {
1691
		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1692 1693 1694 1695 1696
		memcpy(&t->smart_threshold[i], &smart_t_data,
				sizeof(smart_t_data));
	}
}

1697 1698
static int nfit_test0_alloc(struct nfit_test *t)
{
1699
	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
1700 1701
			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
1702 1703
			+ offsetof(struct acpi_nfit_control_region,
					window_size) * NUM_DCR
1704
			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
1705
			+ (sizeof(struct acpi_nfit_flush_address)
1706 1707
					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
			+ sizeof(struct acpi_nfit_capabilities);
1708 1709 1710 1711 1712 1713 1714
	int i;

	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
	if (!t->nfit_buf)
		return -ENOMEM;
	t->nfit_size = nfit_size;

1715
	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
1716 1717 1718
	if (!t->spa_set[0])
		return -ENOMEM;

1719
	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
1720 1721 1722
	if (!t->spa_set[1])
		return -ENOMEM;

1723
	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
V
Vishal Verma 已提交
1724 1725 1726
	if (!t->spa_set[2])
		return -ENOMEM;

1727
	for (i = 0; i < t->num_dcr; i++) {
1728 1729 1730 1731 1732 1733 1734 1735
		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
		if (!t->dimm[i])
			return -ENOMEM;

		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
		if (!t->label[i])
			return -ENOMEM;
		sprintf(t->label[i], "label%d", i);
1736

1737 1738
		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
					sizeof(u64) * NUM_HINTS),
1739
				&t->flush_dma[i]);
1740 1741
		if (!t->flush[i])
			return -ENOMEM;
1742 1743
	}

1744
	for (i = 0; i < t->num_dcr; i++) {
1745 1746 1747 1748 1749
		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
		if (!t->dcr[i])
			return -ENOMEM;
	}

1750 1751 1752 1753
	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
	if (!t->_fit)
		return -ENOMEM;

1754
	if (nfit_test_dimm_init(t))
1755
		return -ENOMEM;
1756
	smart_init(t);
1757
	security_init(t);
1758
	return ars_state_init(&t->pdev.dev, &t->ars_state);
1759 1760 1761 1762
}

static int nfit_test1_alloc(struct nfit_test *t)
{
1763
	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1764 1765
		+ sizeof(struct acpi_nfit_memory_map) * 2
		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1766
	int i;
1767 1768 1769 1770 1771 1772

	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
	if (!t->nfit_buf)
		return -ENOMEM;
	t->nfit_size = nfit_size;

1773
	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
1774 1775 1776
	if (!t->spa_set[0])
		return -ENOMEM;

1777 1778 1779 1780 1781 1782 1783
	for (i = 0; i < t->num_dcr; i++) {
		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
		if (!t->label[i])
			return -ENOMEM;
		sprintf(t->label[i], "label%d", i);
	}

1784 1785 1786 1787
	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
	if (!t->spa_set[1])
		return -ENOMEM;

1788 1789
	if (nfit_test_dimm_init(t))
		return -ENOMEM;
1790
	smart_init(t);
1791
	return ars_state_init(&t->pdev.dev, &t->ars_state);
1792 1793
}

1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
static void dcr_common_init(struct acpi_nfit_control_region *dcr)
{
	dcr->vendor_id = 0xabcd;
	dcr->device_id = 0;
	dcr->revision_id = 1;
	dcr->valid_fields = 1;
	dcr->manufacturing_location = 0xa;
	dcr->manufacturing_date = cpu_to_be16(2016);
}

1804 1805
static void nfit_test0_setup(struct nfit_test *t)
{
1806 1807
	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
		+ (sizeof(u64) * NUM_HINTS);
1808 1809 1810 1811 1812 1813
	struct acpi_nfit_desc *acpi_desc;
	struct acpi_nfit_memory_map *memdev;
	void *nfit_buf = t->nfit_buf;
	struct acpi_nfit_system_address *spa;
	struct acpi_nfit_control_region *dcr;
	struct acpi_nfit_data_region *bdw;
1814
	struct acpi_nfit_flush_address *flush;
1815
	struct acpi_nfit_capabilities *pcap;
1816
	unsigned int offset = 0, i;
1817 1818 1819 1820 1821 1822

	/*
	 * spa0 (interleave first half of dimm0 and dimm1, note storage
	 * does not actually alias the related block-data-window
	 * regions)
	 */
1823
	spa = nfit_buf;
1824 1825 1826 1827 1828 1829
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
	spa->range_index = 0+1;
	spa->address = t->spa_set_dma[0];
	spa->length = SPA0_SIZE;
1830
	offset += spa->header.length;
1831 1832 1833 1834 1835 1836

	/*
	 * spa1 (interleave last half of the 4 DIMMS, note storage
	 * does not actually alias the related block-data-window
	 * regions)
	 */
1837
	spa = nfit_buf + offset;
1838 1839 1840 1841 1842 1843
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
	spa->range_index = 1+1;
	spa->address = t->spa_set_dma[1];
	spa->length = SPA1_SIZE;
1844
	offset += spa->header.length;
1845 1846

	/* spa2 (dcr0) dimm0 */
1847
	spa = nfit_buf + offset;
1848 1849 1850 1851 1852 1853
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
	spa->range_index = 2+1;
	spa->address = t->dcr_dma[0];
	spa->length = DCR_SIZE;
1854
	offset += spa->header.length;
1855 1856

	/* spa3 (dcr1) dimm1 */
1857
	spa = nfit_buf + offset;
1858 1859 1860 1861 1862 1863
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
	spa->range_index = 3+1;
	spa->address = t->dcr_dma[1];
	spa->length = DCR_SIZE;
1864
	offset += spa->header.length;
1865 1866

	/* spa4 (dcr2) dimm2 */
1867
	spa = nfit_buf + offset;
1868 1869 1870 1871 1872 1873
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
	spa->range_index = 4+1;
	spa->address = t->dcr_dma[2];
	spa->length = DCR_SIZE;
1874
	offset += spa->header.length;
1875 1876

	/* spa5 (dcr3) dimm3 */
1877
	spa = nfit_buf + offset;
1878 1879 1880 1881 1882 1883
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
	spa->range_index = 5+1;
	spa->address = t->dcr_dma[3];
	spa->length = DCR_SIZE;
1884
	offset += spa->header.length;
1885 1886

	/* spa6 (bdw for dcr0) dimm0 */
1887
	spa = nfit_buf + offset;
1888 1889 1890 1891 1892 1893
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
	spa->range_index = 6+1;
	spa->address = t->dimm_dma[0];
	spa->length = DIMM_SIZE;
1894
	offset += spa->header.length;
1895 1896

	/* spa7 (bdw for dcr1) dimm1 */
1897
	spa = nfit_buf + offset;
1898 1899 1900 1901 1902 1903
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
	spa->range_index = 7+1;
	spa->address = t->dimm_dma[1];
	spa->length = DIMM_SIZE;
1904
	offset += spa->header.length;
1905 1906

	/* spa8 (bdw for dcr2) dimm2 */
1907
	spa = nfit_buf + offset;
1908 1909 1910 1911 1912 1913
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
	spa->range_index = 8+1;
	spa->address = t->dimm_dma[2];
	spa->length = DIMM_SIZE;
1914
	offset += spa->header.length;
1915 1916

	/* spa9 (bdw for dcr3) dimm3 */
1917
	spa = nfit_buf + offset;
1918 1919 1920 1921 1922 1923
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
	spa->range_index = 9+1;
	spa->address = t->dimm_dma[3];
	spa->length = DIMM_SIZE;
1924
	offset += spa->header.length;
1925 1926 1927 1928 1929 1930 1931 1932 1933

	/* mem-region0 (spa0, dimm0) */
	memdev = nfit_buf + offset;
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[0];
	memdev->physical_id = 0;
	memdev->region_id = 0;
	memdev->range_index = 0+1;
1934
	memdev->region_index = 4+1;
1935
	memdev->region_size = SPA0_SIZE/2;
1936
	memdev->region_offset = 1;
1937 1938 1939
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 2;
1940
	offset += memdev->header.length;
1941 1942

	/* mem-region1 (spa0, dimm1) */
1943
	memdev = nfit_buf + offset;
1944 1945 1946 1947 1948 1949
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[1];
	memdev->physical_id = 1;
	memdev->region_id = 0;
	memdev->range_index = 0+1;
1950
	memdev->region_index = 5+1;
1951
	memdev->region_size = SPA0_SIZE/2;
1952
	memdev->region_offset = (1 << 8);
1953 1954 1955
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 2;
1956
	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1957
	offset += memdev->header.length;
1958 1959

	/* mem-region2 (spa1, dimm0) */
1960
	memdev = nfit_buf + offset;
1961 1962 1963 1964 1965 1966
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[0];
	memdev->physical_id = 0;
	memdev->region_id = 1;
	memdev->range_index = 1+1;
1967
	memdev->region_index = 4+1;
1968
	memdev->region_size = SPA1_SIZE/4;
1969
	memdev->region_offset = (1 << 16);
1970 1971 1972
	memdev->address = SPA0_SIZE/2;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 4;
1973
	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1974
	offset += memdev->header.length;
1975 1976

	/* mem-region3 (spa1, dimm1) */
1977
	memdev = nfit_buf + offset;
1978 1979 1980 1981 1982 1983
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[1];
	memdev->physical_id = 1;
	memdev->region_id = 1;
	memdev->range_index = 1+1;
1984
	memdev->region_index = 5+1;
1985
	memdev->region_size = SPA1_SIZE/4;
1986
	memdev->region_offset = (1 << 24);
1987 1988 1989
	memdev->address = SPA0_SIZE/2;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 4;
1990
	offset += memdev->header.length;
1991 1992

	/* mem-region4 (spa1, dimm2) */
1993
	memdev = nfit_buf + offset;
1994 1995 1996 1997 1998 1999
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[2];
	memdev->physical_id = 2;
	memdev->region_id = 0;
	memdev->range_index = 1+1;
2000
	memdev->region_index = 6+1;
2001
	memdev->region_size = SPA1_SIZE/4;
2002
	memdev->region_offset = (1ULL << 32);
2003 2004 2005
	memdev->address = SPA0_SIZE/2;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 4;
2006
	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2007
	offset += memdev->header.length;
2008 2009

	/* mem-region5 (spa1, dimm3) */
2010
	memdev = nfit_buf + offset;
2011 2012 2013 2014 2015 2016
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[3];
	memdev->physical_id = 3;
	memdev->region_id = 0;
	memdev->range_index = 1+1;
2017
	memdev->region_index = 7+1;
2018
	memdev->region_size = SPA1_SIZE/4;
2019
	memdev->region_offset = (1ULL << 40);
2020 2021 2022
	memdev->address = SPA0_SIZE/2;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 4;
2023
	offset += memdev->header.length;
2024 2025

	/* mem-region6 (spa/dcr0, dimm0) */
2026
	memdev = nfit_buf + offset;
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[0];
	memdev->physical_id = 0;
	memdev->region_id = 0;
	memdev->range_index = 2+1;
	memdev->region_index = 0+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2039
	offset += memdev->header.length;
2040 2041

	/* mem-region7 (spa/dcr1, dimm1) */
2042
	memdev = nfit_buf + offset;
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[1];
	memdev->physical_id = 1;
	memdev->region_id = 0;
	memdev->range_index = 3+1;
	memdev->region_index = 1+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2055
	offset += memdev->header.length;
2056 2057

	/* mem-region8 (spa/dcr2, dimm2) */
2058
	memdev = nfit_buf + offset;
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[2];
	memdev->physical_id = 2;
	memdev->region_id = 0;
	memdev->range_index = 4+1;
	memdev->region_index = 2+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2071
	offset += memdev->header.length;
2072 2073

	/* mem-region9 (spa/dcr3, dimm3) */
2074
	memdev = nfit_buf + offset;
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[3];
	memdev->physical_id = 3;
	memdev->region_id = 0;
	memdev->range_index = 5+1;
	memdev->region_index = 3+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2087
	offset += memdev->header.length;
2088 2089

	/* mem-region10 (spa/bdw0, dimm0) */
2090
	memdev = nfit_buf + offset;
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[0];
	memdev->physical_id = 0;
	memdev->region_id = 0;
	memdev->range_index = 6+1;
	memdev->region_index = 0+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2103
	offset += memdev->header.length;
2104 2105

	/* mem-region11 (spa/bdw1, dimm1) */
2106
	memdev = nfit_buf + offset;
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[1];
	memdev->physical_id = 1;
	memdev->region_id = 0;
	memdev->range_index = 7+1;
	memdev->region_index = 1+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2119
	offset += memdev->header.length;
2120 2121

	/* mem-region12 (spa/bdw2, dimm2) */
2122
	memdev = nfit_buf + offset;
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[2];
	memdev->physical_id = 2;
	memdev->region_id = 0;
	memdev->range_index = 8+1;
	memdev->region_index = 2+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2135
	offset += memdev->header.length;
2136 2137

	/* mem-region13 (spa/dcr3, dimm3) */
2138
	memdev = nfit_buf + offset;
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[3];
	memdev->physical_id = 3;
	memdev->region_id = 0;
	memdev->range_index = 9+1;
	memdev->region_index = 3+1;
	memdev->region_size = 0;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2151
	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2152
	offset += memdev->header.length;
2153

2154
	/* dcr-descriptor0: blk */
2155 2156
	dcr = nfit_buf + offset;
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2157
	dcr->header.length = sizeof(*dcr);
2158
	dcr->region_index = 0+1;
2159
	dcr_common_init(dcr);
2160
	dcr->serial_number = ~handle[0];
2161
	dcr->code = NFIT_FIC_BLK;
2162 2163 2164 2165 2166 2167
	dcr->windows = 1;
	dcr->window_size = DCR_SIZE;
	dcr->command_offset = 0;
	dcr->command_size = 8;
	dcr->status_offset = 8;
	dcr->status_size = 4;
2168
	offset += dcr->header.length;
2169

2170
	/* dcr-descriptor1: blk */
2171
	dcr = nfit_buf + offset;
2172
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2173
	dcr->header.length = sizeof(*dcr);
2174
	dcr->region_index = 1+1;
2175
	dcr_common_init(dcr);
2176
	dcr->serial_number = ~handle[1];
2177
	dcr->code = NFIT_FIC_BLK;
2178 2179 2180 2181 2182 2183
	dcr->windows = 1;
	dcr->window_size = DCR_SIZE;
	dcr->command_offset = 0;
	dcr->command_size = 8;
	dcr->status_offset = 8;
	dcr->status_size = 4;
2184
	offset += dcr->header.length;
2185

2186
	/* dcr-descriptor2: blk */
2187
	dcr = nfit_buf + offset;
2188
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2189
	dcr->header.length = sizeof(*dcr);
2190
	dcr->region_index = 2+1;
2191
	dcr_common_init(dcr);
2192
	dcr->serial_number = ~handle[2];
2193
	dcr->code = NFIT_FIC_BLK;
2194 2195 2196 2197 2198 2199
	dcr->windows = 1;
	dcr->window_size = DCR_SIZE;
	dcr->command_offset = 0;
	dcr->command_size = 8;
	dcr->status_offset = 8;
	dcr->status_size = 4;
2200
	offset += dcr->header.length;
2201

2202
	/* dcr-descriptor3: blk */
2203
	dcr = nfit_buf + offset;
2204
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2205
	dcr->header.length = sizeof(*dcr);
2206
	dcr->region_index = 3+1;
2207
	dcr_common_init(dcr);
2208
	dcr->serial_number = ~handle[3];
2209
	dcr->code = NFIT_FIC_BLK;
2210 2211 2212 2213 2214 2215
	dcr->windows = 1;
	dcr->window_size = DCR_SIZE;
	dcr->command_offset = 0;
	dcr->command_size = 8;
	dcr->status_offset = 8;
	dcr->status_size = 4;
2216
	offset += dcr->header.length;
2217

2218 2219 2220 2221 2222 2223
	/* dcr-descriptor0: pmem */
	dcr = nfit_buf + offset;
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
	dcr->header.length = offsetof(struct acpi_nfit_control_region,
			window_size);
	dcr->region_index = 4+1;
2224
	dcr_common_init(dcr);
2225 2226 2227
	dcr->serial_number = ~handle[0];
	dcr->code = NFIT_FIC_BYTEN;
	dcr->windows = 0;
2228
	offset += dcr->header.length;
2229 2230

	/* dcr-descriptor1: pmem */
2231
	dcr = nfit_buf + offset;
2232 2233 2234 2235
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
	dcr->header.length = offsetof(struct acpi_nfit_control_region,
			window_size);
	dcr->region_index = 5+1;
2236
	dcr_common_init(dcr);
2237 2238 2239
	dcr->serial_number = ~handle[1];
	dcr->code = NFIT_FIC_BYTEN;
	dcr->windows = 0;
2240
	offset += dcr->header.length;
2241 2242

	/* dcr-descriptor2: pmem */
2243
	dcr = nfit_buf + offset;
2244 2245 2246 2247
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
	dcr->header.length = offsetof(struct acpi_nfit_control_region,
			window_size);
	dcr->region_index = 6+1;
2248
	dcr_common_init(dcr);
2249 2250 2251
	dcr->serial_number = ~handle[2];
	dcr->code = NFIT_FIC_BYTEN;
	dcr->windows = 0;
2252
	offset += dcr->header.length;
2253 2254

	/* dcr-descriptor3: pmem */
2255
	dcr = nfit_buf + offset;
2256 2257 2258 2259
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
	dcr->header.length = offsetof(struct acpi_nfit_control_region,
			window_size);
	dcr->region_index = 7+1;
2260
	dcr_common_init(dcr);
2261 2262 2263
	dcr->serial_number = ~handle[3];
	dcr->code = NFIT_FIC_BYTEN;
	dcr->windows = 0;
2264
	offset += dcr->header.length;
2265

2266 2267 2268
	/* bdw0 (spa/dcr0, dimm0) */
	bdw = nfit_buf + offset;
	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2269
	bdw->header.length = sizeof(*bdw);
2270 2271 2272 2273 2274 2275
	bdw->region_index = 0+1;
	bdw->windows = 1;
	bdw->offset = 0;
	bdw->size = BDW_SIZE;
	bdw->capacity = DIMM_SIZE;
	bdw->start_address = 0;
2276
	offset += bdw->header.length;
2277 2278

	/* bdw1 (spa/dcr1, dimm1) */
2279
	bdw = nfit_buf + offset;
2280
	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2281
	bdw->header.length = sizeof(*bdw);
2282 2283 2284 2285 2286 2287
	bdw->region_index = 1+1;
	bdw->windows = 1;
	bdw->offset = 0;
	bdw->size = BDW_SIZE;
	bdw->capacity = DIMM_SIZE;
	bdw->start_address = 0;
2288
	offset += bdw->header.length;
2289 2290

	/* bdw2 (spa/dcr2, dimm2) */
2291
	bdw = nfit_buf + offset;
2292
	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2293
	bdw->header.length = sizeof(*bdw);
2294 2295 2296 2297 2298 2299
	bdw->region_index = 2+1;
	bdw->windows = 1;
	bdw->offset = 0;
	bdw->size = BDW_SIZE;
	bdw->capacity = DIMM_SIZE;
	bdw->start_address = 0;
2300
	offset += bdw->header.length;
2301 2302

	/* bdw3 (spa/dcr3, dimm3) */
2303
	bdw = nfit_buf + offset;
2304
	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2305
	bdw->header.length = sizeof(*bdw);
2306 2307 2308 2309 2310 2311
	bdw->region_index = 3+1;
	bdw->windows = 1;
	bdw->offset = 0;
	bdw->size = BDW_SIZE;
	bdw->capacity = DIMM_SIZE;
	bdw->start_address = 0;
2312
	offset += bdw->header.length;
2313

2314 2315 2316
	/* flush0 (dimm0) */
	flush = nfit_buf + offset;
	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
2317
	flush->header.length = flush_hint_size;
2318
	flush->device_handle = handle[0];
2319 2320 2321
	flush->hint_count = NUM_HINTS;
	for (i = 0; i < NUM_HINTS; i++)
		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2322
	offset += flush->header.length;
2323 2324

	/* flush1 (dimm1) */
2325
	flush = nfit_buf + offset;
2326
	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
2327
	flush->header.length = flush_hint_size;
2328
	flush->device_handle = handle[1];
2329 2330 2331
	flush->hint_count = NUM_HINTS;
	for (i = 0; i < NUM_HINTS; i++)
		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2332
	offset += flush->header.length;
2333 2334

	/* flush2 (dimm2) */
2335
	flush = nfit_buf + offset;
2336
	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
2337
	flush->header.length = flush_hint_size;
2338
	flush->device_handle = handle[2];
2339 2340 2341
	flush->hint_count = NUM_HINTS;
	for (i = 0; i < NUM_HINTS; i++)
		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2342
	offset += flush->header.length;
2343 2344

	/* flush3 (dimm3) */
2345
	flush = nfit_buf + offset;
2346
	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
2347
	flush->header.length = flush_hint_size;
2348
	flush->device_handle = handle[3];
2349 2350 2351
	flush->hint_count = NUM_HINTS;
	for (i = 0; i < NUM_HINTS; i++)
		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2352
	offset += flush->header.length;
2353

2354
	/* platform capabilities */
2355
	pcap = nfit_buf + offset;
2356 2357 2358
	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
	pcap->header.length = sizeof(*pcap);
	pcap->highest_capability = 1;
2359
	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2360
	offset += pcap->header.length;
2361

V
Vishal Verma 已提交
2362
	if (t->setup_hotplug) {
2363
		/* dcr-descriptor4: blk */
V
Vishal Verma 已提交
2364 2365
		dcr = nfit_buf + offset;
		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2366
		dcr->header.length = sizeof(*dcr);
2367
		dcr->region_index = 8+1;
2368
		dcr_common_init(dcr);
V
Vishal Verma 已提交
2369
		dcr->serial_number = ~handle[4];
2370
		dcr->code = NFIT_FIC_BLK;
V
Vishal Verma 已提交
2371 2372 2373 2374 2375 2376
		dcr->windows = 1;
		dcr->window_size = DCR_SIZE;
		dcr->command_offset = 0;
		dcr->command_size = 8;
		dcr->status_offset = 8;
		dcr->status_size = 4;
2377
		offset += dcr->header.length;
V
Vishal Verma 已提交
2378

2379 2380 2381 2382 2383 2384
		/* dcr-descriptor4: pmem */
		dcr = nfit_buf + offset;
		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
		dcr->header.length = offsetof(struct acpi_nfit_control_region,
				window_size);
		dcr->region_index = 9+1;
2385
		dcr_common_init(dcr);
2386 2387 2388
		dcr->serial_number = ~handle[4];
		dcr->code = NFIT_FIC_BYTEN;
		dcr->windows = 0;
2389
		offset += dcr->header.length;
2390

V
Vishal Verma 已提交
2391 2392 2393
		/* bdw4 (spa/dcr4, dimm4) */
		bdw = nfit_buf + offset;
		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2394
		bdw->header.length = sizeof(*bdw);
2395
		bdw->region_index = 8+1;
V
Vishal Verma 已提交
2396 2397 2398 2399 2400
		bdw->windows = 1;
		bdw->offset = 0;
		bdw->size = BDW_SIZE;
		bdw->capacity = DIMM_SIZE;
		bdw->start_address = 0;
2401
		offset += bdw->header.length;
V
Vishal Verma 已提交
2402 2403 2404 2405 2406 2407 2408 2409 2410

		/* spa10 (dcr4) dimm4 */
		spa = nfit_buf + offset;
		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
		spa->header.length = sizeof(*spa);
		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
		spa->range_index = 10+1;
		spa->address = t->dcr_dma[4];
		spa->length = DCR_SIZE;
2411
		offset += spa->header.length;
V
Vishal Verma 已提交
2412 2413 2414 2415 2416 2417

		/*
		 * spa11 (single-dimm interleave for hotplug, note storage
		 * does not actually alias the related block-data-window
		 * regions)
		 */
2418
		spa = nfit_buf + offset;
V
Vishal Verma 已提交
2419 2420 2421 2422 2423 2424
		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
		spa->header.length = sizeof(*spa);
		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
		spa->range_index = 11+1;
		spa->address = t->spa_set_dma[2];
		spa->length = SPA0_SIZE;
2425
		offset += spa->header.length;
V
Vishal Verma 已提交
2426 2427

		/* spa12 (bdw for dcr4) dimm4 */
2428
		spa = nfit_buf + offset;
V
Vishal Verma 已提交
2429 2430 2431 2432 2433 2434
		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
		spa->header.length = sizeof(*spa);
		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
		spa->range_index = 12+1;
		spa->address = t->dimm_dma[4];
		spa->length = DIMM_SIZE;
2435
		offset += spa->header.length;
V
Vishal Verma 已提交
2436 2437 2438 2439 2440 2441 2442 2443 2444

		/* mem-region14 (spa/dcr4, dimm4) */
		memdev = nfit_buf + offset;
		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
		memdev->header.length = sizeof(*memdev);
		memdev->device_handle = handle[4];
		memdev->physical_id = 4;
		memdev->region_id = 0;
		memdev->range_index = 10+1;
2445
		memdev->region_index = 8+1;
V
Vishal Verma 已提交
2446 2447 2448 2449 2450
		memdev->region_size = 0;
		memdev->region_offset = 0;
		memdev->address = 0;
		memdev->interleave_index = 0;
		memdev->interleave_ways = 1;
2451
		offset += memdev->header.length;
V
Vishal Verma 已提交
2452

2453 2454
		/* mem-region15 (spa11, dimm4) */
		memdev = nfit_buf + offset;
V
Vishal Verma 已提交
2455 2456 2457 2458 2459 2460
		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
		memdev->header.length = sizeof(*memdev);
		memdev->device_handle = handle[4];
		memdev->physical_id = 4;
		memdev->region_id = 0;
		memdev->range_index = 11+1;
2461
		memdev->region_index = 9+1;
V
Vishal Verma 已提交
2462
		memdev->region_size = SPA0_SIZE;
2463
		memdev->region_offset = (1ULL << 48);
V
Vishal Verma 已提交
2464 2465 2466
		memdev->address = 0;
		memdev->interleave_index = 0;
		memdev->interleave_ways = 1;
2467
		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2468
		offset += memdev->header.length;
V
Vishal Verma 已提交
2469

2470
		/* mem-region16 (spa/bdw4, dimm4) */
2471
		memdev = nfit_buf + offset;
V
Vishal Verma 已提交
2472 2473 2474 2475 2476 2477
		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
		memdev->header.length = sizeof(*memdev);
		memdev->device_handle = handle[4];
		memdev->physical_id = 4;
		memdev->region_id = 0;
		memdev->range_index = 12+1;
2478
		memdev->region_index = 8+1;
V
Vishal Verma 已提交
2479 2480 2481 2482 2483
		memdev->region_size = 0;
		memdev->region_offset = 0;
		memdev->address = 0;
		memdev->interleave_index = 0;
		memdev->interleave_ways = 1;
2484
		offset += memdev->header.length;
V
Vishal Verma 已提交
2485 2486 2487 2488

		/* flush3 (dimm4) */
		flush = nfit_buf + offset;
		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
2489
		flush->header.length = flush_hint_size;
V
Vishal Verma 已提交
2490
		flush->device_handle = handle[4];
2491 2492 2493 2494
		flush->hint_count = NUM_HINTS;
		for (i = 0; i < NUM_HINTS; i++)
			flush->hint_address[i] = t->flush_dma[4]
				+ i * sizeof(u64);
2495
		offset += flush->header.length;
2496 2497 2498

		/* sanity check to make sure we've filled the buffer */
		WARN_ON(offset != t->nfit_size);
V
Vishal Verma 已提交
2499 2500
	}

2501 2502
	t->nfit_filled = offset;

D
Dave Jiang 已提交
2503 2504
	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
			SPA0_SIZE);
2505

2506
	acpi_desc = &t->acpi_desc;
2507 2508 2509
	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2510 2511 2512
	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2513
	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2514 2515 2516 2517
	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2518 2519
	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
D
Dave Jiang 已提交
2520 2521 2522
	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2523 2524 2525 2526 2527
	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2528
	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
2529 2530 2531 2532 2533 2534 2535 2536
	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
			&acpi_desc->dimm_cmd_force_en);
	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
			&acpi_desc->dimm_cmd_force_en);
	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2537 2538
	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2539 2540 2541 2542
	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
			&acpi_desc->dimm_cmd_force_en);
	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
			&acpi_desc->dimm_cmd_force_en);
2543 2544 2545 2546
}

static void nfit_test1_setup(struct nfit_test *t)
{
2547
	size_t offset;
2548 2549 2550 2551
	void *nfit_buf = t->nfit_buf;
	struct acpi_nfit_memory_map *memdev;
	struct acpi_nfit_control_region *dcr;
	struct acpi_nfit_system_address *spa;
2552
	struct acpi_nfit_desc *acpi_desc;
2553

2554
	offset = 0;
2555 2556 2557 2558 2559 2560 2561 2562
	/* spa0 (flat range with no bdw aliasing) */
	spa = nfit_buf + offset;
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
	spa->range_index = 0+1;
	spa->address = t->spa_set_dma[0];
	spa->length = SPA2_SIZE;
2563
	offset += spa->header.length;
2564

2565
	/* virtual cd region */
2566
	spa = nfit_buf + offset;
2567 2568 2569 2570 2571 2572
	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
	spa->header.length = sizeof(*spa);
	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
	spa->range_index = 0;
	spa->address = t->spa_set_dma[1];
	spa->length = SPA_VCD_SIZE;
2573
	offset += spa->header.length;
2574

2575 2576 2577 2578
	/* mem-region0 (spa0, dimm0) */
	memdev = nfit_buf + offset;
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
2579
	memdev->device_handle = handle[5];
2580 2581 2582 2583 2584 2585 2586 2587 2588
	memdev->physical_id = 0;
	memdev->region_id = 0;
	memdev->range_index = 0+1;
	memdev->region_index = 0+1;
	memdev->region_size = SPA2_SIZE;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
2589 2590
	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2591
		| ACPI_NFIT_MEM_NOT_ARMED;
2592
	offset += memdev->header.length;
2593 2594 2595 2596

	/* dcr-descriptor0 */
	dcr = nfit_buf + offset;
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2597 2598
	dcr->header.length = offsetof(struct acpi_nfit_control_region,
			window_size);
2599
	dcr->region_index = 0+1;
2600
	dcr_common_init(dcr);
2601
	dcr->serial_number = ~handle[5];
2602
	dcr->code = NFIT_FIC_BYTE;
2603
	dcr->windows = 0;
2604
	offset += dcr->header.length;
2605

2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
	memdev = nfit_buf + offset;
	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
	memdev->header.length = sizeof(*memdev);
	memdev->device_handle = handle[6];
	memdev->physical_id = 0;
	memdev->region_id = 0;
	memdev->range_index = 0;
	memdev->region_index = 0+2;
	memdev->region_size = SPA2_SIZE;
	memdev->region_offset = 0;
	memdev->address = 0;
	memdev->interleave_index = 0;
	memdev->interleave_ways = 1;
	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2620
	offset += memdev->header.length;
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631

	/* dcr-descriptor1 */
	dcr = nfit_buf + offset;
	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
	dcr->header.length = offsetof(struct acpi_nfit_control_region,
			window_size);
	dcr->region_index = 0+2;
	dcr_common_init(dcr);
	dcr->serial_number = ~handle[6];
	dcr->code = NFIT_FIC_BYTE;
	dcr->windows = 0;
2632
	offset += dcr->header.length;
2633

2634 2635 2636
	/* sanity check to make sure we've filled the buffer */
	WARN_ON(offset != t->nfit_size);

2637 2638
	t->nfit_filled = offset;

D
Dave Jiang 已提交
2639 2640
	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
			SPA2_SIZE);
2641

2642
	acpi_desc = &t->acpi_desc;
2643 2644 2645 2646
	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2647
	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
2648 2649 2650
	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
}

static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
		void *iobuf, u64 len, int rw)
{
	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
	struct nd_region *nd_region = &ndbr->nd_region;
	unsigned int lane;

	lane = nd_region_acquire_lane(nd_region);
	if (rw)
2663 2664 2665 2666
		memcpy(mmio->addr.base + dpa, iobuf, len);
	else {
		memcpy(iobuf, mmio->addr.base + dpa, len);

2667 2668
		/* give us some some coverage of the arch_invalidate_pmem() API */
		arch_invalidate_pmem(mmio->addr.base + dpa, len);
2669
	}
2670 2671 2672 2673 2674
	nd_region_release_lane(nd_region, lane);

	return 0;
}

2675 2676 2677 2678 2679
static unsigned long nfit_ctl_handle;

union acpi_object *result;

static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
2680
		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
{
	if (handle != &nfit_ctl_handle)
		return ERR_PTR(-ENXIO);

	return result;
}

static int setup_result(void *buf, size_t size)
{
	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
	if (!result)
		return -ENOMEM;
	result->package.type = ACPI_TYPE_BUFFER,
	result->buffer.pointer = (void *) (result + 1);
	result->buffer.length = size;
	memcpy(result->buffer.pointer, buf, size);
	memset(buf, 0, size);
	return 0;
}

static int nfit_ctl_test(struct device *dev)
{
	int rc, cmd_rc;
	struct nvdimm *nvdimm;
	struct acpi_device *adev;
	struct nfit_mem *nfit_mem;
	struct nd_ars_record *record;
	struct acpi_nfit_desc *acpi_desc;
	const u64 test_val = 0x0123456789abcdefULL;
	unsigned long mask, cmd_size, offset;
	union {
		struct nd_cmd_get_config_size cfg_size;
2713
		struct nd_cmd_clear_error clear_err;
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
		struct nd_cmd_ars_status ars_stat;
		struct nd_cmd_ars_cap ars_cap;
		char buf[sizeof(struct nd_cmd_ars_status)
			+ sizeof(struct nd_ars_record)];
	} cmds;

	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
	if (!adev)
		return -ENOMEM;
	*adev = (struct acpi_device) {
		.handle = &nfit_ctl_handle,
		.dev = {
			.init_name = "test-adev",
		},
	};

	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
	if (!acpi_desc)
		return -ENOMEM;
	*acpi_desc = (struct acpi_nfit_desc) {
		.nd_desc = {
			.cmd_mask = 1UL << ND_CMD_ARS_CAP
				| 1UL << ND_CMD_ARS_START
				| 1UL << ND_CMD_ARS_STATUS
2738 2739
				| 1UL << ND_CMD_CLEAR_ERROR
				| 1UL << ND_CMD_CALL,
2740 2741 2742
			.module = THIS_MODULE,
			.provider_name = "ACPI.NFIT",
			.ndctl = acpi_nfit_ctl,
D
Dave Jiang 已提交
2743 2744 2745 2746
			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
				| 1UL << NFIT_CMD_ARS_INJECT_SET
				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
		},
		.dev = &adev->dev,
	};

	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
	if (!nfit_mem)
		return -ENOMEM;

	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
		| 1UL << ND_CMD_VENDOR;
	*nfit_mem = (struct nfit_mem) {
		.adev = adev,
		.family = NVDIMM_FAMILY_INTEL,
		.dsm_mask = mask,
	};

	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
	if (!nvdimm)
		return -ENOMEM;
	*nvdimm = (struct nvdimm) {
		.provider_data = nfit_mem,
		.cmd_mask = mask,
		.dev = {
			.init_name = "test-dimm",
		},
	};


	/* basic checkout of a typical 'get config size' command */
	cmd_size = sizeof(cmds.cfg_size);
	cmds.cfg_size = (struct nd_cmd_get_config_size) {
		.status = 0,
		.config_size = SZ_128K,
		.max_xfer = SZ_4K,
	};
	rc = setup_result(cmds.buf, cmd_size);
	if (rc)
		return rc;
	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
			cmds.buf, cmd_size, &cmd_rc);

	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
			|| cmds.cfg_size.config_size != SZ_128K
			|| cmds.cfg_size.max_xfer != SZ_4K) {
		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
				__func__, __LINE__, rc, cmd_rc);
		return -EIO;
	}


	/* test ars_status with zero output */
	cmd_size = offsetof(struct nd_cmd_ars_status, address);
	cmds.ars_stat = (struct nd_cmd_ars_status) {
		.out_length = 0,
	};
	rc = setup_result(cmds.buf, cmd_size);
	if (rc)
		return rc;
	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
			cmds.buf, cmd_size, &cmd_rc);

	if (rc < 0 || cmd_rc) {
		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
				__func__, __LINE__, rc, cmd_rc);
		return -EIO;
	}


	/* test ars_cap with benign extended status */
	cmd_size = sizeof(cmds.ars_cap);
	cmds.ars_cap = (struct nd_cmd_ars_cap) {
		.status = ND_ARS_PERSISTENT << 16,
	};
	offset = offsetof(struct nd_cmd_ars_cap, status);
	rc = setup_result(cmds.buf + offset, cmd_size - offset);
	if (rc)
		return rc;
	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
			cmds.buf, cmd_size, &cmd_rc);

	if (rc < 0 || cmd_rc) {
		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
				__func__, __LINE__, rc, cmd_rc);
		return -EIO;
	}


	/* test ars_status with 'status' trimmed from 'out_length' */
	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
	cmds.ars_stat = (struct nd_cmd_ars_status) {
		.out_length = cmd_size - 4,
	};
	record = &cmds.ars_stat.records[0];
	*record = (struct nd_ars_record) {
		.length = test_val,
	};
	rc = setup_result(cmds.buf, cmd_size);
	if (rc)
		return rc;
	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
			cmds.buf, cmd_size, &cmd_rc);

	if (rc < 0 || cmd_rc || record->length != test_val) {
		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
				__func__, __LINE__, rc, cmd_rc);
		return -EIO;
	}


	/* test ars_status with 'Output (Size)' including 'status' */
	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
	cmds.ars_stat = (struct nd_cmd_ars_status) {
		.out_length = cmd_size,
	};
	record = &cmds.ars_stat.records[0];
	*record = (struct nd_ars_record) {
		.length = test_val,
	};
	rc = setup_result(cmds.buf, cmd_size);
	if (rc)
		return rc;
	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
			cmds.buf, cmd_size, &cmd_rc);

	if (rc < 0 || cmd_rc || record->length != test_val) {
		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
				__func__, __LINE__, rc, cmd_rc);
		return -EIO;
	}


	/* test extended status for get_config_size results in failure */
	cmd_size = sizeof(cmds.cfg_size);
	cmds.cfg_size = (struct nd_cmd_get_config_size) {
		.status = 1 << 16,
	};
	rc = setup_result(cmds.buf, cmd_size);
	if (rc)
		return rc;
	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
			cmds.buf, cmd_size, &cmd_rc);

	if (rc < 0 || cmd_rc >= 0) {
		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
				__func__, __LINE__, rc, cmd_rc);
		return -EIO;
	}

2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
	/* test clear error */
	cmd_size = sizeof(cmds.clear_err);
	cmds.clear_err = (struct nd_cmd_clear_error) {
		.length = 512,
		.cleared = 512,
	};
	rc = setup_result(cmds.buf, cmd_size);
	if (rc)
		return rc;
	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
			cmds.buf, cmd_size, &cmd_rc);
	if (rc < 0 || cmd_rc) {
		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
				__func__, __LINE__, rc, cmd_rc);
		return -EIO;
	}

2914 2915 2916
	return 0;
}

2917 2918 2919 2920 2921 2922
static int nfit_test_probe(struct platform_device *pdev)
{
	struct nvdimm_bus_descriptor *nd_desc;
	struct acpi_nfit_desc *acpi_desc;
	struct device *dev = &pdev->dev;
	struct nfit_test *nfit_test;
2923
	struct nfit_mem *nfit_mem;
2924
	union acpi_object *obj;
2925 2926
	int rc;

2927 2928 2929 2930 2931 2932
	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
		rc = nfit_ctl_test(&pdev->dev);
		if (rc)
			return rc;
	}

2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
	nfit_test = to_nfit_test(&pdev->dev);

	/* common alloc */
	if (nfit_test->num_dcr) {
		int num = nfit_test->num_dcr;

		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
				GFP_KERNEL);
		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
				GFP_KERNEL);
2943 2944 2945 2946
		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
				GFP_KERNEL);
		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
				GFP_KERNEL);
2947 2948 2949 2950 2951 2952 2953 2954
		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
				GFP_KERNEL);
		nfit_test->label_dma = devm_kcalloc(dev, num,
				sizeof(dma_addr_t), GFP_KERNEL);
		nfit_test->dcr = devm_kcalloc(dev, num,
				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
		nfit_test->dcr_dma = devm_kcalloc(dev, num,
				sizeof(dma_addr_t), GFP_KERNEL);
2955 2956 2957 2958 2959
		nfit_test->smart = devm_kcalloc(dev, num,
				sizeof(struct nd_intel_smart), GFP_KERNEL);
		nfit_test->smart_threshold = devm_kcalloc(dev, num,
				sizeof(struct nd_intel_smart_threshold),
				GFP_KERNEL);
2960 2961
		nfit_test->fw = devm_kcalloc(dev, num,
				sizeof(struct nfit_test_fw), GFP_KERNEL);
2962 2963
		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
				&& nfit_test->label_dma && nfit_test->dcr
2964
				&& nfit_test->dcr_dma && nfit_test->flush
2965 2966
				&& nfit_test->flush_dma
				&& nfit_test->fw)
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
			/* pass */;
		else
			return -ENOMEM;
	}

	if (nfit_test->num_pm) {
		int num = nfit_test->num_pm;

		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
				GFP_KERNEL);
		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
				sizeof(dma_addr_t), GFP_KERNEL);
		if (nfit_test->spa_set && nfit_test->spa_set_dma)
			/* pass */;
		else
			return -ENOMEM;
	}

	/* per-nfit specific alloc */
	if (nfit_test->alloc(nfit_test))
		return -ENOMEM;

	nfit_test->setup(nfit_test);
	acpi_desc = &nfit_test->acpi_desc;
2991
	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
2992 2993
	acpi_desc->blk_do_io = nfit_test_blk_do_io;
	nd_desc = &acpi_desc->nd_desc;
2994
	nd_desc->provider_name = NULL;
2995
	nd_desc->module = THIS_MODULE;
2996
	nd_desc->ndctl = nfit_test_ctl;
2997

2998
	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
2999
			nfit_test->nfit_filled);
3000
	if (rc)
V
Vishal Verma 已提交
3001 3002
		return rc;

3003 3004 3005 3006
	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
	if (rc)
		return rc;

V
Vishal Verma 已提交
3007 3008 3009 3010 3011 3012
	if (nfit_test->setup != nfit_test0_setup)
		return 0;

	nfit_test->setup_hotplug = 1;
	nfit_test->setup(nfit_test);

3013 3014 3015 3016 3017 3018 3019 3020
	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
	if (!obj)
		return -ENOMEM;
	obj->type = ACPI_TYPE_BUFFER;
	obj->buffer.length = nfit_test->nfit_size;
	obj->buffer.pointer = nfit_test->nfit_buf;
	*(nfit_test->_fit) = obj;
	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
3021 3022 3023 3024 3025 3026 3027

	/* associate dimm devices with nfit_mem data for notification testing */
	mutex_lock(&acpi_desc->init_mutex);
	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
		int i;

3028
		for (i = 0; i < ARRAY_SIZE(handle); i++)
3029 3030 3031 3032 3033
			if (nfit_handle == handle[i])
				dev_set_drvdata(nfit_test->dimm_dev[i],
						nfit_mem);
	}
	mutex_unlock(&acpi_desc->init_mutex);
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063

	return 0;
}

static int nfit_test_remove(struct platform_device *pdev)
{
	return 0;
}

static void nfit_test_release(struct device *dev)
{
	struct nfit_test *nfit_test = to_nfit_test(dev);

	kfree(nfit_test);
}

static const struct platform_device_id nfit_test_id[] = {
	{ KBUILD_MODNAME },
	{ },
};

static struct platform_driver nfit_test_driver = {
	.probe = nfit_test_probe,
	.remove = nfit_test_remove,
	.driver = {
		.name = KBUILD_MODNAME,
	},
	.id_table = nfit_test_id,
};

3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));

enum INJECT {
	INJECT_NONE,
	INJECT_SRC,
	INJECT_DST,
};

static void mcsafe_test_init(char *dst, char *src, size_t size)
{
	size_t i;

	memset(dst, 0xff, size);
	for (i = 0; i < size; i++)
		src[i] = (char) i;
}

static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
		size_t size, unsigned long rem)
{
	size_t i;

	for (i = 0; i < size - rem; i++)
		if (dst[i] != (unsigned char) i) {
			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
					__func__, __LINE__, i, dst[i],
					(unsigned char) i);
			return false;
		}
	for (i = size - rem; i < size; i++)
		if (dst[i] != 0xffU) {
			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
					__func__, __LINE__, i, dst[i]);
			return false;
		}
	return true;
}

void mcsafe_test(void)
{
	char *inject_desc[] = { "none", "source", "destination" };
	enum INJECT inj;

	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
		pr_info("%s: run...\n", __func__);
	} else {
		pr_info("%s: disabled, skip.\n", __func__);
		return;
	}

	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
		int i;

		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
		for (i = 0; i < 512; i++) {
			unsigned long expect, rem;
			void *src, *dst;
			bool valid;

			switch (inj) {
			case INJECT_NONE:
				mcsafe_inject_src(NULL);
				mcsafe_inject_dst(NULL);
				dst = &mcsafe_buf[2048];
				src = &mcsafe_buf[1024 - i];
				expect = 0;
				break;
			case INJECT_SRC:
				mcsafe_inject_src(&mcsafe_buf[1024]);
				mcsafe_inject_dst(NULL);
				dst = &mcsafe_buf[2048];
				src = &mcsafe_buf[1024 - i];
				expect = 512 - i;
				break;
			case INJECT_DST:
				mcsafe_inject_src(NULL);
				mcsafe_inject_dst(&mcsafe_buf[2048]);
				dst = &mcsafe_buf[2048 - i];
				src = &mcsafe_buf[1024];
				expect = 512 - i;
				break;
			}

			mcsafe_test_init(dst, src, 512);
			rem = __memcpy_mcsafe(dst, src, 512);
			valid = mcsafe_test_validate(dst, src, 512, expect);
			if (rem == expect && valid)
				continue;
			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
					__func__,
					((unsigned long) dst) & ~PAGE_MASK,
					((unsigned long ) src) & ~PAGE_MASK,
					512, i, rem, valid ? "valid" : "bad",
					expect);
		}
	}

	mcsafe_inject_src(NULL);
	mcsafe_inject_dst(NULL);
}

3165 3166 3167 3168
static __init int nfit_test_init(void)
{
	int rc, i;

3169 3170 3171 3172
	pmem_test();
	libnvdimm_test();
	acpi_nfit_test();
	device_dax_test();
3173
	mcsafe_test();
3174 3175 3176
	dax_pmem_test();
	dax_pmem_core_test();
	dax_pmem_compat_test();
3177

3178
	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3179

D
Dave Jiang 已提交
3180 3181 3182 3183
	nfit_wq = create_singlethread_workqueue("nfit");
	if (!nfit_wq)
		return -ENOMEM;

3184 3185 3186 3187 3188
	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
	if (IS_ERR(nfit_test_dimm)) {
		rc = PTR_ERR(nfit_test_dimm);
		goto err_register;
	}
3189

3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
	nfit_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
	if (!nfit_pool) {
		rc = -ENOMEM;
		goto err_register;
	}

	if (gen_pool_add(nfit_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
		rc = -ENOMEM;
		goto err_register;
	}

3201 3202 3203 3204 3205 3206 3207 3208 3209 3210
	for (i = 0; i < NUM_NFITS; i++) {
		struct nfit_test *nfit_test;
		struct platform_device *pdev;

		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
		if (!nfit_test) {
			rc = -ENOMEM;
			goto err_register;
		}
		INIT_LIST_HEAD(&nfit_test->resources);
D
Dave Jiang 已提交
3211
		badrange_init(&nfit_test->badrange);
3212 3213 3214
		switch (i) {
		case 0:
			nfit_test->num_pm = NUM_PM;
3215
			nfit_test->dcr_idx = 0;
3216 3217 3218 3219 3220
			nfit_test->num_dcr = NUM_DCR;
			nfit_test->alloc = nfit_test0_alloc;
			nfit_test->setup = nfit_test0_setup;
			break;
		case 1:
3221
			nfit_test->num_pm = 2;
3222
			nfit_test->dcr_idx = NUM_DCR;
3223
			nfit_test->num_dcr = 2;
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
			nfit_test->alloc = nfit_test1_alloc;
			nfit_test->setup = nfit_test1_setup;
			break;
		default:
			rc = -EINVAL;
			goto err_register;
		}
		pdev = &nfit_test->pdev;
		pdev->name = KBUILD_MODNAME;
		pdev->id = i;
		pdev->dev.release = nfit_test_release;
		rc = platform_device_register(pdev);
		if (rc) {
			put_device(&pdev->dev);
			goto err_register;
		}
3240
		get_device(&pdev->dev);
3241 3242 3243 3244 3245 3246

		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
		if (rc)
			goto err_register;

		instances[i] = nfit_test;
D
Dave Jiang 已提交
3247
		INIT_WORK(&nfit_test->work, uc_error_notify);
3248 3249 3250 3251 3252 3253 3254 3255
	}

	rc = platform_driver_register(&nfit_test_driver);
	if (rc)
		goto err_register;
	return 0;

 err_register:
3256 3257 3258
	if (nfit_pool)
		gen_pool_destroy(nfit_pool);

D
Dave Jiang 已提交
3259
	destroy_workqueue(nfit_wq);
3260 3261 3262 3263
	for (i = 0; i < NUM_NFITS; i++)
		if (instances[i])
			platform_device_unregister(&instances[i]->pdev);
	nfit_test_teardown();
3264 3265 3266 3267
	for (i = 0; i < NUM_NFITS; i++)
		if (instances[i])
			put_device(&instances[i]->pdev.dev);

3268 3269 3270 3271 3272 3273 3274
	return rc;
}

static __exit void nfit_test_exit(void)
{
	int i;

D
Dave Jiang 已提交
3275 3276
	flush_workqueue(nfit_wq);
	destroy_workqueue(nfit_wq);
3277 3278
	for (i = 0; i < NUM_NFITS; i++)
		platform_device_unregister(&instances[i]->pdev);
3279
	platform_driver_unregister(&nfit_test_driver);
3280
	nfit_test_teardown();
3281

3282 3283
	gen_pool_destroy(nfit_pool);

3284 3285
	for (i = 0; i < NUM_NFITS; i++)
		put_device(&instances[i]->pdev.dev);
3286
	class_destroy(nfit_test_dimm);
3287 3288 3289 3290 3291 3292
}

module_init(nfit_test_init);
module_exit(nfit_test_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Intel Corporation");