xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 5b497af42fab12cadc0e29bcb7052cf9963603f5)
1*5b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26bc75619SDan Williams /*
36bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
46bc75619SDan Williams  */
56bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
66bc75619SDan Williams #include <linux/platform_device.h>
76bc75619SDan Williams #include <linux/dma-mapping.h>
8d8d378faSDan Williams #include <linux/workqueue.h>
96bc75619SDan Williams #include <linux/libnvdimm.h>
10e3f5df76SDan Williams #include <linux/genalloc.h>
116bc75619SDan Williams #include <linux/vmalloc.h>
126bc75619SDan Williams #include <linux/device.h>
136bc75619SDan Williams #include <linux/module.h>
1420985164SVishal Verma #include <linux/mutex.h>
156bc75619SDan Williams #include <linux/ndctl.h>
166bc75619SDan Williams #include <linux/sizes.h>
1720985164SVishal Verma #include <linux/list.h>
186bc75619SDan Williams #include <linux/slab.h>
19a7de92daSDan Williams #include <nd-core.h>
200ead1118SDan Williams #include <intel.h>
216bc75619SDan Williams #include <nfit.h>
226bc75619SDan Williams #include <nd.h>
236bc75619SDan Williams #include "nfit_test.h"
240fb5c8dfSDan Williams #include "../watermark.h"
256bc75619SDan Williams 
265d8beee2SDan Williams #include <asm/mcsafe_test.h>
275d8beee2SDan Williams 
286bc75619SDan Williams /*
296bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
306bc75619SDan Williams  *
316bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
326bc75619SDan Williams  *
336bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
346bc75619SDan Williams  *           +----------+--------------+----------+---------+
356bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
366bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
376bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
386bc75619SDan Williams  *    |      +----------+--------------v----------v         v
396bc75619SDan Williams  * +--+---+                            |                    |
406bc75619SDan Williams  * | cpu0 |                                    region1
416bc75619SDan Williams  * +--+---+                            |                    |
426bc75619SDan Williams  *    |      +-------------------------^----------^         ^
436bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
446bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
456bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
466bc75619SDan Williams  *           +-------------------------+----------+-+-------+
476bc75619SDan Williams  *
4820985164SVishal Verma  * +--+---+
4920985164SVishal Verma  * | cpu1 |
5020985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5120985164SVishal Verma  *    |      +----------------------------------------------+
5220985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5320985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5420985164SVishal Verma  * +------+
5520985164SVishal Verma  *
5620985164SVishal Verma  *
576bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
586bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
596bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
606bc75619SDan Williams  *
616bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
626bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
636bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
646bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
656bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
666bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
676bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
686bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
696bc75619SDan Williams  *    names that can be assigned to a namespace.
706bc75619SDan Williams  *
716bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
726bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
736bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
746bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
756bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
766bc75619SDan Williams  *    "blk5.0".
776bc75619SDan Williams  *
786bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
796bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
806bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
816bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
826bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
836bc75619SDan Williams  *
846bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
856bc75619SDan Williams  *
866bc75619SDan Williams  *  region2
876bc75619SDan Williams  * +---------------------+
886bc75619SDan Williams  * |---------------------|
896bc75619SDan Williams  * ||       pm2.0       ||
906bc75619SDan Williams  * |---------------------|
916bc75619SDan Williams  * +---------------------+
926bc75619SDan Williams  *
936bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
946bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
956bc75619SDan Williams  *    reference an NVDIMM.
966bc75619SDan Williams  */
976bc75619SDan Williams enum {
9820985164SVishal Verma 	NUM_PM  = 3,
9920985164SVishal Verma 	NUM_DCR = 5,
10085d3fa02SDan Williams 	NUM_HINTS = 8,
1016bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1026bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1039741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1049741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1056bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1066bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1077bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1086bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1096bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1106bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1116bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1126bc75619SDan Williams 	DCR_SIZE = 12,
1136bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1146bc75619SDan Williams };
1156bc75619SDan Williams 
1166bc75619SDan Williams struct nfit_test_dcr {
1176bc75619SDan Williams 	__le64 bdw_addr;
1186bc75619SDan Williams 	__le32 bdw_status;
1196bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1206bc75619SDan Williams };
1216bc75619SDan Williams 
1226bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1236bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1246bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1256bc75619SDan Williams 
126dafb1048SDan Williams static u32 handle[] = {
1276bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1286bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1296bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1306bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13120985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
132dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
133ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1346bc75619SDan Williams };
1356bc75619SDan Williams 
136af31b04bSMasayoshi Mizuma static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
137af31b04bSMasayoshi Mizuma static int dimm_fail_cmd_code[ARRAY_SIZE(handle)];
1383c13e2acSDave Jiang struct nfit_test_sec {
1393c13e2acSDave Jiang 	u8 state;
140ecaa4a97SDave Jiang 	u8 ext_state;
1412170a0d5SDave Jiang 	u8 old_state;
1423c13e2acSDave Jiang 	u8 passphrase[32];
143ecaa4a97SDave Jiang 	u8 master_passphrase[32];
144926f7480SDave Jiang 	u64 overwrite_end_time;
1453c13e2acSDave Jiang } dimm_sec_info[NUM_DCR];
14673606afdSDan Williams 
147b4d4702fSVishal Verma static const struct nd_intel_smart smart_def = {
148b4d4702fSVishal Verma 	.flags = ND_INTEL_SMART_HEALTH_VALID
149b4d4702fSVishal Verma 		| ND_INTEL_SMART_SPARES_VALID
150b4d4702fSVishal Verma 		| ND_INTEL_SMART_ALARM_VALID
151b4d4702fSVishal Verma 		| ND_INTEL_SMART_USED_VALID
152b4d4702fSVishal Verma 		| ND_INTEL_SMART_SHUTDOWN_VALID
153f1101766SDan Williams 		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
154b4d4702fSVishal Verma 		| ND_INTEL_SMART_MTEMP_VALID
155b4d4702fSVishal Verma 		| ND_INTEL_SMART_CTEMP_VALID,
156b4d4702fSVishal Verma 	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
157b4d4702fSVishal Verma 	.media_temperature = 23 * 16,
158b4d4702fSVishal Verma 	.ctrl_temperature = 25 * 16,
159b4d4702fSVishal Verma 	.pmic_temperature = 40 * 16,
160b4d4702fSVishal Verma 	.spares = 75,
161b4d4702fSVishal Verma 	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
162b4d4702fSVishal Verma 		| ND_INTEL_SMART_TEMP_TRIP,
163b4d4702fSVishal Verma 	.ait_status = 1,
164b4d4702fSVishal Verma 	.life_used = 5,
165b4d4702fSVishal Verma 	.shutdown_state = 0,
166f1101766SDan Williams 	.shutdown_count = 42,
167b4d4702fSVishal Verma 	.vendor_size = 0,
168b4d4702fSVishal Verma };
169b4d4702fSVishal Verma 
170bfbaa952SDave Jiang struct nfit_test_fw {
171bfbaa952SDave Jiang 	enum intel_fw_update_state state;
172bfbaa952SDave Jiang 	u32 context;
173bfbaa952SDave Jiang 	u64 version;
174bfbaa952SDave Jiang 	u32 size_received;
175bfbaa952SDave Jiang 	u64 end_time;
176bfbaa952SDave Jiang };
177bfbaa952SDave Jiang 
1786bc75619SDan Williams struct nfit_test {
1796bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1806bc75619SDan Williams 	struct platform_device pdev;
1816bc75619SDan Williams 	struct list_head resources;
1826bc75619SDan Williams 	void *nfit_buf;
1836bc75619SDan Williams 	dma_addr_t nfit_dma;
1846bc75619SDan Williams 	size_t nfit_size;
1851526f9e2SRoss Zwisler 	size_t nfit_filled;
186dafb1048SDan Williams 	int dcr_idx;
1876bc75619SDan Williams 	int num_dcr;
1886bc75619SDan Williams 	int num_pm;
1896bc75619SDan Williams 	void **dimm;
1906bc75619SDan Williams 	dma_addr_t *dimm_dma;
1919d27a87eSDan Williams 	void **flush;
1929d27a87eSDan Williams 	dma_addr_t *flush_dma;
1936bc75619SDan Williams 	void **label;
1946bc75619SDan Williams 	dma_addr_t *label_dma;
1956bc75619SDan Williams 	void **spa_set;
1966bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1976bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1986bc75619SDan Williams 	dma_addr_t *dcr_dma;
1996bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
2006bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
20120985164SVishal Verma 	int setup_hotplug;
202c14a868aSDan Williams 	union acpi_object **_fit;
203c14a868aSDan Williams 	dma_addr_t _fit_dma;
204f471f1a7SDan Williams 	struct ars_state {
205f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
206f471f1a7SDan Williams 		unsigned long deadline;
207f471f1a7SDan Williams 		spinlock_t lock;
208f471f1a7SDan Williams 	} ars_state;
209af31b04bSMasayoshi Mizuma 	struct device *dimm_dev[ARRAY_SIZE(handle)];
210ed07c433SDan Williams 	struct nd_intel_smart *smart;
211ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
2129fb1a190SDave Jiang 	struct badrange badrange;
2139fb1a190SDave Jiang 	struct work_struct work;
214bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
2156bc75619SDan Williams };
2166bc75619SDan Williams 
2179fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
2189fb1a190SDave Jiang 
219e3f5df76SDan Williams static struct gen_pool *nfit_pool;
220e3f5df76SDan Williams 
221037c8489SDave Jiang static const char zero_key[NVDIMM_PASSPHRASE_LEN];
222037c8489SDave Jiang 
2236bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
2246bc75619SDan Williams {
2256bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
2266bc75619SDan Williams 
2276bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
2286bc75619SDan Williams }
2296bc75619SDan Williams 
230bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
231bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
232bfbaa952SDave Jiang 		int idx)
233bfbaa952SDave Jiang {
234bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
235bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
236bfbaa952SDave Jiang 
237bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
238bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
239bfbaa952SDave Jiang 
240bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
241bfbaa952SDave Jiang 		return -EINVAL;
242bfbaa952SDave Jiang 
243bfbaa952SDave Jiang 	nd_cmd->status = 0;
244bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
245bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
246bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
247bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
248bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
249bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
250bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
251bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
252bfbaa952SDave Jiang 
253bfbaa952SDave Jiang 	return 0;
254bfbaa952SDave Jiang }
255bfbaa952SDave Jiang 
256bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
257bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
258bfbaa952SDave Jiang 		int idx)
259bfbaa952SDave Jiang {
260bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
261bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
262bfbaa952SDave Jiang 
263bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
264bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
265bfbaa952SDave Jiang 
266bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
267bfbaa952SDave Jiang 		return -EINVAL;
268bfbaa952SDave Jiang 
269bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
270bfbaa952SDave Jiang 		/* extended status, FW update in progress */
271bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
272bfbaa952SDave Jiang 		return 0;
273bfbaa952SDave Jiang 	}
274bfbaa952SDave Jiang 
275bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
276bfbaa952SDave Jiang 	fw->context++;
277bfbaa952SDave Jiang 	fw->size_received = 0;
278bfbaa952SDave Jiang 	nd_cmd->status = 0;
279bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
280bfbaa952SDave Jiang 
281bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
282bfbaa952SDave Jiang 
283bfbaa952SDave Jiang 	return 0;
284bfbaa952SDave Jiang }
285bfbaa952SDave Jiang 
286bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
287bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
288bfbaa952SDave Jiang 		int idx)
289bfbaa952SDave Jiang {
290bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
291bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
292bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
293bfbaa952SDave Jiang 
294bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
295bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
296bfbaa952SDave Jiang 
297bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
298bfbaa952SDave Jiang 		return -EINVAL;
299bfbaa952SDave Jiang 
300bfbaa952SDave Jiang 
301bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
302bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
303bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
304bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
305bfbaa952SDave Jiang 
306bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
307bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
308bfbaa952SDave Jiang 		*status = 0x5;
309bfbaa952SDave Jiang 		return 0;
310bfbaa952SDave Jiang 	}
311bfbaa952SDave Jiang 
312bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
313bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
314bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
315bfbaa952SDave Jiang 		*status = 0x10007;
316bfbaa952SDave Jiang 		return 0;
317bfbaa952SDave Jiang 	}
318bfbaa952SDave Jiang 
319bfbaa952SDave Jiang 	/*
320bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
321bfbaa952SDave Jiang 	 * check length is > max send length
322bfbaa952SDave Jiang 	 */
323bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
324bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
325bfbaa952SDave Jiang 		*status = 0x3;
326bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
327bfbaa952SDave Jiang 		return 0;
328bfbaa952SDave Jiang 	}
329bfbaa952SDave Jiang 
330bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
331bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
332bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
333bfbaa952SDave Jiang 	*status = 0;
334bfbaa952SDave Jiang 	return 0;
335bfbaa952SDave Jiang }
336bfbaa952SDave Jiang 
337bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
338bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
339bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
340bfbaa952SDave Jiang {
341bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
342bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
343bfbaa952SDave Jiang 
344bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
345bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
346bfbaa952SDave Jiang 
347bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
348bfbaa952SDave Jiang 		/* update already done, need cold boot */
349bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
350bfbaa952SDave Jiang 		return 0;
351bfbaa952SDave Jiang 	}
352bfbaa952SDave Jiang 
353bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
354bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
355bfbaa952SDave Jiang 
356bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
357bfbaa952SDave Jiang 	case 0: /* finish */
358bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
359bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
360bfbaa952SDave Jiang 					__func__, nd_cmd->context,
361bfbaa952SDave Jiang 					fw->context);
362bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
363bfbaa952SDave Jiang 			return 0;
364bfbaa952SDave Jiang 		}
365bfbaa952SDave Jiang 		nd_cmd->status = 0;
366bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
367bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
368bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
369bfbaa952SDave Jiang 		break;
370bfbaa952SDave Jiang 
371bfbaa952SDave Jiang 	case 1: /* abort */
372bfbaa952SDave Jiang 		fw->size_received = 0;
373bfbaa952SDave Jiang 		/* successfully aborted status */
374bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
375bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
376bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
377bfbaa952SDave Jiang 		break;
378bfbaa952SDave Jiang 
379bfbaa952SDave Jiang 	default: /* bad control flag */
380bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
381bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
382bfbaa952SDave Jiang 		return -EINVAL;
383bfbaa952SDave Jiang 	}
384bfbaa952SDave Jiang 
385bfbaa952SDave Jiang 	return 0;
386bfbaa952SDave Jiang }
387bfbaa952SDave Jiang 
388bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
389bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
390bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
391bfbaa952SDave Jiang {
392bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
393bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
394bfbaa952SDave Jiang 
395bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
396bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
397bfbaa952SDave Jiang 
398bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
399bfbaa952SDave Jiang 		return -EINVAL;
400bfbaa952SDave Jiang 
401bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
402bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
403bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
404bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
405bfbaa952SDave Jiang 		return 0;
406bfbaa952SDave Jiang 	}
407bfbaa952SDave Jiang 
408bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
409bfbaa952SDave Jiang 
410bfbaa952SDave Jiang 	switch (fw->state) {
411bfbaa952SDave Jiang 	case FW_STATE_NEW:
412bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
413bfbaa952SDave Jiang 		nd_cmd->status = 0;
414bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
415bfbaa952SDave Jiang 		break;
416bfbaa952SDave Jiang 
417bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
418bfbaa952SDave Jiang 		/* sequencing error */
419bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
420bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
421bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
422bfbaa952SDave Jiang 		break;
423bfbaa952SDave Jiang 
424bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
425bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
426bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
427bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
428bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
429bfbaa952SDave Jiang 			break;
430bfbaa952SDave Jiang 		}
431bfbaa952SDave Jiang 
432bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
433bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
434bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
435bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
436bfbaa952SDave Jiang 		nd_cmd->status = 0;
437bfbaa952SDave Jiang 		/* bogus test version */
438bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
439bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
440bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
441bfbaa952SDave Jiang 		break;
442bfbaa952SDave Jiang 
443bfbaa952SDave Jiang 	default: /* we should never get here */
444bfbaa952SDave Jiang 		return -EINVAL;
445bfbaa952SDave Jiang 	}
446bfbaa952SDave Jiang 
447bfbaa952SDave Jiang 	return 0;
448bfbaa952SDave Jiang }
449bfbaa952SDave Jiang 
45039c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4516bc75619SDan Williams 		unsigned int buf_len)
4526bc75619SDan Williams {
4536bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4546bc75619SDan Williams 		return -EINVAL;
45539c686b8SVishal Verma 
4566bc75619SDan Williams 	nd_cmd->status = 0;
4576bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4586bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
45939c686b8SVishal Verma 
46039c686b8SVishal Verma 	return 0;
4616bc75619SDan Williams }
46239c686b8SVishal Verma 
46339c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
46439c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
46539c686b8SVishal Verma {
4666bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
46739c686b8SVishal Verma 	int rc;
4686bc75619SDan Williams 
4696bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4706bc75619SDan Williams 		return -EINVAL;
4716bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4726bc75619SDan Williams 		return -EINVAL;
4736bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4746bc75619SDan Williams 		return -EINVAL;
4756bc75619SDan Williams 
4766bc75619SDan Williams 	nd_cmd->status = 0;
4776bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47839c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4796bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
48039c686b8SVishal Verma 
48139c686b8SVishal Verma 	return rc;
4826bc75619SDan Williams }
48339c686b8SVishal Verma 
48439c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
48539c686b8SVishal Verma 		unsigned int buf_len, void *label)
48639c686b8SVishal Verma {
4876bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4886bc75619SDan Williams 	u32 *status;
48939c686b8SVishal Verma 	int rc;
4906bc75619SDan Williams 
4916bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4926bc75619SDan Williams 		return -EINVAL;
4936bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4946bc75619SDan Williams 		return -EINVAL;
4956bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4966bc75619SDan Williams 		return -EINVAL;
4976bc75619SDan Williams 
49839c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4996bc75619SDan Williams 	*status = 0;
5006bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
50139c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
5026bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
50339c686b8SVishal Verma 
50439c686b8SVishal Verma 	return rc;
5056bc75619SDan Williams }
50639c686b8SVishal Verma 
507d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
508747ffe11SDan Williams 
50939c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
51039c686b8SVishal Verma 		unsigned int buf_len)
51139c686b8SVishal Verma {
5129fb1a190SDave Jiang 	int ars_recs;
5139fb1a190SDave Jiang 
51439c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
51539c686b8SVishal Verma 		return -EINVAL;
51639c686b8SVishal Verma 
5179fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5189fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5199fb1a190SDave Jiang 
520747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5219fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
52239c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
523d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
52439c686b8SVishal Verma 
52539c686b8SVishal Verma 	return 0;
52639c686b8SVishal Verma }
52739c686b8SVishal Verma 
5289fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5299fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
53039c686b8SVishal Verma {
531f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
532f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5339fb1a190SDave Jiang 	struct badrange_entry *be;
5349fb1a190SDave Jiang 	u64 end = addr + len - 1;
5359fb1a190SDave Jiang 	int i = 0;
536f471f1a7SDan Williams 
537f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
538f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
539f471f1a7SDan Williams 	ars_status->status = 0;
540f471f1a7SDan Williams 	ars_status->address = addr;
541f471f1a7SDan Williams 	ars_status->length = len;
542f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5439fb1a190SDave Jiang 
5449fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5459fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5469fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5479fb1a190SDave Jiang 		u64 rstart, rend;
5489fb1a190SDave Jiang 
5499fb1a190SDave Jiang 		/* skip entries outside the range */
5509fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5519fb1a190SDave Jiang 			continue;
5529fb1a190SDave Jiang 
5539fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5549fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5559fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
556f471f1a7SDan Williams 		ars_record->handle = 0;
5579fb1a190SDave Jiang 		ars_record->err_address = rstart;
5589fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5599fb1a190SDave Jiang 		i++;
5609fb1a190SDave Jiang 	}
5619fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5629fb1a190SDave Jiang 	ars_status->num_records = i;
5639fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5649fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
565f471f1a7SDan Williams }
566f471f1a7SDan Williams 
5679fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5689fb1a190SDave Jiang 		struct ars_state *ars_state,
569f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
570f471f1a7SDan Williams 		int *cmd_rc)
571f471f1a7SDan Williams {
572f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
57339c686b8SVishal Verma 		return -EINVAL;
57439c686b8SVishal Verma 
575f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
576f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
577f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
578f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
579f471f1a7SDan Williams 	} else {
580f471f1a7SDan Williams 		ars_start->status = 0;
581f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5829fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
583f471f1a7SDan Williams 				ars_start->length);
584f471f1a7SDan Williams 		*cmd_rc = 0;
585f471f1a7SDan Williams 	}
586f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
58739c686b8SVishal Verma 
58839c686b8SVishal Verma 	return 0;
58939c686b8SVishal Verma }
59039c686b8SVishal Verma 
591f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
592f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
593f471f1a7SDan Williams 		int *cmd_rc)
59439c686b8SVishal Verma {
595f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
59639c686b8SVishal Verma 		return -EINVAL;
59739c686b8SVishal Verma 
598f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
599f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
600f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
601f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
602f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
603f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
604f471f1a7SDan Williams 	} else {
605f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
606f471f1a7SDan Williams 				ars_state->ars_status->out_length);
607f471f1a7SDan Williams 		*cmd_rc = 0;
608f471f1a7SDan Williams 	}
609f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
61039c686b8SVishal Verma 	return 0;
61139c686b8SVishal Verma }
61239c686b8SVishal Verma 
6135e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6145e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
615d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
616d4f32367SDan Williams {
617d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
618d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
619d4f32367SDan Williams 		return -EINVAL;
620d4f32367SDan Williams 
621d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
622d4f32367SDan Williams 		return -EINVAL;
623d4f32367SDan Williams 
6245e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
625d4f32367SDan Williams 	clear_err->status = 0;
626d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
627d4f32367SDan Williams 	*cmd_rc = 0;
628d4f32367SDan Williams 	return 0;
629d4f32367SDan Williams }
630d4f32367SDan Williams 
63110246dc8SYasunori Goto struct region_search_spa {
63210246dc8SYasunori Goto 	u64 addr;
63310246dc8SYasunori Goto 	struct nd_region *region;
63410246dc8SYasunori Goto };
63510246dc8SYasunori Goto 
63610246dc8SYasunori Goto static int is_region_device(struct device *dev)
63710246dc8SYasunori Goto {
63810246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
63910246dc8SYasunori Goto }
64010246dc8SYasunori Goto 
64110246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
64210246dc8SYasunori Goto {
64310246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
64410246dc8SYasunori Goto 	struct nd_region *nd_region;
64510246dc8SYasunori Goto 	resource_size_t ndr_end;
64610246dc8SYasunori Goto 
64710246dc8SYasunori Goto 	if (!is_region_device(dev))
64810246dc8SYasunori Goto 		return 0;
64910246dc8SYasunori Goto 
65010246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
65110246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
65210246dc8SYasunori Goto 
65310246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
65410246dc8SYasunori Goto 		ctx->region = nd_region;
65510246dc8SYasunori Goto 		return 1;
65610246dc8SYasunori Goto 	}
65710246dc8SYasunori Goto 
65810246dc8SYasunori Goto 	return 0;
65910246dc8SYasunori Goto }
66010246dc8SYasunori Goto 
66110246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
66210246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
66310246dc8SYasunori Goto {
66410246dc8SYasunori Goto 	int ret;
66510246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
66610246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
66710246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
66810246dc8SYasunori Goto 	struct region_search_spa ctx = {
66910246dc8SYasunori Goto 		.addr = spa->spa,
67010246dc8SYasunori Goto 		.region = NULL,
67110246dc8SYasunori Goto 	};
67210246dc8SYasunori Goto 	u64 dpa;
67310246dc8SYasunori Goto 
67410246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
67510246dc8SYasunori Goto 				nfit_test_search_region_spa);
67610246dc8SYasunori Goto 
67710246dc8SYasunori Goto 	if (!ret)
67810246dc8SYasunori Goto 		return -ENODEV;
67910246dc8SYasunori Goto 
68010246dc8SYasunori Goto 	nd_region = ctx.region;
68110246dc8SYasunori Goto 
68210246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
68310246dc8SYasunori Goto 
68410246dc8SYasunori Goto 	/*
68510246dc8SYasunori Goto 	 * last dimm is selected for test
68610246dc8SYasunori Goto 	 */
68710246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
68810246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
68910246dc8SYasunori Goto 
69010246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
69110246dc8SYasunori Goto 	spa->num_nvdimms = 1;
69210246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
69310246dc8SYasunori Goto 
69410246dc8SYasunori Goto 	return 0;
69510246dc8SYasunori Goto }
69610246dc8SYasunori Goto 
69710246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
69810246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
69910246dc8SYasunori Goto {
70010246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
70110246dc8SYasunori Goto 		return -EINVAL;
70210246dc8SYasunori Goto 
70310246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
70410246dc8SYasunori Goto 		spa->status = 2;
70510246dc8SYasunori Goto 
70610246dc8SYasunori Goto 	return 0;
70710246dc8SYasunori Goto }
70810246dc8SYasunori Goto 
709ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
710ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
711baa51277SDan Williams {
712baa51277SDan Williams 	if (buf_len < sizeof(*smart))
713baa51277SDan Williams 		return -EINVAL;
714ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
715baa51277SDan Williams 	return 0;
716baa51277SDan Williams }
717baa51277SDan Williams 
718cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
719ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
720ed07c433SDan Williams 		unsigned int buf_len,
721ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
722baa51277SDan Williams {
723baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
724baa51277SDan Williams 		return -EINVAL;
725ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
726ed07c433SDan Williams 	return 0;
727ed07c433SDan Williams }
728ed07c433SDan Williams 
729ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
730ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
731ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
732ed07c433SDan Williams {
733ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
734ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
735ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
736ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
737ed07c433SDan Williams 			smart->ctrl_temperature);
738ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
739ed07c433SDan Williams 				&& smart->spares
740ed07c433SDan Williams 				<= thresh->spares)
741ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
742ed07c433SDan Williams 				&& smart->media_temperature
743ed07c433SDan Williams 				>= thresh->media_temperature)
744ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
745ed07c433SDan Williams 				&& smart->ctrl_temperature
7464cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7474cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7484cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
749ed07c433SDan Williams 		device_lock(bus_dev);
750ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
751ed07c433SDan Williams 		device_unlock(bus_dev);
752ed07c433SDan Williams 	}
753ed07c433SDan Williams }
754ed07c433SDan Williams 
755ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
756ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
757ed07c433SDan Williams 		unsigned int buf_len,
758ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
759ed07c433SDan Williams 		struct nd_intel_smart *smart,
760ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
761ed07c433SDan Williams {
762ed07c433SDan Williams 	unsigned int size;
763ed07c433SDan Williams 
764ed07c433SDan Williams 	size = sizeof(*in) - 4;
765ed07c433SDan Williams 	if (buf_len < size)
766ed07c433SDan Williams 		return -EINVAL;
767ed07c433SDan Williams 	memcpy(thresh->data, in, size);
768ed07c433SDan Williams 	in->status = 0;
769ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
770ed07c433SDan Williams 
771baa51277SDan Williams 	return 0;
772baa51277SDan Williams }
773baa51277SDan Williams 
7744cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7754cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7764cf260fcSVishal Verma 		unsigned int buf_len,
7774cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7784cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7794cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7804cf260fcSVishal Verma {
7814cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7824cf260fcSVishal Verma 		return -EINVAL;
7834cf260fcSVishal Verma 
784b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7854cf260fcSVishal Verma 		if (inj->mtemp_enable)
7864cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
787b4d4702fSVishal Verma 		else
788b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
789b4d4702fSVishal Verma 	}
790b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7914cf260fcSVishal Verma 		if (inj->spare_enable)
7924cf260fcSVishal Verma 			smart->spares = inj->spares;
793b4d4702fSVishal Verma 		else
794b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
795b4d4702fSVishal Verma 	}
796b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
7974cf260fcSVishal Verma 		if (inj->fatal_enable)
7984cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
799b4d4702fSVishal Verma 		else
800b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
801b4d4702fSVishal Verma 	}
802b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
8034cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
8044cf260fcSVishal Verma 			smart->shutdown_state = 1;
8054cf260fcSVishal Verma 			smart->shutdown_count++;
806b4d4702fSVishal Verma 		} else
807b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8084cf260fcSVishal Verma 	}
8094cf260fcSVishal Verma 	inj->status = 0;
8104cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8114cf260fcSVishal Verma 
8124cf260fcSVishal Verma 	return 0;
8134cf260fcSVishal Verma }
8144cf260fcSVishal Verma 
8159fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8169fb1a190SDave Jiang {
8179fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8189fb1a190SDave Jiang 
8199fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8209fb1a190SDave Jiang }
8219fb1a190SDave Jiang 
8229fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8239fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8249fb1a190SDave Jiang {
8259fb1a190SDave Jiang 	int rc;
8269fb1a190SDave Jiang 
82741cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8289fb1a190SDave Jiang 		rc = -EINVAL;
8299fb1a190SDave Jiang 		goto err;
8309fb1a190SDave Jiang 	}
8319fb1a190SDave Jiang 
8329fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8339fb1a190SDave Jiang 		rc = -EINVAL;
8349fb1a190SDave Jiang 		goto err;
8359fb1a190SDave Jiang 	}
8369fb1a190SDave Jiang 
8379fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8389fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8399fb1a190SDave Jiang 	if (rc < 0)
8409fb1a190SDave Jiang 		goto err;
8419fb1a190SDave Jiang 
8429fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8439fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8449fb1a190SDave Jiang 
8459fb1a190SDave Jiang 	err_inj->status = 0;
8469fb1a190SDave Jiang 	return 0;
8479fb1a190SDave Jiang 
8489fb1a190SDave Jiang err:
8499fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8509fb1a190SDave Jiang 	return rc;
8519fb1a190SDave Jiang }
8529fb1a190SDave Jiang 
8539fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8549fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8559fb1a190SDave Jiang {
8569fb1a190SDave Jiang 	int rc;
8579fb1a190SDave Jiang 
85841cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8599fb1a190SDave Jiang 		rc = -EINVAL;
8609fb1a190SDave Jiang 		goto err;
8619fb1a190SDave Jiang 	}
8629fb1a190SDave Jiang 
8639fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8649fb1a190SDave Jiang 		rc = -EINVAL;
8659fb1a190SDave Jiang 		goto err;
8669fb1a190SDave Jiang 	}
8679fb1a190SDave Jiang 
8689fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8699fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8709fb1a190SDave Jiang 
8719fb1a190SDave Jiang 	err_clr->status = 0;
8729fb1a190SDave Jiang 	return 0;
8739fb1a190SDave Jiang 
8749fb1a190SDave Jiang err:
8759fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8769fb1a190SDave Jiang 	return rc;
8779fb1a190SDave Jiang }
8789fb1a190SDave Jiang 
8799fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8809fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8819fb1a190SDave Jiang 		unsigned int buf_len)
8829fb1a190SDave Jiang {
8839fb1a190SDave Jiang 	struct badrange_entry *be;
8849fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8859fb1a190SDave Jiang 	int i = 0;
8869fb1a190SDave Jiang 
8879fb1a190SDave Jiang 	err_stat->status = 0;
8889fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8899fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8909fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8919fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8929fb1a190SDave Jiang 		i++;
8939fb1a190SDave Jiang 		if (i > max)
8949fb1a190SDave Jiang 			break;
8959fb1a190SDave Jiang 	}
8969fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8979fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8989fb1a190SDave Jiang 
8999fb1a190SDave Jiang 	return 0;
9009fb1a190SDave Jiang }
9019fb1a190SDave Jiang 
902674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
903674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
904674d8bdeSDave Jiang {
905674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
906674d8bdeSDave Jiang 
907674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
908674d8bdeSDave Jiang 		return -EINVAL;
909674d8bdeSDave Jiang 
910674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
911674d8bdeSDave Jiang 	case 0:
912674d8bdeSDave Jiang 		nd_cmd->status = 0;
913674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
914674d8bdeSDave Jiang 				__func__);
915674d8bdeSDave Jiang 		break;
916674d8bdeSDave Jiang 	case 1:
917674d8bdeSDave Jiang 		nd_cmd->status = 0;
918674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
919674d8bdeSDave Jiang 				__func__);
920674d8bdeSDave Jiang 		break;
921674d8bdeSDave Jiang 	default:
922674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
923674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
924674d8bdeSDave Jiang 		break;
925674d8bdeSDave Jiang 	}
926674d8bdeSDave Jiang 
927674d8bdeSDave Jiang 
928674d8bdeSDave Jiang 	return 0;
929674d8bdeSDave Jiang }
930674d8bdeSDave Jiang 
93139611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
93239611e83SDan Williams {
93339611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
93439611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
93539611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
93639611e83SDan Williams 		return -EIO;
93739611e83SDan Williams 	}
93839611e83SDan Williams 	return rc;
93939611e83SDan Williams }
94039611e83SDan Williams 
9413c13e2acSDave Jiang static int nd_intel_test_cmd_security_status(struct nfit_test *t,
9423c13e2acSDave Jiang 		struct nd_intel_get_security_state *nd_cmd,
9433c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9443c13e2acSDave Jiang {
9453c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9463c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9473c13e2acSDave Jiang 
9483c13e2acSDave Jiang 	nd_cmd->status = 0;
9493c13e2acSDave Jiang 	nd_cmd->state = sec->state;
950ecaa4a97SDave Jiang 	nd_cmd->extended_state = sec->ext_state;
9513c13e2acSDave Jiang 	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);
9523c13e2acSDave Jiang 
9533c13e2acSDave Jiang 	return 0;
9543c13e2acSDave Jiang }
9553c13e2acSDave Jiang 
9563c13e2acSDave Jiang static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
9573c13e2acSDave Jiang 		struct nd_intel_unlock_unit *nd_cmd,
9583c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9593c13e2acSDave Jiang {
9603c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9613c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9623c13e2acSDave Jiang 
9633c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
9643c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
9653c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9663c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
9673c13e2acSDave Jiang 				sec->state);
9683c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
9693c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9703c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9713c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid passphrase\n");
9723c13e2acSDave Jiang 	} else {
9733c13e2acSDave Jiang 		nd_cmd->status = 0;
9743c13e2acSDave Jiang 		sec->state = ND_INTEL_SEC_STATE_ENABLED;
9753c13e2acSDave Jiang 		dev_dbg(dev, "Unit unlocked\n");
9763c13e2acSDave Jiang 	}
9773c13e2acSDave Jiang 
9783c13e2acSDave Jiang 	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
9793c13e2acSDave Jiang 	return 0;
9803c13e2acSDave Jiang }
9813c13e2acSDave Jiang 
9823c13e2acSDave Jiang static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
9833c13e2acSDave Jiang 		struct nd_intel_set_passphrase *nd_cmd,
9843c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9853c13e2acSDave Jiang {
9863c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9873c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9883c13e2acSDave Jiang 
9893c13e2acSDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
9903c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9913c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong security state\n");
9923c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
9933c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9943c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9953c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong passphrase\n");
9963c13e2acSDave Jiang 	} else {
9973c13e2acSDave Jiang 		memcpy(sec->passphrase, nd_cmd->new_pass,
9983c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
9993c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
10003c13e2acSDave Jiang 		nd_cmd->status = 0;
10013c13e2acSDave Jiang 		dev_dbg(dev, "passphrase updated\n");
10023c13e2acSDave Jiang 	}
10033c13e2acSDave Jiang 
10043c13e2acSDave Jiang 	return 0;
10053c13e2acSDave Jiang }
10063c13e2acSDave Jiang 
10073c13e2acSDave Jiang static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
10083c13e2acSDave Jiang 		struct nd_intel_freeze_lock *nd_cmd,
10093c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10103c13e2acSDave Jiang {
10113c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10123c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10133c13e2acSDave Jiang 
10143c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
10153c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10163c13e2acSDave Jiang 		dev_dbg(dev, "freeze lock: wrong security state\n");
10173c13e2acSDave Jiang 	} else {
10183c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
10193c13e2acSDave Jiang 		nd_cmd->status = 0;
10203c13e2acSDave Jiang 		dev_dbg(dev, "security frozen\n");
10213c13e2acSDave Jiang 	}
10223c13e2acSDave Jiang 
10233c13e2acSDave Jiang 	return 0;
10243c13e2acSDave Jiang }
10253c13e2acSDave Jiang 
10263c13e2acSDave Jiang static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
10273c13e2acSDave Jiang 		struct nd_intel_disable_passphrase *nd_cmd,
10283c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10293c13e2acSDave Jiang {
10303c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10313c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10323c13e2acSDave Jiang 
10333c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
10343c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
10353c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10363c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong security state\n");
10373c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10383c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10393c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10403c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
10413c13e2acSDave Jiang 	} else {
10423c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10433c13e2acSDave Jiang 		sec->state = 0;
10443c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: done\n");
10453c13e2acSDave Jiang 	}
10463c13e2acSDave Jiang 
10473c13e2acSDave Jiang 	return 0;
10483c13e2acSDave Jiang }
10493c13e2acSDave Jiang 
10503c13e2acSDave Jiang static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
10513c13e2acSDave Jiang 		struct nd_intel_secure_erase *nd_cmd,
10523c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10533c13e2acSDave Jiang {
10543c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10553c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10563c13e2acSDave Jiang 
1057037c8489SDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
10583c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10593c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong security state\n");
10603c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10613c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10623c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10633c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong passphrase\n");
10643c13e2acSDave Jiang 	} else {
1065037c8489SDave Jiang 		if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)
1066037c8489SDave Jiang 				&& (memcmp(nd_cmd->passphrase, zero_key,
1067037c8489SDave Jiang 					ND_INTEL_PASSPHRASE_SIZE) != 0)) {
1068037c8489SDave Jiang 			dev_dbg(dev, "invalid zero key\n");
1069037c8489SDave Jiang 			return 0;
1070037c8489SDave Jiang 		}
10713c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1072ecaa4a97SDave Jiang 		memset(sec->master_passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10733c13e2acSDave Jiang 		sec->state = 0;
1074ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
10753c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: done\n");
10763c13e2acSDave Jiang 	}
10773c13e2acSDave Jiang 
10783c13e2acSDave Jiang 	return 0;
10793c13e2acSDave Jiang }
10803c13e2acSDave Jiang 
1081926f7480SDave Jiang static int nd_intel_test_cmd_overwrite(struct nfit_test *t,
1082926f7480SDave Jiang 		struct nd_intel_overwrite *nd_cmd,
1083926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1084926f7480SDave Jiang {
1085926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1086926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1087926f7480SDave Jiang 
1088926f7480SDave Jiang 	if ((sec->state & ND_INTEL_SEC_STATE_ENABLED) &&
1089926f7480SDave Jiang 			memcmp(nd_cmd->passphrase, sec->passphrase,
1090926f7480SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1091926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1092926f7480SDave Jiang 		dev_dbg(dev, "overwrite: wrong passphrase\n");
1093926f7480SDave Jiang 		return 0;
1094926f7480SDave Jiang 	}
1095926f7480SDave Jiang 
10962170a0d5SDave Jiang 	sec->old_state = sec->state;
1097926f7480SDave Jiang 	sec->state = ND_INTEL_SEC_STATE_OVERWRITE;
1098926f7480SDave Jiang 	dev_dbg(dev, "overwrite progressing.\n");
1099926f7480SDave Jiang 	sec->overwrite_end_time = get_jiffies_64() + 5 * HZ;
1100926f7480SDave Jiang 
1101926f7480SDave Jiang 	return 0;
1102926f7480SDave Jiang }
1103926f7480SDave Jiang 
1104926f7480SDave Jiang static int nd_intel_test_cmd_query_overwrite(struct nfit_test *t,
1105926f7480SDave Jiang 		struct nd_intel_query_overwrite *nd_cmd,
1106926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1107926f7480SDave Jiang {
1108926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1109926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1110926f7480SDave Jiang 
1111926f7480SDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_OVERWRITE)) {
1112926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_SEQUENCE_ERR;
1113926f7480SDave Jiang 		return 0;
1114926f7480SDave Jiang 	}
1115926f7480SDave Jiang 
1116926f7480SDave Jiang 	if (time_is_before_jiffies64(sec->overwrite_end_time)) {
1117926f7480SDave Jiang 		sec->overwrite_end_time = 0;
11182170a0d5SDave Jiang 		sec->state = sec->old_state;
11192170a0d5SDave Jiang 		sec->old_state = 0;
1120ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1121926f7480SDave Jiang 		dev_dbg(dev, "overwrite is complete\n");
1122926f7480SDave Jiang 	} else
1123926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_INPROGRESS;
1124926f7480SDave Jiang 	return 0;
1125926f7480SDave Jiang }
1126926f7480SDave Jiang 
1127ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_set_pass(struct nfit_test *t,
1128ecaa4a97SDave Jiang 		struct nd_intel_set_master_passphrase *nd_cmd,
1129ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1130ecaa4a97SDave Jiang {
1131ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1132ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1133ecaa4a97SDave Jiang 
1134ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1135ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1136ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong state\n");
1137ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1138ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1139ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong security state\n");
1140ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->master_passphrase,
1141ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1142ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1143ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: wrong passphrase\n");
1144ecaa4a97SDave Jiang 	} else {
1145ecaa4a97SDave Jiang 		memcpy(sec->master_passphrase, nd_cmd->new_pass,
1146ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
1147ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1148ecaa4a97SDave Jiang 		dev_dbg(dev, "master passphrase: updated\n");
1149ecaa4a97SDave Jiang 	}
1150ecaa4a97SDave Jiang 
1151ecaa4a97SDave Jiang 	return 0;
1152ecaa4a97SDave Jiang }
1153ecaa4a97SDave Jiang 
1154ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_secure_erase(struct nfit_test *t,
1155ecaa4a97SDave Jiang 		struct nd_intel_master_secure_erase *nd_cmd,
1156ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1157ecaa4a97SDave Jiang {
1158ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1159ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1160ecaa4a97SDave Jiang 
1161ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1162ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1163ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong state\n");
1164ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1165ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1166ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong security state\n");
1167ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->master_passphrase,
1168ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1169ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1170ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: wrong passphrase\n");
1171ecaa4a97SDave Jiang 	} else {
1172ecaa4a97SDave Jiang 		/* we do not erase master state passphrase ever */
1173ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1174ecaa4a97SDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1175ecaa4a97SDave Jiang 		sec->state = 0;
1176ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: done\n");
1177ecaa4a97SDave Jiang 	}
1178ecaa4a97SDave Jiang 
1179ecaa4a97SDave Jiang 	return 0;
1180ecaa4a97SDave Jiang }
1181ecaa4a97SDave Jiang 
1182ecaa4a97SDave Jiang 
1183bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
1184bfbaa952SDave Jiang {
1185bfbaa952SDave Jiang 	int i;
1186bfbaa952SDave Jiang 
1187bfbaa952SDave Jiang 	/* lookup per-dimm data */
1188bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1189bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1190bfbaa952SDave Jiang 			break;
1191bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
1192bfbaa952SDave Jiang 		return -ENXIO;
1193bfbaa952SDave Jiang 	return i;
1194bfbaa952SDave Jiang }
1195bfbaa952SDave Jiang 
119639c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
119739c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1198aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
119939c686b8SVishal Verma {
120039c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
120139c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
12026634fb06SDan Williams 	unsigned int func = cmd;
1203f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1204f471f1a7SDan Williams 
1205f471f1a7SDan Williams 	if (!cmd_rc)
1206f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1207f471f1a7SDan Williams 	*cmd_rc = 0;
120839c686b8SVishal Verma 
120939c686b8SVishal Verma 	if (nvdimm) {
121039c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1211e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
121239c686b8SVishal Verma 
12136634fb06SDan Williams 		if (!nfit_mem)
12146634fb06SDan Williams 			return -ENOTTY;
12156634fb06SDan Williams 
12166634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
12176634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
12186634fb06SDan Williams 
12196634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
12206634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
12216634fb06SDan Williams 			func = call_pkg->nd_command;
12226634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
12236634fb06SDan Williams 				return -ENOTTY;
1224bfbaa952SDave Jiang 
1225bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1226bfbaa952SDave Jiang 			if (i < 0)
1227bfbaa952SDave Jiang 				return i;
1228bfbaa952SDave Jiang 
1229bfbaa952SDave Jiang 			switch (func) {
12303c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
12313c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
12323c13e2acSDave Jiang 						buf, buf_len, i);
12333c13e2acSDave Jiang 				break;
12343c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
12353c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
12363c13e2acSDave Jiang 						buf, buf_len, i);
12373c13e2acSDave Jiang 				break;
12383c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
12393c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
12403c13e2acSDave Jiang 						buf, buf_len, i);
12413c13e2acSDave Jiang 				break;
12423c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
12433c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
12443c13e2acSDave Jiang 						buf, buf_len, i);
12453c13e2acSDave Jiang 				break;
12463c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
12473c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
12483c13e2acSDave Jiang 						buf, buf_len, i);
12493c13e2acSDave Jiang 				break;
12503c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
12513c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
12523c13e2acSDave Jiang 						buf, buf_len, i);
12533c13e2acSDave Jiang 				break;
1254926f7480SDave Jiang 			case NVDIMM_INTEL_OVERWRITE:
1255926f7480SDave Jiang 				rc = nd_intel_test_cmd_overwrite(t,
1256926f7480SDave Jiang 						buf, buf_len, i - t->dcr_idx);
1257926f7480SDave Jiang 				break;
1258926f7480SDave Jiang 			case NVDIMM_INTEL_QUERY_OVERWRITE:
1259926f7480SDave Jiang 				rc = nd_intel_test_cmd_query_overwrite(t,
1260926f7480SDave Jiang 						buf, buf_len, i - t->dcr_idx);
1261926f7480SDave Jiang 				break;
1262ecaa4a97SDave Jiang 			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
1263ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_set_pass(t,
1264ecaa4a97SDave Jiang 						buf, buf_len, i);
1265ecaa4a97SDave Jiang 				break;
1266ecaa4a97SDave Jiang 			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
1267ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_secure_erase(t,
1268ecaa4a97SDave Jiang 						buf, buf_len, i);
1269ecaa4a97SDave Jiang 				break;
1270674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
127139611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1272674d8bdeSDave Jiang 						buf, buf_len);
127339611e83SDan Williams 				break;
1274bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
127539611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
1276bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
127739611e83SDan Williams 				break;
1278bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
127939611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
1280bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128139611e83SDan Williams 				break;
1282bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
128339611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
1284bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128539611e83SDan Williams 				break;
1286bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
128739611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
1288bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128939611e83SDan Williams 				break;
1290bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
129139611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
1292bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
129339611e83SDan Williams 				break;
1294bfbaa952SDave Jiang 			case ND_INTEL_SMART:
129539611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
1296bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
129739611e83SDan Williams 				break;
1298bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
129939611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1300bfbaa952SDave Jiang 						buf_len,
1301bfbaa952SDave Jiang 						&t->smart_threshold[i -
1302bfbaa952SDave Jiang 							t->dcr_idx]);
130339611e83SDan Williams 				break;
1304bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
130539611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1306bfbaa952SDave Jiang 						buf_len,
1307bfbaa952SDave Jiang 						&t->smart_threshold[i -
1308bfbaa952SDave Jiang 							t->dcr_idx],
1309bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
1310bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
131139611e83SDan Williams 				break;
13124cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
131339611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
13144cf260fcSVishal Verma 						buf_len,
13154cf260fcSVishal Verma 						&t->smart_threshold[i -
13164cf260fcSVishal Verma 							t->dcr_idx],
13174cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
13184cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
131939611e83SDan Williams 				break;
1320bfbaa952SDave Jiang 			default:
1321bfbaa952SDave Jiang 				return -ENOTTY;
1322bfbaa952SDave Jiang 			}
132339611e83SDan Williams 			return override_return_code(i, func, rc);
13246634fb06SDan Williams 		}
13256634fb06SDan Williams 
13266634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
13276634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
132839c686b8SVishal Verma 			return -ENOTTY;
132939c686b8SVishal Verma 
1330bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1331bfbaa952SDave Jiang 		if (i < 0)
1332bfbaa952SDave Jiang 			return i;
133373606afdSDan Williams 
13346634fb06SDan Williams 		switch (func) {
133539c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
133639c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
133739c686b8SVishal Verma 			break;
133839c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
133939c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1340dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
134139c686b8SVishal Verma 			break;
134239c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
134339c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1344dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
134539c686b8SVishal Verma 			break;
13466bc75619SDan Williams 		default:
13476bc75619SDan Williams 			return -ENOTTY;
13486bc75619SDan Williams 		}
134939611e83SDan Williams 		return override_return_code(i, func, rc);
135039c686b8SVishal Verma 	} else {
1351f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
135210246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
135310246dc8SYasunori Goto 
135410246dc8SYasunori Goto 		if (!nd_desc)
135510246dc8SYasunori Goto 			return -ENOTTY;
135610246dc8SYasunori Goto 
135710246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
135810246dc8SYasunori Goto 			func = call_pkg->nd_command;
135910246dc8SYasunori Goto 
136010246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
136110246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
136210246dc8SYasunori Goto 
136310246dc8SYasunori Goto 			switch (func) {
136410246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
136510246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
136610246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
136710246dc8SYasunori Goto 				return rc;
13689fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
13699fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
13709fb1a190SDave Jiang 					buf_len);
13719fb1a190SDave Jiang 				return rc;
13729fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
13739fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
13749fb1a190SDave Jiang 					buf_len);
13759fb1a190SDave Jiang 				return rc;
13769fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
13779fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
13789fb1a190SDave Jiang 					buf_len);
13799fb1a190SDave Jiang 				return rc;
138010246dc8SYasunori Goto 			default:
138110246dc8SYasunori Goto 				return -ENOTTY;
138210246dc8SYasunori Goto 			}
138310246dc8SYasunori Goto 		}
1384f471f1a7SDan Williams 
1385e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
138639c686b8SVishal Verma 			return -ENOTTY;
138739c686b8SVishal Verma 
13886634fb06SDan Williams 		switch (func) {
138939c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
139039c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
139139c686b8SVishal Verma 			break;
139239c686b8SVishal Verma 		case ND_CMD_ARS_START:
13939fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
13949fb1a190SDave Jiang 					buf_len, cmd_rc);
139539c686b8SVishal Verma 			break;
139639c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1397f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1398f471f1a7SDan Williams 					cmd_rc);
139939c686b8SVishal Verma 			break;
1400d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
14015e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1402d4f32367SDan Williams 			break;
140339c686b8SVishal Verma 		default:
140439c686b8SVishal Verma 			return -ENOTTY;
140539c686b8SVishal Verma 		}
140639c686b8SVishal Verma 	}
14076bc75619SDan Williams 
14086bc75619SDan Williams 	return rc;
14096bc75619SDan Williams }
14106bc75619SDan Williams 
14116bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
14126bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
14136bc75619SDan Williams 
14146bc75619SDan Williams static void release_nfit_res(void *data)
14156bc75619SDan Williams {
14166bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
14176bc75619SDan Williams 
14186bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14196bc75619SDan Williams 	list_del(&nfit_res->list);
14206bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14216bc75619SDan Williams 
1422e3f5df76SDan Williams 	if (resource_size(&nfit_res->res) >= DIMM_SIZE)
1423e3f5df76SDan Williams 		gen_pool_free(nfit_pool, nfit_res->res.start,
1424e3f5df76SDan Williams 				resource_size(&nfit_res->res));
14256bc75619SDan Williams 	vfree(nfit_res->buf);
14266bc75619SDan Williams 	kfree(nfit_res);
14276bc75619SDan Williams }
14286bc75619SDan Williams 
14296bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
14306bc75619SDan Williams 		void *buf)
14316bc75619SDan Williams {
14326bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
14336bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
14346bc75619SDan Williams 			GFP_KERNEL);
14356bc75619SDan Williams 	int rc;
14366bc75619SDan Williams 
1437e3f5df76SDan Williams 	if (!buf || !nfit_res || !*dma)
14386bc75619SDan Williams 		goto err;
14396bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
14406bc75619SDan Williams 	if (rc)
14416bc75619SDan Williams 		goto err;
14426bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
14436bc75619SDan Williams 	memset(buf, 0, size);
14446bc75619SDan Williams 	nfit_res->dev = dev;
14456bc75619SDan Williams 	nfit_res->buf = buf;
1446bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1447bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1448bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1449bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1450bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
14516bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14526bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
14536bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14546bc75619SDan Williams 
14556bc75619SDan Williams 	return nfit_res->buf;
14566bc75619SDan Williams  err:
1457e3f5df76SDan Williams 	if (*dma && size >= DIMM_SIZE)
1458e3f5df76SDan Williams 		gen_pool_free(nfit_pool, *dma, size);
1459ee8520feSDan Williams 	if (buf)
14606bc75619SDan Williams 		vfree(buf);
14616bc75619SDan Williams 	kfree(nfit_res);
14626bc75619SDan Williams 	return NULL;
14636bc75619SDan Williams }
14646bc75619SDan Williams 
14656bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
14666bc75619SDan Williams {
1467e3f5df76SDan Williams 	struct genpool_data_align data = {
1468e3f5df76SDan Williams 		.align = SZ_128M,
1469e3f5df76SDan Williams 	};
14706bc75619SDan Williams 	void *buf = vmalloc(size);
14716bc75619SDan Williams 
1472e3f5df76SDan Williams 	if (size >= DIMM_SIZE)
1473e3f5df76SDan Williams 		*dma = gen_pool_alloc_algo(nfit_pool, size,
1474e3f5df76SDan Williams 				gen_pool_first_fit_align, &data);
1475e3f5df76SDan Williams 	else
14766bc75619SDan Williams 		*dma = (unsigned long) buf;
14776bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
14786bc75619SDan Williams }
14796bc75619SDan Williams 
14806bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
14816bc75619SDan Williams {
14826bc75619SDan Williams 	int i;
14836bc75619SDan Williams 
14846bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
14856bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
14866bc75619SDan Williams 		struct nfit_test *t = instances[i];
14876bc75619SDan Williams 
14886bc75619SDan Williams 		if (!t)
14896bc75619SDan Williams 			continue;
14906bc75619SDan Williams 		spin_lock(&nfit_test_lock);
14916bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1492bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1493bd4cd745SDan Williams 						+ resource_size(&n->res))) {
14946bc75619SDan Williams 				nfit_res = n;
14956bc75619SDan Williams 				break;
14966bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
14976bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1498bd4cd745SDan Williams 						+ resource_size(&n->res))) {
14996bc75619SDan Williams 				nfit_res = n;
15006bc75619SDan Williams 				break;
15016bc75619SDan Williams 			}
15026bc75619SDan Williams 		}
15036bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
15046bc75619SDan Williams 		if (nfit_res)
15056bc75619SDan Williams 			return nfit_res;
15066bc75619SDan Williams 	}
15076bc75619SDan Williams 
15086bc75619SDan Williams 	return NULL;
15096bc75619SDan Williams }
15106bc75619SDan Williams 
1511f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1512f471f1a7SDan Williams {
15139fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1514f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
15159fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1516f471f1a7SDan Williams 	if (!ars_state->ars_status)
1517f471f1a7SDan Williams 		return -ENOMEM;
1518f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1519f471f1a7SDan Williams 	return 0;
1520f471f1a7SDan Williams }
1521f471f1a7SDan Williams 
1522231bf117SDan Williams static void put_dimms(void *data)
1523231bf117SDan Williams {
1524718fda67SDan Williams 	struct nfit_test *t = data;
1525231bf117SDan Williams 	int i;
1526231bf117SDan Williams 
1527718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1528718fda67SDan Williams 		if (t->dimm_dev[i])
1529718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1530231bf117SDan Williams }
1531231bf117SDan Williams 
1532231bf117SDan Williams static struct class *nfit_test_dimm;
1533231bf117SDan Williams 
153473606afdSDan Williams static int dimm_name_to_id(struct device *dev)
153573606afdSDan Williams {
153673606afdSDan Williams 	int dimm;
153773606afdSDan Williams 
1538718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
153973606afdSDan Williams 		return -ENXIO;
154073606afdSDan Williams 	return dimm;
154173606afdSDan Williams }
154273606afdSDan Williams 
154373606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
154473606afdSDan Williams 		char *buf)
154573606afdSDan Williams {
154673606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
154773606afdSDan Williams 
154873606afdSDan Williams 	if (dimm < 0)
154973606afdSDan Williams 		return dimm;
155073606afdSDan Williams 
155119357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
155273606afdSDan Williams }
155373606afdSDan Williams DEVICE_ATTR_RO(handle);
155473606afdSDan Williams 
155573606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
155673606afdSDan Williams 		char *buf)
155773606afdSDan Williams {
155873606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
155973606afdSDan Williams 
156073606afdSDan Williams 	if (dimm < 0)
156173606afdSDan Williams 		return dimm;
156273606afdSDan Williams 
156373606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
156473606afdSDan Williams }
156573606afdSDan Williams 
156673606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
156773606afdSDan Williams 		const char *buf, size_t size)
156873606afdSDan Williams {
156973606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
157073606afdSDan Williams 	unsigned long val;
157173606afdSDan Williams 	ssize_t rc;
157273606afdSDan Williams 
157373606afdSDan Williams 	if (dimm < 0)
157473606afdSDan Williams 		return dimm;
157573606afdSDan Williams 
157673606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
157773606afdSDan Williams 	if (rc)
157873606afdSDan Williams 		return rc;
157973606afdSDan Williams 
158073606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
158173606afdSDan Williams 	return size;
158273606afdSDan Williams }
158373606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
158473606afdSDan Williams 
158555c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
158655c72ab6SDan Williams 		char *buf)
158755c72ab6SDan Williams {
158855c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
158955c72ab6SDan Williams 
159055c72ab6SDan Williams 	if (dimm < 0)
159155c72ab6SDan Williams 		return dimm;
159255c72ab6SDan Williams 
159355c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
159455c72ab6SDan Williams }
159555c72ab6SDan Williams 
159655c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
159755c72ab6SDan Williams 		const char *buf, size_t size)
159855c72ab6SDan Williams {
159955c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
160055c72ab6SDan Williams 	unsigned long val;
160155c72ab6SDan Williams 	ssize_t rc;
160255c72ab6SDan Williams 
160355c72ab6SDan Williams 	if (dimm < 0)
160455c72ab6SDan Williams 		return dimm;
160555c72ab6SDan Williams 
160655c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
160755c72ab6SDan Williams 	if (rc)
160855c72ab6SDan Williams 		return rc;
160955c72ab6SDan Williams 
161055c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
161155c72ab6SDan Williams 	return size;
161255c72ab6SDan Williams }
161355c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
161455c72ab6SDan Williams 
16153c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
16163c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
16173c13e2acSDave Jiang {
16183c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
16193c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
16203c13e2acSDave Jiang 
16213c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
16223c13e2acSDave Jiang 	return size;
16233c13e2acSDave Jiang }
16243c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
16253c13e2acSDave Jiang 
162673606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
162773606afdSDan Williams 	&dev_attr_fail_cmd.attr,
162855c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
162973606afdSDan Williams 	&dev_attr_handle.attr,
16303c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
163173606afdSDan Williams 	NULL,
163273606afdSDan Williams };
163373606afdSDan Williams 
163473606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
163573606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
163673606afdSDan Williams };
163773606afdSDan Williams 
163873606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
163973606afdSDan Williams 	&nfit_test_dimm_attribute_group,
164073606afdSDan Williams 	NULL,
164173606afdSDan Williams };
164273606afdSDan Williams 
1643718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1644718fda67SDan Williams {
1645718fda67SDan Williams 	int i;
1646718fda67SDan Williams 
1647718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1648718fda67SDan Williams 		return -ENOMEM;
1649718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1650718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1651718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1652718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1653718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1654718fda67SDan Williams 		if (!t->dimm_dev[i])
1655718fda67SDan Williams 			return -ENOMEM;
1656718fda67SDan Williams 	}
1657718fda67SDan Williams 	return 0;
1658718fda67SDan Williams }
1659718fda67SDan Williams 
1660ecaa4a97SDave Jiang static void security_init(struct nfit_test *t)
1661ecaa4a97SDave Jiang {
1662ecaa4a97SDave Jiang 	int i;
1663ecaa4a97SDave Jiang 
1664ecaa4a97SDave Jiang 	for (i = 0; i < t->num_dcr; i++) {
1665ecaa4a97SDave Jiang 		struct nfit_test_sec *sec = &dimm_sec_info[i];
1666ecaa4a97SDave Jiang 
1667ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1668ecaa4a97SDave Jiang 	}
1669ecaa4a97SDave Jiang }
1670ecaa4a97SDave Jiang 
1671ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1672ed07c433SDan Williams {
1673ed07c433SDan Williams 	int i;
1674ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1675ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1676ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1677ed07c433SDan Williams 		.media_temperature = 40 * 16,
1678ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1679ed07c433SDan Williams 		.spares = 5,
1680ed07c433SDan Williams 	};
1681ed07c433SDan Williams 
1682ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1683b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1684ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1685ed07c433SDan Williams 				sizeof(smart_t_data));
1686ed07c433SDan Williams 	}
1687ed07c433SDan Williams }
1688ed07c433SDan Williams 
16896bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
16906bc75619SDan Williams {
16916b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
16926bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
16936bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
16943b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
16953b87356fSDan Williams 					window_size) * NUM_DCR
16969d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
169785d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1698f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1699f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
17006bc75619SDan Williams 	int i;
17016bc75619SDan Williams 
17026bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17036bc75619SDan Williams 	if (!t->nfit_buf)
17046bc75619SDan Williams 		return -ENOMEM;
17056bc75619SDan Williams 	t->nfit_size = nfit_size;
17066bc75619SDan Williams 
1707ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
17086bc75619SDan Williams 	if (!t->spa_set[0])
17096bc75619SDan Williams 		return -ENOMEM;
17106bc75619SDan Williams 
1711ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
17126bc75619SDan Williams 	if (!t->spa_set[1])
17136bc75619SDan Williams 		return -ENOMEM;
17146bc75619SDan Williams 
1715ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
171620985164SVishal Verma 	if (!t->spa_set[2])
171720985164SVishal Verma 		return -ENOMEM;
171820985164SVishal Verma 
1719dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17206bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
17216bc75619SDan Williams 		if (!t->dimm[i])
17226bc75619SDan Williams 			return -ENOMEM;
17236bc75619SDan Williams 
17246bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
17256bc75619SDan Williams 		if (!t->label[i])
17266bc75619SDan Williams 			return -ENOMEM;
17276bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
17289d27a87eSDan Williams 
17299d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
17309d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
173185d3fa02SDan Williams 				&t->flush_dma[i]);
17329d27a87eSDan Williams 		if (!t->flush[i])
17339d27a87eSDan Williams 			return -ENOMEM;
17346bc75619SDan Williams 	}
17356bc75619SDan Williams 
1736dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17376bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
17386bc75619SDan Williams 		if (!t->dcr[i])
17396bc75619SDan Williams 			return -ENOMEM;
17406bc75619SDan Williams 	}
17416bc75619SDan Williams 
1742c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1743c14a868aSDan Williams 	if (!t->_fit)
1744c14a868aSDan Williams 		return -ENOMEM;
1745c14a868aSDan Williams 
1746718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1747231bf117SDan Williams 		return -ENOMEM;
1748ed07c433SDan Williams 	smart_init(t);
1749ecaa4a97SDave Jiang 	security_init(t);
1750f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17516bc75619SDan Williams }
17526bc75619SDan Williams 
17536bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
17546bc75619SDan Williams {
17557bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1756ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1757ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1758dafb1048SDan Williams 	int i;
17596bc75619SDan Williams 
17606bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17616bc75619SDan Williams 	if (!t->nfit_buf)
17626bc75619SDan Williams 		return -ENOMEM;
17636bc75619SDan Williams 	t->nfit_size = nfit_size;
17646bc75619SDan Williams 
1765ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
17666bc75619SDan Williams 	if (!t->spa_set[0])
17676bc75619SDan Williams 		return -ENOMEM;
17686bc75619SDan Williams 
1769dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1770dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1771dafb1048SDan Williams 		if (!t->label[i])
1772dafb1048SDan Williams 			return -ENOMEM;
1773dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1774dafb1048SDan Williams 	}
1775dafb1048SDan Williams 
17767bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
17777bfe97c7SDan Williams 	if (!t->spa_set[1])
17787bfe97c7SDan Williams 		return -ENOMEM;
17797bfe97c7SDan Williams 
1780718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1781718fda67SDan Williams 		return -ENOMEM;
1782ed07c433SDan Williams 	smart_init(t);
1783f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17846bc75619SDan Williams }
17856bc75619SDan Williams 
17865dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
17875dc68e55SDan Williams {
17885dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
17895dc68e55SDan Williams 	dcr->device_id = 0;
17905dc68e55SDan Williams 	dcr->revision_id = 1;
17915dc68e55SDan Williams 	dcr->valid_fields = 1;
17925dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
17935dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
17945dc68e55SDan Williams }
17955dc68e55SDan Williams 
17966bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
17976bc75619SDan Williams {
179885d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
179985d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
18006bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
18016bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
18026bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
18036bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
18046bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
18056bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
18069d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1807f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1808d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
18096bc75619SDan Williams 
18106bc75619SDan Williams 	/*
18116bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
18126bc75619SDan Williams 	 * does not actually alias the related block-data-window
18136bc75619SDan Williams 	 * regions)
18146bc75619SDan Williams 	 */
18156b577c9dSLinda Knippers 	spa = nfit_buf;
18166bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18176bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18186bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18196bc75619SDan Williams 	spa->range_index = 0+1;
18206bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
18216bc75619SDan Williams 	spa->length = SPA0_SIZE;
1822d7d8464dSRoss Zwisler 	offset += spa->header.length;
18236bc75619SDan Williams 
18246bc75619SDan Williams 	/*
18256bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
18266bc75619SDan Williams 	 * does not actually alias the related block-data-window
18276bc75619SDan Williams 	 * regions)
18286bc75619SDan Williams 	 */
1829d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18306bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18316bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18326bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18336bc75619SDan Williams 	spa->range_index = 1+1;
18346bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
18356bc75619SDan Williams 	spa->length = SPA1_SIZE;
1836d7d8464dSRoss Zwisler 	offset += spa->header.length;
18376bc75619SDan Williams 
18386bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1839d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18406bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18416bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18426bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18436bc75619SDan Williams 	spa->range_index = 2+1;
18446bc75619SDan Williams 	spa->address = t->dcr_dma[0];
18456bc75619SDan Williams 	spa->length = DCR_SIZE;
1846d7d8464dSRoss Zwisler 	offset += spa->header.length;
18476bc75619SDan Williams 
18486bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1849d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18506bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18516bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18526bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18536bc75619SDan Williams 	spa->range_index = 3+1;
18546bc75619SDan Williams 	spa->address = t->dcr_dma[1];
18556bc75619SDan Williams 	spa->length = DCR_SIZE;
1856d7d8464dSRoss Zwisler 	offset += spa->header.length;
18576bc75619SDan Williams 
18586bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1859d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18606bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18616bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18626bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18636bc75619SDan Williams 	spa->range_index = 4+1;
18646bc75619SDan Williams 	spa->address = t->dcr_dma[2];
18656bc75619SDan Williams 	spa->length = DCR_SIZE;
1866d7d8464dSRoss Zwisler 	offset += spa->header.length;
18676bc75619SDan Williams 
18686bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1869d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18706bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18716bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18726bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18736bc75619SDan Williams 	spa->range_index = 5+1;
18746bc75619SDan Williams 	spa->address = t->dcr_dma[3];
18756bc75619SDan Williams 	spa->length = DCR_SIZE;
1876d7d8464dSRoss Zwisler 	offset += spa->header.length;
18776bc75619SDan Williams 
18786bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1879d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18806bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18816bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18826bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18836bc75619SDan Williams 	spa->range_index = 6+1;
18846bc75619SDan Williams 	spa->address = t->dimm_dma[0];
18856bc75619SDan Williams 	spa->length = DIMM_SIZE;
1886d7d8464dSRoss Zwisler 	offset += spa->header.length;
18876bc75619SDan Williams 
18886bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1889d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18906bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18916bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18926bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18936bc75619SDan Williams 	spa->range_index = 7+1;
18946bc75619SDan Williams 	spa->address = t->dimm_dma[1];
18956bc75619SDan Williams 	spa->length = DIMM_SIZE;
1896d7d8464dSRoss Zwisler 	offset += spa->header.length;
18976bc75619SDan Williams 
18986bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1899d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19006bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19016bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19026bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19036bc75619SDan Williams 	spa->range_index = 8+1;
19046bc75619SDan Williams 	spa->address = t->dimm_dma[2];
19056bc75619SDan Williams 	spa->length = DIMM_SIZE;
1906d7d8464dSRoss Zwisler 	offset += spa->header.length;
19076bc75619SDan Williams 
19086bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1909d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19106bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19116bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19126bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19136bc75619SDan Williams 	spa->range_index = 9+1;
19146bc75619SDan Williams 	spa->address = t->dimm_dma[3];
19156bc75619SDan Williams 	spa->length = DIMM_SIZE;
1916d7d8464dSRoss Zwisler 	offset += spa->header.length;
19176bc75619SDan Williams 
19186bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
19196bc75619SDan Williams 	memdev = nfit_buf + offset;
19206bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19216bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19226bc75619SDan Williams 	memdev->device_handle = handle[0];
19236bc75619SDan Williams 	memdev->physical_id = 0;
19246bc75619SDan Williams 	memdev->region_id = 0;
19256bc75619SDan Williams 	memdev->range_index = 0+1;
19263b87356fSDan Williams 	memdev->region_index = 4+1;
19276bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1928df06a2d5SDan Williams 	memdev->region_offset = 1;
19296bc75619SDan Williams 	memdev->address = 0;
19306bc75619SDan Williams 	memdev->interleave_index = 0;
19316bc75619SDan Williams 	memdev->interleave_ways = 2;
1932d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19336bc75619SDan Williams 
19346bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1935d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19366bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19376bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19386bc75619SDan Williams 	memdev->device_handle = handle[1];
19396bc75619SDan Williams 	memdev->physical_id = 1;
19406bc75619SDan Williams 	memdev->region_id = 0;
19416bc75619SDan Williams 	memdev->range_index = 0+1;
19423b87356fSDan Williams 	memdev->region_index = 5+1;
19436bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1944df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
19456bc75619SDan Williams 	memdev->address = 0;
19466bc75619SDan Williams 	memdev->interleave_index = 0;
19476bc75619SDan Williams 	memdev->interleave_ways = 2;
1948ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1949d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19506bc75619SDan Williams 
19516bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1952d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19536bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19546bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19556bc75619SDan Williams 	memdev->device_handle = handle[0];
19566bc75619SDan Williams 	memdev->physical_id = 0;
19576bc75619SDan Williams 	memdev->region_id = 1;
19586bc75619SDan Williams 	memdev->range_index = 1+1;
19593b87356fSDan Williams 	memdev->region_index = 4+1;
19606bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1961df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
19626bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19636bc75619SDan Williams 	memdev->interleave_index = 0;
19646bc75619SDan Williams 	memdev->interleave_ways = 4;
1965ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1966d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19676bc75619SDan Williams 
19686bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1969d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19706bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19716bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19726bc75619SDan Williams 	memdev->device_handle = handle[1];
19736bc75619SDan Williams 	memdev->physical_id = 1;
19746bc75619SDan Williams 	memdev->region_id = 1;
19756bc75619SDan Williams 	memdev->range_index = 1+1;
19763b87356fSDan Williams 	memdev->region_index = 5+1;
19776bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1978df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
19796bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19806bc75619SDan Williams 	memdev->interleave_index = 0;
19816bc75619SDan Williams 	memdev->interleave_ways = 4;
1982d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19836bc75619SDan Williams 
19846bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1985d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19866bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19876bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19886bc75619SDan Williams 	memdev->device_handle = handle[2];
19896bc75619SDan Williams 	memdev->physical_id = 2;
19906bc75619SDan Williams 	memdev->region_id = 0;
19916bc75619SDan Williams 	memdev->range_index = 1+1;
19923b87356fSDan Williams 	memdev->region_index = 6+1;
19936bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1994df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
19956bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19966bc75619SDan Williams 	memdev->interleave_index = 0;
19976bc75619SDan Williams 	memdev->interleave_ways = 4;
1998ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1999d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20006bc75619SDan Williams 
20016bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
2002d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20036bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20046bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20056bc75619SDan Williams 	memdev->device_handle = handle[3];
20066bc75619SDan Williams 	memdev->physical_id = 3;
20076bc75619SDan Williams 	memdev->region_id = 0;
20086bc75619SDan Williams 	memdev->range_index = 1+1;
20093b87356fSDan Williams 	memdev->region_index = 7+1;
20106bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2011df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
20126bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
20136bc75619SDan Williams 	memdev->interleave_index = 0;
20146bc75619SDan Williams 	memdev->interleave_ways = 4;
2015d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20166bc75619SDan Williams 
20176bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
2018d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20196bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20206bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20216bc75619SDan Williams 	memdev->device_handle = handle[0];
20226bc75619SDan Williams 	memdev->physical_id = 0;
20236bc75619SDan Williams 	memdev->region_id = 0;
20246bc75619SDan Williams 	memdev->range_index = 2+1;
20256bc75619SDan Williams 	memdev->region_index = 0+1;
20266bc75619SDan Williams 	memdev->region_size = 0;
20276bc75619SDan Williams 	memdev->region_offset = 0;
20286bc75619SDan Williams 	memdev->address = 0;
20296bc75619SDan Williams 	memdev->interleave_index = 0;
20306bc75619SDan Williams 	memdev->interleave_ways = 1;
2031d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20326bc75619SDan Williams 
20336bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
2034d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20356bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20366bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20376bc75619SDan Williams 	memdev->device_handle = handle[1];
20386bc75619SDan Williams 	memdev->physical_id = 1;
20396bc75619SDan Williams 	memdev->region_id = 0;
20406bc75619SDan Williams 	memdev->range_index = 3+1;
20416bc75619SDan Williams 	memdev->region_index = 1+1;
20426bc75619SDan Williams 	memdev->region_size = 0;
20436bc75619SDan Williams 	memdev->region_offset = 0;
20446bc75619SDan Williams 	memdev->address = 0;
20456bc75619SDan Williams 	memdev->interleave_index = 0;
20466bc75619SDan Williams 	memdev->interleave_ways = 1;
2047d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20486bc75619SDan Williams 
20496bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
2050d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20516bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20526bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20536bc75619SDan Williams 	memdev->device_handle = handle[2];
20546bc75619SDan Williams 	memdev->physical_id = 2;
20556bc75619SDan Williams 	memdev->region_id = 0;
20566bc75619SDan Williams 	memdev->range_index = 4+1;
20576bc75619SDan Williams 	memdev->region_index = 2+1;
20586bc75619SDan Williams 	memdev->region_size = 0;
20596bc75619SDan Williams 	memdev->region_offset = 0;
20606bc75619SDan Williams 	memdev->address = 0;
20616bc75619SDan Williams 	memdev->interleave_index = 0;
20626bc75619SDan Williams 	memdev->interleave_ways = 1;
2063d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20646bc75619SDan Williams 
20656bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
2066d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20676bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20686bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20696bc75619SDan Williams 	memdev->device_handle = handle[3];
20706bc75619SDan Williams 	memdev->physical_id = 3;
20716bc75619SDan Williams 	memdev->region_id = 0;
20726bc75619SDan Williams 	memdev->range_index = 5+1;
20736bc75619SDan Williams 	memdev->region_index = 3+1;
20746bc75619SDan Williams 	memdev->region_size = 0;
20756bc75619SDan Williams 	memdev->region_offset = 0;
20766bc75619SDan Williams 	memdev->address = 0;
20776bc75619SDan Williams 	memdev->interleave_index = 0;
20786bc75619SDan Williams 	memdev->interleave_ways = 1;
2079d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20806bc75619SDan Williams 
20816bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
2082d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20836bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20846bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20856bc75619SDan Williams 	memdev->device_handle = handle[0];
20866bc75619SDan Williams 	memdev->physical_id = 0;
20876bc75619SDan Williams 	memdev->region_id = 0;
20886bc75619SDan Williams 	memdev->range_index = 6+1;
20896bc75619SDan Williams 	memdev->region_index = 0+1;
20906bc75619SDan Williams 	memdev->region_size = 0;
20916bc75619SDan Williams 	memdev->region_offset = 0;
20926bc75619SDan Williams 	memdev->address = 0;
20936bc75619SDan Williams 	memdev->interleave_index = 0;
20946bc75619SDan Williams 	memdev->interleave_ways = 1;
2095d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20966bc75619SDan Williams 
20976bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
2098d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20996bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21006bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21016bc75619SDan Williams 	memdev->device_handle = handle[1];
21026bc75619SDan Williams 	memdev->physical_id = 1;
21036bc75619SDan Williams 	memdev->region_id = 0;
21046bc75619SDan Williams 	memdev->range_index = 7+1;
21056bc75619SDan Williams 	memdev->region_index = 1+1;
21066bc75619SDan Williams 	memdev->region_size = 0;
21076bc75619SDan Williams 	memdev->region_offset = 0;
21086bc75619SDan Williams 	memdev->address = 0;
21096bc75619SDan Williams 	memdev->interleave_index = 0;
21106bc75619SDan Williams 	memdev->interleave_ways = 1;
2111d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21126bc75619SDan Williams 
21136bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
2114d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21156bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21166bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21176bc75619SDan Williams 	memdev->device_handle = handle[2];
21186bc75619SDan Williams 	memdev->physical_id = 2;
21196bc75619SDan Williams 	memdev->region_id = 0;
21206bc75619SDan Williams 	memdev->range_index = 8+1;
21216bc75619SDan Williams 	memdev->region_index = 2+1;
21226bc75619SDan Williams 	memdev->region_size = 0;
21236bc75619SDan Williams 	memdev->region_offset = 0;
21246bc75619SDan Williams 	memdev->address = 0;
21256bc75619SDan Williams 	memdev->interleave_index = 0;
21266bc75619SDan Williams 	memdev->interleave_ways = 1;
2127d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21286bc75619SDan Williams 
21296bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
2130d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21316bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21326bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21336bc75619SDan Williams 	memdev->device_handle = handle[3];
21346bc75619SDan Williams 	memdev->physical_id = 3;
21356bc75619SDan Williams 	memdev->region_id = 0;
21366bc75619SDan Williams 	memdev->range_index = 9+1;
21376bc75619SDan Williams 	memdev->region_index = 3+1;
21386bc75619SDan Williams 	memdev->region_size = 0;
21396bc75619SDan Williams 	memdev->region_offset = 0;
21406bc75619SDan Williams 	memdev->address = 0;
21416bc75619SDan Williams 	memdev->interleave_index = 0;
21426bc75619SDan Williams 	memdev->interleave_ways = 1;
2143ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2144d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21456bc75619SDan Williams 
21463b87356fSDan Williams 	/* dcr-descriptor0: blk */
21476bc75619SDan Williams 	dcr = nfit_buf + offset;
21486bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2149d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21506bc75619SDan Williams 	dcr->region_index = 0+1;
21515dc68e55SDan Williams 	dcr_common_init(dcr);
21526bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2153be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21546bc75619SDan Williams 	dcr->windows = 1;
21556bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21566bc75619SDan Williams 	dcr->command_offset = 0;
21576bc75619SDan Williams 	dcr->command_size = 8;
21586bc75619SDan Williams 	dcr->status_offset = 8;
21596bc75619SDan Williams 	dcr->status_size = 4;
2160d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21616bc75619SDan Williams 
21623b87356fSDan Williams 	/* dcr-descriptor1: blk */
2163d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21646bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2165d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21666bc75619SDan Williams 	dcr->region_index = 1+1;
21675dc68e55SDan Williams 	dcr_common_init(dcr);
21686bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2169be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21706bc75619SDan Williams 	dcr->windows = 1;
21716bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21726bc75619SDan Williams 	dcr->command_offset = 0;
21736bc75619SDan Williams 	dcr->command_size = 8;
21746bc75619SDan Williams 	dcr->status_offset = 8;
21756bc75619SDan Williams 	dcr->status_size = 4;
2176d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21776bc75619SDan Williams 
21783b87356fSDan Williams 	/* dcr-descriptor2: blk */
2179d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21806bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2181d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21826bc75619SDan Williams 	dcr->region_index = 2+1;
21835dc68e55SDan Williams 	dcr_common_init(dcr);
21846bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2185be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21866bc75619SDan Williams 	dcr->windows = 1;
21876bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21886bc75619SDan Williams 	dcr->command_offset = 0;
21896bc75619SDan Williams 	dcr->command_size = 8;
21906bc75619SDan Williams 	dcr->status_offset = 8;
21916bc75619SDan Williams 	dcr->status_size = 4;
2192d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21936bc75619SDan Williams 
21943b87356fSDan Williams 	/* dcr-descriptor3: blk */
2195d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21966bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2197d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21986bc75619SDan Williams 	dcr->region_index = 3+1;
21995dc68e55SDan Williams 	dcr_common_init(dcr);
22006bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2201be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
22026bc75619SDan Williams 	dcr->windows = 1;
22036bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
22046bc75619SDan Williams 	dcr->command_offset = 0;
22056bc75619SDan Williams 	dcr->command_size = 8;
22066bc75619SDan Williams 	dcr->status_offset = 8;
22076bc75619SDan Williams 	dcr->status_size = 4;
2208d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22096bc75619SDan Williams 
22103b87356fSDan Williams 	/* dcr-descriptor0: pmem */
22113b87356fSDan Williams 	dcr = nfit_buf + offset;
22123b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22133b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22143b87356fSDan Williams 			window_size);
22153b87356fSDan Williams 	dcr->region_index = 4+1;
22165dc68e55SDan Williams 	dcr_common_init(dcr);
22173b87356fSDan Williams 	dcr->serial_number = ~handle[0];
22183b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22193b87356fSDan Williams 	dcr->windows = 0;
2220d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22213b87356fSDan Williams 
22223b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2223d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22243b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22253b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22263b87356fSDan Williams 			window_size);
22273b87356fSDan Williams 	dcr->region_index = 5+1;
22285dc68e55SDan Williams 	dcr_common_init(dcr);
22293b87356fSDan Williams 	dcr->serial_number = ~handle[1];
22303b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22313b87356fSDan Williams 	dcr->windows = 0;
2232d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22333b87356fSDan Williams 
22343b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2235d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22363b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22373b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22383b87356fSDan Williams 			window_size);
22393b87356fSDan Williams 	dcr->region_index = 6+1;
22405dc68e55SDan Williams 	dcr_common_init(dcr);
22413b87356fSDan Williams 	dcr->serial_number = ~handle[2];
22423b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22433b87356fSDan Williams 	dcr->windows = 0;
2244d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22453b87356fSDan Williams 
22463b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2247d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22483b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22493b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22503b87356fSDan Williams 			window_size);
22513b87356fSDan Williams 	dcr->region_index = 7+1;
22525dc68e55SDan Williams 	dcr_common_init(dcr);
22533b87356fSDan Williams 	dcr->serial_number = ~handle[3];
22543b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22553b87356fSDan Williams 	dcr->windows = 0;
2256d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22573b87356fSDan Williams 
22586bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
22596bc75619SDan Williams 	bdw = nfit_buf + offset;
22606bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2261d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22626bc75619SDan Williams 	bdw->region_index = 0+1;
22636bc75619SDan Williams 	bdw->windows = 1;
22646bc75619SDan Williams 	bdw->offset = 0;
22656bc75619SDan Williams 	bdw->size = BDW_SIZE;
22666bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22676bc75619SDan Williams 	bdw->start_address = 0;
2268d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22696bc75619SDan Williams 
22706bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2271d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22726bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2273d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22746bc75619SDan Williams 	bdw->region_index = 1+1;
22756bc75619SDan Williams 	bdw->windows = 1;
22766bc75619SDan Williams 	bdw->offset = 0;
22776bc75619SDan Williams 	bdw->size = BDW_SIZE;
22786bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22796bc75619SDan Williams 	bdw->start_address = 0;
2280d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22816bc75619SDan Williams 
22826bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2283d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22846bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2285d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22866bc75619SDan Williams 	bdw->region_index = 2+1;
22876bc75619SDan Williams 	bdw->windows = 1;
22886bc75619SDan Williams 	bdw->offset = 0;
22896bc75619SDan Williams 	bdw->size = BDW_SIZE;
22906bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22916bc75619SDan Williams 	bdw->start_address = 0;
2292d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22936bc75619SDan Williams 
22946bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2295d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22966bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2297d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22986bc75619SDan Williams 	bdw->region_index = 3+1;
22996bc75619SDan Williams 	bdw->windows = 1;
23006bc75619SDan Williams 	bdw->offset = 0;
23016bc75619SDan Williams 	bdw->size = BDW_SIZE;
23026bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
23036bc75619SDan Williams 	bdw->start_address = 0;
2304d7d8464dSRoss Zwisler 	offset += bdw->header.length;
23056bc75619SDan Williams 
23069d27a87eSDan Williams 	/* flush0 (dimm0) */
23079d27a87eSDan Williams 	flush = nfit_buf + offset;
23089d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
230985d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23109d27a87eSDan Williams 	flush->device_handle = handle[0];
231185d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
231285d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
231385d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2314d7d8464dSRoss Zwisler 	offset += flush->header.length;
23159d27a87eSDan Williams 
23169d27a87eSDan Williams 	/* flush1 (dimm1) */
2317d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23189d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
231985d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23209d27a87eSDan Williams 	flush->device_handle = handle[1];
232185d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
232285d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
232385d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2324d7d8464dSRoss Zwisler 	offset += flush->header.length;
23259d27a87eSDan Williams 
23269d27a87eSDan Williams 	/* flush2 (dimm2) */
2327d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23289d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
232985d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23309d27a87eSDan Williams 	flush->device_handle = handle[2];
233185d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
233285d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
233385d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2334d7d8464dSRoss Zwisler 	offset += flush->header.length;
23359d27a87eSDan Williams 
23369d27a87eSDan Williams 	/* flush3 (dimm3) */
2337d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23389d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
233985d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23409d27a87eSDan Williams 	flush->device_handle = handle[3];
234185d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
234285d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
234385d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2344d7d8464dSRoss Zwisler 	offset += flush->header.length;
23459d27a87eSDan Williams 
2346f81e1d35SDave Jiang 	/* platform capabilities */
2347d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2348f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2349f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2350f81e1d35SDave Jiang 	pcap->highest_capability = 1;
23511273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2352d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2353f81e1d35SDave Jiang 
235420985164SVishal Verma 	if (t->setup_hotplug) {
23553b87356fSDan Williams 		/* dcr-descriptor4: blk */
235620985164SVishal Verma 		dcr = nfit_buf + offset;
235720985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2358d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
23593b87356fSDan Williams 		dcr->region_index = 8+1;
23605dc68e55SDan Williams 		dcr_common_init(dcr);
236120985164SVishal Verma 		dcr->serial_number = ~handle[4];
2362be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
236320985164SVishal Verma 		dcr->windows = 1;
236420985164SVishal Verma 		dcr->window_size = DCR_SIZE;
236520985164SVishal Verma 		dcr->command_offset = 0;
236620985164SVishal Verma 		dcr->command_size = 8;
236720985164SVishal Verma 		dcr->status_offset = 8;
236820985164SVishal Verma 		dcr->status_size = 4;
2369d7d8464dSRoss Zwisler 		offset += dcr->header.length;
237020985164SVishal Verma 
23713b87356fSDan Williams 		/* dcr-descriptor4: pmem */
23723b87356fSDan Williams 		dcr = nfit_buf + offset;
23733b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
23743b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
23753b87356fSDan Williams 				window_size);
23763b87356fSDan Williams 		dcr->region_index = 9+1;
23775dc68e55SDan Williams 		dcr_common_init(dcr);
23783b87356fSDan Williams 		dcr->serial_number = ~handle[4];
23793b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
23803b87356fSDan Williams 		dcr->windows = 0;
2381d7d8464dSRoss Zwisler 		offset += dcr->header.length;
23823b87356fSDan Williams 
238320985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
238420985164SVishal Verma 		bdw = nfit_buf + offset;
238520985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2386d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
23873b87356fSDan Williams 		bdw->region_index = 8+1;
238820985164SVishal Verma 		bdw->windows = 1;
238920985164SVishal Verma 		bdw->offset = 0;
239020985164SVishal Verma 		bdw->size = BDW_SIZE;
239120985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
239220985164SVishal Verma 		bdw->start_address = 0;
2393d7d8464dSRoss Zwisler 		offset += bdw->header.length;
239420985164SVishal Verma 
239520985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
239620985164SVishal Verma 		spa = nfit_buf + offset;
239720985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
239820985164SVishal Verma 		spa->header.length = sizeof(*spa);
239920985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
240020985164SVishal Verma 		spa->range_index = 10+1;
240120985164SVishal Verma 		spa->address = t->dcr_dma[4];
240220985164SVishal Verma 		spa->length = DCR_SIZE;
2403d7d8464dSRoss Zwisler 		offset += spa->header.length;
240420985164SVishal Verma 
240520985164SVishal Verma 		/*
240620985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
240720985164SVishal Verma 		 * does not actually alias the related block-data-window
240820985164SVishal Verma 		 * regions)
240920985164SVishal Verma 		 */
2410d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
241120985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
241220985164SVishal Verma 		spa->header.length = sizeof(*spa);
241320985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
241420985164SVishal Verma 		spa->range_index = 11+1;
241520985164SVishal Verma 		spa->address = t->spa_set_dma[2];
241620985164SVishal Verma 		spa->length = SPA0_SIZE;
2417d7d8464dSRoss Zwisler 		offset += spa->header.length;
241820985164SVishal Verma 
241920985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2420d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
242120985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
242220985164SVishal Verma 		spa->header.length = sizeof(*spa);
242320985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
242420985164SVishal Verma 		spa->range_index = 12+1;
242520985164SVishal Verma 		spa->address = t->dimm_dma[4];
242620985164SVishal Verma 		spa->length = DIMM_SIZE;
2427d7d8464dSRoss Zwisler 		offset += spa->header.length;
242820985164SVishal Verma 
242920985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
243020985164SVishal Verma 		memdev = nfit_buf + offset;
243120985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
243220985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
243320985164SVishal Verma 		memdev->device_handle = handle[4];
243420985164SVishal Verma 		memdev->physical_id = 4;
243520985164SVishal Verma 		memdev->region_id = 0;
243620985164SVishal Verma 		memdev->range_index = 10+1;
24373b87356fSDan Williams 		memdev->region_index = 8+1;
243820985164SVishal Verma 		memdev->region_size = 0;
243920985164SVishal Verma 		memdev->region_offset = 0;
244020985164SVishal Verma 		memdev->address = 0;
244120985164SVishal Verma 		memdev->interleave_index = 0;
244220985164SVishal Verma 		memdev->interleave_ways = 1;
2443d7d8464dSRoss Zwisler 		offset += memdev->header.length;
244420985164SVishal Verma 
2445d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2446d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
244720985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
244820985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
244920985164SVishal Verma 		memdev->device_handle = handle[4];
245020985164SVishal Verma 		memdev->physical_id = 4;
245120985164SVishal Verma 		memdev->region_id = 0;
245220985164SVishal Verma 		memdev->range_index = 11+1;
24533b87356fSDan Williams 		memdev->region_index = 9+1;
245420985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2455df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
245620985164SVishal Verma 		memdev->address = 0;
245720985164SVishal Verma 		memdev->interleave_index = 0;
245820985164SVishal Verma 		memdev->interleave_ways = 1;
2459ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2460d7d8464dSRoss Zwisler 		offset += memdev->header.length;
246120985164SVishal Verma 
24623b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2463d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
246420985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
246520985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
246620985164SVishal Verma 		memdev->device_handle = handle[4];
246720985164SVishal Verma 		memdev->physical_id = 4;
246820985164SVishal Verma 		memdev->region_id = 0;
246920985164SVishal Verma 		memdev->range_index = 12+1;
24703b87356fSDan Williams 		memdev->region_index = 8+1;
247120985164SVishal Verma 		memdev->region_size = 0;
247220985164SVishal Verma 		memdev->region_offset = 0;
247320985164SVishal Verma 		memdev->address = 0;
247420985164SVishal Verma 		memdev->interleave_index = 0;
247520985164SVishal Verma 		memdev->interleave_ways = 1;
2476d7d8464dSRoss Zwisler 		offset += memdev->header.length;
247720985164SVishal Verma 
247820985164SVishal Verma 		/* flush3 (dimm4) */
247920985164SVishal Verma 		flush = nfit_buf + offset;
248020985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
248185d3fa02SDan Williams 		flush->header.length = flush_hint_size;
248220985164SVishal Verma 		flush->device_handle = handle[4];
248385d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
248485d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
248585d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
248685d3fa02SDan Williams 				+ i * sizeof(u64);
2487d7d8464dSRoss Zwisler 		offset += flush->header.length;
24889741a559SRoss Zwisler 
24899741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
24909741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
249120985164SVishal Verma 	}
249220985164SVishal Verma 
24931526f9e2SRoss Zwisler 	t->nfit_filled = offset;
24941526f9e2SRoss Zwisler 
24959fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
24969fb1a190SDave Jiang 			SPA0_SIZE);
2497f471f1a7SDan Williams 
24986bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2499e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2500e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2501e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2502ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2503ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2504ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
25054cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2506e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2507e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2508e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2509e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
251010246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
251110246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
25129fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
25139fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
25149fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2515bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2516bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2517bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2518bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2519bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2520674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
25213c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
25223c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25233c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
25243c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
25253c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25263c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
25273c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
25283c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2529926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2530926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2531ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
2532ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2533ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
2534ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25356bc75619SDan Williams }
25366bc75619SDan Williams 
25376bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
25386bc75619SDan Williams {
25396b577c9dSLinda Knippers 	size_t offset;
25406bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
25416bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
25426bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
25436bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2544d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25456bc75619SDan Williams 
25466b577c9dSLinda Knippers 	offset = 0;
25476bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
25486bc75619SDan Williams 	spa = nfit_buf + offset;
25496bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25506bc75619SDan Williams 	spa->header.length = sizeof(*spa);
25516bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
25526bc75619SDan Williams 	spa->range_index = 0+1;
25536bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
25546bc75619SDan Williams 	spa->length = SPA2_SIZE;
2555d7d8464dSRoss Zwisler 	offset += spa->header.length;
25566bc75619SDan Williams 
25577bfe97c7SDan Williams 	/* virtual cd region */
2558d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
25597bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25607bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
25617bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
25627bfe97c7SDan Williams 	spa->range_index = 0;
25637bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
25647bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2565d7d8464dSRoss Zwisler 	offset += spa->header.length;
25667bfe97c7SDan Williams 
25676bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
25686bc75619SDan Williams 	memdev = nfit_buf + offset;
25696bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
25706bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2571dafb1048SDan Williams 	memdev->device_handle = handle[5];
25726bc75619SDan Williams 	memdev->physical_id = 0;
25736bc75619SDan Williams 	memdev->region_id = 0;
25746bc75619SDan Williams 	memdev->range_index = 0+1;
25756bc75619SDan Williams 	memdev->region_index = 0+1;
25766bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
25776bc75619SDan Williams 	memdev->region_offset = 0;
25786bc75619SDan Williams 	memdev->address = 0;
25796bc75619SDan Williams 	memdev->interleave_index = 0;
25806bc75619SDan Williams 	memdev->interleave_ways = 1;
258158138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
258258138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2583f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2584d7d8464dSRoss Zwisler 	offset += memdev->header.length;
25856bc75619SDan Williams 
25866bc75619SDan Williams 	/* dcr-descriptor0 */
25876bc75619SDan Williams 	dcr = nfit_buf + offset;
25886bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
25893b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
25903b87356fSDan Williams 			window_size);
25916bc75619SDan Williams 	dcr->region_index = 0+1;
25925dc68e55SDan Williams 	dcr_common_init(dcr);
2593dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2594be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
25956bc75619SDan Williams 	dcr->windows = 0;
2596ac40b675SDan Williams 	offset += dcr->header.length;
2597d7d8464dSRoss Zwisler 
2598ac40b675SDan Williams 	memdev = nfit_buf + offset;
2599ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2600ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2601ac40b675SDan Williams 	memdev->device_handle = handle[6];
2602ac40b675SDan Williams 	memdev->physical_id = 0;
2603ac40b675SDan Williams 	memdev->region_id = 0;
2604ac40b675SDan Williams 	memdev->range_index = 0;
2605ac40b675SDan Williams 	memdev->region_index = 0+2;
2606ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2607ac40b675SDan Williams 	memdev->region_offset = 0;
2608ac40b675SDan Williams 	memdev->address = 0;
2609ac40b675SDan Williams 	memdev->interleave_index = 0;
2610ac40b675SDan Williams 	memdev->interleave_ways = 1;
2611ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2612d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2613ac40b675SDan Williams 
2614ac40b675SDan Williams 	/* dcr-descriptor1 */
2615ac40b675SDan Williams 	dcr = nfit_buf + offset;
2616ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2617ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2618ac40b675SDan Williams 			window_size);
2619ac40b675SDan Williams 	dcr->region_index = 0+2;
2620ac40b675SDan Williams 	dcr_common_init(dcr);
2621ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2622ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2623ac40b675SDan Williams 	dcr->windows = 0;
2624d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2625ac40b675SDan Williams 
26269741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
26279741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
26289741a559SRoss Zwisler 
26291526f9e2SRoss Zwisler 	t->nfit_filled = offset;
26301526f9e2SRoss Zwisler 
26319fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
26329fb1a190SDave Jiang 			SPA2_SIZE);
2633f471f1a7SDan Williams 
2634d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2635e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2636e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2637e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2638e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2639674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
26409484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
26419484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26429484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26436bc75619SDan Williams }
26446bc75619SDan Williams 
26456bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
26466bc75619SDan Williams 		void *iobuf, u64 len, int rw)
26476bc75619SDan Williams {
26486bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
26496bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
26506bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
26516bc75619SDan Williams 	unsigned int lane;
26526bc75619SDan Williams 
26536bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
26546bc75619SDan Williams 	if (rw)
265567a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
265667a3e8feSRoss Zwisler 	else {
265767a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
265867a3e8feSRoss Zwisler 
26595deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
26605deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
266167a3e8feSRoss Zwisler 	}
26626bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
26636bc75619SDan Williams 
26646bc75619SDan Williams 	return 0;
26656bc75619SDan Williams }
26666bc75619SDan Williams 
2667a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2668a7de92daSDan Williams 
2669a7de92daSDan Williams union acpi_object *result;
2670a7de92daSDan Williams 
2671a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
267294116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2673a7de92daSDan Williams {
2674a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2675a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2676a7de92daSDan Williams 
2677a7de92daSDan Williams 	return result;
2678a7de92daSDan Williams }
2679a7de92daSDan Williams 
2680a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2681a7de92daSDan Williams {
2682a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2683a7de92daSDan Williams 	if (!result)
2684a7de92daSDan Williams 		return -ENOMEM;
2685a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2686a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2687a7de92daSDan Williams 	result->buffer.length = size;
2688a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2689a7de92daSDan Williams 	memset(buf, 0, size);
2690a7de92daSDan Williams 	return 0;
2691a7de92daSDan Williams }
2692a7de92daSDan Williams 
2693a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2694a7de92daSDan Williams {
2695a7de92daSDan Williams 	int rc, cmd_rc;
2696a7de92daSDan Williams 	struct nvdimm *nvdimm;
2697a7de92daSDan Williams 	struct acpi_device *adev;
2698a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2699a7de92daSDan Williams 	struct nd_ars_record *record;
2700a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2701a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2702a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2703a7de92daSDan Williams 	union {
2704a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2705fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2706a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2707a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2708a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2709a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2710a7de92daSDan Williams 	} cmds;
2711a7de92daSDan Williams 
2712a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2713a7de92daSDan Williams 	if (!adev)
2714a7de92daSDan Williams 		return -ENOMEM;
2715a7de92daSDan Williams 	*adev = (struct acpi_device) {
2716a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2717a7de92daSDan Williams 		.dev = {
2718a7de92daSDan Williams 			.init_name = "test-adev",
2719a7de92daSDan Williams 		},
2720a7de92daSDan Williams 	};
2721a7de92daSDan Williams 
2722a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2723a7de92daSDan Williams 	if (!acpi_desc)
2724a7de92daSDan Williams 		return -ENOMEM;
2725a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2726a7de92daSDan Williams 		.nd_desc = {
2727a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2728a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2729a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
273010246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
273110246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2732a7de92daSDan Williams 			.module = THIS_MODULE,
2733a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2734a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
27359fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
27369fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
27379fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
27389fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2739a7de92daSDan Williams 		},
2740a7de92daSDan Williams 		.dev = &adev->dev,
2741a7de92daSDan Williams 	};
2742a7de92daSDan Williams 
2743a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2744a7de92daSDan Williams 	if (!nfit_mem)
2745a7de92daSDan Williams 		return -ENOMEM;
2746a7de92daSDan Williams 
2747a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2748a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2749a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2750a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2751a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2752a7de92daSDan Williams 		.adev = adev,
2753a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2754a7de92daSDan Williams 		.dsm_mask = mask,
2755a7de92daSDan Williams 	};
2756a7de92daSDan Williams 
2757a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2758a7de92daSDan Williams 	if (!nvdimm)
2759a7de92daSDan Williams 		return -ENOMEM;
2760a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2761a7de92daSDan Williams 		.provider_data = nfit_mem,
2762a7de92daSDan Williams 		.cmd_mask = mask,
2763a7de92daSDan Williams 		.dev = {
2764a7de92daSDan Williams 			.init_name = "test-dimm",
2765a7de92daSDan Williams 		},
2766a7de92daSDan Williams 	};
2767a7de92daSDan Williams 
2768a7de92daSDan Williams 
2769a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2770a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2771a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2772a7de92daSDan Williams 		.status = 0,
2773a7de92daSDan Williams 		.config_size = SZ_128K,
2774a7de92daSDan Williams 		.max_xfer = SZ_4K,
2775a7de92daSDan Williams 	};
2776a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2777a7de92daSDan Williams 	if (rc)
2778a7de92daSDan Williams 		return rc;
2779a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2780a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2781a7de92daSDan Williams 
2782a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2783a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2784a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2785a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2786a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2787a7de92daSDan Williams 		return -EIO;
2788a7de92daSDan Williams 	}
2789a7de92daSDan Williams 
2790a7de92daSDan Williams 
2791a7de92daSDan Williams 	/* test ars_status with zero output */
2792a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2793a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2794a7de92daSDan Williams 		.out_length = 0,
2795a7de92daSDan Williams 	};
2796a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2797a7de92daSDan Williams 	if (rc)
2798a7de92daSDan Williams 		return rc;
2799a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2800a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2801a7de92daSDan Williams 
2802a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2803a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2804a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2805a7de92daSDan Williams 		return -EIO;
2806a7de92daSDan Williams 	}
2807a7de92daSDan Williams 
2808a7de92daSDan Williams 
2809a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2810a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2811a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2812a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2813a7de92daSDan Williams 	};
2814a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2815a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2816a7de92daSDan Williams 	if (rc)
2817a7de92daSDan Williams 		return rc;
2818a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2819a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2820a7de92daSDan Williams 
2821a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2822a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2823a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2824a7de92daSDan Williams 		return -EIO;
2825a7de92daSDan Williams 	}
2826a7de92daSDan Williams 
2827a7de92daSDan Williams 
2828a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2829a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2830a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2831a7de92daSDan Williams 		.out_length = cmd_size - 4,
2832a7de92daSDan Williams 	};
2833a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2834a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2835a7de92daSDan Williams 		.length = test_val,
2836a7de92daSDan Williams 	};
2837a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2838a7de92daSDan Williams 	if (rc)
2839a7de92daSDan Williams 		return rc;
2840a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2841a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2842a7de92daSDan Williams 
2843a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2844a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2845a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2846a7de92daSDan Williams 		return -EIO;
2847a7de92daSDan Williams 	}
2848a7de92daSDan Williams 
2849a7de92daSDan Williams 
2850a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2851a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2852a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2853a7de92daSDan Williams 		.out_length = cmd_size,
2854a7de92daSDan Williams 	};
2855a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2856a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2857a7de92daSDan Williams 		.length = test_val,
2858a7de92daSDan Williams 	};
2859a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2860a7de92daSDan Williams 	if (rc)
2861a7de92daSDan Williams 		return rc;
2862a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2863a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2864a7de92daSDan Williams 
2865a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2866a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2867a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2868a7de92daSDan Williams 		return -EIO;
2869a7de92daSDan Williams 	}
2870a7de92daSDan Williams 
2871a7de92daSDan Williams 
2872a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2873a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2874a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2875a7de92daSDan Williams 		.status = 1 << 16,
2876a7de92daSDan Williams 	};
2877a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2878a7de92daSDan Williams 	if (rc)
2879a7de92daSDan Williams 		return rc;
2880a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2881a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2882a7de92daSDan Williams 
2883a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2884a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2885a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2886a7de92daSDan Williams 		return -EIO;
2887a7de92daSDan Williams 	}
2888a7de92daSDan Williams 
2889fb2a1748SDan Williams 	/* test clear error */
2890fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2891fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2892fb2a1748SDan Williams 		.length = 512,
2893fb2a1748SDan Williams 		.cleared = 512,
2894fb2a1748SDan Williams 	};
2895fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2896fb2a1748SDan Williams 	if (rc)
2897fb2a1748SDan Williams 		return rc;
2898fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2899fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2900fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2901fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2902fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2903fb2a1748SDan Williams 		return -EIO;
2904fb2a1748SDan Williams 	}
2905fb2a1748SDan Williams 
2906a7de92daSDan Williams 	return 0;
2907a7de92daSDan Williams }
2908a7de92daSDan Williams 
29096bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
29106bc75619SDan Williams {
29116bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
29126bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
29136bc75619SDan Williams 	struct device *dev = &pdev->dev;
29146bc75619SDan Williams 	struct nfit_test *nfit_test;
2915231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2916c14a868aSDan Williams 	union acpi_object *obj;
29176bc75619SDan Williams 	int rc;
29186bc75619SDan Williams 
2919a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2920a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2921a7de92daSDan Williams 		if (rc)
2922a7de92daSDan Williams 			return rc;
2923a7de92daSDan Williams 	}
2924a7de92daSDan Williams 
29256bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
29266bc75619SDan Williams 
29276bc75619SDan Williams 	/* common alloc */
29286bc75619SDan Williams 	if (nfit_test->num_dcr) {
29296bc75619SDan Williams 		int num = nfit_test->num_dcr;
29306bc75619SDan Williams 
29316bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
29326bc75619SDan Williams 				GFP_KERNEL);
29336bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29346bc75619SDan Williams 				GFP_KERNEL);
29359d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
29369d27a87eSDan Williams 				GFP_KERNEL);
29379d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29389d27a87eSDan Williams 				GFP_KERNEL);
29396bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
29406bc75619SDan Williams 				GFP_KERNEL);
29416bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
29426bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29436bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
29446bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
29456bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
29466bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2947ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2948ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2949ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2950ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2951ed07c433SDan Williams 				GFP_KERNEL);
2952bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2953bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
29546bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
29556bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
29569d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2957bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2958bfbaa952SDave Jiang 				&& nfit_test->fw)
29596bc75619SDan Williams 			/* pass */;
29606bc75619SDan Williams 		else
29616bc75619SDan Williams 			return -ENOMEM;
29626bc75619SDan Williams 	}
29636bc75619SDan Williams 
29646bc75619SDan Williams 	if (nfit_test->num_pm) {
29656bc75619SDan Williams 		int num = nfit_test->num_pm;
29666bc75619SDan Williams 
29676bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
29686bc75619SDan Williams 				GFP_KERNEL);
29696bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
29706bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29716bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
29726bc75619SDan Williams 			/* pass */;
29736bc75619SDan Williams 		else
29746bc75619SDan Williams 			return -ENOMEM;
29756bc75619SDan Williams 	}
29766bc75619SDan Williams 
29776bc75619SDan Williams 	/* per-nfit specific alloc */
29786bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
29796bc75619SDan Williams 		return -ENOMEM;
29806bc75619SDan Williams 
29816bc75619SDan Williams 	nfit_test->setup(nfit_test);
29826bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2983a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
29846bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
29856bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2986a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2987bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2988a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
29896bc75619SDan Williams 
2990e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
29911526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
299258cd71b4SDan Williams 	if (rc)
299320985164SVishal Verma 		return rc;
299420985164SVishal Verma 
2995fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2996fbabd829SDan Williams 	if (rc)
2997fbabd829SDan Williams 		return rc;
2998fbabd829SDan Williams 
299920985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
300020985164SVishal Verma 		return 0;
300120985164SVishal Verma 
300220985164SVishal Verma 	nfit_test->setup_hotplug = 1;
300320985164SVishal Verma 	nfit_test->setup(nfit_test);
300420985164SVishal Verma 
3005c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3006c14a868aSDan Williams 	if (!obj)
3007c14a868aSDan Williams 		return -ENOMEM;
3008c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
3009c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
3010c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
3011c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
3012c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
3013231bf117SDan Williams 
3014231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
3015231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
3016231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
3017231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
3018231bf117SDan Williams 		int i;
3019231bf117SDan Williams 
3020af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
3021231bf117SDan Williams 			if (nfit_handle == handle[i])
3022231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
3023231bf117SDan Williams 						nfit_mem);
3024231bf117SDan Williams 	}
3025231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
30266bc75619SDan Williams 
30276bc75619SDan Williams 	return 0;
30286bc75619SDan Williams }
30296bc75619SDan Williams 
30306bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
30316bc75619SDan Williams {
30326bc75619SDan Williams 	return 0;
30336bc75619SDan Williams }
30346bc75619SDan Williams 
30356bc75619SDan Williams static void nfit_test_release(struct device *dev)
30366bc75619SDan Williams {
30376bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
30386bc75619SDan Williams 
30396bc75619SDan Williams 	kfree(nfit_test);
30406bc75619SDan Williams }
30416bc75619SDan Williams 
30426bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
30436bc75619SDan Williams 	{ KBUILD_MODNAME },
30446bc75619SDan Williams 	{ },
30456bc75619SDan Williams };
30466bc75619SDan Williams 
30476bc75619SDan Williams static struct platform_driver nfit_test_driver = {
30486bc75619SDan Williams 	.probe = nfit_test_probe,
30496bc75619SDan Williams 	.remove = nfit_test_remove,
30506bc75619SDan Williams 	.driver = {
30516bc75619SDan Williams 		.name = KBUILD_MODNAME,
30526bc75619SDan Williams 	},
30536bc75619SDan Williams 	.id_table = nfit_test_id,
30546bc75619SDan Williams };
30556bc75619SDan Williams 
30565d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
30575d8beee2SDan Williams 
30585d8beee2SDan Williams enum INJECT {
30595d8beee2SDan Williams 	INJECT_NONE,
30605d8beee2SDan Williams 	INJECT_SRC,
30615d8beee2SDan Williams 	INJECT_DST,
30625d8beee2SDan Williams };
30635d8beee2SDan Williams 
30645d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
30655d8beee2SDan Williams {
30665d8beee2SDan Williams 	size_t i;
30675d8beee2SDan Williams 
30685d8beee2SDan Williams 	memset(dst, 0xff, size);
30695d8beee2SDan Williams 	for (i = 0; i < size; i++)
30705d8beee2SDan Williams 		src[i] = (char) i;
30715d8beee2SDan Williams }
30725d8beee2SDan Williams 
30735d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
30745d8beee2SDan Williams 		size_t size, unsigned long rem)
30755d8beee2SDan Williams {
30765d8beee2SDan Williams 	size_t i;
30775d8beee2SDan Williams 
30785d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
30795d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
30805d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
30815d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
30825d8beee2SDan Williams 					(unsigned char) i);
30835d8beee2SDan Williams 			return false;
30845d8beee2SDan Williams 		}
30855d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
30865d8beee2SDan Williams 		if (dst[i] != 0xffU) {
30875d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
30885d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
30895d8beee2SDan Williams 			return false;
30905d8beee2SDan Williams 		}
30915d8beee2SDan Williams 	return true;
30925d8beee2SDan Williams }
30935d8beee2SDan Williams 
30945d8beee2SDan Williams void mcsafe_test(void)
30955d8beee2SDan Williams {
30965d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
30975d8beee2SDan Williams 	enum INJECT inj;
30985d8beee2SDan Williams 
30995d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
31005d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
31015d8beee2SDan Williams 	} else {
31025d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
31035d8beee2SDan Williams 		return;
31045d8beee2SDan Williams 	}
31055d8beee2SDan Williams 
31065d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
31075d8beee2SDan Williams 		int i;
31085d8beee2SDan Williams 
31095d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
31105d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
31115d8beee2SDan Williams 			unsigned long expect, rem;
31125d8beee2SDan Williams 			void *src, *dst;
31135d8beee2SDan Williams 			bool valid;
31145d8beee2SDan Williams 
31155d8beee2SDan Williams 			switch (inj) {
31165d8beee2SDan Williams 			case INJECT_NONE:
31175d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31185d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31195d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31205d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31215d8beee2SDan Williams 				expect = 0;
31225d8beee2SDan Williams 				break;
31235d8beee2SDan Williams 			case INJECT_SRC:
31245d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
31255d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31265d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31275d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31285d8beee2SDan Williams 				expect = 512 - i;
31295d8beee2SDan Williams 				break;
31305d8beee2SDan Williams 			case INJECT_DST:
31315d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31325d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
31335d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
31345d8beee2SDan Williams 				src = &mcsafe_buf[1024];
31355d8beee2SDan Williams 				expect = 512 - i;
31365d8beee2SDan Williams 				break;
31375d8beee2SDan Williams 			}
31385d8beee2SDan Williams 
31395d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
31405d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
31415d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
31425d8beee2SDan Williams 			if (rem == expect && valid)
31435d8beee2SDan Williams 				continue;
31445d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
31455d8beee2SDan Williams 					__func__,
31465d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
31475d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
31485d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
31495d8beee2SDan Williams 					expect);
31505d8beee2SDan Williams 		}
31515d8beee2SDan Williams 	}
31525d8beee2SDan Williams 
31535d8beee2SDan Williams 	mcsafe_inject_src(NULL);
31545d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
31555d8beee2SDan Williams }
31565d8beee2SDan Williams 
31576bc75619SDan Williams static __init int nfit_test_init(void)
31586bc75619SDan Williams {
31596bc75619SDan Williams 	int rc, i;
31606bc75619SDan Williams 
31610fb5c8dfSDan Williams 	pmem_test();
31620fb5c8dfSDan Williams 	libnvdimm_test();
31630fb5c8dfSDan Williams 	acpi_nfit_test();
31640fb5c8dfSDan Williams 	device_dax_test();
31655d8beee2SDan Williams 	mcsafe_test();
316692f6f2d7SVishal Verma 	dax_pmem_test();
316792f6f2d7SVishal Verma 	dax_pmem_core_test();
316892f6f2d7SVishal Verma 	dax_pmem_compat_test();
31690fb5c8dfSDan Williams 
3170a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3171231bf117SDan Williams 
31729fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
31739fb1a190SDave Jiang 	if (!nfit_wq)
31749fb1a190SDave Jiang 		return -ENOMEM;
31759fb1a190SDave Jiang 
3176a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
3177a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
3178a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
3179a7de92daSDan Williams 		goto err_register;
3180a7de92daSDan Williams 	}
31816bc75619SDan Williams 
3182e3f5df76SDan Williams 	nfit_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
3183e3f5df76SDan Williams 	if (!nfit_pool) {
3184e3f5df76SDan Williams 		rc = -ENOMEM;
3185e3f5df76SDan Williams 		goto err_register;
3186e3f5df76SDan Williams 	}
3187e3f5df76SDan Williams 
3188e3f5df76SDan Williams 	if (gen_pool_add(nfit_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
3189e3f5df76SDan Williams 		rc = -ENOMEM;
3190e3f5df76SDan Williams 		goto err_register;
3191e3f5df76SDan Williams 	}
3192e3f5df76SDan Williams 
31936bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
31946bc75619SDan Williams 		struct nfit_test *nfit_test;
31956bc75619SDan Williams 		struct platform_device *pdev;
31966bc75619SDan Williams 
31976bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
31986bc75619SDan Williams 		if (!nfit_test) {
31996bc75619SDan Williams 			rc = -ENOMEM;
32006bc75619SDan Williams 			goto err_register;
32016bc75619SDan Williams 		}
32026bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
32039fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
32046bc75619SDan Williams 		switch (i) {
32056bc75619SDan Williams 		case 0:
32066bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3207dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
32086bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
32096bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
32106bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
32116bc75619SDan Williams 			break;
32126bc75619SDan Williams 		case 1:
3213a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3214dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3215ac40b675SDan Williams 			nfit_test->num_dcr = 2;
32166bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
32176bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
32186bc75619SDan Williams 			break;
32196bc75619SDan Williams 		default:
32206bc75619SDan Williams 			rc = -EINVAL;
32216bc75619SDan Williams 			goto err_register;
32226bc75619SDan Williams 		}
32236bc75619SDan Williams 		pdev = &nfit_test->pdev;
32246bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
32256bc75619SDan Williams 		pdev->id = i;
32266bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
32276bc75619SDan Williams 		rc = platform_device_register(pdev);
32286bc75619SDan Williams 		if (rc) {
32296bc75619SDan Williams 			put_device(&pdev->dev);
32306bc75619SDan Williams 			goto err_register;
32316bc75619SDan Williams 		}
32328b06b884SDan Williams 		get_device(&pdev->dev);
32336bc75619SDan Williams 
32346bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
32356bc75619SDan Williams 		if (rc)
32366bc75619SDan Williams 			goto err_register;
32376bc75619SDan Williams 
32386bc75619SDan Williams 		instances[i] = nfit_test;
32399fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
32406bc75619SDan Williams 	}
32416bc75619SDan Williams 
32426bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
32436bc75619SDan Williams 	if (rc)
32446bc75619SDan Williams 		goto err_register;
32456bc75619SDan Williams 	return 0;
32466bc75619SDan Williams 
32476bc75619SDan Williams  err_register:
3248e3f5df76SDan Williams 	if (nfit_pool)
3249e3f5df76SDan Williams 		gen_pool_destroy(nfit_pool);
3250e3f5df76SDan Williams 
32519fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32526bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32536bc75619SDan Williams 		if (instances[i])
32546bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
32556bc75619SDan Williams 	nfit_test_teardown();
32568b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32578b06b884SDan Williams 		if (instances[i])
32588b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
32598b06b884SDan Williams 
32606bc75619SDan Williams 	return rc;
32616bc75619SDan Williams }
32626bc75619SDan Williams 
32636bc75619SDan Williams static __exit void nfit_test_exit(void)
32646bc75619SDan Williams {
32656bc75619SDan Williams 	int i;
32666bc75619SDan Williams 
32679fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
32689fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32696bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32706bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
32718b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
32726bc75619SDan Williams 	nfit_test_teardown();
32738b06b884SDan Williams 
3274e3f5df76SDan Williams 	gen_pool_destroy(nfit_pool);
3275e3f5df76SDan Williams 
32768b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32778b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3278231bf117SDan Williams 	class_destroy(nfit_test_dimm);
32796bc75619SDan Williams }
32806bc75619SDan Williams 
32816bc75619SDan Williams module_init(nfit_test_init);
32826bc75619SDan Williams module_exit(nfit_test_exit);
32836bc75619SDan Williams MODULE_LICENSE("GPL v2");
32846bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3285