xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 2170a0d53bee1a6c1a4ebd042f99d85aafc6c0ea)
16bc75619SDan Williams /*
26bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
36bc75619SDan Williams  *
46bc75619SDan Williams  * This program is free software; you can redistribute it and/or modify
56bc75619SDan Williams  * it under the terms of version 2 of the GNU General Public License as
66bc75619SDan Williams  * published by the Free Software Foundation.
76bc75619SDan Williams  *
86bc75619SDan Williams  * This program is distributed in the hope that it will be useful, but
96bc75619SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
106bc75619SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116bc75619SDan Williams  * General Public License for more details.
126bc75619SDan Williams  */
136bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
146bc75619SDan Williams #include <linux/platform_device.h>
156bc75619SDan Williams #include <linux/dma-mapping.h>
16d8d378faSDan Williams #include <linux/workqueue.h>
176bc75619SDan Williams #include <linux/libnvdimm.h>
18e3f5df76SDan Williams #include <linux/genalloc.h>
196bc75619SDan Williams #include <linux/vmalloc.h>
206bc75619SDan Williams #include <linux/device.h>
216bc75619SDan Williams #include <linux/module.h>
2220985164SVishal Verma #include <linux/mutex.h>
236bc75619SDan Williams #include <linux/ndctl.h>
246bc75619SDan Williams #include <linux/sizes.h>
2520985164SVishal Verma #include <linux/list.h>
266bc75619SDan Williams #include <linux/slab.h>
27a7de92daSDan Williams #include <nd-core.h>
280ead1118SDan Williams #include <intel.h>
296bc75619SDan Williams #include <nfit.h>
306bc75619SDan Williams #include <nd.h>
316bc75619SDan Williams #include "nfit_test.h"
320fb5c8dfSDan Williams #include "../watermark.h"
336bc75619SDan Williams 
345d8beee2SDan Williams #include <asm/mcsafe_test.h>
355d8beee2SDan Williams 
366bc75619SDan Williams /*
376bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
386bc75619SDan Williams  *
396bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
406bc75619SDan Williams  *
416bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
426bc75619SDan Williams  *           +----------+--------------+----------+---------+
436bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
446bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
456bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
466bc75619SDan Williams  *    |      +----------+--------------v----------v         v
476bc75619SDan Williams  * +--+---+                            |                    |
486bc75619SDan Williams  * | cpu0 |                                    region1
496bc75619SDan Williams  * +--+---+                            |                    |
506bc75619SDan Williams  *    |      +-------------------------^----------^         ^
516bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
526bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
536bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
546bc75619SDan Williams  *           +-------------------------+----------+-+-------+
556bc75619SDan Williams  *
5620985164SVishal Verma  * +--+---+
5720985164SVishal Verma  * | cpu1 |
5820985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5920985164SVishal Verma  *    |      +----------------------------------------------+
6020985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
6120985164SVishal Verma  * | imc0 +--+----------------------------------------------+
6220985164SVishal Verma  * +------+
6320985164SVishal Verma  *
6420985164SVishal Verma  *
656bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
666bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
676bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
686bc75619SDan Williams  *
696bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
706bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
716bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
726bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
736bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
746bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
756bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
766bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
776bc75619SDan Williams  *    names that can be assigned to a namespace.
786bc75619SDan Williams  *
796bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
806bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
816bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
826bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
836bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
846bc75619SDan Williams  *    "blk5.0".
856bc75619SDan Williams  *
866bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
876bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
886bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
896bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
906bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
916bc75619SDan Williams  *
926bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
936bc75619SDan Williams  *
946bc75619SDan Williams  *  region2
956bc75619SDan Williams  * +---------------------+
966bc75619SDan Williams  * |---------------------|
976bc75619SDan Williams  * ||       pm2.0       ||
986bc75619SDan Williams  * |---------------------|
996bc75619SDan Williams  * +---------------------+
1006bc75619SDan Williams  *
1016bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
1026bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
1036bc75619SDan Williams  *    reference an NVDIMM.
1046bc75619SDan Williams  */
1056bc75619SDan Williams enum {
10620985164SVishal Verma 	NUM_PM  = 3,
10720985164SVishal Verma 	NUM_DCR = 5,
10885d3fa02SDan Williams 	NUM_HINTS = 8,
1096bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1106bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1119741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1129741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1136bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1146bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1157bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1166bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1176bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1186bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1196bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1206bc75619SDan Williams 	DCR_SIZE = 12,
1216bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1226bc75619SDan Williams };
1236bc75619SDan Williams 
1246bc75619SDan Williams struct nfit_test_dcr {
1256bc75619SDan Williams 	__le64 bdw_addr;
1266bc75619SDan Williams 	__le32 bdw_status;
1276bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1286bc75619SDan Williams };
1296bc75619SDan Williams 
1306bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1316bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1326bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1336bc75619SDan Williams 
134dafb1048SDan Williams static u32 handle[] = {
1356bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1366bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1376bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1386bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13920985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
140dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
141ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1426bc75619SDan Williams };
1436bc75619SDan Williams 
144af31b04bSMasayoshi Mizuma static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
145af31b04bSMasayoshi Mizuma static int dimm_fail_cmd_code[ARRAY_SIZE(handle)];
1463c13e2acSDave Jiang struct nfit_test_sec {
1473c13e2acSDave Jiang 	u8 state;
148ecaa4a97SDave Jiang 	u8 ext_state;
149*2170a0d5SDave Jiang 	u8 old_state;
1503c13e2acSDave Jiang 	u8 passphrase[32];
151ecaa4a97SDave Jiang 	u8 master_passphrase[32];
152926f7480SDave Jiang 	u64 overwrite_end_time;
1533c13e2acSDave Jiang } dimm_sec_info[NUM_DCR];
15473606afdSDan Williams 
155b4d4702fSVishal Verma static const struct nd_intel_smart smart_def = {
156b4d4702fSVishal Verma 	.flags = ND_INTEL_SMART_HEALTH_VALID
157b4d4702fSVishal Verma 		| ND_INTEL_SMART_SPARES_VALID
158b4d4702fSVishal Verma 		| ND_INTEL_SMART_ALARM_VALID
159b4d4702fSVishal Verma 		| ND_INTEL_SMART_USED_VALID
160b4d4702fSVishal Verma 		| ND_INTEL_SMART_SHUTDOWN_VALID
161f1101766SDan Williams 		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
162b4d4702fSVishal Verma 		| ND_INTEL_SMART_MTEMP_VALID
163b4d4702fSVishal Verma 		| ND_INTEL_SMART_CTEMP_VALID,
164b4d4702fSVishal Verma 	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
165b4d4702fSVishal Verma 	.media_temperature = 23 * 16,
166b4d4702fSVishal Verma 	.ctrl_temperature = 25 * 16,
167b4d4702fSVishal Verma 	.pmic_temperature = 40 * 16,
168b4d4702fSVishal Verma 	.spares = 75,
169b4d4702fSVishal Verma 	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
170b4d4702fSVishal Verma 		| ND_INTEL_SMART_TEMP_TRIP,
171b4d4702fSVishal Verma 	.ait_status = 1,
172b4d4702fSVishal Verma 	.life_used = 5,
173b4d4702fSVishal Verma 	.shutdown_state = 0,
174f1101766SDan Williams 	.shutdown_count = 42,
175b4d4702fSVishal Verma 	.vendor_size = 0,
176b4d4702fSVishal Verma };
177b4d4702fSVishal Verma 
178bfbaa952SDave Jiang struct nfit_test_fw {
179bfbaa952SDave Jiang 	enum intel_fw_update_state state;
180bfbaa952SDave Jiang 	u32 context;
181bfbaa952SDave Jiang 	u64 version;
182bfbaa952SDave Jiang 	u32 size_received;
183bfbaa952SDave Jiang 	u64 end_time;
184bfbaa952SDave Jiang };
185bfbaa952SDave Jiang 
1866bc75619SDan Williams struct nfit_test {
1876bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1886bc75619SDan Williams 	struct platform_device pdev;
1896bc75619SDan Williams 	struct list_head resources;
1906bc75619SDan Williams 	void *nfit_buf;
1916bc75619SDan Williams 	dma_addr_t nfit_dma;
1926bc75619SDan Williams 	size_t nfit_size;
1931526f9e2SRoss Zwisler 	size_t nfit_filled;
194dafb1048SDan Williams 	int dcr_idx;
1956bc75619SDan Williams 	int num_dcr;
1966bc75619SDan Williams 	int num_pm;
1976bc75619SDan Williams 	void **dimm;
1986bc75619SDan Williams 	dma_addr_t *dimm_dma;
1999d27a87eSDan Williams 	void **flush;
2009d27a87eSDan Williams 	dma_addr_t *flush_dma;
2016bc75619SDan Williams 	void **label;
2026bc75619SDan Williams 	dma_addr_t *label_dma;
2036bc75619SDan Williams 	void **spa_set;
2046bc75619SDan Williams 	dma_addr_t *spa_set_dma;
2056bc75619SDan Williams 	struct nfit_test_dcr **dcr;
2066bc75619SDan Williams 	dma_addr_t *dcr_dma;
2076bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
2086bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
20920985164SVishal Verma 	int setup_hotplug;
210c14a868aSDan Williams 	union acpi_object **_fit;
211c14a868aSDan Williams 	dma_addr_t _fit_dma;
212f471f1a7SDan Williams 	struct ars_state {
213f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
214f471f1a7SDan Williams 		unsigned long deadline;
215f471f1a7SDan Williams 		spinlock_t lock;
216f471f1a7SDan Williams 	} ars_state;
217af31b04bSMasayoshi Mizuma 	struct device *dimm_dev[ARRAY_SIZE(handle)];
218ed07c433SDan Williams 	struct nd_intel_smart *smart;
219ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
2209fb1a190SDave Jiang 	struct badrange badrange;
2219fb1a190SDave Jiang 	struct work_struct work;
222bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
2236bc75619SDan Williams };
2246bc75619SDan Williams 
2259fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
2269fb1a190SDave Jiang 
227e3f5df76SDan Williams static struct gen_pool *nfit_pool;
228e3f5df76SDan Williams 
229037c8489SDave Jiang static const char zero_key[NVDIMM_PASSPHRASE_LEN];
230037c8489SDave Jiang 
2316bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
2326bc75619SDan Williams {
2336bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
2346bc75619SDan Williams 
2356bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
2366bc75619SDan Williams }
2376bc75619SDan Williams 
238bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
239bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
240bfbaa952SDave Jiang 		int idx)
241bfbaa952SDave Jiang {
242bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
243bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
244bfbaa952SDave Jiang 
245bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
246bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
247bfbaa952SDave Jiang 
248bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
249bfbaa952SDave Jiang 		return -EINVAL;
250bfbaa952SDave Jiang 
251bfbaa952SDave Jiang 	nd_cmd->status = 0;
252bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
253bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
254bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
255bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
256bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
257bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
258bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
259bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
260bfbaa952SDave Jiang 
261bfbaa952SDave Jiang 	return 0;
262bfbaa952SDave Jiang }
263bfbaa952SDave Jiang 
264bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
265bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
266bfbaa952SDave Jiang 		int idx)
267bfbaa952SDave Jiang {
268bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
269bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
270bfbaa952SDave Jiang 
271bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
272bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
273bfbaa952SDave Jiang 
274bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
275bfbaa952SDave Jiang 		return -EINVAL;
276bfbaa952SDave Jiang 
277bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
278bfbaa952SDave Jiang 		/* extended status, FW update in progress */
279bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
280bfbaa952SDave Jiang 		return 0;
281bfbaa952SDave Jiang 	}
282bfbaa952SDave Jiang 
283bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
284bfbaa952SDave Jiang 	fw->context++;
285bfbaa952SDave Jiang 	fw->size_received = 0;
286bfbaa952SDave Jiang 	nd_cmd->status = 0;
287bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
288bfbaa952SDave Jiang 
289bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
290bfbaa952SDave Jiang 
291bfbaa952SDave Jiang 	return 0;
292bfbaa952SDave Jiang }
293bfbaa952SDave Jiang 
294bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
295bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
296bfbaa952SDave Jiang 		int idx)
297bfbaa952SDave Jiang {
298bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
299bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
300bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
301bfbaa952SDave Jiang 
302bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
303bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
304bfbaa952SDave Jiang 
305bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
306bfbaa952SDave Jiang 		return -EINVAL;
307bfbaa952SDave Jiang 
308bfbaa952SDave Jiang 
309bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
310bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
311bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
312bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
313bfbaa952SDave Jiang 
314bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
315bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
316bfbaa952SDave Jiang 		*status = 0x5;
317bfbaa952SDave Jiang 		return 0;
318bfbaa952SDave Jiang 	}
319bfbaa952SDave Jiang 
320bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
321bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
322bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
323bfbaa952SDave Jiang 		*status = 0x10007;
324bfbaa952SDave Jiang 		return 0;
325bfbaa952SDave Jiang 	}
326bfbaa952SDave Jiang 
327bfbaa952SDave Jiang 	/*
328bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
329bfbaa952SDave Jiang 	 * check length is > max send length
330bfbaa952SDave Jiang 	 */
331bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
332bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
333bfbaa952SDave Jiang 		*status = 0x3;
334bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
335bfbaa952SDave Jiang 		return 0;
336bfbaa952SDave Jiang 	}
337bfbaa952SDave Jiang 
338bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
339bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
340bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
341bfbaa952SDave Jiang 	*status = 0;
342bfbaa952SDave Jiang 	return 0;
343bfbaa952SDave Jiang }
344bfbaa952SDave Jiang 
345bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
346bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
347bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
348bfbaa952SDave Jiang {
349bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
350bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
351bfbaa952SDave Jiang 
352bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
353bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
354bfbaa952SDave Jiang 
355bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
356bfbaa952SDave Jiang 		/* update already done, need cold boot */
357bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
358bfbaa952SDave Jiang 		return 0;
359bfbaa952SDave Jiang 	}
360bfbaa952SDave Jiang 
361bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
362bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
363bfbaa952SDave Jiang 
364bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
365bfbaa952SDave Jiang 	case 0: /* finish */
366bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
367bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
368bfbaa952SDave Jiang 					__func__, nd_cmd->context,
369bfbaa952SDave Jiang 					fw->context);
370bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
371bfbaa952SDave Jiang 			return 0;
372bfbaa952SDave Jiang 		}
373bfbaa952SDave Jiang 		nd_cmd->status = 0;
374bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
375bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
376bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
377bfbaa952SDave Jiang 		break;
378bfbaa952SDave Jiang 
379bfbaa952SDave Jiang 	case 1: /* abort */
380bfbaa952SDave Jiang 		fw->size_received = 0;
381bfbaa952SDave Jiang 		/* successfully aborted status */
382bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
383bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
384bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
385bfbaa952SDave Jiang 		break;
386bfbaa952SDave Jiang 
387bfbaa952SDave Jiang 	default: /* bad control flag */
388bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
389bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
390bfbaa952SDave Jiang 		return -EINVAL;
391bfbaa952SDave Jiang 	}
392bfbaa952SDave Jiang 
393bfbaa952SDave Jiang 	return 0;
394bfbaa952SDave Jiang }
395bfbaa952SDave Jiang 
396bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
397bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
398bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
399bfbaa952SDave Jiang {
400bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
401bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
402bfbaa952SDave Jiang 
403bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
404bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
405bfbaa952SDave Jiang 
406bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
407bfbaa952SDave Jiang 		return -EINVAL;
408bfbaa952SDave Jiang 
409bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
410bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
411bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
412bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
413bfbaa952SDave Jiang 		return 0;
414bfbaa952SDave Jiang 	}
415bfbaa952SDave Jiang 
416bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
417bfbaa952SDave Jiang 
418bfbaa952SDave Jiang 	switch (fw->state) {
419bfbaa952SDave Jiang 	case FW_STATE_NEW:
420bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
421bfbaa952SDave Jiang 		nd_cmd->status = 0;
422bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
423bfbaa952SDave Jiang 		break;
424bfbaa952SDave Jiang 
425bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
426bfbaa952SDave Jiang 		/* sequencing error */
427bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
428bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
429bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
430bfbaa952SDave Jiang 		break;
431bfbaa952SDave Jiang 
432bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
433bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
434bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
435bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
436bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
437bfbaa952SDave Jiang 			break;
438bfbaa952SDave Jiang 		}
439bfbaa952SDave Jiang 
440bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
441bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
442bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
443bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
444bfbaa952SDave Jiang 		nd_cmd->status = 0;
445bfbaa952SDave Jiang 		/* bogus test version */
446bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
447bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
448bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
449bfbaa952SDave Jiang 		break;
450bfbaa952SDave Jiang 
451bfbaa952SDave Jiang 	default: /* we should never get here */
452bfbaa952SDave Jiang 		return -EINVAL;
453bfbaa952SDave Jiang 	}
454bfbaa952SDave Jiang 
455bfbaa952SDave Jiang 	return 0;
456bfbaa952SDave Jiang }
457bfbaa952SDave Jiang 
45839c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4596bc75619SDan Williams 		unsigned int buf_len)
4606bc75619SDan Williams {
4616bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4626bc75619SDan Williams 		return -EINVAL;
46339c686b8SVishal Verma 
4646bc75619SDan Williams 	nd_cmd->status = 0;
4656bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4666bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
46739c686b8SVishal Verma 
46839c686b8SVishal Verma 	return 0;
4696bc75619SDan Williams }
47039c686b8SVishal Verma 
47139c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
47239c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
47339c686b8SVishal Verma {
4746bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
47539c686b8SVishal Verma 	int rc;
4766bc75619SDan Williams 
4776bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4786bc75619SDan Williams 		return -EINVAL;
4796bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4806bc75619SDan Williams 		return -EINVAL;
4816bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4826bc75619SDan Williams 		return -EINVAL;
4836bc75619SDan Williams 
4846bc75619SDan Williams 	nd_cmd->status = 0;
4856bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
48639c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4876bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
48839c686b8SVishal Verma 
48939c686b8SVishal Verma 	return rc;
4906bc75619SDan Williams }
49139c686b8SVishal Verma 
49239c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
49339c686b8SVishal Verma 		unsigned int buf_len, void *label)
49439c686b8SVishal Verma {
4956bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4966bc75619SDan Williams 	u32 *status;
49739c686b8SVishal Verma 	int rc;
4986bc75619SDan Williams 
4996bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
5006bc75619SDan Williams 		return -EINVAL;
5016bc75619SDan Williams 	if (offset >= LABEL_SIZE)
5026bc75619SDan Williams 		return -EINVAL;
5036bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
5046bc75619SDan Williams 		return -EINVAL;
5056bc75619SDan Williams 
50639c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
5076bc75619SDan Williams 	*status = 0;
5086bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
50939c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
5106bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
51139c686b8SVishal Verma 
51239c686b8SVishal Verma 	return rc;
5136bc75619SDan Williams }
51439c686b8SVishal Verma 
515d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
516747ffe11SDan Williams 
51739c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
51839c686b8SVishal Verma 		unsigned int buf_len)
51939c686b8SVishal Verma {
5209fb1a190SDave Jiang 	int ars_recs;
5219fb1a190SDave Jiang 
52239c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
52339c686b8SVishal Verma 		return -EINVAL;
52439c686b8SVishal Verma 
5259fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5269fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5279fb1a190SDave Jiang 
528747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5299fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
53039c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
531d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
53239c686b8SVishal Verma 
53339c686b8SVishal Verma 	return 0;
53439c686b8SVishal Verma }
53539c686b8SVishal Verma 
5369fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5379fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
53839c686b8SVishal Verma {
539f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
540f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5419fb1a190SDave Jiang 	struct badrange_entry *be;
5429fb1a190SDave Jiang 	u64 end = addr + len - 1;
5439fb1a190SDave Jiang 	int i = 0;
544f471f1a7SDan Williams 
545f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
546f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
547f471f1a7SDan Williams 	ars_status->status = 0;
548f471f1a7SDan Williams 	ars_status->address = addr;
549f471f1a7SDan Williams 	ars_status->length = len;
550f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5519fb1a190SDave Jiang 
5529fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5539fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5549fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5559fb1a190SDave Jiang 		u64 rstart, rend;
5569fb1a190SDave Jiang 
5579fb1a190SDave Jiang 		/* skip entries outside the range */
5589fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5599fb1a190SDave Jiang 			continue;
5609fb1a190SDave Jiang 
5619fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5629fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5639fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
564f471f1a7SDan Williams 		ars_record->handle = 0;
5659fb1a190SDave Jiang 		ars_record->err_address = rstart;
5669fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5679fb1a190SDave Jiang 		i++;
5689fb1a190SDave Jiang 	}
5699fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5709fb1a190SDave Jiang 	ars_status->num_records = i;
5719fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5729fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
573f471f1a7SDan Williams }
574f471f1a7SDan Williams 
5759fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5769fb1a190SDave Jiang 		struct ars_state *ars_state,
577f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
578f471f1a7SDan Williams 		int *cmd_rc)
579f471f1a7SDan Williams {
580f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
58139c686b8SVishal Verma 		return -EINVAL;
58239c686b8SVishal Verma 
583f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
584f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
585f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
586f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
587f471f1a7SDan Williams 	} else {
588f471f1a7SDan Williams 		ars_start->status = 0;
589f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5909fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
591f471f1a7SDan Williams 				ars_start->length);
592f471f1a7SDan Williams 		*cmd_rc = 0;
593f471f1a7SDan Williams 	}
594f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
59539c686b8SVishal Verma 
59639c686b8SVishal Verma 	return 0;
59739c686b8SVishal Verma }
59839c686b8SVishal Verma 
599f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
600f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
601f471f1a7SDan Williams 		int *cmd_rc)
60239c686b8SVishal Verma {
603f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
60439c686b8SVishal Verma 		return -EINVAL;
60539c686b8SVishal Verma 
606f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
607f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
608f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
609f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
610f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
611f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
612f471f1a7SDan Williams 	} else {
613f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
614f471f1a7SDan Williams 				ars_state->ars_status->out_length);
615f471f1a7SDan Williams 		*cmd_rc = 0;
616f471f1a7SDan Williams 	}
617f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
61839c686b8SVishal Verma 	return 0;
61939c686b8SVishal Verma }
62039c686b8SVishal Verma 
6215e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6225e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
623d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
624d4f32367SDan Williams {
625d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
626d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
627d4f32367SDan Williams 		return -EINVAL;
628d4f32367SDan Williams 
629d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
630d4f32367SDan Williams 		return -EINVAL;
631d4f32367SDan Williams 
6325e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
633d4f32367SDan Williams 	clear_err->status = 0;
634d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
635d4f32367SDan Williams 	*cmd_rc = 0;
636d4f32367SDan Williams 	return 0;
637d4f32367SDan Williams }
638d4f32367SDan Williams 
63910246dc8SYasunori Goto struct region_search_spa {
64010246dc8SYasunori Goto 	u64 addr;
64110246dc8SYasunori Goto 	struct nd_region *region;
64210246dc8SYasunori Goto };
64310246dc8SYasunori Goto 
64410246dc8SYasunori Goto static int is_region_device(struct device *dev)
64510246dc8SYasunori Goto {
64610246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
64710246dc8SYasunori Goto }
64810246dc8SYasunori Goto 
64910246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
65010246dc8SYasunori Goto {
65110246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
65210246dc8SYasunori Goto 	struct nd_region *nd_region;
65310246dc8SYasunori Goto 	resource_size_t ndr_end;
65410246dc8SYasunori Goto 
65510246dc8SYasunori Goto 	if (!is_region_device(dev))
65610246dc8SYasunori Goto 		return 0;
65710246dc8SYasunori Goto 
65810246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
65910246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
66010246dc8SYasunori Goto 
66110246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
66210246dc8SYasunori Goto 		ctx->region = nd_region;
66310246dc8SYasunori Goto 		return 1;
66410246dc8SYasunori Goto 	}
66510246dc8SYasunori Goto 
66610246dc8SYasunori Goto 	return 0;
66710246dc8SYasunori Goto }
66810246dc8SYasunori Goto 
66910246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
67010246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
67110246dc8SYasunori Goto {
67210246dc8SYasunori Goto 	int ret;
67310246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
67410246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
67510246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
67610246dc8SYasunori Goto 	struct region_search_spa ctx = {
67710246dc8SYasunori Goto 		.addr = spa->spa,
67810246dc8SYasunori Goto 		.region = NULL,
67910246dc8SYasunori Goto 	};
68010246dc8SYasunori Goto 	u64 dpa;
68110246dc8SYasunori Goto 
68210246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
68310246dc8SYasunori Goto 				nfit_test_search_region_spa);
68410246dc8SYasunori Goto 
68510246dc8SYasunori Goto 	if (!ret)
68610246dc8SYasunori Goto 		return -ENODEV;
68710246dc8SYasunori Goto 
68810246dc8SYasunori Goto 	nd_region = ctx.region;
68910246dc8SYasunori Goto 
69010246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
69110246dc8SYasunori Goto 
69210246dc8SYasunori Goto 	/*
69310246dc8SYasunori Goto 	 * last dimm is selected for test
69410246dc8SYasunori Goto 	 */
69510246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
69610246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
69710246dc8SYasunori Goto 
69810246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
69910246dc8SYasunori Goto 	spa->num_nvdimms = 1;
70010246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
70110246dc8SYasunori Goto 
70210246dc8SYasunori Goto 	return 0;
70310246dc8SYasunori Goto }
70410246dc8SYasunori Goto 
70510246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
70610246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
70710246dc8SYasunori Goto {
70810246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
70910246dc8SYasunori Goto 		return -EINVAL;
71010246dc8SYasunori Goto 
71110246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
71210246dc8SYasunori Goto 		spa->status = 2;
71310246dc8SYasunori Goto 
71410246dc8SYasunori Goto 	return 0;
71510246dc8SYasunori Goto }
71610246dc8SYasunori Goto 
717ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
718ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
719baa51277SDan Williams {
720baa51277SDan Williams 	if (buf_len < sizeof(*smart))
721baa51277SDan Williams 		return -EINVAL;
722ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
723baa51277SDan Williams 	return 0;
724baa51277SDan Williams }
725baa51277SDan Williams 
726cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
727ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
728ed07c433SDan Williams 		unsigned int buf_len,
729ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
730baa51277SDan Williams {
731baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
732baa51277SDan Williams 		return -EINVAL;
733ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
734ed07c433SDan Williams 	return 0;
735ed07c433SDan Williams }
736ed07c433SDan Williams 
737ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
738ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
739ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
740ed07c433SDan Williams {
741ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
742ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
743ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
744ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
745ed07c433SDan Williams 			smart->ctrl_temperature);
746ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
747ed07c433SDan Williams 				&& smart->spares
748ed07c433SDan Williams 				<= thresh->spares)
749ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
750ed07c433SDan Williams 				&& smart->media_temperature
751ed07c433SDan Williams 				>= thresh->media_temperature)
752ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
753ed07c433SDan Williams 				&& smart->ctrl_temperature
7544cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7554cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7564cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
757ed07c433SDan Williams 		device_lock(bus_dev);
758ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
759ed07c433SDan Williams 		device_unlock(bus_dev);
760ed07c433SDan Williams 	}
761ed07c433SDan Williams }
762ed07c433SDan Williams 
763ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
764ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
765ed07c433SDan Williams 		unsigned int buf_len,
766ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
767ed07c433SDan Williams 		struct nd_intel_smart *smart,
768ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
769ed07c433SDan Williams {
770ed07c433SDan Williams 	unsigned int size;
771ed07c433SDan Williams 
772ed07c433SDan Williams 	size = sizeof(*in) - 4;
773ed07c433SDan Williams 	if (buf_len < size)
774ed07c433SDan Williams 		return -EINVAL;
775ed07c433SDan Williams 	memcpy(thresh->data, in, size);
776ed07c433SDan Williams 	in->status = 0;
777ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
778ed07c433SDan Williams 
779baa51277SDan Williams 	return 0;
780baa51277SDan Williams }
781baa51277SDan Williams 
7824cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7834cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7844cf260fcSVishal Verma 		unsigned int buf_len,
7854cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7864cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7874cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7884cf260fcSVishal Verma {
7894cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7904cf260fcSVishal Verma 		return -EINVAL;
7914cf260fcSVishal Verma 
792b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7934cf260fcSVishal Verma 		if (inj->mtemp_enable)
7944cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
795b4d4702fSVishal Verma 		else
796b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
797b4d4702fSVishal Verma 	}
798b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7994cf260fcSVishal Verma 		if (inj->spare_enable)
8004cf260fcSVishal Verma 			smart->spares = inj->spares;
801b4d4702fSVishal Verma 		else
802b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
803b4d4702fSVishal Verma 	}
804b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
8054cf260fcSVishal Verma 		if (inj->fatal_enable)
8064cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
807b4d4702fSVishal Verma 		else
808b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
809b4d4702fSVishal Verma 	}
810b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
8114cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
8124cf260fcSVishal Verma 			smart->shutdown_state = 1;
8134cf260fcSVishal Verma 			smart->shutdown_count++;
814b4d4702fSVishal Verma 		} else
815b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8164cf260fcSVishal Verma 	}
8174cf260fcSVishal Verma 	inj->status = 0;
8184cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8194cf260fcSVishal Verma 
8204cf260fcSVishal Verma 	return 0;
8214cf260fcSVishal Verma }
8224cf260fcSVishal Verma 
8239fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8249fb1a190SDave Jiang {
8259fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8269fb1a190SDave Jiang 
8279fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8289fb1a190SDave Jiang }
8299fb1a190SDave Jiang 
8309fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8319fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8329fb1a190SDave Jiang {
8339fb1a190SDave Jiang 	int rc;
8349fb1a190SDave Jiang 
83541cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8369fb1a190SDave Jiang 		rc = -EINVAL;
8379fb1a190SDave Jiang 		goto err;
8389fb1a190SDave Jiang 	}
8399fb1a190SDave Jiang 
8409fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8419fb1a190SDave Jiang 		rc = -EINVAL;
8429fb1a190SDave Jiang 		goto err;
8439fb1a190SDave Jiang 	}
8449fb1a190SDave Jiang 
8459fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8469fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8479fb1a190SDave Jiang 	if (rc < 0)
8489fb1a190SDave Jiang 		goto err;
8499fb1a190SDave Jiang 
8509fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8519fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8529fb1a190SDave Jiang 
8539fb1a190SDave Jiang 	err_inj->status = 0;
8549fb1a190SDave Jiang 	return 0;
8559fb1a190SDave Jiang 
8569fb1a190SDave Jiang err:
8579fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8589fb1a190SDave Jiang 	return rc;
8599fb1a190SDave Jiang }
8609fb1a190SDave Jiang 
8619fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8629fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8639fb1a190SDave Jiang {
8649fb1a190SDave Jiang 	int rc;
8659fb1a190SDave Jiang 
86641cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8679fb1a190SDave Jiang 		rc = -EINVAL;
8689fb1a190SDave Jiang 		goto err;
8699fb1a190SDave Jiang 	}
8709fb1a190SDave Jiang 
8719fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8729fb1a190SDave Jiang 		rc = -EINVAL;
8739fb1a190SDave Jiang 		goto err;
8749fb1a190SDave Jiang 	}
8759fb1a190SDave Jiang 
8769fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8779fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8789fb1a190SDave Jiang 
8799fb1a190SDave Jiang 	err_clr->status = 0;
8809fb1a190SDave Jiang 	return 0;
8819fb1a190SDave Jiang 
8829fb1a190SDave Jiang err:
8839fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8849fb1a190SDave Jiang 	return rc;
8859fb1a190SDave Jiang }
8869fb1a190SDave Jiang 
8879fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8889fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8899fb1a190SDave Jiang 		unsigned int buf_len)
8909fb1a190SDave Jiang {
8919fb1a190SDave Jiang 	struct badrange_entry *be;
8929fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8939fb1a190SDave Jiang 	int i = 0;
8949fb1a190SDave Jiang 
8959fb1a190SDave Jiang 	err_stat->status = 0;
8969fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8979fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8989fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8999fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
9009fb1a190SDave Jiang 		i++;
9019fb1a190SDave Jiang 		if (i > max)
9029fb1a190SDave Jiang 			break;
9039fb1a190SDave Jiang 	}
9049fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
9059fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
9069fb1a190SDave Jiang 
9079fb1a190SDave Jiang 	return 0;
9089fb1a190SDave Jiang }
9099fb1a190SDave Jiang 
910674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
911674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
912674d8bdeSDave Jiang {
913674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
914674d8bdeSDave Jiang 
915674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
916674d8bdeSDave Jiang 		return -EINVAL;
917674d8bdeSDave Jiang 
918674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
919674d8bdeSDave Jiang 	case 0:
920674d8bdeSDave Jiang 		nd_cmd->status = 0;
921674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
922674d8bdeSDave Jiang 				__func__);
923674d8bdeSDave Jiang 		break;
924674d8bdeSDave Jiang 	case 1:
925674d8bdeSDave Jiang 		nd_cmd->status = 0;
926674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
927674d8bdeSDave Jiang 				__func__);
928674d8bdeSDave Jiang 		break;
929674d8bdeSDave Jiang 	default:
930674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
931674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
932674d8bdeSDave Jiang 		break;
933674d8bdeSDave Jiang 	}
934674d8bdeSDave Jiang 
935674d8bdeSDave Jiang 
936674d8bdeSDave Jiang 	return 0;
937674d8bdeSDave Jiang }
938674d8bdeSDave Jiang 
93939611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
94039611e83SDan Williams {
94139611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
94239611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
94339611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
94439611e83SDan Williams 		return -EIO;
94539611e83SDan Williams 	}
94639611e83SDan Williams 	return rc;
94739611e83SDan Williams }
94839611e83SDan Williams 
9493c13e2acSDave Jiang static int nd_intel_test_cmd_security_status(struct nfit_test *t,
9503c13e2acSDave Jiang 		struct nd_intel_get_security_state *nd_cmd,
9513c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9523c13e2acSDave Jiang {
9533c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9543c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9553c13e2acSDave Jiang 
9563c13e2acSDave Jiang 	nd_cmd->status = 0;
9573c13e2acSDave Jiang 	nd_cmd->state = sec->state;
958ecaa4a97SDave Jiang 	nd_cmd->extended_state = sec->ext_state;
9593c13e2acSDave Jiang 	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);
9603c13e2acSDave Jiang 
9613c13e2acSDave Jiang 	return 0;
9623c13e2acSDave Jiang }
9633c13e2acSDave Jiang 
9643c13e2acSDave Jiang static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
9653c13e2acSDave Jiang 		struct nd_intel_unlock_unit *nd_cmd,
9663c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9673c13e2acSDave Jiang {
9683c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9693c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9703c13e2acSDave Jiang 
9713c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
9723c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
9733c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9743c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
9753c13e2acSDave Jiang 				sec->state);
9763c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
9773c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9783c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9793c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid passphrase\n");
9803c13e2acSDave Jiang 	} else {
9813c13e2acSDave Jiang 		nd_cmd->status = 0;
9823c13e2acSDave Jiang 		sec->state = ND_INTEL_SEC_STATE_ENABLED;
9833c13e2acSDave Jiang 		dev_dbg(dev, "Unit unlocked\n");
9843c13e2acSDave Jiang 	}
9853c13e2acSDave Jiang 
9863c13e2acSDave Jiang 	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
9873c13e2acSDave Jiang 	return 0;
9883c13e2acSDave Jiang }
9893c13e2acSDave Jiang 
9903c13e2acSDave Jiang static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
9913c13e2acSDave Jiang 		struct nd_intel_set_passphrase *nd_cmd,
9923c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9933c13e2acSDave Jiang {
9943c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9953c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9963c13e2acSDave Jiang 
9973c13e2acSDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
9983c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9993c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong security state\n");
10003c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
10013c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10023c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10033c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong passphrase\n");
10043c13e2acSDave Jiang 	} else {
10053c13e2acSDave Jiang 		memcpy(sec->passphrase, nd_cmd->new_pass,
10063c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
10073c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
10083c13e2acSDave Jiang 		nd_cmd->status = 0;
10093c13e2acSDave Jiang 		dev_dbg(dev, "passphrase updated\n");
10103c13e2acSDave Jiang 	}
10113c13e2acSDave Jiang 
10123c13e2acSDave Jiang 	return 0;
10133c13e2acSDave Jiang }
10143c13e2acSDave Jiang 
10153c13e2acSDave Jiang static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
10163c13e2acSDave Jiang 		struct nd_intel_freeze_lock *nd_cmd,
10173c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10183c13e2acSDave Jiang {
10193c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10203c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10213c13e2acSDave Jiang 
10223c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
10233c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10243c13e2acSDave Jiang 		dev_dbg(dev, "freeze lock: wrong security state\n");
10253c13e2acSDave Jiang 	} else {
10263c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
10273c13e2acSDave Jiang 		nd_cmd->status = 0;
10283c13e2acSDave Jiang 		dev_dbg(dev, "security frozen\n");
10293c13e2acSDave Jiang 	}
10303c13e2acSDave Jiang 
10313c13e2acSDave Jiang 	return 0;
10323c13e2acSDave Jiang }
10333c13e2acSDave Jiang 
10343c13e2acSDave Jiang static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
10353c13e2acSDave Jiang 		struct nd_intel_disable_passphrase *nd_cmd,
10363c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10373c13e2acSDave Jiang {
10383c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10393c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10403c13e2acSDave Jiang 
10413c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
10423c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
10433c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10443c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong security state\n");
10453c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10463c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10473c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10483c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
10493c13e2acSDave Jiang 	} else {
10503c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10513c13e2acSDave Jiang 		sec->state = 0;
10523c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: done\n");
10533c13e2acSDave Jiang 	}
10543c13e2acSDave Jiang 
10553c13e2acSDave Jiang 	return 0;
10563c13e2acSDave Jiang }
10573c13e2acSDave Jiang 
10583c13e2acSDave Jiang static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
10593c13e2acSDave Jiang 		struct nd_intel_secure_erase *nd_cmd,
10603c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10613c13e2acSDave Jiang {
10623c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10633c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10643c13e2acSDave Jiang 
1065037c8489SDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
10663c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10673c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong security state\n");
10683c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10693c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10703c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10713c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong passphrase\n");
10723c13e2acSDave Jiang 	} else {
1073037c8489SDave Jiang 		if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)
1074037c8489SDave Jiang 				&& (memcmp(nd_cmd->passphrase, zero_key,
1075037c8489SDave Jiang 					ND_INTEL_PASSPHRASE_SIZE) != 0)) {
1076037c8489SDave Jiang 			dev_dbg(dev, "invalid zero key\n");
1077037c8489SDave Jiang 			return 0;
1078037c8489SDave Jiang 		}
10793c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1080ecaa4a97SDave Jiang 		memset(sec->master_passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10813c13e2acSDave Jiang 		sec->state = 0;
1082ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
10833c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: done\n");
10843c13e2acSDave Jiang 	}
10853c13e2acSDave Jiang 
10863c13e2acSDave Jiang 	return 0;
10873c13e2acSDave Jiang }
10883c13e2acSDave Jiang 
1089926f7480SDave Jiang static int nd_intel_test_cmd_overwrite(struct nfit_test *t,
1090926f7480SDave Jiang 		struct nd_intel_overwrite *nd_cmd,
1091926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1092926f7480SDave Jiang {
1093926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1094926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1095926f7480SDave Jiang 
1096926f7480SDave Jiang 	if ((sec->state & ND_INTEL_SEC_STATE_ENABLED) &&
1097926f7480SDave Jiang 			memcmp(nd_cmd->passphrase, sec->passphrase,
1098926f7480SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1099926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1100926f7480SDave Jiang 		dev_dbg(dev, "overwrite: wrong passphrase\n");
1101926f7480SDave Jiang 		return 0;
1102926f7480SDave Jiang 	}
1103926f7480SDave Jiang 
1104*2170a0d5SDave Jiang 	sec->old_state = sec->state;
1105926f7480SDave Jiang 	sec->state = ND_INTEL_SEC_STATE_OVERWRITE;
1106926f7480SDave Jiang 	dev_dbg(dev, "overwrite progressing.\n");
1107926f7480SDave Jiang 	sec->overwrite_end_time = get_jiffies_64() + 5 * HZ;
1108926f7480SDave Jiang 
1109926f7480SDave Jiang 	return 0;
1110926f7480SDave Jiang }
1111926f7480SDave Jiang 
1112926f7480SDave Jiang static int nd_intel_test_cmd_query_overwrite(struct nfit_test *t,
1113926f7480SDave Jiang 		struct nd_intel_query_overwrite *nd_cmd,
1114926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1115926f7480SDave Jiang {
1116926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1117926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1118926f7480SDave Jiang 
1119926f7480SDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_OVERWRITE)) {
1120926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_SEQUENCE_ERR;
1121926f7480SDave Jiang 		return 0;
1122926f7480SDave Jiang 	}
1123926f7480SDave Jiang 
1124926f7480SDave Jiang 	if (time_is_before_jiffies64(sec->overwrite_end_time)) {
1125926f7480SDave Jiang 		sec->overwrite_end_time = 0;
1126*2170a0d5SDave Jiang 		sec->state = sec->old_state;
1127*2170a0d5SDave Jiang 		sec->old_state = 0;
1128ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1129926f7480SDave Jiang 		dev_dbg(dev, "overwrite is complete\n");
1130926f7480SDave Jiang 	} else
1131926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_INPROGRESS;
1132926f7480SDave Jiang 	return 0;
1133926f7480SDave Jiang }
1134926f7480SDave Jiang 
1135ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_set_pass(struct nfit_test *t,
1136ecaa4a97SDave Jiang 		struct nd_intel_set_master_passphrase *nd_cmd,
1137ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1138ecaa4a97SDave Jiang {
1139ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1140ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1141ecaa4a97SDave Jiang 
1142ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1143ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1144ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong state\n");
1145ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1146ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1147ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong security state\n");
1148ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->master_passphrase,
1149ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1150ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1151ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: wrong passphrase\n");
1152ecaa4a97SDave Jiang 	} else {
1153ecaa4a97SDave Jiang 		memcpy(sec->master_passphrase, nd_cmd->new_pass,
1154ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
1155ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1156ecaa4a97SDave Jiang 		dev_dbg(dev, "master passphrase: updated\n");
1157ecaa4a97SDave Jiang 	}
1158ecaa4a97SDave Jiang 
1159ecaa4a97SDave Jiang 	return 0;
1160ecaa4a97SDave Jiang }
1161ecaa4a97SDave Jiang 
1162ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_secure_erase(struct nfit_test *t,
1163ecaa4a97SDave Jiang 		struct nd_intel_master_secure_erase *nd_cmd,
1164ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1165ecaa4a97SDave Jiang {
1166ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1167ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1168ecaa4a97SDave Jiang 
1169ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1170ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1171ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong state\n");
1172ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1173ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1174ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong security state\n");
1175ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->master_passphrase,
1176ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1177ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1178ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: wrong passphrase\n");
1179ecaa4a97SDave Jiang 	} else {
1180ecaa4a97SDave Jiang 		/* we do not erase master state passphrase ever */
1181ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1182ecaa4a97SDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1183ecaa4a97SDave Jiang 		sec->state = 0;
1184ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: done\n");
1185ecaa4a97SDave Jiang 	}
1186ecaa4a97SDave Jiang 
1187ecaa4a97SDave Jiang 	return 0;
1188ecaa4a97SDave Jiang }
1189ecaa4a97SDave Jiang 
1190ecaa4a97SDave Jiang 
1191bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
1192bfbaa952SDave Jiang {
1193bfbaa952SDave Jiang 	int i;
1194bfbaa952SDave Jiang 
1195bfbaa952SDave Jiang 	/* lookup per-dimm data */
1196bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1197bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1198bfbaa952SDave Jiang 			break;
1199bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
1200bfbaa952SDave Jiang 		return -ENXIO;
1201bfbaa952SDave Jiang 	return i;
1202bfbaa952SDave Jiang }
1203bfbaa952SDave Jiang 
120439c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
120539c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1206aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
120739c686b8SVishal Verma {
120839c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
120939c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
12106634fb06SDan Williams 	unsigned int func = cmd;
1211f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1212f471f1a7SDan Williams 
1213f471f1a7SDan Williams 	if (!cmd_rc)
1214f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1215f471f1a7SDan Williams 	*cmd_rc = 0;
121639c686b8SVishal Verma 
121739c686b8SVishal Verma 	if (nvdimm) {
121839c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1219e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
122039c686b8SVishal Verma 
12216634fb06SDan Williams 		if (!nfit_mem)
12226634fb06SDan Williams 			return -ENOTTY;
12236634fb06SDan Williams 
12246634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
12256634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
12266634fb06SDan Williams 
12276634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
12286634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
12296634fb06SDan Williams 			func = call_pkg->nd_command;
12306634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
12316634fb06SDan Williams 				return -ENOTTY;
1232bfbaa952SDave Jiang 
1233bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1234bfbaa952SDave Jiang 			if (i < 0)
1235bfbaa952SDave Jiang 				return i;
1236bfbaa952SDave Jiang 
1237bfbaa952SDave Jiang 			switch (func) {
12383c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
12393c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
12403c13e2acSDave Jiang 						buf, buf_len, i);
12413c13e2acSDave Jiang 				break;
12423c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
12433c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
12443c13e2acSDave Jiang 						buf, buf_len, i);
12453c13e2acSDave Jiang 				break;
12463c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
12473c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
12483c13e2acSDave Jiang 						buf, buf_len, i);
12493c13e2acSDave Jiang 				break;
12503c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
12513c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
12523c13e2acSDave Jiang 						buf, buf_len, i);
12533c13e2acSDave Jiang 				break;
12543c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
12553c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
12563c13e2acSDave Jiang 						buf, buf_len, i);
12573c13e2acSDave Jiang 				break;
12583c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
12593c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
12603c13e2acSDave Jiang 						buf, buf_len, i);
12613c13e2acSDave Jiang 				break;
1262926f7480SDave Jiang 			case NVDIMM_INTEL_OVERWRITE:
1263926f7480SDave Jiang 				rc = nd_intel_test_cmd_overwrite(t,
1264926f7480SDave Jiang 						buf, buf_len, i - t->dcr_idx);
1265926f7480SDave Jiang 				break;
1266926f7480SDave Jiang 			case NVDIMM_INTEL_QUERY_OVERWRITE:
1267926f7480SDave Jiang 				rc = nd_intel_test_cmd_query_overwrite(t,
1268926f7480SDave Jiang 						buf, buf_len, i - t->dcr_idx);
1269926f7480SDave Jiang 				break;
1270ecaa4a97SDave Jiang 			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
1271ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_set_pass(t,
1272ecaa4a97SDave Jiang 						buf, buf_len, i);
1273ecaa4a97SDave Jiang 				break;
1274ecaa4a97SDave Jiang 			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
1275ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_secure_erase(t,
1276ecaa4a97SDave Jiang 						buf, buf_len, i);
1277ecaa4a97SDave Jiang 				break;
1278674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
127939611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1280674d8bdeSDave Jiang 						buf, buf_len);
128139611e83SDan Williams 				break;
1282bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
128339611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
1284bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128539611e83SDan Williams 				break;
1286bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
128739611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
1288bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128939611e83SDan Williams 				break;
1290bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
129139611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
1292bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
129339611e83SDan Williams 				break;
1294bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
129539611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
1296bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
129739611e83SDan Williams 				break;
1298bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
129939611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
1300bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
130139611e83SDan Williams 				break;
1302bfbaa952SDave Jiang 			case ND_INTEL_SMART:
130339611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
1304bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
130539611e83SDan Williams 				break;
1306bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
130739611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1308bfbaa952SDave Jiang 						buf_len,
1309bfbaa952SDave Jiang 						&t->smart_threshold[i -
1310bfbaa952SDave Jiang 							t->dcr_idx]);
131139611e83SDan Williams 				break;
1312bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
131339611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1314bfbaa952SDave Jiang 						buf_len,
1315bfbaa952SDave Jiang 						&t->smart_threshold[i -
1316bfbaa952SDave Jiang 							t->dcr_idx],
1317bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
1318bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
131939611e83SDan Williams 				break;
13204cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
132139611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
13224cf260fcSVishal Verma 						buf_len,
13234cf260fcSVishal Verma 						&t->smart_threshold[i -
13244cf260fcSVishal Verma 							t->dcr_idx],
13254cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
13264cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
132739611e83SDan Williams 				break;
1328bfbaa952SDave Jiang 			default:
1329bfbaa952SDave Jiang 				return -ENOTTY;
1330bfbaa952SDave Jiang 			}
133139611e83SDan Williams 			return override_return_code(i, func, rc);
13326634fb06SDan Williams 		}
13336634fb06SDan Williams 
13346634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
13356634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
133639c686b8SVishal Verma 			return -ENOTTY;
133739c686b8SVishal Verma 
1338bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1339bfbaa952SDave Jiang 		if (i < 0)
1340bfbaa952SDave Jiang 			return i;
134173606afdSDan Williams 
13426634fb06SDan Williams 		switch (func) {
134339c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
134439c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
134539c686b8SVishal Verma 			break;
134639c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
134739c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1348dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
134939c686b8SVishal Verma 			break;
135039c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
135139c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1352dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
135339c686b8SVishal Verma 			break;
13546bc75619SDan Williams 		default:
13556bc75619SDan Williams 			return -ENOTTY;
13566bc75619SDan Williams 		}
135739611e83SDan Williams 		return override_return_code(i, func, rc);
135839c686b8SVishal Verma 	} else {
1359f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
136010246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
136110246dc8SYasunori Goto 
136210246dc8SYasunori Goto 		if (!nd_desc)
136310246dc8SYasunori Goto 			return -ENOTTY;
136410246dc8SYasunori Goto 
136510246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
136610246dc8SYasunori Goto 			func = call_pkg->nd_command;
136710246dc8SYasunori Goto 
136810246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
136910246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
137010246dc8SYasunori Goto 
137110246dc8SYasunori Goto 			switch (func) {
137210246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
137310246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
137410246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
137510246dc8SYasunori Goto 				return rc;
13769fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
13779fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
13789fb1a190SDave Jiang 					buf_len);
13799fb1a190SDave Jiang 				return rc;
13809fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
13819fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
13829fb1a190SDave Jiang 					buf_len);
13839fb1a190SDave Jiang 				return rc;
13849fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
13859fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
13869fb1a190SDave Jiang 					buf_len);
13879fb1a190SDave Jiang 				return rc;
138810246dc8SYasunori Goto 			default:
138910246dc8SYasunori Goto 				return -ENOTTY;
139010246dc8SYasunori Goto 			}
139110246dc8SYasunori Goto 		}
1392f471f1a7SDan Williams 
1393e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
139439c686b8SVishal Verma 			return -ENOTTY;
139539c686b8SVishal Verma 
13966634fb06SDan Williams 		switch (func) {
139739c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
139839c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
139939c686b8SVishal Verma 			break;
140039c686b8SVishal Verma 		case ND_CMD_ARS_START:
14019fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
14029fb1a190SDave Jiang 					buf_len, cmd_rc);
140339c686b8SVishal Verma 			break;
140439c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1405f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1406f471f1a7SDan Williams 					cmd_rc);
140739c686b8SVishal Verma 			break;
1408d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
14095e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1410d4f32367SDan Williams 			break;
141139c686b8SVishal Verma 		default:
141239c686b8SVishal Verma 			return -ENOTTY;
141339c686b8SVishal Verma 		}
141439c686b8SVishal Verma 	}
14156bc75619SDan Williams 
14166bc75619SDan Williams 	return rc;
14176bc75619SDan Williams }
14186bc75619SDan Williams 
14196bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
14206bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
14216bc75619SDan Williams 
14226bc75619SDan Williams static void release_nfit_res(void *data)
14236bc75619SDan Williams {
14246bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
14256bc75619SDan Williams 
14266bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14276bc75619SDan Williams 	list_del(&nfit_res->list);
14286bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14296bc75619SDan Williams 
1430e3f5df76SDan Williams 	if (resource_size(&nfit_res->res) >= DIMM_SIZE)
1431e3f5df76SDan Williams 		gen_pool_free(nfit_pool, nfit_res->res.start,
1432e3f5df76SDan Williams 				resource_size(&nfit_res->res));
14336bc75619SDan Williams 	vfree(nfit_res->buf);
14346bc75619SDan Williams 	kfree(nfit_res);
14356bc75619SDan Williams }
14366bc75619SDan Williams 
14376bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
14386bc75619SDan Williams 		void *buf)
14396bc75619SDan Williams {
14406bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
14416bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
14426bc75619SDan Williams 			GFP_KERNEL);
14436bc75619SDan Williams 	int rc;
14446bc75619SDan Williams 
1445e3f5df76SDan Williams 	if (!buf || !nfit_res || !*dma)
14466bc75619SDan Williams 		goto err;
14476bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
14486bc75619SDan Williams 	if (rc)
14496bc75619SDan Williams 		goto err;
14506bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
14516bc75619SDan Williams 	memset(buf, 0, size);
14526bc75619SDan Williams 	nfit_res->dev = dev;
14536bc75619SDan Williams 	nfit_res->buf = buf;
1454bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1455bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1456bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1457bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1458bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
14596bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14606bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
14616bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14626bc75619SDan Williams 
14636bc75619SDan Williams 	return nfit_res->buf;
14646bc75619SDan Williams  err:
1465e3f5df76SDan Williams 	if (*dma && size >= DIMM_SIZE)
1466e3f5df76SDan Williams 		gen_pool_free(nfit_pool, *dma, size);
1467ee8520feSDan Williams 	if (buf)
14686bc75619SDan Williams 		vfree(buf);
14696bc75619SDan Williams 	kfree(nfit_res);
14706bc75619SDan Williams 	return NULL;
14716bc75619SDan Williams }
14726bc75619SDan Williams 
14736bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
14746bc75619SDan Williams {
1475e3f5df76SDan Williams 	struct genpool_data_align data = {
1476e3f5df76SDan Williams 		.align = SZ_128M,
1477e3f5df76SDan Williams 	};
14786bc75619SDan Williams 	void *buf = vmalloc(size);
14796bc75619SDan Williams 
1480e3f5df76SDan Williams 	if (size >= DIMM_SIZE)
1481e3f5df76SDan Williams 		*dma = gen_pool_alloc_algo(nfit_pool, size,
1482e3f5df76SDan Williams 				gen_pool_first_fit_align, &data);
1483e3f5df76SDan Williams 	else
14846bc75619SDan Williams 		*dma = (unsigned long) buf;
14856bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
14866bc75619SDan Williams }
14876bc75619SDan Williams 
14886bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
14896bc75619SDan Williams {
14906bc75619SDan Williams 	int i;
14916bc75619SDan Williams 
14926bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
14936bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
14946bc75619SDan Williams 		struct nfit_test *t = instances[i];
14956bc75619SDan Williams 
14966bc75619SDan Williams 		if (!t)
14976bc75619SDan Williams 			continue;
14986bc75619SDan Williams 		spin_lock(&nfit_test_lock);
14996bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1500bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1501bd4cd745SDan Williams 						+ resource_size(&n->res))) {
15026bc75619SDan Williams 				nfit_res = n;
15036bc75619SDan Williams 				break;
15046bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
15056bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1506bd4cd745SDan Williams 						+ resource_size(&n->res))) {
15076bc75619SDan Williams 				nfit_res = n;
15086bc75619SDan Williams 				break;
15096bc75619SDan Williams 			}
15106bc75619SDan Williams 		}
15116bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
15126bc75619SDan Williams 		if (nfit_res)
15136bc75619SDan Williams 			return nfit_res;
15146bc75619SDan Williams 	}
15156bc75619SDan Williams 
15166bc75619SDan Williams 	return NULL;
15176bc75619SDan Williams }
15186bc75619SDan Williams 
1519f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1520f471f1a7SDan Williams {
15219fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1522f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
15239fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1524f471f1a7SDan Williams 	if (!ars_state->ars_status)
1525f471f1a7SDan Williams 		return -ENOMEM;
1526f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1527f471f1a7SDan Williams 	return 0;
1528f471f1a7SDan Williams }
1529f471f1a7SDan Williams 
1530231bf117SDan Williams static void put_dimms(void *data)
1531231bf117SDan Williams {
1532718fda67SDan Williams 	struct nfit_test *t = data;
1533231bf117SDan Williams 	int i;
1534231bf117SDan Williams 
1535718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1536718fda67SDan Williams 		if (t->dimm_dev[i])
1537718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1538231bf117SDan Williams }
1539231bf117SDan Williams 
1540231bf117SDan Williams static struct class *nfit_test_dimm;
1541231bf117SDan Williams 
154273606afdSDan Williams static int dimm_name_to_id(struct device *dev)
154373606afdSDan Williams {
154473606afdSDan Williams 	int dimm;
154573606afdSDan Williams 
1546718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
154773606afdSDan Williams 		return -ENXIO;
154873606afdSDan Williams 	return dimm;
154973606afdSDan Williams }
155073606afdSDan Williams 
155173606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
155273606afdSDan Williams 		char *buf)
155373606afdSDan Williams {
155473606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
155573606afdSDan Williams 
155673606afdSDan Williams 	if (dimm < 0)
155773606afdSDan Williams 		return dimm;
155873606afdSDan Williams 
155919357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
156073606afdSDan Williams }
156173606afdSDan Williams DEVICE_ATTR_RO(handle);
156273606afdSDan Williams 
156373606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
156473606afdSDan Williams 		char *buf)
156573606afdSDan Williams {
156673606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
156773606afdSDan Williams 
156873606afdSDan Williams 	if (dimm < 0)
156973606afdSDan Williams 		return dimm;
157073606afdSDan Williams 
157173606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
157273606afdSDan Williams }
157373606afdSDan Williams 
157473606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
157573606afdSDan Williams 		const char *buf, size_t size)
157673606afdSDan Williams {
157773606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
157873606afdSDan Williams 	unsigned long val;
157973606afdSDan Williams 	ssize_t rc;
158073606afdSDan Williams 
158173606afdSDan Williams 	if (dimm < 0)
158273606afdSDan Williams 		return dimm;
158373606afdSDan Williams 
158473606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
158573606afdSDan Williams 	if (rc)
158673606afdSDan Williams 		return rc;
158773606afdSDan Williams 
158873606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
158973606afdSDan Williams 	return size;
159073606afdSDan Williams }
159173606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
159273606afdSDan Williams 
159355c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
159455c72ab6SDan Williams 		char *buf)
159555c72ab6SDan Williams {
159655c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
159755c72ab6SDan Williams 
159855c72ab6SDan Williams 	if (dimm < 0)
159955c72ab6SDan Williams 		return dimm;
160055c72ab6SDan Williams 
160155c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
160255c72ab6SDan Williams }
160355c72ab6SDan Williams 
160455c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
160555c72ab6SDan Williams 		const char *buf, size_t size)
160655c72ab6SDan Williams {
160755c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
160855c72ab6SDan Williams 	unsigned long val;
160955c72ab6SDan Williams 	ssize_t rc;
161055c72ab6SDan Williams 
161155c72ab6SDan Williams 	if (dimm < 0)
161255c72ab6SDan Williams 		return dimm;
161355c72ab6SDan Williams 
161455c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
161555c72ab6SDan Williams 	if (rc)
161655c72ab6SDan Williams 		return rc;
161755c72ab6SDan Williams 
161855c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
161955c72ab6SDan Williams 	return size;
162055c72ab6SDan Williams }
162155c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
162255c72ab6SDan Williams 
16233c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
16243c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
16253c13e2acSDave Jiang {
16263c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
16273c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
16283c13e2acSDave Jiang 
16293c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
16303c13e2acSDave Jiang 	return size;
16313c13e2acSDave Jiang }
16323c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
16333c13e2acSDave Jiang 
163473606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
163573606afdSDan Williams 	&dev_attr_fail_cmd.attr,
163655c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
163773606afdSDan Williams 	&dev_attr_handle.attr,
16383c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
163973606afdSDan Williams 	NULL,
164073606afdSDan Williams };
164173606afdSDan Williams 
164273606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
164373606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
164473606afdSDan Williams };
164573606afdSDan Williams 
164673606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
164773606afdSDan Williams 	&nfit_test_dimm_attribute_group,
164873606afdSDan Williams 	NULL,
164973606afdSDan Williams };
165073606afdSDan Williams 
1651718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1652718fda67SDan Williams {
1653718fda67SDan Williams 	int i;
1654718fda67SDan Williams 
1655718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1656718fda67SDan Williams 		return -ENOMEM;
1657718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1658718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1659718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1660718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1661718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1662718fda67SDan Williams 		if (!t->dimm_dev[i])
1663718fda67SDan Williams 			return -ENOMEM;
1664718fda67SDan Williams 	}
1665718fda67SDan Williams 	return 0;
1666718fda67SDan Williams }
1667718fda67SDan Williams 
1668ecaa4a97SDave Jiang static void security_init(struct nfit_test *t)
1669ecaa4a97SDave Jiang {
1670ecaa4a97SDave Jiang 	int i;
1671ecaa4a97SDave Jiang 
1672ecaa4a97SDave Jiang 	for (i = 0; i < t->num_dcr; i++) {
1673ecaa4a97SDave Jiang 		struct nfit_test_sec *sec = &dimm_sec_info[i];
1674ecaa4a97SDave Jiang 
1675ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1676ecaa4a97SDave Jiang 	}
1677ecaa4a97SDave Jiang }
1678ecaa4a97SDave Jiang 
1679ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1680ed07c433SDan Williams {
1681ed07c433SDan Williams 	int i;
1682ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1683ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1684ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1685ed07c433SDan Williams 		.media_temperature = 40 * 16,
1686ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1687ed07c433SDan Williams 		.spares = 5,
1688ed07c433SDan Williams 	};
1689ed07c433SDan Williams 
1690ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1691b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1692ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1693ed07c433SDan Williams 				sizeof(smart_t_data));
1694ed07c433SDan Williams 	}
1695ed07c433SDan Williams }
1696ed07c433SDan Williams 
16976bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
16986bc75619SDan Williams {
16996b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
17006bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
17016bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
17023b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
17033b87356fSDan Williams 					window_size) * NUM_DCR
17049d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
170585d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1706f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1707f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
17086bc75619SDan Williams 	int i;
17096bc75619SDan Williams 
17106bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17116bc75619SDan Williams 	if (!t->nfit_buf)
17126bc75619SDan Williams 		return -ENOMEM;
17136bc75619SDan Williams 	t->nfit_size = nfit_size;
17146bc75619SDan Williams 
1715ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
17166bc75619SDan Williams 	if (!t->spa_set[0])
17176bc75619SDan Williams 		return -ENOMEM;
17186bc75619SDan Williams 
1719ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
17206bc75619SDan Williams 	if (!t->spa_set[1])
17216bc75619SDan Williams 		return -ENOMEM;
17226bc75619SDan Williams 
1723ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
172420985164SVishal Verma 	if (!t->spa_set[2])
172520985164SVishal Verma 		return -ENOMEM;
172620985164SVishal Verma 
1727dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17286bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
17296bc75619SDan Williams 		if (!t->dimm[i])
17306bc75619SDan Williams 			return -ENOMEM;
17316bc75619SDan Williams 
17326bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
17336bc75619SDan Williams 		if (!t->label[i])
17346bc75619SDan Williams 			return -ENOMEM;
17356bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
17369d27a87eSDan Williams 
17379d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
17389d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
173985d3fa02SDan Williams 				&t->flush_dma[i]);
17409d27a87eSDan Williams 		if (!t->flush[i])
17419d27a87eSDan Williams 			return -ENOMEM;
17426bc75619SDan Williams 	}
17436bc75619SDan Williams 
1744dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17456bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
17466bc75619SDan Williams 		if (!t->dcr[i])
17476bc75619SDan Williams 			return -ENOMEM;
17486bc75619SDan Williams 	}
17496bc75619SDan Williams 
1750c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1751c14a868aSDan Williams 	if (!t->_fit)
1752c14a868aSDan Williams 		return -ENOMEM;
1753c14a868aSDan Williams 
1754718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1755231bf117SDan Williams 		return -ENOMEM;
1756ed07c433SDan Williams 	smart_init(t);
1757ecaa4a97SDave Jiang 	security_init(t);
1758f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17596bc75619SDan Williams }
17606bc75619SDan Williams 
17616bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
17626bc75619SDan Williams {
17637bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1764ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1765ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1766dafb1048SDan Williams 	int i;
17676bc75619SDan Williams 
17686bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17696bc75619SDan Williams 	if (!t->nfit_buf)
17706bc75619SDan Williams 		return -ENOMEM;
17716bc75619SDan Williams 	t->nfit_size = nfit_size;
17726bc75619SDan Williams 
1773ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
17746bc75619SDan Williams 	if (!t->spa_set[0])
17756bc75619SDan Williams 		return -ENOMEM;
17766bc75619SDan Williams 
1777dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1778dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1779dafb1048SDan Williams 		if (!t->label[i])
1780dafb1048SDan Williams 			return -ENOMEM;
1781dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1782dafb1048SDan Williams 	}
1783dafb1048SDan Williams 
17847bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
17857bfe97c7SDan Williams 	if (!t->spa_set[1])
17867bfe97c7SDan Williams 		return -ENOMEM;
17877bfe97c7SDan Williams 
1788718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1789718fda67SDan Williams 		return -ENOMEM;
1790ed07c433SDan Williams 	smart_init(t);
1791f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17926bc75619SDan Williams }
17936bc75619SDan Williams 
17945dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
17955dc68e55SDan Williams {
17965dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
17975dc68e55SDan Williams 	dcr->device_id = 0;
17985dc68e55SDan Williams 	dcr->revision_id = 1;
17995dc68e55SDan Williams 	dcr->valid_fields = 1;
18005dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
18015dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
18025dc68e55SDan Williams }
18035dc68e55SDan Williams 
18046bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
18056bc75619SDan Williams {
180685d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
180785d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
18086bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
18096bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
18106bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
18116bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
18126bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
18136bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
18149d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1815f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1816d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
18176bc75619SDan Williams 
18186bc75619SDan Williams 	/*
18196bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
18206bc75619SDan Williams 	 * does not actually alias the related block-data-window
18216bc75619SDan Williams 	 * regions)
18226bc75619SDan Williams 	 */
18236b577c9dSLinda Knippers 	spa = nfit_buf;
18246bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18256bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18266bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18276bc75619SDan Williams 	spa->range_index = 0+1;
18286bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
18296bc75619SDan Williams 	spa->length = SPA0_SIZE;
1830d7d8464dSRoss Zwisler 	offset += spa->header.length;
18316bc75619SDan Williams 
18326bc75619SDan Williams 	/*
18336bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
18346bc75619SDan Williams 	 * does not actually alias the related block-data-window
18356bc75619SDan Williams 	 * regions)
18366bc75619SDan Williams 	 */
1837d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18386bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18396bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18406bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18416bc75619SDan Williams 	spa->range_index = 1+1;
18426bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
18436bc75619SDan Williams 	spa->length = SPA1_SIZE;
1844d7d8464dSRoss Zwisler 	offset += spa->header.length;
18456bc75619SDan Williams 
18466bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1847d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18486bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18496bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18506bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18516bc75619SDan Williams 	spa->range_index = 2+1;
18526bc75619SDan Williams 	spa->address = t->dcr_dma[0];
18536bc75619SDan Williams 	spa->length = DCR_SIZE;
1854d7d8464dSRoss Zwisler 	offset += spa->header.length;
18556bc75619SDan Williams 
18566bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1857d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18586bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18596bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18606bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18616bc75619SDan Williams 	spa->range_index = 3+1;
18626bc75619SDan Williams 	spa->address = t->dcr_dma[1];
18636bc75619SDan Williams 	spa->length = DCR_SIZE;
1864d7d8464dSRoss Zwisler 	offset += spa->header.length;
18656bc75619SDan Williams 
18666bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1867d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18686bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18696bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18706bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18716bc75619SDan Williams 	spa->range_index = 4+1;
18726bc75619SDan Williams 	spa->address = t->dcr_dma[2];
18736bc75619SDan Williams 	spa->length = DCR_SIZE;
1874d7d8464dSRoss Zwisler 	offset += spa->header.length;
18756bc75619SDan Williams 
18766bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1877d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18786bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18796bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18806bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18816bc75619SDan Williams 	spa->range_index = 5+1;
18826bc75619SDan Williams 	spa->address = t->dcr_dma[3];
18836bc75619SDan Williams 	spa->length = DCR_SIZE;
1884d7d8464dSRoss Zwisler 	offset += spa->header.length;
18856bc75619SDan Williams 
18866bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1887d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18886bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18896bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18906bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18916bc75619SDan Williams 	spa->range_index = 6+1;
18926bc75619SDan Williams 	spa->address = t->dimm_dma[0];
18936bc75619SDan Williams 	spa->length = DIMM_SIZE;
1894d7d8464dSRoss Zwisler 	offset += spa->header.length;
18956bc75619SDan Williams 
18966bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1897d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18986bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18996bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19006bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19016bc75619SDan Williams 	spa->range_index = 7+1;
19026bc75619SDan Williams 	spa->address = t->dimm_dma[1];
19036bc75619SDan Williams 	spa->length = DIMM_SIZE;
1904d7d8464dSRoss Zwisler 	offset += spa->header.length;
19056bc75619SDan Williams 
19066bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1907d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19086bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19096bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19106bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19116bc75619SDan Williams 	spa->range_index = 8+1;
19126bc75619SDan Williams 	spa->address = t->dimm_dma[2];
19136bc75619SDan Williams 	spa->length = DIMM_SIZE;
1914d7d8464dSRoss Zwisler 	offset += spa->header.length;
19156bc75619SDan Williams 
19166bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1917d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19186bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19196bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19206bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19216bc75619SDan Williams 	spa->range_index = 9+1;
19226bc75619SDan Williams 	spa->address = t->dimm_dma[3];
19236bc75619SDan Williams 	spa->length = DIMM_SIZE;
1924d7d8464dSRoss Zwisler 	offset += spa->header.length;
19256bc75619SDan Williams 
19266bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
19276bc75619SDan Williams 	memdev = nfit_buf + offset;
19286bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19296bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19306bc75619SDan Williams 	memdev->device_handle = handle[0];
19316bc75619SDan Williams 	memdev->physical_id = 0;
19326bc75619SDan Williams 	memdev->region_id = 0;
19336bc75619SDan Williams 	memdev->range_index = 0+1;
19343b87356fSDan Williams 	memdev->region_index = 4+1;
19356bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1936df06a2d5SDan Williams 	memdev->region_offset = 1;
19376bc75619SDan Williams 	memdev->address = 0;
19386bc75619SDan Williams 	memdev->interleave_index = 0;
19396bc75619SDan Williams 	memdev->interleave_ways = 2;
1940d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19416bc75619SDan Williams 
19426bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1943d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19446bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19456bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19466bc75619SDan Williams 	memdev->device_handle = handle[1];
19476bc75619SDan Williams 	memdev->physical_id = 1;
19486bc75619SDan Williams 	memdev->region_id = 0;
19496bc75619SDan Williams 	memdev->range_index = 0+1;
19503b87356fSDan Williams 	memdev->region_index = 5+1;
19516bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1952df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
19536bc75619SDan Williams 	memdev->address = 0;
19546bc75619SDan Williams 	memdev->interleave_index = 0;
19556bc75619SDan Williams 	memdev->interleave_ways = 2;
1956ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1957d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19586bc75619SDan Williams 
19596bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1960d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19616bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19626bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19636bc75619SDan Williams 	memdev->device_handle = handle[0];
19646bc75619SDan Williams 	memdev->physical_id = 0;
19656bc75619SDan Williams 	memdev->region_id = 1;
19666bc75619SDan Williams 	memdev->range_index = 1+1;
19673b87356fSDan Williams 	memdev->region_index = 4+1;
19686bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1969df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
19706bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19716bc75619SDan Williams 	memdev->interleave_index = 0;
19726bc75619SDan Williams 	memdev->interleave_ways = 4;
1973ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1974d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19756bc75619SDan Williams 
19766bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1977d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19786bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19796bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19806bc75619SDan Williams 	memdev->device_handle = handle[1];
19816bc75619SDan Williams 	memdev->physical_id = 1;
19826bc75619SDan Williams 	memdev->region_id = 1;
19836bc75619SDan Williams 	memdev->range_index = 1+1;
19843b87356fSDan Williams 	memdev->region_index = 5+1;
19856bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1986df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
19876bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19886bc75619SDan Williams 	memdev->interleave_index = 0;
19896bc75619SDan Williams 	memdev->interleave_ways = 4;
1990d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19916bc75619SDan Williams 
19926bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1993d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19946bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19956bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19966bc75619SDan Williams 	memdev->device_handle = handle[2];
19976bc75619SDan Williams 	memdev->physical_id = 2;
19986bc75619SDan Williams 	memdev->region_id = 0;
19996bc75619SDan Williams 	memdev->range_index = 1+1;
20003b87356fSDan Williams 	memdev->region_index = 6+1;
20016bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2002df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
20036bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
20046bc75619SDan Williams 	memdev->interleave_index = 0;
20056bc75619SDan Williams 	memdev->interleave_ways = 4;
2006ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2007d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20086bc75619SDan Williams 
20096bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
2010d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20116bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20126bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20136bc75619SDan Williams 	memdev->device_handle = handle[3];
20146bc75619SDan Williams 	memdev->physical_id = 3;
20156bc75619SDan Williams 	memdev->region_id = 0;
20166bc75619SDan Williams 	memdev->range_index = 1+1;
20173b87356fSDan Williams 	memdev->region_index = 7+1;
20186bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2019df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
20206bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
20216bc75619SDan Williams 	memdev->interleave_index = 0;
20226bc75619SDan Williams 	memdev->interleave_ways = 4;
2023d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20246bc75619SDan Williams 
20256bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
2026d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20276bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20286bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20296bc75619SDan Williams 	memdev->device_handle = handle[0];
20306bc75619SDan Williams 	memdev->physical_id = 0;
20316bc75619SDan Williams 	memdev->region_id = 0;
20326bc75619SDan Williams 	memdev->range_index = 2+1;
20336bc75619SDan Williams 	memdev->region_index = 0+1;
20346bc75619SDan Williams 	memdev->region_size = 0;
20356bc75619SDan Williams 	memdev->region_offset = 0;
20366bc75619SDan Williams 	memdev->address = 0;
20376bc75619SDan Williams 	memdev->interleave_index = 0;
20386bc75619SDan Williams 	memdev->interleave_ways = 1;
2039d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20406bc75619SDan Williams 
20416bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
2042d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20436bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20446bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20456bc75619SDan Williams 	memdev->device_handle = handle[1];
20466bc75619SDan Williams 	memdev->physical_id = 1;
20476bc75619SDan Williams 	memdev->region_id = 0;
20486bc75619SDan Williams 	memdev->range_index = 3+1;
20496bc75619SDan Williams 	memdev->region_index = 1+1;
20506bc75619SDan Williams 	memdev->region_size = 0;
20516bc75619SDan Williams 	memdev->region_offset = 0;
20526bc75619SDan Williams 	memdev->address = 0;
20536bc75619SDan Williams 	memdev->interleave_index = 0;
20546bc75619SDan Williams 	memdev->interleave_ways = 1;
2055d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20566bc75619SDan Williams 
20576bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
2058d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20596bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20606bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20616bc75619SDan Williams 	memdev->device_handle = handle[2];
20626bc75619SDan Williams 	memdev->physical_id = 2;
20636bc75619SDan Williams 	memdev->region_id = 0;
20646bc75619SDan Williams 	memdev->range_index = 4+1;
20656bc75619SDan Williams 	memdev->region_index = 2+1;
20666bc75619SDan Williams 	memdev->region_size = 0;
20676bc75619SDan Williams 	memdev->region_offset = 0;
20686bc75619SDan Williams 	memdev->address = 0;
20696bc75619SDan Williams 	memdev->interleave_index = 0;
20706bc75619SDan Williams 	memdev->interleave_ways = 1;
2071d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20726bc75619SDan Williams 
20736bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
2074d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20756bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20766bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20776bc75619SDan Williams 	memdev->device_handle = handle[3];
20786bc75619SDan Williams 	memdev->physical_id = 3;
20796bc75619SDan Williams 	memdev->region_id = 0;
20806bc75619SDan Williams 	memdev->range_index = 5+1;
20816bc75619SDan Williams 	memdev->region_index = 3+1;
20826bc75619SDan Williams 	memdev->region_size = 0;
20836bc75619SDan Williams 	memdev->region_offset = 0;
20846bc75619SDan Williams 	memdev->address = 0;
20856bc75619SDan Williams 	memdev->interleave_index = 0;
20866bc75619SDan Williams 	memdev->interleave_ways = 1;
2087d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20886bc75619SDan Williams 
20896bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
2090d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20916bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20926bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20936bc75619SDan Williams 	memdev->device_handle = handle[0];
20946bc75619SDan Williams 	memdev->physical_id = 0;
20956bc75619SDan Williams 	memdev->region_id = 0;
20966bc75619SDan Williams 	memdev->range_index = 6+1;
20976bc75619SDan Williams 	memdev->region_index = 0+1;
20986bc75619SDan Williams 	memdev->region_size = 0;
20996bc75619SDan Williams 	memdev->region_offset = 0;
21006bc75619SDan Williams 	memdev->address = 0;
21016bc75619SDan Williams 	memdev->interleave_index = 0;
21026bc75619SDan Williams 	memdev->interleave_ways = 1;
2103d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21046bc75619SDan Williams 
21056bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
2106d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21076bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21086bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21096bc75619SDan Williams 	memdev->device_handle = handle[1];
21106bc75619SDan Williams 	memdev->physical_id = 1;
21116bc75619SDan Williams 	memdev->region_id = 0;
21126bc75619SDan Williams 	memdev->range_index = 7+1;
21136bc75619SDan Williams 	memdev->region_index = 1+1;
21146bc75619SDan Williams 	memdev->region_size = 0;
21156bc75619SDan Williams 	memdev->region_offset = 0;
21166bc75619SDan Williams 	memdev->address = 0;
21176bc75619SDan Williams 	memdev->interleave_index = 0;
21186bc75619SDan Williams 	memdev->interleave_ways = 1;
2119d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21206bc75619SDan Williams 
21216bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
2122d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21236bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21246bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21256bc75619SDan Williams 	memdev->device_handle = handle[2];
21266bc75619SDan Williams 	memdev->physical_id = 2;
21276bc75619SDan Williams 	memdev->region_id = 0;
21286bc75619SDan Williams 	memdev->range_index = 8+1;
21296bc75619SDan Williams 	memdev->region_index = 2+1;
21306bc75619SDan Williams 	memdev->region_size = 0;
21316bc75619SDan Williams 	memdev->region_offset = 0;
21326bc75619SDan Williams 	memdev->address = 0;
21336bc75619SDan Williams 	memdev->interleave_index = 0;
21346bc75619SDan Williams 	memdev->interleave_ways = 1;
2135d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21366bc75619SDan Williams 
21376bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
2138d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21396bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21406bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21416bc75619SDan Williams 	memdev->device_handle = handle[3];
21426bc75619SDan Williams 	memdev->physical_id = 3;
21436bc75619SDan Williams 	memdev->region_id = 0;
21446bc75619SDan Williams 	memdev->range_index = 9+1;
21456bc75619SDan Williams 	memdev->region_index = 3+1;
21466bc75619SDan Williams 	memdev->region_size = 0;
21476bc75619SDan Williams 	memdev->region_offset = 0;
21486bc75619SDan Williams 	memdev->address = 0;
21496bc75619SDan Williams 	memdev->interleave_index = 0;
21506bc75619SDan Williams 	memdev->interleave_ways = 1;
2151ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2152d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21536bc75619SDan Williams 
21543b87356fSDan Williams 	/* dcr-descriptor0: blk */
21556bc75619SDan Williams 	dcr = nfit_buf + offset;
21566bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2157d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21586bc75619SDan Williams 	dcr->region_index = 0+1;
21595dc68e55SDan Williams 	dcr_common_init(dcr);
21606bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2161be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21626bc75619SDan Williams 	dcr->windows = 1;
21636bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21646bc75619SDan Williams 	dcr->command_offset = 0;
21656bc75619SDan Williams 	dcr->command_size = 8;
21666bc75619SDan Williams 	dcr->status_offset = 8;
21676bc75619SDan Williams 	dcr->status_size = 4;
2168d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21696bc75619SDan Williams 
21703b87356fSDan Williams 	/* dcr-descriptor1: blk */
2171d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21726bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2173d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21746bc75619SDan Williams 	dcr->region_index = 1+1;
21755dc68e55SDan Williams 	dcr_common_init(dcr);
21766bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2177be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21786bc75619SDan Williams 	dcr->windows = 1;
21796bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21806bc75619SDan Williams 	dcr->command_offset = 0;
21816bc75619SDan Williams 	dcr->command_size = 8;
21826bc75619SDan Williams 	dcr->status_offset = 8;
21836bc75619SDan Williams 	dcr->status_size = 4;
2184d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21856bc75619SDan Williams 
21863b87356fSDan Williams 	/* dcr-descriptor2: blk */
2187d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21886bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2189d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21906bc75619SDan Williams 	dcr->region_index = 2+1;
21915dc68e55SDan Williams 	dcr_common_init(dcr);
21926bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2193be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21946bc75619SDan Williams 	dcr->windows = 1;
21956bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21966bc75619SDan Williams 	dcr->command_offset = 0;
21976bc75619SDan Williams 	dcr->command_size = 8;
21986bc75619SDan Williams 	dcr->status_offset = 8;
21996bc75619SDan Williams 	dcr->status_size = 4;
2200d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22016bc75619SDan Williams 
22023b87356fSDan Williams 	/* dcr-descriptor3: blk */
2203d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22046bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2205d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
22066bc75619SDan Williams 	dcr->region_index = 3+1;
22075dc68e55SDan Williams 	dcr_common_init(dcr);
22086bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2209be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
22106bc75619SDan Williams 	dcr->windows = 1;
22116bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
22126bc75619SDan Williams 	dcr->command_offset = 0;
22136bc75619SDan Williams 	dcr->command_size = 8;
22146bc75619SDan Williams 	dcr->status_offset = 8;
22156bc75619SDan Williams 	dcr->status_size = 4;
2216d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22176bc75619SDan Williams 
22183b87356fSDan Williams 	/* dcr-descriptor0: pmem */
22193b87356fSDan Williams 	dcr = nfit_buf + offset;
22203b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22213b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22223b87356fSDan Williams 			window_size);
22233b87356fSDan Williams 	dcr->region_index = 4+1;
22245dc68e55SDan Williams 	dcr_common_init(dcr);
22253b87356fSDan Williams 	dcr->serial_number = ~handle[0];
22263b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22273b87356fSDan Williams 	dcr->windows = 0;
2228d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22293b87356fSDan Williams 
22303b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2231d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22323b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22333b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22343b87356fSDan Williams 			window_size);
22353b87356fSDan Williams 	dcr->region_index = 5+1;
22365dc68e55SDan Williams 	dcr_common_init(dcr);
22373b87356fSDan Williams 	dcr->serial_number = ~handle[1];
22383b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22393b87356fSDan Williams 	dcr->windows = 0;
2240d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22413b87356fSDan Williams 
22423b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2243d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22443b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22453b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22463b87356fSDan Williams 			window_size);
22473b87356fSDan Williams 	dcr->region_index = 6+1;
22485dc68e55SDan Williams 	dcr_common_init(dcr);
22493b87356fSDan Williams 	dcr->serial_number = ~handle[2];
22503b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22513b87356fSDan Williams 	dcr->windows = 0;
2252d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22533b87356fSDan Williams 
22543b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2255d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22563b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22573b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22583b87356fSDan Williams 			window_size);
22593b87356fSDan Williams 	dcr->region_index = 7+1;
22605dc68e55SDan Williams 	dcr_common_init(dcr);
22613b87356fSDan Williams 	dcr->serial_number = ~handle[3];
22623b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22633b87356fSDan Williams 	dcr->windows = 0;
2264d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22653b87356fSDan Williams 
22666bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
22676bc75619SDan Williams 	bdw = nfit_buf + offset;
22686bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2269d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22706bc75619SDan Williams 	bdw->region_index = 0+1;
22716bc75619SDan Williams 	bdw->windows = 1;
22726bc75619SDan Williams 	bdw->offset = 0;
22736bc75619SDan Williams 	bdw->size = BDW_SIZE;
22746bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22756bc75619SDan Williams 	bdw->start_address = 0;
2276d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22776bc75619SDan Williams 
22786bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2279d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22806bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2281d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22826bc75619SDan Williams 	bdw->region_index = 1+1;
22836bc75619SDan Williams 	bdw->windows = 1;
22846bc75619SDan Williams 	bdw->offset = 0;
22856bc75619SDan Williams 	bdw->size = BDW_SIZE;
22866bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22876bc75619SDan Williams 	bdw->start_address = 0;
2288d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22896bc75619SDan Williams 
22906bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2291d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22926bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2293d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22946bc75619SDan Williams 	bdw->region_index = 2+1;
22956bc75619SDan Williams 	bdw->windows = 1;
22966bc75619SDan Williams 	bdw->offset = 0;
22976bc75619SDan Williams 	bdw->size = BDW_SIZE;
22986bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22996bc75619SDan Williams 	bdw->start_address = 0;
2300d7d8464dSRoss Zwisler 	offset += bdw->header.length;
23016bc75619SDan Williams 
23026bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2303d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
23046bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2305d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
23066bc75619SDan Williams 	bdw->region_index = 3+1;
23076bc75619SDan Williams 	bdw->windows = 1;
23086bc75619SDan Williams 	bdw->offset = 0;
23096bc75619SDan Williams 	bdw->size = BDW_SIZE;
23106bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
23116bc75619SDan Williams 	bdw->start_address = 0;
2312d7d8464dSRoss Zwisler 	offset += bdw->header.length;
23136bc75619SDan Williams 
23149d27a87eSDan Williams 	/* flush0 (dimm0) */
23159d27a87eSDan Williams 	flush = nfit_buf + offset;
23169d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
231785d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23189d27a87eSDan Williams 	flush->device_handle = handle[0];
231985d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
232085d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
232185d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2322d7d8464dSRoss Zwisler 	offset += flush->header.length;
23239d27a87eSDan Williams 
23249d27a87eSDan Williams 	/* flush1 (dimm1) */
2325d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23269d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
232785d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23289d27a87eSDan Williams 	flush->device_handle = handle[1];
232985d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
233085d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
233185d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2332d7d8464dSRoss Zwisler 	offset += flush->header.length;
23339d27a87eSDan Williams 
23349d27a87eSDan Williams 	/* flush2 (dimm2) */
2335d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23369d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
233785d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23389d27a87eSDan Williams 	flush->device_handle = handle[2];
233985d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
234085d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
234185d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2342d7d8464dSRoss Zwisler 	offset += flush->header.length;
23439d27a87eSDan Williams 
23449d27a87eSDan Williams 	/* flush3 (dimm3) */
2345d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23469d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
234785d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23489d27a87eSDan Williams 	flush->device_handle = handle[3];
234985d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
235085d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
235185d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2352d7d8464dSRoss Zwisler 	offset += flush->header.length;
23539d27a87eSDan Williams 
2354f81e1d35SDave Jiang 	/* platform capabilities */
2355d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2356f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2357f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2358f81e1d35SDave Jiang 	pcap->highest_capability = 1;
23591273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2360d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2361f81e1d35SDave Jiang 
236220985164SVishal Verma 	if (t->setup_hotplug) {
23633b87356fSDan Williams 		/* dcr-descriptor4: blk */
236420985164SVishal Verma 		dcr = nfit_buf + offset;
236520985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2366d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
23673b87356fSDan Williams 		dcr->region_index = 8+1;
23685dc68e55SDan Williams 		dcr_common_init(dcr);
236920985164SVishal Verma 		dcr->serial_number = ~handle[4];
2370be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
237120985164SVishal Verma 		dcr->windows = 1;
237220985164SVishal Verma 		dcr->window_size = DCR_SIZE;
237320985164SVishal Verma 		dcr->command_offset = 0;
237420985164SVishal Verma 		dcr->command_size = 8;
237520985164SVishal Verma 		dcr->status_offset = 8;
237620985164SVishal Verma 		dcr->status_size = 4;
2377d7d8464dSRoss Zwisler 		offset += dcr->header.length;
237820985164SVishal Verma 
23793b87356fSDan Williams 		/* dcr-descriptor4: pmem */
23803b87356fSDan Williams 		dcr = nfit_buf + offset;
23813b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
23823b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
23833b87356fSDan Williams 				window_size);
23843b87356fSDan Williams 		dcr->region_index = 9+1;
23855dc68e55SDan Williams 		dcr_common_init(dcr);
23863b87356fSDan Williams 		dcr->serial_number = ~handle[4];
23873b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
23883b87356fSDan Williams 		dcr->windows = 0;
2389d7d8464dSRoss Zwisler 		offset += dcr->header.length;
23903b87356fSDan Williams 
239120985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
239220985164SVishal Verma 		bdw = nfit_buf + offset;
239320985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2394d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
23953b87356fSDan Williams 		bdw->region_index = 8+1;
239620985164SVishal Verma 		bdw->windows = 1;
239720985164SVishal Verma 		bdw->offset = 0;
239820985164SVishal Verma 		bdw->size = BDW_SIZE;
239920985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
240020985164SVishal Verma 		bdw->start_address = 0;
2401d7d8464dSRoss Zwisler 		offset += bdw->header.length;
240220985164SVishal Verma 
240320985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
240420985164SVishal Verma 		spa = nfit_buf + offset;
240520985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
240620985164SVishal Verma 		spa->header.length = sizeof(*spa);
240720985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
240820985164SVishal Verma 		spa->range_index = 10+1;
240920985164SVishal Verma 		spa->address = t->dcr_dma[4];
241020985164SVishal Verma 		spa->length = DCR_SIZE;
2411d7d8464dSRoss Zwisler 		offset += spa->header.length;
241220985164SVishal Verma 
241320985164SVishal Verma 		/*
241420985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
241520985164SVishal Verma 		 * does not actually alias the related block-data-window
241620985164SVishal Verma 		 * regions)
241720985164SVishal Verma 		 */
2418d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
241920985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
242020985164SVishal Verma 		spa->header.length = sizeof(*spa);
242120985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
242220985164SVishal Verma 		spa->range_index = 11+1;
242320985164SVishal Verma 		spa->address = t->spa_set_dma[2];
242420985164SVishal Verma 		spa->length = SPA0_SIZE;
2425d7d8464dSRoss Zwisler 		offset += spa->header.length;
242620985164SVishal Verma 
242720985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2428d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
242920985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
243020985164SVishal Verma 		spa->header.length = sizeof(*spa);
243120985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
243220985164SVishal Verma 		spa->range_index = 12+1;
243320985164SVishal Verma 		spa->address = t->dimm_dma[4];
243420985164SVishal Verma 		spa->length = DIMM_SIZE;
2435d7d8464dSRoss Zwisler 		offset += spa->header.length;
243620985164SVishal Verma 
243720985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
243820985164SVishal Verma 		memdev = nfit_buf + offset;
243920985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
244020985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
244120985164SVishal Verma 		memdev->device_handle = handle[4];
244220985164SVishal Verma 		memdev->physical_id = 4;
244320985164SVishal Verma 		memdev->region_id = 0;
244420985164SVishal Verma 		memdev->range_index = 10+1;
24453b87356fSDan Williams 		memdev->region_index = 8+1;
244620985164SVishal Verma 		memdev->region_size = 0;
244720985164SVishal Verma 		memdev->region_offset = 0;
244820985164SVishal Verma 		memdev->address = 0;
244920985164SVishal Verma 		memdev->interleave_index = 0;
245020985164SVishal Verma 		memdev->interleave_ways = 1;
2451d7d8464dSRoss Zwisler 		offset += memdev->header.length;
245220985164SVishal Verma 
2453d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2454d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
245520985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
245620985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
245720985164SVishal Verma 		memdev->device_handle = handle[4];
245820985164SVishal Verma 		memdev->physical_id = 4;
245920985164SVishal Verma 		memdev->region_id = 0;
246020985164SVishal Verma 		memdev->range_index = 11+1;
24613b87356fSDan Williams 		memdev->region_index = 9+1;
246220985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2463df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
246420985164SVishal Verma 		memdev->address = 0;
246520985164SVishal Verma 		memdev->interleave_index = 0;
246620985164SVishal Verma 		memdev->interleave_ways = 1;
2467ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2468d7d8464dSRoss Zwisler 		offset += memdev->header.length;
246920985164SVishal Verma 
24703b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2471d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
247220985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
247320985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
247420985164SVishal Verma 		memdev->device_handle = handle[4];
247520985164SVishal Verma 		memdev->physical_id = 4;
247620985164SVishal Verma 		memdev->region_id = 0;
247720985164SVishal Verma 		memdev->range_index = 12+1;
24783b87356fSDan Williams 		memdev->region_index = 8+1;
247920985164SVishal Verma 		memdev->region_size = 0;
248020985164SVishal Verma 		memdev->region_offset = 0;
248120985164SVishal Verma 		memdev->address = 0;
248220985164SVishal Verma 		memdev->interleave_index = 0;
248320985164SVishal Verma 		memdev->interleave_ways = 1;
2484d7d8464dSRoss Zwisler 		offset += memdev->header.length;
248520985164SVishal Verma 
248620985164SVishal Verma 		/* flush3 (dimm4) */
248720985164SVishal Verma 		flush = nfit_buf + offset;
248820985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
248985d3fa02SDan Williams 		flush->header.length = flush_hint_size;
249020985164SVishal Verma 		flush->device_handle = handle[4];
249185d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
249285d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
249385d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
249485d3fa02SDan Williams 				+ i * sizeof(u64);
2495d7d8464dSRoss Zwisler 		offset += flush->header.length;
24969741a559SRoss Zwisler 
24979741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
24989741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
249920985164SVishal Verma 	}
250020985164SVishal Verma 
25011526f9e2SRoss Zwisler 	t->nfit_filled = offset;
25021526f9e2SRoss Zwisler 
25039fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
25049fb1a190SDave Jiang 			SPA0_SIZE);
2505f471f1a7SDan Williams 
25066bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2507e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2508e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2509e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2510ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2511ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2512ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
25134cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2514e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2515e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2516e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2517e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
251810246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
251910246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
25209fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
25219fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
25229fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2523bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2524bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2525bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2526bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2527bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2528674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
25293c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
25303c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25313c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
25323c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
25333c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25343c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
25353c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
25363c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2537926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2538926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2539ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
2540ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2541ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
2542ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25436bc75619SDan Williams }
25446bc75619SDan Williams 
25456bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
25466bc75619SDan Williams {
25476b577c9dSLinda Knippers 	size_t offset;
25486bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
25496bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
25506bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
25516bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2552d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25536bc75619SDan Williams 
25546b577c9dSLinda Knippers 	offset = 0;
25556bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
25566bc75619SDan Williams 	spa = nfit_buf + offset;
25576bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25586bc75619SDan Williams 	spa->header.length = sizeof(*spa);
25596bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
25606bc75619SDan Williams 	spa->range_index = 0+1;
25616bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
25626bc75619SDan Williams 	spa->length = SPA2_SIZE;
2563d7d8464dSRoss Zwisler 	offset += spa->header.length;
25646bc75619SDan Williams 
25657bfe97c7SDan Williams 	/* virtual cd region */
2566d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
25677bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25687bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
25697bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
25707bfe97c7SDan Williams 	spa->range_index = 0;
25717bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
25727bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2573d7d8464dSRoss Zwisler 	offset += spa->header.length;
25747bfe97c7SDan Williams 
25756bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
25766bc75619SDan Williams 	memdev = nfit_buf + offset;
25776bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
25786bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2579dafb1048SDan Williams 	memdev->device_handle = handle[5];
25806bc75619SDan Williams 	memdev->physical_id = 0;
25816bc75619SDan Williams 	memdev->region_id = 0;
25826bc75619SDan Williams 	memdev->range_index = 0+1;
25836bc75619SDan Williams 	memdev->region_index = 0+1;
25846bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
25856bc75619SDan Williams 	memdev->region_offset = 0;
25866bc75619SDan Williams 	memdev->address = 0;
25876bc75619SDan Williams 	memdev->interleave_index = 0;
25886bc75619SDan Williams 	memdev->interleave_ways = 1;
258958138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
259058138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2591f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2592d7d8464dSRoss Zwisler 	offset += memdev->header.length;
25936bc75619SDan Williams 
25946bc75619SDan Williams 	/* dcr-descriptor0 */
25956bc75619SDan Williams 	dcr = nfit_buf + offset;
25966bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
25973b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
25983b87356fSDan Williams 			window_size);
25996bc75619SDan Williams 	dcr->region_index = 0+1;
26005dc68e55SDan Williams 	dcr_common_init(dcr);
2601dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2602be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
26036bc75619SDan Williams 	dcr->windows = 0;
2604ac40b675SDan Williams 	offset += dcr->header.length;
2605d7d8464dSRoss Zwisler 
2606ac40b675SDan Williams 	memdev = nfit_buf + offset;
2607ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2608ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2609ac40b675SDan Williams 	memdev->device_handle = handle[6];
2610ac40b675SDan Williams 	memdev->physical_id = 0;
2611ac40b675SDan Williams 	memdev->region_id = 0;
2612ac40b675SDan Williams 	memdev->range_index = 0;
2613ac40b675SDan Williams 	memdev->region_index = 0+2;
2614ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2615ac40b675SDan Williams 	memdev->region_offset = 0;
2616ac40b675SDan Williams 	memdev->address = 0;
2617ac40b675SDan Williams 	memdev->interleave_index = 0;
2618ac40b675SDan Williams 	memdev->interleave_ways = 1;
2619ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2620d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2621ac40b675SDan Williams 
2622ac40b675SDan Williams 	/* dcr-descriptor1 */
2623ac40b675SDan Williams 	dcr = nfit_buf + offset;
2624ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2625ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2626ac40b675SDan Williams 			window_size);
2627ac40b675SDan Williams 	dcr->region_index = 0+2;
2628ac40b675SDan Williams 	dcr_common_init(dcr);
2629ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2630ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2631ac40b675SDan Williams 	dcr->windows = 0;
2632d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2633ac40b675SDan Williams 
26349741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
26359741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
26369741a559SRoss Zwisler 
26371526f9e2SRoss Zwisler 	t->nfit_filled = offset;
26381526f9e2SRoss Zwisler 
26399fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
26409fb1a190SDave Jiang 			SPA2_SIZE);
2641f471f1a7SDan Williams 
2642d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2643e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2644e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2645e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2646e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2647674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
26489484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
26499484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26509484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26516bc75619SDan Williams }
26526bc75619SDan Williams 
26536bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
26546bc75619SDan Williams 		void *iobuf, u64 len, int rw)
26556bc75619SDan Williams {
26566bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
26576bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
26586bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
26596bc75619SDan Williams 	unsigned int lane;
26606bc75619SDan Williams 
26616bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
26626bc75619SDan Williams 	if (rw)
266367a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
266467a3e8feSRoss Zwisler 	else {
266567a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
266667a3e8feSRoss Zwisler 
26675deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
26685deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
266967a3e8feSRoss Zwisler 	}
26706bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
26716bc75619SDan Williams 
26726bc75619SDan Williams 	return 0;
26736bc75619SDan Williams }
26746bc75619SDan Williams 
2675a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2676a7de92daSDan Williams 
2677a7de92daSDan Williams union acpi_object *result;
2678a7de92daSDan Williams 
2679a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
268094116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2681a7de92daSDan Williams {
2682a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2683a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2684a7de92daSDan Williams 
2685a7de92daSDan Williams 	return result;
2686a7de92daSDan Williams }
2687a7de92daSDan Williams 
2688a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2689a7de92daSDan Williams {
2690a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2691a7de92daSDan Williams 	if (!result)
2692a7de92daSDan Williams 		return -ENOMEM;
2693a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2694a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2695a7de92daSDan Williams 	result->buffer.length = size;
2696a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2697a7de92daSDan Williams 	memset(buf, 0, size);
2698a7de92daSDan Williams 	return 0;
2699a7de92daSDan Williams }
2700a7de92daSDan Williams 
2701a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2702a7de92daSDan Williams {
2703a7de92daSDan Williams 	int rc, cmd_rc;
2704a7de92daSDan Williams 	struct nvdimm *nvdimm;
2705a7de92daSDan Williams 	struct acpi_device *adev;
2706a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2707a7de92daSDan Williams 	struct nd_ars_record *record;
2708a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2709a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2710a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2711a7de92daSDan Williams 	union {
2712a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2713fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2714a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2715a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2716a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2717a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2718a7de92daSDan Williams 	} cmds;
2719a7de92daSDan Williams 
2720a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2721a7de92daSDan Williams 	if (!adev)
2722a7de92daSDan Williams 		return -ENOMEM;
2723a7de92daSDan Williams 	*adev = (struct acpi_device) {
2724a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2725a7de92daSDan Williams 		.dev = {
2726a7de92daSDan Williams 			.init_name = "test-adev",
2727a7de92daSDan Williams 		},
2728a7de92daSDan Williams 	};
2729a7de92daSDan Williams 
2730a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2731a7de92daSDan Williams 	if (!acpi_desc)
2732a7de92daSDan Williams 		return -ENOMEM;
2733a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2734a7de92daSDan Williams 		.nd_desc = {
2735a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2736a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2737a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
273810246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
273910246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2740a7de92daSDan Williams 			.module = THIS_MODULE,
2741a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2742a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
27439fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
27449fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
27459fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
27469fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2747a7de92daSDan Williams 		},
2748a7de92daSDan Williams 		.dev = &adev->dev,
2749a7de92daSDan Williams 	};
2750a7de92daSDan Williams 
2751a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2752a7de92daSDan Williams 	if (!nfit_mem)
2753a7de92daSDan Williams 		return -ENOMEM;
2754a7de92daSDan Williams 
2755a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2756a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2757a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2758a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2759a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2760a7de92daSDan Williams 		.adev = adev,
2761a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2762a7de92daSDan Williams 		.dsm_mask = mask,
2763a7de92daSDan Williams 	};
2764a7de92daSDan Williams 
2765a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2766a7de92daSDan Williams 	if (!nvdimm)
2767a7de92daSDan Williams 		return -ENOMEM;
2768a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2769a7de92daSDan Williams 		.provider_data = nfit_mem,
2770a7de92daSDan Williams 		.cmd_mask = mask,
2771a7de92daSDan Williams 		.dev = {
2772a7de92daSDan Williams 			.init_name = "test-dimm",
2773a7de92daSDan Williams 		},
2774a7de92daSDan Williams 	};
2775a7de92daSDan Williams 
2776a7de92daSDan Williams 
2777a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2778a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2779a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2780a7de92daSDan Williams 		.status = 0,
2781a7de92daSDan Williams 		.config_size = SZ_128K,
2782a7de92daSDan Williams 		.max_xfer = SZ_4K,
2783a7de92daSDan Williams 	};
2784a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2785a7de92daSDan Williams 	if (rc)
2786a7de92daSDan Williams 		return rc;
2787a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2788a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2789a7de92daSDan Williams 
2790a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2791a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2792a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2793a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2794a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2795a7de92daSDan Williams 		return -EIO;
2796a7de92daSDan Williams 	}
2797a7de92daSDan Williams 
2798a7de92daSDan Williams 
2799a7de92daSDan Williams 	/* test ars_status with zero output */
2800a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2801a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2802a7de92daSDan Williams 		.out_length = 0,
2803a7de92daSDan Williams 	};
2804a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2805a7de92daSDan Williams 	if (rc)
2806a7de92daSDan Williams 		return rc;
2807a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2808a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2809a7de92daSDan Williams 
2810a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2811a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2812a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2813a7de92daSDan Williams 		return -EIO;
2814a7de92daSDan Williams 	}
2815a7de92daSDan Williams 
2816a7de92daSDan Williams 
2817a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2818a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2819a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2820a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2821a7de92daSDan Williams 	};
2822a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2823a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2824a7de92daSDan Williams 	if (rc)
2825a7de92daSDan Williams 		return rc;
2826a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2827a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2828a7de92daSDan Williams 
2829a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2830a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2831a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2832a7de92daSDan Williams 		return -EIO;
2833a7de92daSDan Williams 	}
2834a7de92daSDan Williams 
2835a7de92daSDan Williams 
2836a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2837a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2838a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2839a7de92daSDan Williams 		.out_length = cmd_size - 4,
2840a7de92daSDan Williams 	};
2841a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2842a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2843a7de92daSDan Williams 		.length = test_val,
2844a7de92daSDan Williams 	};
2845a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2846a7de92daSDan Williams 	if (rc)
2847a7de92daSDan Williams 		return rc;
2848a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2849a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2850a7de92daSDan Williams 
2851a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2852a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2853a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2854a7de92daSDan Williams 		return -EIO;
2855a7de92daSDan Williams 	}
2856a7de92daSDan Williams 
2857a7de92daSDan Williams 
2858a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2859a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2860a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2861a7de92daSDan Williams 		.out_length = cmd_size,
2862a7de92daSDan Williams 	};
2863a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2864a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2865a7de92daSDan Williams 		.length = test_val,
2866a7de92daSDan Williams 	};
2867a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2868a7de92daSDan Williams 	if (rc)
2869a7de92daSDan Williams 		return rc;
2870a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2871a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2872a7de92daSDan Williams 
2873a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2874a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2875a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2876a7de92daSDan Williams 		return -EIO;
2877a7de92daSDan Williams 	}
2878a7de92daSDan Williams 
2879a7de92daSDan Williams 
2880a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2881a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2882a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2883a7de92daSDan Williams 		.status = 1 << 16,
2884a7de92daSDan Williams 	};
2885a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2886a7de92daSDan Williams 	if (rc)
2887a7de92daSDan Williams 		return rc;
2888a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2889a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2890a7de92daSDan Williams 
2891a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2892a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2893a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2894a7de92daSDan Williams 		return -EIO;
2895a7de92daSDan Williams 	}
2896a7de92daSDan Williams 
2897fb2a1748SDan Williams 	/* test clear error */
2898fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2899fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2900fb2a1748SDan Williams 		.length = 512,
2901fb2a1748SDan Williams 		.cleared = 512,
2902fb2a1748SDan Williams 	};
2903fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2904fb2a1748SDan Williams 	if (rc)
2905fb2a1748SDan Williams 		return rc;
2906fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2907fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2908fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2909fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2910fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2911fb2a1748SDan Williams 		return -EIO;
2912fb2a1748SDan Williams 	}
2913fb2a1748SDan Williams 
2914a7de92daSDan Williams 	return 0;
2915a7de92daSDan Williams }
2916a7de92daSDan Williams 
29176bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
29186bc75619SDan Williams {
29196bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
29206bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
29216bc75619SDan Williams 	struct device *dev = &pdev->dev;
29226bc75619SDan Williams 	struct nfit_test *nfit_test;
2923231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2924c14a868aSDan Williams 	union acpi_object *obj;
29256bc75619SDan Williams 	int rc;
29266bc75619SDan Williams 
2927a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2928a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2929a7de92daSDan Williams 		if (rc)
2930a7de92daSDan Williams 			return rc;
2931a7de92daSDan Williams 	}
2932a7de92daSDan Williams 
29336bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
29346bc75619SDan Williams 
29356bc75619SDan Williams 	/* common alloc */
29366bc75619SDan Williams 	if (nfit_test->num_dcr) {
29376bc75619SDan Williams 		int num = nfit_test->num_dcr;
29386bc75619SDan Williams 
29396bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
29406bc75619SDan Williams 				GFP_KERNEL);
29416bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29426bc75619SDan Williams 				GFP_KERNEL);
29439d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
29449d27a87eSDan Williams 				GFP_KERNEL);
29459d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29469d27a87eSDan Williams 				GFP_KERNEL);
29476bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
29486bc75619SDan Williams 				GFP_KERNEL);
29496bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
29506bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29516bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
29526bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
29536bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
29546bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2955ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2956ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2957ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2958ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2959ed07c433SDan Williams 				GFP_KERNEL);
2960bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2961bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
29626bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
29636bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
29649d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2965bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2966bfbaa952SDave Jiang 				&& nfit_test->fw)
29676bc75619SDan Williams 			/* pass */;
29686bc75619SDan Williams 		else
29696bc75619SDan Williams 			return -ENOMEM;
29706bc75619SDan Williams 	}
29716bc75619SDan Williams 
29726bc75619SDan Williams 	if (nfit_test->num_pm) {
29736bc75619SDan Williams 		int num = nfit_test->num_pm;
29746bc75619SDan Williams 
29756bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
29766bc75619SDan Williams 				GFP_KERNEL);
29776bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
29786bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29796bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
29806bc75619SDan Williams 			/* pass */;
29816bc75619SDan Williams 		else
29826bc75619SDan Williams 			return -ENOMEM;
29836bc75619SDan Williams 	}
29846bc75619SDan Williams 
29856bc75619SDan Williams 	/* per-nfit specific alloc */
29866bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
29876bc75619SDan Williams 		return -ENOMEM;
29886bc75619SDan Williams 
29896bc75619SDan Williams 	nfit_test->setup(nfit_test);
29906bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2991a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
29926bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
29936bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2994a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2995bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2996a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
29976bc75619SDan Williams 
2998e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
29991526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
300058cd71b4SDan Williams 	if (rc)
300120985164SVishal Verma 		return rc;
300220985164SVishal Verma 
3003fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
3004fbabd829SDan Williams 	if (rc)
3005fbabd829SDan Williams 		return rc;
3006fbabd829SDan Williams 
300720985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
300820985164SVishal Verma 		return 0;
300920985164SVishal Verma 
301020985164SVishal Verma 	nfit_test->setup_hotplug = 1;
301120985164SVishal Verma 	nfit_test->setup(nfit_test);
301220985164SVishal Verma 
3013c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3014c14a868aSDan Williams 	if (!obj)
3015c14a868aSDan Williams 		return -ENOMEM;
3016c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
3017c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
3018c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
3019c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
3020c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
3021231bf117SDan Williams 
3022231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
3023231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
3024231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
3025231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
3026231bf117SDan Williams 		int i;
3027231bf117SDan Williams 
3028af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
3029231bf117SDan Williams 			if (nfit_handle == handle[i])
3030231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
3031231bf117SDan Williams 						nfit_mem);
3032231bf117SDan Williams 	}
3033231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
30346bc75619SDan Williams 
30356bc75619SDan Williams 	return 0;
30366bc75619SDan Williams }
30376bc75619SDan Williams 
30386bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
30396bc75619SDan Williams {
30406bc75619SDan Williams 	return 0;
30416bc75619SDan Williams }
30426bc75619SDan Williams 
30436bc75619SDan Williams static void nfit_test_release(struct device *dev)
30446bc75619SDan Williams {
30456bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
30466bc75619SDan Williams 
30476bc75619SDan Williams 	kfree(nfit_test);
30486bc75619SDan Williams }
30496bc75619SDan Williams 
30506bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
30516bc75619SDan Williams 	{ KBUILD_MODNAME },
30526bc75619SDan Williams 	{ },
30536bc75619SDan Williams };
30546bc75619SDan Williams 
30556bc75619SDan Williams static struct platform_driver nfit_test_driver = {
30566bc75619SDan Williams 	.probe = nfit_test_probe,
30576bc75619SDan Williams 	.remove = nfit_test_remove,
30586bc75619SDan Williams 	.driver = {
30596bc75619SDan Williams 		.name = KBUILD_MODNAME,
30606bc75619SDan Williams 	},
30616bc75619SDan Williams 	.id_table = nfit_test_id,
30626bc75619SDan Williams };
30636bc75619SDan Williams 
30645d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
30655d8beee2SDan Williams 
30665d8beee2SDan Williams enum INJECT {
30675d8beee2SDan Williams 	INJECT_NONE,
30685d8beee2SDan Williams 	INJECT_SRC,
30695d8beee2SDan Williams 	INJECT_DST,
30705d8beee2SDan Williams };
30715d8beee2SDan Williams 
30725d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
30735d8beee2SDan Williams {
30745d8beee2SDan Williams 	size_t i;
30755d8beee2SDan Williams 
30765d8beee2SDan Williams 	memset(dst, 0xff, size);
30775d8beee2SDan Williams 	for (i = 0; i < size; i++)
30785d8beee2SDan Williams 		src[i] = (char) i;
30795d8beee2SDan Williams }
30805d8beee2SDan Williams 
30815d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
30825d8beee2SDan Williams 		size_t size, unsigned long rem)
30835d8beee2SDan Williams {
30845d8beee2SDan Williams 	size_t i;
30855d8beee2SDan Williams 
30865d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
30875d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
30885d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
30895d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
30905d8beee2SDan Williams 					(unsigned char) i);
30915d8beee2SDan Williams 			return false;
30925d8beee2SDan Williams 		}
30935d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
30945d8beee2SDan Williams 		if (dst[i] != 0xffU) {
30955d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
30965d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
30975d8beee2SDan Williams 			return false;
30985d8beee2SDan Williams 		}
30995d8beee2SDan Williams 	return true;
31005d8beee2SDan Williams }
31015d8beee2SDan Williams 
31025d8beee2SDan Williams void mcsafe_test(void)
31035d8beee2SDan Williams {
31045d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
31055d8beee2SDan Williams 	enum INJECT inj;
31065d8beee2SDan Williams 
31075d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
31085d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
31095d8beee2SDan Williams 	} else {
31105d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
31115d8beee2SDan Williams 		return;
31125d8beee2SDan Williams 	}
31135d8beee2SDan Williams 
31145d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
31155d8beee2SDan Williams 		int i;
31165d8beee2SDan Williams 
31175d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
31185d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
31195d8beee2SDan Williams 			unsigned long expect, rem;
31205d8beee2SDan Williams 			void *src, *dst;
31215d8beee2SDan Williams 			bool valid;
31225d8beee2SDan Williams 
31235d8beee2SDan Williams 			switch (inj) {
31245d8beee2SDan Williams 			case INJECT_NONE:
31255d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31265d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31275d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31285d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31295d8beee2SDan Williams 				expect = 0;
31305d8beee2SDan Williams 				break;
31315d8beee2SDan Williams 			case INJECT_SRC:
31325d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
31335d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31345d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31355d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31365d8beee2SDan Williams 				expect = 512 - i;
31375d8beee2SDan Williams 				break;
31385d8beee2SDan Williams 			case INJECT_DST:
31395d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31405d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
31415d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
31425d8beee2SDan Williams 				src = &mcsafe_buf[1024];
31435d8beee2SDan Williams 				expect = 512 - i;
31445d8beee2SDan Williams 				break;
31455d8beee2SDan Williams 			}
31465d8beee2SDan Williams 
31475d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
31485d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
31495d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
31505d8beee2SDan Williams 			if (rem == expect && valid)
31515d8beee2SDan Williams 				continue;
31525d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
31535d8beee2SDan Williams 					__func__,
31545d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
31555d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
31565d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
31575d8beee2SDan Williams 					expect);
31585d8beee2SDan Williams 		}
31595d8beee2SDan Williams 	}
31605d8beee2SDan Williams 
31615d8beee2SDan Williams 	mcsafe_inject_src(NULL);
31625d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
31635d8beee2SDan Williams }
31645d8beee2SDan Williams 
31656bc75619SDan Williams static __init int nfit_test_init(void)
31666bc75619SDan Williams {
31676bc75619SDan Williams 	int rc, i;
31686bc75619SDan Williams 
31690fb5c8dfSDan Williams 	pmem_test();
31700fb5c8dfSDan Williams 	libnvdimm_test();
31710fb5c8dfSDan Williams 	acpi_nfit_test();
31720fb5c8dfSDan Williams 	device_dax_test();
31735d8beee2SDan Williams 	mcsafe_test();
31740fb5c8dfSDan Williams 
3175a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3176231bf117SDan Williams 
31779fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
31789fb1a190SDave Jiang 	if (!nfit_wq)
31799fb1a190SDave Jiang 		return -ENOMEM;
31809fb1a190SDave Jiang 
3181a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
3182a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
3183a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
3184a7de92daSDan Williams 		goto err_register;
3185a7de92daSDan Williams 	}
31866bc75619SDan Williams 
3187e3f5df76SDan Williams 	nfit_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
3188e3f5df76SDan Williams 	if (!nfit_pool) {
3189e3f5df76SDan Williams 		rc = -ENOMEM;
3190e3f5df76SDan Williams 		goto err_register;
3191e3f5df76SDan Williams 	}
3192e3f5df76SDan Williams 
3193e3f5df76SDan Williams 	if (gen_pool_add(nfit_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
3194e3f5df76SDan Williams 		rc = -ENOMEM;
3195e3f5df76SDan Williams 		goto err_register;
3196e3f5df76SDan Williams 	}
3197e3f5df76SDan Williams 
31986bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
31996bc75619SDan Williams 		struct nfit_test *nfit_test;
32006bc75619SDan Williams 		struct platform_device *pdev;
32016bc75619SDan Williams 
32026bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
32036bc75619SDan Williams 		if (!nfit_test) {
32046bc75619SDan Williams 			rc = -ENOMEM;
32056bc75619SDan Williams 			goto err_register;
32066bc75619SDan Williams 		}
32076bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
32089fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
32096bc75619SDan Williams 		switch (i) {
32106bc75619SDan Williams 		case 0:
32116bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3212dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
32136bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
32146bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
32156bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
32166bc75619SDan Williams 			break;
32176bc75619SDan Williams 		case 1:
3218a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3219dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3220ac40b675SDan Williams 			nfit_test->num_dcr = 2;
32216bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
32226bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
32236bc75619SDan Williams 			break;
32246bc75619SDan Williams 		default:
32256bc75619SDan Williams 			rc = -EINVAL;
32266bc75619SDan Williams 			goto err_register;
32276bc75619SDan Williams 		}
32286bc75619SDan Williams 		pdev = &nfit_test->pdev;
32296bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
32306bc75619SDan Williams 		pdev->id = i;
32316bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
32326bc75619SDan Williams 		rc = platform_device_register(pdev);
32336bc75619SDan Williams 		if (rc) {
32346bc75619SDan Williams 			put_device(&pdev->dev);
32356bc75619SDan Williams 			goto err_register;
32366bc75619SDan Williams 		}
32378b06b884SDan Williams 		get_device(&pdev->dev);
32386bc75619SDan Williams 
32396bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
32406bc75619SDan Williams 		if (rc)
32416bc75619SDan Williams 			goto err_register;
32426bc75619SDan Williams 
32436bc75619SDan Williams 		instances[i] = nfit_test;
32449fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
32456bc75619SDan Williams 	}
32466bc75619SDan Williams 
32476bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
32486bc75619SDan Williams 	if (rc)
32496bc75619SDan Williams 		goto err_register;
32506bc75619SDan Williams 	return 0;
32516bc75619SDan Williams 
32526bc75619SDan Williams  err_register:
3253e3f5df76SDan Williams 	if (nfit_pool)
3254e3f5df76SDan Williams 		gen_pool_destroy(nfit_pool);
3255e3f5df76SDan Williams 
32569fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32576bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32586bc75619SDan Williams 		if (instances[i])
32596bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
32606bc75619SDan Williams 	nfit_test_teardown();
32618b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32628b06b884SDan Williams 		if (instances[i])
32638b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
32648b06b884SDan Williams 
32656bc75619SDan Williams 	return rc;
32666bc75619SDan Williams }
32676bc75619SDan Williams 
32686bc75619SDan Williams static __exit void nfit_test_exit(void)
32696bc75619SDan Williams {
32706bc75619SDan Williams 	int i;
32716bc75619SDan Williams 
32729fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
32739fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32746bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32756bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
32768b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
32776bc75619SDan Williams 	nfit_test_teardown();
32788b06b884SDan Williams 
3279e3f5df76SDan Williams 	gen_pool_destroy(nfit_pool);
3280e3f5df76SDan Williams 
32818b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32828b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3283231bf117SDan Williams 	class_destroy(nfit_test_dimm);
32846bc75619SDan Williams }
32856bc75619SDan Williams 
32866bc75619SDan Williams module_init(nfit_test_init);
32876bc75619SDan Williams module_exit(nfit_test_exit);
32886bc75619SDan Williams MODULE_LICENSE("GPL v2");
32896bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3290