xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 24770658dc03bc568dd217b470cba827aeaed582)
15b497af4SThomas 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 		dev_dbg(dev, "%s: transition out verify\n", __func__);
432bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
4335518ba4eSDan Williams 		/* fall through */
434bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
435bfbaa952SDave Jiang 		nd_cmd->status = 0;
436bfbaa952SDave Jiang 		/* bogus test version */
437bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
438bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
439bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
440bfbaa952SDave Jiang 		break;
441bfbaa952SDave Jiang 
442bfbaa952SDave Jiang 	default: /* we should never get here */
443bfbaa952SDave Jiang 		return -EINVAL;
444bfbaa952SDave Jiang 	}
445bfbaa952SDave Jiang 
446bfbaa952SDave Jiang 	return 0;
447bfbaa952SDave Jiang }
448bfbaa952SDave Jiang 
44939c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4506bc75619SDan Williams 		unsigned int buf_len)
4516bc75619SDan Williams {
4526bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4536bc75619SDan Williams 		return -EINVAL;
45439c686b8SVishal Verma 
4556bc75619SDan Williams 	nd_cmd->status = 0;
4566bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4576bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
45839c686b8SVishal Verma 
45939c686b8SVishal Verma 	return 0;
4606bc75619SDan Williams }
46139c686b8SVishal Verma 
46239c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
46339c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
46439c686b8SVishal Verma {
4656bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
46639c686b8SVishal Verma 	int rc;
4676bc75619SDan Williams 
4686bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4696bc75619SDan Williams 		return -EINVAL;
4706bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4716bc75619SDan Williams 		return -EINVAL;
4726bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4736bc75619SDan Williams 		return -EINVAL;
4746bc75619SDan Williams 
4756bc75619SDan Williams 	nd_cmd->status = 0;
4766bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47739c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4786bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
47939c686b8SVishal Verma 
48039c686b8SVishal Verma 	return rc;
4816bc75619SDan Williams }
48239c686b8SVishal Verma 
48339c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
48439c686b8SVishal Verma 		unsigned int buf_len, void *label)
48539c686b8SVishal Verma {
4866bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4876bc75619SDan Williams 	u32 *status;
48839c686b8SVishal Verma 	int rc;
4896bc75619SDan Williams 
4906bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4916bc75619SDan Williams 		return -EINVAL;
4926bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4936bc75619SDan Williams 		return -EINVAL;
4946bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4956bc75619SDan Williams 		return -EINVAL;
4966bc75619SDan Williams 
49739c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4986bc75619SDan Williams 	*status = 0;
4996bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
50039c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
5016bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
50239c686b8SVishal Verma 
50339c686b8SVishal Verma 	return rc;
5046bc75619SDan Williams }
50539c686b8SVishal Verma 
506d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
507747ffe11SDan Williams 
50839c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
50939c686b8SVishal Verma 		unsigned int buf_len)
51039c686b8SVishal Verma {
5119fb1a190SDave Jiang 	int ars_recs;
5129fb1a190SDave Jiang 
51339c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
51439c686b8SVishal Verma 		return -EINVAL;
51539c686b8SVishal Verma 
5169fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5179fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5189fb1a190SDave Jiang 
519747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5209fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
52139c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
522d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
52339c686b8SVishal Verma 
52439c686b8SVishal Verma 	return 0;
52539c686b8SVishal Verma }
52639c686b8SVishal Verma 
5279fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5289fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
52939c686b8SVishal Verma {
530f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
531f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5329fb1a190SDave Jiang 	struct badrange_entry *be;
5339fb1a190SDave Jiang 	u64 end = addr + len - 1;
5349fb1a190SDave Jiang 	int i = 0;
535f471f1a7SDan Williams 
536f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
537f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
538f471f1a7SDan Williams 	ars_status->status = 0;
539f471f1a7SDan Williams 	ars_status->address = addr;
540f471f1a7SDan Williams 	ars_status->length = len;
541f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5429fb1a190SDave Jiang 
5439fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5449fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5459fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5469fb1a190SDave Jiang 		u64 rstart, rend;
5479fb1a190SDave Jiang 
5489fb1a190SDave Jiang 		/* skip entries outside the range */
5499fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5509fb1a190SDave Jiang 			continue;
5519fb1a190SDave Jiang 
5529fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5539fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5549fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
555f471f1a7SDan Williams 		ars_record->handle = 0;
5569fb1a190SDave Jiang 		ars_record->err_address = rstart;
5579fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5589fb1a190SDave Jiang 		i++;
5599fb1a190SDave Jiang 	}
5609fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5619fb1a190SDave Jiang 	ars_status->num_records = i;
5629fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5639fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
564f471f1a7SDan Williams }
565f471f1a7SDan Williams 
5669fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5679fb1a190SDave Jiang 		struct ars_state *ars_state,
568f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
569f471f1a7SDan Williams 		int *cmd_rc)
570f471f1a7SDan Williams {
571f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
57239c686b8SVishal Verma 		return -EINVAL;
57339c686b8SVishal Verma 
574f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
575f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
576f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
577f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
578f471f1a7SDan Williams 	} else {
579f471f1a7SDan Williams 		ars_start->status = 0;
580f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5819fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
582f471f1a7SDan Williams 				ars_start->length);
583f471f1a7SDan Williams 		*cmd_rc = 0;
584f471f1a7SDan Williams 	}
585f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
58639c686b8SVishal Verma 
58739c686b8SVishal Verma 	return 0;
58839c686b8SVishal Verma }
58939c686b8SVishal Verma 
590f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
591f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
592f471f1a7SDan Williams 		int *cmd_rc)
59339c686b8SVishal Verma {
594f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
59539c686b8SVishal Verma 		return -EINVAL;
59639c686b8SVishal Verma 
597f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
598f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
599f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
600f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
601f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
602f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
603f471f1a7SDan Williams 	} else {
604f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
605f471f1a7SDan Williams 				ars_state->ars_status->out_length);
606f471f1a7SDan Williams 		*cmd_rc = 0;
607f471f1a7SDan Williams 	}
608f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
60939c686b8SVishal Verma 	return 0;
61039c686b8SVishal Verma }
61139c686b8SVishal Verma 
6125e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6135e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
614d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
615d4f32367SDan Williams {
616d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
617d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
618d4f32367SDan Williams 		return -EINVAL;
619d4f32367SDan Williams 
620d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
621d4f32367SDan Williams 		return -EINVAL;
622d4f32367SDan Williams 
6235e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
624d4f32367SDan Williams 	clear_err->status = 0;
625d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
626d4f32367SDan Williams 	*cmd_rc = 0;
627d4f32367SDan Williams 	return 0;
628d4f32367SDan Williams }
629d4f32367SDan Williams 
63010246dc8SYasunori Goto struct region_search_spa {
63110246dc8SYasunori Goto 	u64 addr;
63210246dc8SYasunori Goto 	struct nd_region *region;
63310246dc8SYasunori Goto };
63410246dc8SYasunori Goto 
63510246dc8SYasunori Goto static int is_region_device(struct device *dev)
63610246dc8SYasunori Goto {
63710246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
63810246dc8SYasunori Goto }
63910246dc8SYasunori Goto 
64010246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
64110246dc8SYasunori Goto {
64210246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
64310246dc8SYasunori Goto 	struct nd_region *nd_region;
64410246dc8SYasunori Goto 	resource_size_t ndr_end;
64510246dc8SYasunori Goto 
64610246dc8SYasunori Goto 	if (!is_region_device(dev))
64710246dc8SYasunori Goto 		return 0;
64810246dc8SYasunori Goto 
64910246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
65010246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
65110246dc8SYasunori Goto 
65210246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
65310246dc8SYasunori Goto 		ctx->region = nd_region;
65410246dc8SYasunori Goto 		return 1;
65510246dc8SYasunori Goto 	}
65610246dc8SYasunori Goto 
65710246dc8SYasunori Goto 	return 0;
65810246dc8SYasunori Goto }
65910246dc8SYasunori Goto 
66010246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
66110246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
66210246dc8SYasunori Goto {
66310246dc8SYasunori Goto 	int ret;
66410246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
66510246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
66610246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
66710246dc8SYasunori Goto 	struct region_search_spa ctx = {
66810246dc8SYasunori Goto 		.addr = spa->spa,
66910246dc8SYasunori Goto 		.region = NULL,
67010246dc8SYasunori Goto 	};
67110246dc8SYasunori Goto 	u64 dpa;
67210246dc8SYasunori Goto 
67310246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
67410246dc8SYasunori Goto 				nfit_test_search_region_spa);
67510246dc8SYasunori Goto 
67610246dc8SYasunori Goto 	if (!ret)
67710246dc8SYasunori Goto 		return -ENODEV;
67810246dc8SYasunori Goto 
67910246dc8SYasunori Goto 	nd_region = ctx.region;
68010246dc8SYasunori Goto 
68110246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
68210246dc8SYasunori Goto 
68310246dc8SYasunori Goto 	/*
68410246dc8SYasunori Goto 	 * last dimm is selected for test
68510246dc8SYasunori Goto 	 */
68610246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
68710246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
68810246dc8SYasunori Goto 
68910246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
69010246dc8SYasunori Goto 	spa->num_nvdimms = 1;
69110246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
69210246dc8SYasunori Goto 
69310246dc8SYasunori Goto 	return 0;
69410246dc8SYasunori Goto }
69510246dc8SYasunori Goto 
69610246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
69710246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
69810246dc8SYasunori Goto {
69910246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
70010246dc8SYasunori Goto 		return -EINVAL;
70110246dc8SYasunori Goto 
70210246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
70310246dc8SYasunori Goto 		spa->status = 2;
70410246dc8SYasunori Goto 
70510246dc8SYasunori Goto 	return 0;
70610246dc8SYasunori Goto }
70710246dc8SYasunori Goto 
708ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
709ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
710baa51277SDan Williams {
711baa51277SDan Williams 	if (buf_len < sizeof(*smart))
712baa51277SDan Williams 		return -EINVAL;
713ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
714baa51277SDan Williams 	return 0;
715baa51277SDan Williams }
716baa51277SDan Williams 
717cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
718ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
719ed07c433SDan Williams 		unsigned int buf_len,
720ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
721baa51277SDan Williams {
722baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
723baa51277SDan Williams 		return -EINVAL;
724ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
725ed07c433SDan Williams 	return 0;
726ed07c433SDan Williams }
727ed07c433SDan Williams 
728ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
729ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
730ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
731ed07c433SDan Williams {
732ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
733ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
734ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
735ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
736ed07c433SDan Williams 			smart->ctrl_temperature);
737ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
738ed07c433SDan Williams 				&& smart->spares
739ed07c433SDan Williams 				<= thresh->spares)
740ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
741ed07c433SDan Williams 				&& smart->media_temperature
742ed07c433SDan Williams 				>= thresh->media_temperature)
743ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
744ed07c433SDan Williams 				&& smart->ctrl_temperature
7454cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7464cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7474cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
748ed07c433SDan Williams 		device_lock(bus_dev);
749ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
750ed07c433SDan Williams 		device_unlock(bus_dev);
751ed07c433SDan Williams 	}
752ed07c433SDan Williams }
753ed07c433SDan Williams 
754ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
755ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
756ed07c433SDan Williams 		unsigned int buf_len,
757ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
758ed07c433SDan Williams 		struct nd_intel_smart *smart,
759ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
760ed07c433SDan Williams {
761ed07c433SDan Williams 	unsigned int size;
762ed07c433SDan Williams 
763ed07c433SDan Williams 	size = sizeof(*in) - 4;
764ed07c433SDan Williams 	if (buf_len < size)
765ed07c433SDan Williams 		return -EINVAL;
766ed07c433SDan Williams 	memcpy(thresh->data, in, size);
767ed07c433SDan Williams 	in->status = 0;
768ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
769ed07c433SDan Williams 
770baa51277SDan Williams 	return 0;
771baa51277SDan Williams }
772baa51277SDan Williams 
7734cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7744cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7754cf260fcSVishal Verma 		unsigned int buf_len,
7764cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7774cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7784cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7794cf260fcSVishal Verma {
7804cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7814cf260fcSVishal Verma 		return -EINVAL;
7824cf260fcSVishal Verma 
783b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7844cf260fcSVishal Verma 		if (inj->mtemp_enable)
7854cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
786b4d4702fSVishal Verma 		else
787b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
788b4d4702fSVishal Verma 	}
789b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7904cf260fcSVishal Verma 		if (inj->spare_enable)
7914cf260fcSVishal Verma 			smart->spares = inj->spares;
792b4d4702fSVishal Verma 		else
793b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
794b4d4702fSVishal Verma 	}
795b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
7964cf260fcSVishal Verma 		if (inj->fatal_enable)
7974cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
798b4d4702fSVishal Verma 		else
799b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
800b4d4702fSVishal Verma 	}
801b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
8024cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
8034cf260fcSVishal Verma 			smart->shutdown_state = 1;
8044cf260fcSVishal Verma 			smart->shutdown_count++;
805b4d4702fSVishal Verma 		} else
806b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8074cf260fcSVishal Verma 	}
8084cf260fcSVishal Verma 	inj->status = 0;
8094cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8104cf260fcSVishal Verma 
8114cf260fcSVishal Verma 	return 0;
8124cf260fcSVishal Verma }
8134cf260fcSVishal Verma 
8149fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8159fb1a190SDave Jiang {
8169fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8179fb1a190SDave Jiang 
8189fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8199fb1a190SDave Jiang }
8209fb1a190SDave Jiang 
8219fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8229fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8239fb1a190SDave Jiang {
8249fb1a190SDave Jiang 	int rc;
8259fb1a190SDave Jiang 
82641cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8279fb1a190SDave Jiang 		rc = -EINVAL;
8289fb1a190SDave Jiang 		goto err;
8299fb1a190SDave Jiang 	}
8309fb1a190SDave Jiang 
8319fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8329fb1a190SDave Jiang 		rc = -EINVAL;
8339fb1a190SDave Jiang 		goto err;
8349fb1a190SDave Jiang 	}
8359fb1a190SDave Jiang 
8369fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8379fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8389fb1a190SDave Jiang 	if (rc < 0)
8399fb1a190SDave Jiang 		goto err;
8409fb1a190SDave Jiang 
8419fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8429fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8439fb1a190SDave Jiang 
8449fb1a190SDave Jiang 	err_inj->status = 0;
8459fb1a190SDave Jiang 	return 0;
8469fb1a190SDave Jiang 
8479fb1a190SDave Jiang err:
8489fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8499fb1a190SDave Jiang 	return rc;
8509fb1a190SDave Jiang }
8519fb1a190SDave Jiang 
8529fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8539fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8549fb1a190SDave Jiang {
8559fb1a190SDave Jiang 	int rc;
8569fb1a190SDave Jiang 
85741cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8589fb1a190SDave Jiang 		rc = -EINVAL;
8599fb1a190SDave Jiang 		goto err;
8609fb1a190SDave Jiang 	}
8619fb1a190SDave Jiang 
8629fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8639fb1a190SDave Jiang 		rc = -EINVAL;
8649fb1a190SDave Jiang 		goto err;
8659fb1a190SDave Jiang 	}
8669fb1a190SDave Jiang 
8679fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8689fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8699fb1a190SDave Jiang 
8709fb1a190SDave Jiang 	err_clr->status = 0;
8719fb1a190SDave Jiang 	return 0;
8729fb1a190SDave Jiang 
8739fb1a190SDave Jiang err:
8749fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8759fb1a190SDave Jiang 	return rc;
8769fb1a190SDave Jiang }
8779fb1a190SDave Jiang 
8789fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8799fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8809fb1a190SDave Jiang 		unsigned int buf_len)
8819fb1a190SDave Jiang {
8829fb1a190SDave Jiang 	struct badrange_entry *be;
8839fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8849fb1a190SDave Jiang 	int i = 0;
8859fb1a190SDave Jiang 
8869fb1a190SDave Jiang 	err_stat->status = 0;
8879fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8889fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8899fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8909fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8919fb1a190SDave Jiang 		i++;
8929fb1a190SDave Jiang 		if (i > max)
8939fb1a190SDave Jiang 			break;
8949fb1a190SDave Jiang 	}
8959fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8969fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8979fb1a190SDave Jiang 
8989fb1a190SDave Jiang 	return 0;
8999fb1a190SDave Jiang }
9009fb1a190SDave Jiang 
901674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
902674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
903674d8bdeSDave Jiang {
904674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
905674d8bdeSDave Jiang 
906674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
907674d8bdeSDave Jiang 		return -EINVAL;
908674d8bdeSDave Jiang 
909674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
910674d8bdeSDave Jiang 	case 0:
911674d8bdeSDave Jiang 		nd_cmd->status = 0;
912674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
913674d8bdeSDave Jiang 				__func__);
914674d8bdeSDave Jiang 		break;
915674d8bdeSDave Jiang 	case 1:
916674d8bdeSDave Jiang 		nd_cmd->status = 0;
917674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
918674d8bdeSDave Jiang 				__func__);
919674d8bdeSDave Jiang 		break;
920674d8bdeSDave Jiang 	default:
921674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
922674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
923674d8bdeSDave Jiang 		break;
924674d8bdeSDave Jiang 	}
925674d8bdeSDave Jiang 
926674d8bdeSDave Jiang 
927674d8bdeSDave Jiang 	return 0;
928674d8bdeSDave Jiang }
929674d8bdeSDave Jiang 
93039611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
93139611e83SDan Williams {
93239611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
93339611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
93439611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
93539611e83SDan Williams 		return -EIO;
93639611e83SDan Williams 	}
93739611e83SDan Williams 	return rc;
93839611e83SDan Williams }
93939611e83SDan Williams 
9403c13e2acSDave Jiang static int nd_intel_test_cmd_security_status(struct nfit_test *t,
9413c13e2acSDave Jiang 		struct nd_intel_get_security_state *nd_cmd,
9423c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9433c13e2acSDave Jiang {
9443c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9453c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9463c13e2acSDave Jiang 
9473c13e2acSDave Jiang 	nd_cmd->status = 0;
9483c13e2acSDave Jiang 	nd_cmd->state = sec->state;
949ecaa4a97SDave Jiang 	nd_cmd->extended_state = sec->ext_state;
9503c13e2acSDave Jiang 	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);
9513c13e2acSDave Jiang 
9523c13e2acSDave Jiang 	return 0;
9533c13e2acSDave Jiang }
9543c13e2acSDave Jiang 
9553c13e2acSDave Jiang static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
9563c13e2acSDave Jiang 		struct nd_intel_unlock_unit *nd_cmd,
9573c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9583c13e2acSDave Jiang {
9593c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9603c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9613c13e2acSDave Jiang 
9623c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
9633c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
9643c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9653c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
9663c13e2acSDave Jiang 				sec->state);
9673c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
9683c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9693c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9703c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid passphrase\n");
9713c13e2acSDave Jiang 	} else {
9723c13e2acSDave Jiang 		nd_cmd->status = 0;
9733c13e2acSDave Jiang 		sec->state = ND_INTEL_SEC_STATE_ENABLED;
9743c13e2acSDave Jiang 		dev_dbg(dev, "Unit unlocked\n");
9753c13e2acSDave Jiang 	}
9763c13e2acSDave Jiang 
9773c13e2acSDave Jiang 	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
9783c13e2acSDave Jiang 	return 0;
9793c13e2acSDave Jiang }
9803c13e2acSDave Jiang 
9813c13e2acSDave Jiang static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
9823c13e2acSDave Jiang 		struct nd_intel_set_passphrase *nd_cmd,
9833c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9843c13e2acSDave Jiang {
9853c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9863c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9873c13e2acSDave Jiang 
9883c13e2acSDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
9893c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9903c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong security state\n");
9913c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
9923c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9933c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9943c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong passphrase\n");
9953c13e2acSDave Jiang 	} else {
9963c13e2acSDave Jiang 		memcpy(sec->passphrase, nd_cmd->new_pass,
9973c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
9983c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
9993c13e2acSDave Jiang 		nd_cmd->status = 0;
10003c13e2acSDave Jiang 		dev_dbg(dev, "passphrase updated\n");
10013c13e2acSDave Jiang 	}
10023c13e2acSDave Jiang 
10033c13e2acSDave Jiang 	return 0;
10043c13e2acSDave Jiang }
10053c13e2acSDave Jiang 
10063c13e2acSDave Jiang static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
10073c13e2acSDave Jiang 		struct nd_intel_freeze_lock *nd_cmd,
10083c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10093c13e2acSDave Jiang {
10103c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10113c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10123c13e2acSDave Jiang 
10133c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
10143c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10153c13e2acSDave Jiang 		dev_dbg(dev, "freeze lock: wrong security state\n");
10163c13e2acSDave Jiang 	} else {
10173c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
10183c13e2acSDave Jiang 		nd_cmd->status = 0;
10193c13e2acSDave Jiang 		dev_dbg(dev, "security frozen\n");
10203c13e2acSDave Jiang 	}
10213c13e2acSDave Jiang 
10223c13e2acSDave Jiang 	return 0;
10233c13e2acSDave Jiang }
10243c13e2acSDave Jiang 
10253c13e2acSDave Jiang static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
10263c13e2acSDave Jiang 		struct nd_intel_disable_passphrase *nd_cmd,
10273c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10283c13e2acSDave Jiang {
10293c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10303c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10313c13e2acSDave Jiang 
10323c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
10333c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
10343c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10353c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong security state\n");
10363c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10373c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10383c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10393c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
10403c13e2acSDave Jiang 	} else {
10413c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10423c13e2acSDave Jiang 		sec->state = 0;
10433c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: done\n");
10443c13e2acSDave Jiang 	}
10453c13e2acSDave Jiang 
10463c13e2acSDave Jiang 	return 0;
10473c13e2acSDave Jiang }
10483c13e2acSDave Jiang 
10493c13e2acSDave Jiang static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
10503c13e2acSDave Jiang 		struct nd_intel_secure_erase *nd_cmd,
10513c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10523c13e2acSDave Jiang {
10533c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10543c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10553c13e2acSDave Jiang 
1056037c8489SDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
10573c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10583c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong security state\n");
10593c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10603c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10613c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10623c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong passphrase\n");
10633c13e2acSDave Jiang 	} else {
1064037c8489SDave Jiang 		if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)
1065037c8489SDave Jiang 				&& (memcmp(nd_cmd->passphrase, zero_key,
1066037c8489SDave Jiang 					ND_INTEL_PASSPHRASE_SIZE) != 0)) {
1067037c8489SDave Jiang 			dev_dbg(dev, "invalid zero key\n");
1068037c8489SDave Jiang 			return 0;
1069037c8489SDave Jiang 		}
10703c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1071ecaa4a97SDave Jiang 		memset(sec->master_passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10723c13e2acSDave Jiang 		sec->state = 0;
1073ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
10743c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: done\n");
10753c13e2acSDave Jiang 	}
10763c13e2acSDave Jiang 
10773c13e2acSDave Jiang 	return 0;
10783c13e2acSDave Jiang }
10793c13e2acSDave Jiang 
1080926f7480SDave Jiang static int nd_intel_test_cmd_overwrite(struct nfit_test *t,
1081926f7480SDave Jiang 		struct nd_intel_overwrite *nd_cmd,
1082926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1083926f7480SDave Jiang {
1084926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1085926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1086926f7480SDave Jiang 
1087926f7480SDave Jiang 	if ((sec->state & ND_INTEL_SEC_STATE_ENABLED) &&
1088926f7480SDave Jiang 			memcmp(nd_cmd->passphrase, sec->passphrase,
1089926f7480SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1090926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1091926f7480SDave Jiang 		dev_dbg(dev, "overwrite: wrong passphrase\n");
1092926f7480SDave Jiang 		return 0;
1093926f7480SDave Jiang 	}
1094926f7480SDave Jiang 
10952170a0d5SDave Jiang 	sec->old_state = sec->state;
1096926f7480SDave Jiang 	sec->state = ND_INTEL_SEC_STATE_OVERWRITE;
1097926f7480SDave Jiang 	dev_dbg(dev, "overwrite progressing.\n");
1098926f7480SDave Jiang 	sec->overwrite_end_time = get_jiffies_64() + 5 * HZ;
1099926f7480SDave Jiang 
1100926f7480SDave Jiang 	return 0;
1101926f7480SDave Jiang }
1102926f7480SDave Jiang 
1103926f7480SDave Jiang static int nd_intel_test_cmd_query_overwrite(struct nfit_test *t,
1104926f7480SDave Jiang 		struct nd_intel_query_overwrite *nd_cmd,
1105926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1106926f7480SDave Jiang {
1107926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1108926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1109926f7480SDave Jiang 
1110926f7480SDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_OVERWRITE)) {
1111926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_SEQUENCE_ERR;
1112926f7480SDave Jiang 		return 0;
1113926f7480SDave Jiang 	}
1114926f7480SDave Jiang 
1115926f7480SDave Jiang 	if (time_is_before_jiffies64(sec->overwrite_end_time)) {
1116926f7480SDave Jiang 		sec->overwrite_end_time = 0;
11172170a0d5SDave Jiang 		sec->state = sec->old_state;
11182170a0d5SDave Jiang 		sec->old_state = 0;
1119ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1120926f7480SDave Jiang 		dev_dbg(dev, "overwrite is complete\n");
1121926f7480SDave Jiang 	} else
1122926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_INPROGRESS;
1123926f7480SDave Jiang 	return 0;
1124926f7480SDave Jiang }
1125926f7480SDave Jiang 
1126ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_set_pass(struct nfit_test *t,
1127ecaa4a97SDave Jiang 		struct nd_intel_set_master_passphrase *nd_cmd,
1128ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1129ecaa4a97SDave Jiang {
1130ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1131ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1132ecaa4a97SDave Jiang 
1133ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1134ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1135ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong state\n");
1136ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1137ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1138ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong security state\n");
1139ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->master_passphrase,
1140ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1141ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1142ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: wrong passphrase\n");
1143ecaa4a97SDave Jiang 	} else {
1144ecaa4a97SDave Jiang 		memcpy(sec->master_passphrase, nd_cmd->new_pass,
1145ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
1146ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1147ecaa4a97SDave Jiang 		dev_dbg(dev, "master passphrase: updated\n");
1148ecaa4a97SDave Jiang 	}
1149ecaa4a97SDave Jiang 
1150ecaa4a97SDave Jiang 	return 0;
1151ecaa4a97SDave Jiang }
1152ecaa4a97SDave Jiang 
1153ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_secure_erase(struct nfit_test *t,
1154ecaa4a97SDave Jiang 		struct nd_intel_master_secure_erase *nd_cmd,
1155ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1156ecaa4a97SDave Jiang {
1157ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1158ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1159ecaa4a97SDave Jiang 
1160ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1161ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1162ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong state\n");
1163ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1164ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1165ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong security state\n");
1166ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->master_passphrase,
1167ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1168ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1169ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: wrong passphrase\n");
1170ecaa4a97SDave Jiang 	} else {
1171ecaa4a97SDave Jiang 		/* we do not erase master state passphrase ever */
1172ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1173ecaa4a97SDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1174ecaa4a97SDave Jiang 		sec->state = 0;
1175ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: done\n");
1176ecaa4a97SDave Jiang 	}
1177ecaa4a97SDave Jiang 
1178ecaa4a97SDave Jiang 	return 0;
1179ecaa4a97SDave Jiang }
1180ecaa4a97SDave Jiang 
1181ecaa4a97SDave Jiang 
1182bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
1183bfbaa952SDave Jiang {
1184bfbaa952SDave Jiang 	int i;
1185bfbaa952SDave Jiang 
1186bfbaa952SDave Jiang 	/* lookup per-dimm data */
1187bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1188bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1189bfbaa952SDave Jiang 			break;
1190bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
1191bfbaa952SDave Jiang 		return -ENXIO;
1192bfbaa952SDave Jiang 	return i;
1193bfbaa952SDave Jiang }
1194bfbaa952SDave Jiang 
119539c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
119639c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1197aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
119839c686b8SVishal Verma {
119939c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
120039c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
12016634fb06SDan Williams 	unsigned int func = cmd;
1202f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1203f471f1a7SDan Williams 
1204f471f1a7SDan Williams 	if (!cmd_rc)
1205f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1206f471f1a7SDan Williams 	*cmd_rc = 0;
120739c686b8SVishal Verma 
120839c686b8SVishal Verma 	if (nvdimm) {
120939c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1210e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
121139c686b8SVishal Verma 
12126634fb06SDan Williams 		if (!nfit_mem)
12136634fb06SDan Williams 			return -ENOTTY;
12146634fb06SDan Williams 
12156634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
12166634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
12176634fb06SDan Williams 
12186634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
12196634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
12206634fb06SDan Williams 			func = call_pkg->nd_command;
12216634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
12226634fb06SDan Williams 				return -ENOTTY;
1223bfbaa952SDave Jiang 
1224bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1225bfbaa952SDave Jiang 			if (i < 0)
1226bfbaa952SDave Jiang 				return i;
1227*24770658SDan Williams 			if (i >= NUM_DCR) {
1228*24770658SDan Williams 				dev_WARN_ONCE(&t->pdev.dev, 1,
1229*24770658SDan Williams 						"ND_CMD_CALL only valid for nfit_test0\n");
1230*24770658SDan Williams 				return -EINVAL;
1231*24770658SDan Williams 			}
1232bfbaa952SDave Jiang 
1233bfbaa952SDave Jiang 			switch (func) {
12343c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
12353c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
12363c13e2acSDave Jiang 						buf, buf_len, i);
12373c13e2acSDave Jiang 				break;
12383c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
12393c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
12403c13e2acSDave Jiang 						buf, buf_len, i);
12413c13e2acSDave Jiang 				break;
12423c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
12433c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
12443c13e2acSDave Jiang 						buf, buf_len, i);
12453c13e2acSDave Jiang 				break;
12463c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
12473c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
12483c13e2acSDave Jiang 						buf, buf_len, i);
12493c13e2acSDave Jiang 				break;
12503c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
12513c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
12523c13e2acSDave Jiang 						buf, buf_len, i);
12533c13e2acSDave Jiang 				break;
12543c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
12553c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
12563c13e2acSDave Jiang 						buf, buf_len, i);
12573c13e2acSDave Jiang 				break;
1258926f7480SDave Jiang 			case NVDIMM_INTEL_OVERWRITE:
1259926f7480SDave Jiang 				rc = nd_intel_test_cmd_overwrite(t,
1260*24770658SDan Williams 						buf, buf_len, i);
1261926f7480SDave Jiang 				break;
1262926f7480SDave Jiang 			case NVDIMM_INTEL_QUERY_OVERWRITE:
1263926f7480SDave Jiang 				rc = nd_intel_test_cmd_query_overwrite(t,
1264*24770658SDan Williams 						buf, buf_len, i);
1265926f7480SDave Jiang 				break;
1266ecaa4a97SDave Jiang 			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
1267ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_set_pass(t,
1268ecaa4a97SDave Jiang 						buf, buf_len, i);
1269ecaa4a97SDave Jiang 				break;
1270ecaa4a97SDave Jiang 			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
1271ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_secure_erase(t,
1272ecaa4a97SDave Jiang 						buf, buf_len, i);
1273ecaa4a97SDave Jiang 				break;
1274674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
127539611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1276674d8bdeSDave Jiang 						buf, buf_len);
127739611e83SDan Williams 				break;
1278bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
127939611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
1280*24770658SDan Williams 						buf_len, i);
128139611e83SDan Williams 				break;
1282bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
128339611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
1284*24770658SDan Williams 						buf_len, i);
128539611e83SDan Williams 				break;
1286bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
128739611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
1288*24770658SDan Williams 						buf_len, i);
128939611e83SDan Williams 				break;
1290bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
129139611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
1292*24770658SDan Williams 						buf_len, i);
129339611e83SDan Williams 				break;
1294bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
129539611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
1296*24770658SDan Williams 						buf_len, i);
129739611e83SDan Williams 				break;
1298bfbaa952SDave Jiang 			case ND_INTEL_SMART:
129939611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
1300*24770658SDan Williams 						&t->smart[i]);
130139611e83SDan Williams 				break;
1302bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
130339611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1304bfbaa952SDave Jiang 						buf_len,
1305*24770658SDan Williams 						&t->smart_threshold[i]);
130639611e83SDan Williams 				break;
1307bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
130839611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1309bfbaa952SDave Jiang 						buf_len,
1310*24770658SDan Williams 						&t->smart_threshold[i],
1311*24770658SDan Williams 						&t->smart[i],
1312bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
131339611e83SDan Williams 				break;
13144cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
131539611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
13164cf260fcSVishal Verma 						buf_len,
1317*24770658SDan Williams 						&t->smart_threshold[i],
1318*24770658SDan Williams 						&t->smart[i],
13194cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
132039611e83SDan Williams 				break;
1321bfbaa952SDave Jiang 			default:
1322bfbaa952SDave Jiang 				return -ENOTTY;
1323bfbaa952SDave Jiang 			}
132439611e83SDan Williams 			return override_return_code(i, func, rc);
13256634fb06SDan Williams 		}
13266634fb06SDan Williams 
13276634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
13286634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
132939c686b8SVishal Verma 			return -ENOTTY;
133039c686b8SVishal Verma 
1331bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1332bfbaa952SDave Jiang 		if (i < 0)
1333bfbaa952SDave Jiang 			return i;
133473606afdSDan Williams 
13356634fb06SDan Williams 		switch (func) {
133639c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
133739c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
133839c686b8SVishal Verma 			break;
133939c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
134039c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1341dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
134239c686b8SVishal Verma 			break;
134339c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
134439c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1345dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
134639c686b8SVishal Verma 			break;
13476bc75619SDan Williams 		default:
13486bc75619SDan Williams 			return -ENOTTY;
13496bc75619SDan Williams 		}
135039611e83SDan Williams 		return override_return_code(i, func, rc);
135139c686b8SVishal Verma 	} else {
1352f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
135310246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
135410246dc8SYasunori Goto 
135510246dc8SYasunori Goto 		if (!nd_desc)
135610246dc8SYasunori Goto 			return -ENOTTY;
135710246dc8SYasunori Goto 
135810246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
135910246dc8SYasunori Goto 			func = call_pkg->nd_command;
136010246dc8SYasunori Goto 
136110246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
136210246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
136310246dc8SYasunori Goto 
136410246dc8SYasunori Goto 			switch (func) {
136510246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
136610246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
136710246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
136810246dc8SYasunori Goto 				return rc;
13699fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
13709fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
13719fb1a190SDave Jiang 					buf_len);
13729fb1a190SDave Jiang 				return rc;
13739fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
13749fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
13759fb1a190SDave Jiang 					buf_len);
13769fb1a190SDave Jiang 				return rc;
13779fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
13789fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
13799fb1a190SDave Jiang 					buf_len);
13809fb1a190SDave Jiang 				return rc;
138110246dc8SYasunori Goto 			default:
138210246dc8SYasunori Goto 				return -ENOTTY;
138310246dc8SYasunori Goto 			}
138410246dc8SYasunori Goto 		}
1385f471f1a7SDan Williams 
1386e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
138739c686b8SVishal Verma 			return -ENOTTY;
138839c686b8SVishal Verma 
13896634fb06SDan Williams 		switch (func) {
139039c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
139139c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
139239c686b8SVishal Verma 			break;
139339c686b8SVishal Verma 		case ND_CMD_ARS_START:
13949fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
13959fb1a190SDave Jiang 					buf_len, cmd_rc);
139639c686b8SVishal Verma 			break;
139739c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1398f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1399f471f1a7SDan Williams 					cmd_rc);
140039c686b8SVishal Verma 			break;
1401d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
14025e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1403d4f32367SDan Williams 			break;
140439c686b8SVishal Verma 		default:
140539c686b8SVishal Verma 			return -ENOTTY;
140639c686b8SVishal Verma 		}
140739c686b8SVishal Verma 	}
14086bc75619SDan Williams 
14096bc75619SDan Williams 	return rc;
14106bc75619SDan Williams }
14116bc75619SDan Williams 
14126bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
14136bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
14146bc75619SDan Williams 
14156bc75619SDan Williams static void release_nfit_res(void *data)
14166bc75619SDan Williams {
14176bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
14186bc75619SDan Williams 
14196bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14206bc75619SDan Williams 	list_del(&nfit_res->list);
14216bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14226bc75619SDan Williams 
1423e3f5df76SDan Williams 	if (resource_size(&nfit_res->res) >= DIMM_SIZE)
1424e3f5df76SDan Williams 		gen_pool_free(nfit_pool, nfit_res->res.start,
1425e3f5df76SDan Williams 				resource_size(&nfit_res->res));
14266bc75619SDan Williams 	vfree(nfit_res->buf);
14276bc75619SDan Williams 	kfree(nfit_res);
14286bc75619SDan Williams }
14296bc75619SDan Williams 
14306bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
14316bc75619SDan Williams 		void *buf)
14326bc75619SDan Williams {
14336bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
14346bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
14356bc75619SDan Williams 			GFP_KERNEL);
14366bc75619SDan Williams 	int rc;
14376bc75619SDan Williams 
1438e3f5df76SDan Williams 	if (!buf || !nfit_res || !*dma)
14396bc75619SDan Williams 		goto err;
14406bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
14416bc75619SDan Williams 	if (rc)
14426bc75619SDan Williams 		goto err;
14436bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
14446bc75619SDan Williams 	memset(buf, 0, size);
14456bc75619SDan Williams 	nfit_res->dev = dev;
14466bc75619SDan Williams 	nfit_res->buf = buf;
1447bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1448bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1449bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1450bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1451bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
14526bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14536bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
14546bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14556bc75619SDan Williams 
14566bc75619SDan Williams 	return nfit_res->buf;
14576bc75619SDan Williams  err:
1458e3f5df76SDan Williams 	if (*dma && size >= DIMM_SIZE)
1459e3f5df76SDan Williams 		gen_pool_free(nfit_pool, *dma, size);
1460ee8520feSDan Williams 	if (buf)
14616bc75619SDan Williams 		vfree(buf);
14626bc75619SDan Williams 	kfree(nfit_res);
14636bc75619SDan Williams 	return NULL;
14646bc75619SDan Williams }
14656bc75619SDan Williams 
14666bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
14676bc75619SDan Williams {
1468e3f5df76SDan Williams 	struct genpool_data_align data = {
1469e3f5df76SDan Williams 		.align = SZ_128M,
1470e3f5df76SDan Williams 	};
14716bc75619SDan Williams 	void *buf = vmalloc(size);
14726bc75619SDan Williams 
1473e3f5df76SDan Williams 	if (size >= DIMM_SIZE)
1474e3f5df76SDan Williams 		*dma = gen_pool_alloc_algo(nfit_pool, size,
1475e3f5df76SDan Williams 				gen_pool_first_fit_align, &data);
1476e3f5df76SDan Williams 	else
14776bc75619SDan Williams 		*dma = (unsigned long) buf;
14786bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
14796bc75619SDan Williams }
14806bc75619SDan Williams 
14816bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
14826bc75619SDan Williams {
14836bc75619SDan Williams 	int i;
14846bc75619SDan Williams 
14856bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
14866bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
14876bc75619SDan Williams 		struct nfit_test *t = instances[i];
14886bc75619SDan Williams 
14896bc75619SDan Williams 		if (!t)
14906bc75619SDan Williams 			continue;
14916bc75619SDan Williams 		spin_lock(&nfit_test_lock);
14926bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1493bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1494bd4cd745SDan Williams 						+ resource_size(&n->res))) {
14956bc75619SDan Williams 				nfit_res = n;
14966bc75619SDan Williams 				break;
14976bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
14986bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1499bd4cd745SDan Williams 						+ resource_size(&n->res))) {
15006bc75619SDan Williams 				nfit_res = n;
15016bc75619SDan Williams 				break;
15026bc75619SDan Williams 			}
15036bc75619SDan Williams 		}
15046bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
15056bc75619SDan Williams 		if (nfit_res)
15066bc75619SDan Williams 			return nfit_res;
15076bc75619SDan Williams 	}
15086bc75619SDan Williams 
15096bc75619SDan Williams 	return NULL;
15106bc75619SDan Williams }
15116bc75619SDan Williams 
1512f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1513f471f1a7SDan Williams {
15149fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1515f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
15169fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1517f471f1a7SDan Williams 	if (!ars_state->ars_status)
1518f471f1a7SDan Williams 		return -ENOMEM;
1519f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1520f471f1a7SDan Williams 	return 0;
1521f471f1a7SDan Williams }
1522f471f1a7SDan Williams 
1523231bf117SDan Williams static void put_dimms(void *data)
1524231bf117SDan Williams {
1525718fda67SDan Williams 	struct nfit_test *t = data;
1526231bf117SDan Williams 	int i;
1527231bf117SDan Williams 
1528718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1529718fda67SDan Williams 		if (t->dimm_dev[i])
1530718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1531231bf117SDan Williams }
1532231bf117SDan Williams 
1533231bf117SDan Williams static struct class *nfit_test_dimm;
1534231bf117SDan Williams 
153573606afdSDan Williams static int dimm_name_to_id(struct device *dev)
153673606afdSDan Williams {
153773606afdSDan Williams 	int dimm;
153873606afdSDan Williams 
1539718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
154073606afdSDan Williams 		return -ENXIO;
154173606afdSDan Williams 	return dimm;
154273606afdSDan Williams }
154373606afdSDan Williams 
154473606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
154573606afdSDan Williams 		char *buf)
154673606afdSDan Williams {
154773606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
154873606afdSDan Williams 
154973606afdSDan Williams 	if (dimm < 0)
155073606afdSDan Williams 		return dimm;
155173606afdSDan Williams 
155219357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
155373606afdSDan Williams }
155473606afdSDan Williams DEVICE_ATTR_RO(handle);
155573606afdSDan Williams 
155673606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
155773606afdSDan Williams 		char *buf)
155873606afdSDan Williams {
155973606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
156073606afdSDan Williams 
156173606afdSDan Williams 	if (dimm < 0)
156273606afdSDan Williams 		return dimm;
156373606afdSDan Williams 
156473606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
156573606afdSDan Williams }
156673606afdSDan Williams 
156773606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
156873606afdSDan Williams 		const char *buf, size_t size)
156973606afdSDan Williams {
157073606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
157173606afdSDan Williams 	unsigned long val;
157273606afdSDan Williams 	ssize_t rc;
157373606afdSDan Williams 
157473606afdSDan Williams 	if (dimm < 0)
157573606afdSDan Williams 		return dimm;
157673606afdSDan Williams 
157773606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
157873606afdSDan Williams 	if (rc)
157973606afdSDan Williams 		return rc;
158073606afdSDan Williams 
158173606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
158273606afdSDan Williams 	return size;
158373606afdSDan Williams }
158473606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
158573606afdSDan Williams 
158655c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
158755c72ab6SDan Williams 		char *buf)
158855c72ab6SDan Williams {
158955c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
159055c72ab6SDan Williams 
159155c72ab6SDan Williams 	if (dimm < 0)
159255c72ab6SDan Williams 		return dimm;
159355c72ab6SDan Williams 
159455c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
159555c72ab6SDan Williams }
159655c72ab6SDan Williams 
159755c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
159855c72ab6SDan Williams 		const char *buf, size_t size)
159955c72ab6SDan Williams {
160055c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
160155c72ab6SDan Williams 	unsigned long val;
160255c72ab6SDan Williams 	ssize_t rc;
160355c72ab6SDan Williams 
160455c72ab6SDan Williams 	if (dimm < 0)
160555c72ab6SDan Williams 		return dimm;
160655c72ab6SDan Williams 
160755c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
160855c72ab6SDan Williams 	if (rc)
160955c72ab6SDan Williams 		return rc;
161055c72ab6SDan Williams 
161155c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
161255c72ab6SDan Williams 	return size;
161355c72ab6SDan Williams }
161455c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
161555c72ab6SDan Williams 
16163c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
16173c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
16183c13e2acSDave Jiang {
16193c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
16203c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
16213c13e2acSDave Jiang 
16223c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
16233c13e2acSDave Jiang 	return size;
16243c13e2acSDave Jiang }
16253c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
16263c13e2acSDave Jiang 
162773606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
162873606afdSDan Williams 	&dev_attr_fail_cmd.attr,
162955c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
163073606afdSDan Williams 	&dev_attr_handle.attr,
16313c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
163273606afdSDan Williams 	NULL,
163373606afdSDan Williams };
163473606afdSDan Williams 
163573606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
163673606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
163773606afdSDan Williams };
163873606afdSDan Williams 
163973606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
164073606afdSDan Williams 	&nfit_test_dimm_attribute_group,
164173606afdSDan Williams 	NULL,
164273606afdSDan Williams };
164373606afdSDan Williams 
1644718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1645718fda67SDan Williams {
1646718fda67SDan Williams 	int i;
1647718fda67SDan Williams 
1648718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1649718fda67SDan Williams 		return -ENOMEM;
1650718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1651718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1652718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1653718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1654718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1655718fda67SDan Williams 		if (!t->dimm_dev[i])
1656718fda67SDan Williams 			return -ENOMEM;
1657718fda67SDan Williams 	}
1658718fda67SDan Williams 	return 0;
1659718fda67SDan Williams }
1660718fda67SDan Williams 
1661ecaa4a97SDave Jiang static void security_init(struct nfit_test *t)
1662ecaa4a97SDave Jiang {
1663ecaa4a97SDave Jiang 	int i;
1664ecaa4a97SDave Jiang 
1665ecaa4a97SDave Jiang 	for (i = 0; i < t->num_dcr; i++) {
1666ecaa4a97SDave Jiang 		struct nfit_test_sec *sec = &dimm_sec_info[i];
1667ecaa4a97SDave Jiang 
1668ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1669ecaa4a97SDave Jiang 	}
1670ecaa4a97SDave Jiang }
1671ecaa4a97SDave Jiang 
1672ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1673ed07c433SDan Williams {
1674ed07c433SDan Williams 	int i;
1675ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1676ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1677ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1678ed07c433SDan Williams 		.media_temperature = 40 * 16,
1679ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1680ed07c433SDan Williams 		.spares = 5,
1681ed07c433SDan Williams 	};
1682ed07c433SDan Williams 
1683ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1684b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1685ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1686ed07c433SDan Williams 				sizeof(smart_t_data));
1687ed07c433SDan Williams 	}
1688ed07c433SDan Williams }
1689ed07c433SDan Williams 
16906bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
16916bc75619SDan Williams {
16926b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
16936bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
16946bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
16953b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
16963b87356fSDan Williams 					window_size) * NUM_DCR
16979d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
169885d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1699f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1700f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
17016bc75619SDan Williams 	int i;
17026bc75619SDan Williams 
17036bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17046bc75619SDan Williams 	if (!t->nfit_buf)
17056bc75619SDan Williams 		return -ENOMEM;
17066bc75619SDan Williams 	t->nfit_size = nfit_size;
17076bc75619SDan Williams 
1708ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
17096bc75619SDan Williams 	if (!t->spa_set[0])
17106bc75619SDan Williams 		return -ENOMEM;
17116bc75619SDan Williams 
1712ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
17136bc75619SDan Williams 	if (!t->spa_set[1])
17146bc75619SDan Williams 		return -ENOMEM;
17156bc75619SDan Williams 
1716ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
171720985164SVishal Verma 	if (!t->spa_set[2])
171820985164SVishal Verma 		return -ENOMEM;
171920985164SVishal Verma 
1720dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17216bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
17226bc75619SDan Williams 		if (!t->dimm[i])
17236bc75619SDan Williams 			return -ENOMEM;
17246bc75619SDan Williams 
17256bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
17266bc75619SDan Williams 		if (!t->label[i])
17276bc75619SDan Williams 			return -ENOMEM;
17286bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
17299d27a87eSDan Williams 
17309d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
17319d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
173285d3fa02SDan Williams 				&t->flush_dma[i]);
17339d27a87eSDan Williams 		if (!t->flush[i])
17349d27a87eSDan Williams 			return -ENOMEM;
17356bc75619SDan Williams 	}
17366bc75619SDan Williams 
1737dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17386bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
17396bc75619SDan Williams 		if (!t->dcr[i])
17406bc75619SDan Williams 			return -ENOMEM;
17416bc75619SDan Williams 	}
17426bc75619SDan Williams 
1743c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1744c14a868aSDan Williams 	if (!t->_fit)
1745c14a868aSDan Williams 		return -ENOMEM;
1746c14a868aSDan Williams 
1747718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1748231bf117SDan Williams 		return -ENOMEM;
1749ed07c433SDan Williams 	smart_init(t);
1750ecaa4a97SDave Jiang 	security_init(t);
1751f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17526bc75619SDan Williams }
17536bc75619SDan Williams 
17546bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
17556bc75619SDan Williams {
17567bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1757ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1758ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1759dafb1048SDan Williams 	int i;
17606bc75619SDan Williams 
17616bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17626bc75619SDan Williams 	if (!t->nfit_buf)
17636bc75619SDan Williams 		return -ENOMEM;
17646bc75619SDan Williams 	t->nfit_size = nfit_size;
17656bc75619SDan Williams 
1766ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
17676bc75619SDan Williams 	if (!t->spa_set[0])
17686bc75619SDan Williams 		return -ENOMEM;
17696bc75619SDan Williams 
1770dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1771dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1772dafb1048SDan Williams 		if (!t->label[i])
1773dafb1048SDan Williams 			return -ENOMEM;
1774dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1775dafb1048SDan Williams 	}
1776dafb1048SDan Williams 
17777bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
17787bfe97c7SDan Williams 	if (!t->spa_set[1])
17797bfe97c7SDan Williams 		return -ENOMEM;
17807bfe97c7SDan Williams 
1781718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1782718fda67SDan Williams 		return -ENOMEM;
1783ed07c433SDan Williams 	smart_init(t);
1784f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17856bc75619SDan Williams }
17866bc75619SDan Williams 
17875dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
17885dc68e55SDan Williams {
17895dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
17905dc68e55SDan Williams 	dcr->device_id = 0;
17915dc68e55SDan Williams 	dcr->revision_id = 1;
17925dc68e55SDan Williams 	dcr->valid_fields = 1;
17935dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
17945dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
17955dc68e55SDan Williams }
17965dc68e55SDan Williams 
17976bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
17986bc75619SDan Williams {
179985d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
180085d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
18016bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
18026bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
18036bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
18046bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
18056bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
18066bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
18079d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1808f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1809d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
18106bc75619SDan Williams 
18116bc75619SDan Williams 	/*
18126bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
18136bc75619SDan Williams 	 * does not actually alias the related block-data-window
18146bc75619SDan Williams 	 * regions)
18156bc75619SDan Williams 	 */
18166b577c9dSLinda Knippers 	spa = nfit_buf;
18176bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18186bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18196bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18206bc75619SDan Williams 	spa->range_index = 0+1;
18216bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
18226bc75619SDan Williams 	spa->length = SPA0_SIZE;
1823d7d8464dSRoss Zwisler 	offset += spa->header.length;
18246bc75619SDan Williams 
18256bc75619SDan Williams 	/*
18266bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
18276bc75619SDan Williams 	 * does not actually alias the related block-data-window
18286bc75619SDan Williams 	 * regions)
18296bc75619SDan Williams 	 */
1830d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18316bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18326bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18336bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18346bc75619SDan Williams 	spa->range_index = 1+1;
18356bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
18366bc75619SDan Williams 	spa->length = SPA1_SIZE;
1837d7d8464dSRoss Zwisler 	offset += spa->header.length;
18386bc75619SDan Williams 
18396bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1840d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18416bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18426bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18436bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18446bc75619SDan Williams 	spa->range_index = 2+1;
18456bc75619SDan Williams 	spa->address = t->dcr_dma[0];
18466bc75619SDan Williams 	spa->length = DCR_SIZE;
1847d7d8464dSRoss Zwisler 	offset += spa->header.length;
18486bc75619SDan Williams 
18496bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1850d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18516bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18526bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18536bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18546bc75619SDan Williams 	spa->range_index = 3+1;
18556bc75619SDan Williams 	spa->address = t->dcr_dma[1];
18566bc75619SDan Williams 	spa->length = DCR_SIZE;
1857d7d8464dSRoss Zwisler 	offset += spa->header.length;
18586bc75619SDan Williams 
18596bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1860d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18616bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18626bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18636bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18646bc75619SDan Williams 	spa->range_index = 4+1;
18656bc75619SDan Williams 	spa->address = t->dcr_dma[2];
18666bc75619SDan Williams 	spa->length = DCR_SIZE;
1867d7d8464dSRoss Zwisler 	offset += spa->header.length;
18686bc75619SDan Williams 
18696bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1870d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18716bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18726bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18736bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18746bc75619SDan Williams 	spa->range_index = 5+1;
18756bc75619SDan Williams 	spa->address = t->dcr_dma[3];
18766bc75619SDan Williams 	spa->length = DCR_SIZE;
1877d7d8464dSRoss Zwisler 	offset += spa->header.length;
18786bc75619SDan Williams 
18796bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1880d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18816bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18826bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18836bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18846bc75619SDan Williams 	spa->range_index = 6+1;
18856bc75619SDan Williams 	spa->address = t->dimm_dma[0];
18866bc75619SDan Williams 	spa->length = DIMM_SIZE;
1887d7d8464dSRoss Zwisler 	offset += spa->header.length;
18886bc75619SDan Williams 
18896bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1890d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18916bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18926bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18936bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18946bc75619SDan Williams 	spa->range_index = 7+1;
18956bc75619SDan Williams 	spa->address = t->dimm_dma[1];
18966bc75619SDan Williams 	spa->length = DIMM_SIZE;
1897d7d8464dSRoss Zwisler 	offset += spa->header.length;
18986bc75619SDan Williams 
18996bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1900d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19016bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19026bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19036bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19046bc75619SDan Williams 	spa->range_index = 8+1;
19056bc75619SDan Williams 	spa->address = t->dimm_dma[2];
19066bc75619SDan Williams 	spa->length = DIMM_SIZE;
1907d7d8464dSRoss Zwisler 	offset += spa->header.length;
19086bc75619SDan Williams 
19096bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1910d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19116bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19126bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19136bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19146bc75619SDan Williams 	spa->range_index = 9+1;
19156bc75619SDan Williams 	spa->address = t->dimm_dma[3];
19166bc75619SDan Williams 	spa->length = DIMM_SIZE;
1917d7d8464dSRoss Zwisler 	offset += spa->header.length;
19186bc75619SDan Williams 
19196bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
19206bc75619SDan Williams 	memdev = nfit_buf + offset;
19216bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19226bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19236bc75619SDan Williams 	memdev->device_handle = handle[0];
19246bc75619SDan Williams 	memdev->physical_id = 0;
19256bc75619SDan Williams 	memdev->region_id = 0;
19266bc75619SDan Williams 	memdev->range_index = 0+1;
19273b87356fSDan Williams 	memdev->region_index = 4+1;
19286bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1929df06a2d5SDan Williams 	memdev->region_offset = 1;
19306bc75619SDan Williams 	memdev->address = 0;
19316bc75619SDan Williams 	memdev->interleave_index = 0;
19326bc75619SDan Williams 	memdev->interleave_ways = 2;
1933d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19346bc75619SDan Williams 
19356bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1936d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19376bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19386bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19396bc75619SDan Williams 	memdev->device_handle = handle[1];
19406bc75619SDan Williams 	memdev->physical_id = 1;
19416bc75619SDan Williams 	memdev->region_id = 0;
19426bc75619SDan Williams 	memdev->range_index = 0+1;
19433b87356fSDan Williams 	memdev->region_index = 5+1;
19446bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1945df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
19466bc75619SDan Williams 	memdev->address = 0;
19476bc75619SDan Williams 	memdev->interleave_index = 0;
19486bc75619SDan Williams 	memdev->interleave_ways = 2;
1949ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1950d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19516bc75619SDan Williams 
19526bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1953d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19546bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19556bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19566bc75619SDan Williams 	memdev->device_handle = handle[0];
19576bc75619SDan Williams 	memdev->physical_id = 0;
19586bc75619SDan Williams 	memdev->region_id = 1;
19596bc75619SDan Williams 	memdev->range_index = 1+1;
19603b87356fSDan Williams 	memdev->region_index = 4+1;
19616bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1962df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
19636bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19646bc75619SDan Williams 	memdev->interleave_index = 0;
19656bc75619SDan Williams 	memdev->interleave_ways = 4;
1966ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1967d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19686bc75619SDan Williams 
19696bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1970d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19716bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19726bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19736bc75619SDan Williams 	memdev->device_handle = handle[1];
19746bc75619SDan Williams 	memdev->physical_id = 1;
19756bc75619SDan Williams 	memdev->region_id = 1;
19766bc75619SDan Williams 	memdev->range_index = 1+1;
19773b87356fSDan Williams 	memdev->region_index = 5+1;
19786bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1979df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
19806bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19816bc75619SDan Williams 	memdev->interleave_index = 0;
19826bc75619SDan Williams 	memdev->interleave_ways = 4;
1983d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19846bc75619SDan Williams 
19856bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1986d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19876bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19886bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19896bc75619SDan Williams 	memdev->device_handle = handle[2];
19906bc75619SDan Williams 	memdev->physical_id = 2;
19916bc75619SDan Williams 	memdev->region_id = 0;
19926bc75619SDan Williams 	memdev->range_index = 1+1;
19933b87356fSDan Williams 	memdev->region_index = 6+1;
19946bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1995df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
19966bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19976bc75619SDan Williams 	memdev->interleave_index = 0;
19986bc75619SDan Williams 	memdev->interleave_ways = 4;
1999ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2000d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20016bc75619SDan Williams 
20026bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
2003d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20046bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20056bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20066bc75619SDan Williams 	memdev->device_handle = handle[3];
20076bc75619SDan Williams 	memdev->physical_id = 3;
20086bc75619SDan Williams 	memdev->region_id = 0;
20096bc75619SDan Williams 	memdev->range_index = 1+1;
20103b87356fSDan Williams 	memdev->region_index = 7+1;
20116bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2012df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
20136bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
20146bc75619SDan Williams 	memdev->interleave_index = 0;
20156bc75619SDan Williams 	memdev->interleave_ways = 4;
2016d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20176bc75619SDan Williams 
20186bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
2019d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20206bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20216bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20226bc75619SDan Williams 	memdev->device_handle = handle[0];
20236bc75619SDan Williams 	memdev->physical_id = 0;
20246bc75619SDan Williams 	memdev->region_id = 0;
20256bc75619SDan Williams 	memdev->range_index = 2+1;
20266bc75619SDan Williams 	memdev->region_index = 0+1;
20276bc75619SDan Williams 	memdev->region_size = 0;
20286bc75619SDan Williams 	memdev->region_offset = 0;
20296bc75619SDan Williams 	memdev->address = 0;
20306bc75619SDan Williams 	memdev->interleave_index = 0;
20316bc75619SDan Williams 	memdev->interleave_ways = 1;
2032d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20336bc75619SDan Williams 
20346bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
2035d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20366bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20376bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20386bc75619SDan Williams 	memdev->device_handle = handle[1];
20396bc75619SDan Williams 	memdev->physical_id = 1;
20406bc75619SDan Williams 	memdev->region_id = 0;
20416bc75619SDan Williams 	memdev->range_index = 3+1;
20426bc75619SDan Williams 	memdev->region_index = 1+1;
20436bc75619SDan Williams 	memdev->region_size = 0;
20446bc75619SDan Williams 	memdev->region_offset = 0;
20456bc75619SDan Williams 	memdev->address = 0;
20466bc75619SDan Williams 	memdev->interleave_index = 0;
20476bc75619SDan Williams 	memdev->interleave_ways = 1;
2048d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20496bc75619SDan Williams 
20506bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
2051d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20526bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20536bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20546bc75619SDan Williams 	memdev->device_handle = handle[2];
20556bc75619SDan Williams 	memdev->physical_id = 2;
20566bc75619SDan Williams 	memdev->region_id = 0;
20576bc75619SDan Williams 	memdev->range_index = 4+1;
20586bc75619SDan Williams 	memdev->region_index = 2+1;
20596bc75619SDan Williams 	memdev->region_size = 0;
20606bc75619SDan Williams 	memdev->region_offset = 0;
20616bc75619SDan Williams 	memdev->address = 0;
20626bc75619SDan Williams 	memdev->interleave_index = 0;
20636bc75619SDan Williams 	memdev->interleave_ways = 1;
2064d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20656bc75619SDan Williams 
20666bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
2067d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20686bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20696bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20706bc75619SDan Williams 	memdev->device_handle = handle[3];
20716bc75619SDan Williams 	memdev->physical_id = 3;
20726bc75619SDan Williams 	memdev->region_id = 0;
20736bc75619SDan Williams 	memdev->range_index = 5+1;
20746bc75619SDan Williams 	memdev->region_index = 3+1;
20756bc75619SDan Williams 	memdev->region_size = 0;
20766bc75619SDan Williams 	memdev->region_offset = 0;
20776bc75619SDan Williams 	memdev->address = 0;
20786bc75619SDan Williams 	memdev->interleave_index = 0;
20796bc75619SDan Williams 	memdev->interleave_ways = 1;
2080d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20816bc75619SDan Williams 
20826bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
2083d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20846bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20856bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20866bc75619SDan Williams 	memdev->device_handle = handle[0];
20876bc75619SDan Williams 	memdev->physical_id = 0;
20886bc75619SDan Williams 	memdev->region_id = 0;
20896bc75619SDan Williams 	memdev->range_index = 6+1;
20906bc75619SDan Williams 	memdev->region_index = 0+1;
20916bc75619SDan Williams 	memdev->region_size = 0;
20926bc75619SDan Williams 	memdev->region_offset = 0;
20936bc75619SDan Williams 	memdev->address = 0;
20946bc75619SDan Williams 	memdev->interleave_index = 0;
20956bc75619SDan Williams 	memdev->interleave_ways = 1;
2096d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20976bc75619SDan Williams 
20986bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
2099d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21006bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21016bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21026bc75619SDan Williams 	memdev->device_handle = handle[1];
21036bc75619SDan Williams 	memdev->physical_id = 1;
21046bc75619SDan Williams 	memdev->region_id = 0;
21056bc75619SDan Williams 	memdev->range_index = 7+1;
21066bc75619SDan Williams 	memdev->region_index = 1+1;
21076bc75619SDan Williams 	memdev->region_size = 0;
21086bc75619SDan Williams 	memdev->region_offset = 0;
21096bc75619SDan Williams 	memdev->address = 0;
21106bc75619SDan Williams 	memdev->interleave_index = 0;
21116bc75619SDan Williams 	memdev->interleave_ways = 1;
2112d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21136bc75619SDan Williams 
21146bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
2115d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21166bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21176bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21186bc75619SDan Williams 	memdev->device_handle = handle[2];
21196bc75619SDan Williams 	memdev->physical_id = 2;
21206bc75619SDan Williams 	memdev->region_id = 0;
21216bc75619SDan Williams 	memdev->range_index = 8+1;
21226bc75619SDan Williams 	memdev->region_index = 2+1;
21236bc75619SDan Williams 	memdev->region_size = 0;
21246bc75619SDan Williams 	memdev->region_offset = 0;
21256bc75619SDan Williams 	memdev->address = 0;
21266bc75619SDan Williams 	memdev->interleave_index = 0;
21276bc75619SDan Williams 	memdev->interleave_ways = 1;
2128d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21296bc75619SDan Williams 
21306bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
2131d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21326bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21336bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21346bc75619SDan Williams 	memdev->device_handle = handle[3];
21356bc75619SDan Williams 	memdev->physical_id = 3;
21366bc75619SDan Williams 	memdev->region_id = 0;
21376bc75619SDan Williams 	memdev->range_index = 9+1;
21386bc75619SDan Williams 	memdev->region_index = 3+1;
21396bc75619SDan Williams 	memdev->region_size = 0;
21406bc75619SDan Williams 	memdev->region_offset = 0;
21416bc75619SDan Williams 	memdev->address = 0;
21426bc75619SDan Williams 	memdev->interleave_index = 0;
21436bc75619SDan Williams 	memdev->interleave_ways = 1;
2144ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2145d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21466bc75619SDan Williams 
21473b87356fSDan Williams 	/* dcr-descriptor0: blk */
21486bc75619SDan Williams 	dcr = nfit_buf + offset;
21496bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2150d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21516bc75619SDan Williams 	dcr->region_index = 0+1;
21525dc68e55SDan Williams 	dcr_common_init(dcr);
21536bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2154be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21556bc75619SDan Williams 	dcr->windows = 1;
21566bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21576bc75619SDan Williams 	dcr->command_offset = 0;
21586bc75619SDan Williams 	dcr->command_size = 8;
21596bc75619SDan Williams 	dcr->status_offset = 8;
21606bc75619SDan Williams 	dcr->status_size = 4;
2161d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21626bc75619SDan Williams 
21633b87356fSDan Williams 	/* dcr-descriptor1: blk */
2164d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21656bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2166d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21676bc75619SDan Williams 	dcr->region_index = 1+1;
21685dc68e55SDan Williams 	dcr_common_init(dcr);
21696bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2170be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21716bc75619SDan Williams 	dcr->windows = 1;
21726bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21736bc75619SDan Williams 	dcr->command_offset = 0;
21746bc75619SDan Williams 	dcr->command_size = 8;
21756bc75619SDan Williams 	dcr->status_offset = 8;
21766bc75619SDan Williams 	dcr->status_size = 4;
2177d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21786bc75619SDan Williams 
21793b87356fSDan Williams 	/* dcr-descriptor2: blk */
2180d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21816bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2182d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21836bc75619SDan Williams 	dcr->region_index = 2+1;
21845dc68e55SDan Williams 	dcr_common_init(dcr);
21856bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2186be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21876bc75619SDan Williams 	dcr->windows = 1;
21886bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21896bc75619SDan Williams 	dcr->command_offset = 0;
21906bc75619SDan Williams 	dcr->command_size = 8;
21916bc75619SDan Williams 	dcr->status_offset = 8;
21926bc75619SDan Williams 	dcr->status_size = 4;
2193d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21946bc75619SDan Williams 
21953b87356fSDan Williams 	/* dcr-descriptor3: blk */
2196d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21976bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2198d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21996bc75619SDan Williams 	dcr->region_index = 3+1;
22005dc68e55SDan Williams 	dcr_common_init(dcr);
22016bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2202be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
22036bc75619SDan Williams 	dcr->windows = 1;
22046bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
22056bc75619SDan Williams 	dcr->command_offset = 0;
22066bc75619SDan Williams 	dcr->command_size = 8;
22076bc75619SDan Williams 	dcr->status_offset = 8;
22086bc75619SDan Williams 	dcr->status_size = 4;
2209d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22106bc75619SDan Williams 
22113b87356fSDan Williams 	/* dcr-descriptor0: pmem */
22123b87356fSDan Williams 	dcr = nfit_buf + offset;
22133b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22143b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22153b87356fSDan Williams 			window_size);
22163b87356fSDan Williams 	dcr->region_index = 4+1;
22175dc68e55SDan Williams 	dcr_common_init(dcr);
22183b87356fSDan Williams 	dcr->serial_number = ~handle[0];
22193b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22203b87356fSDan Williams 	dcr->windows = 0;
2221d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22223b87356fSDan Williams 
22233b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2224d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22253b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22263b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22273b87356fSDan Williams 			window_size);
22283b87356fSDan Williams 	dcr->region_index = 5+1;
22295dc68e55SDan Williams 	dcr_common_init(dcr);
22303b87356fSDan Williams 	dcr->serial_number = ~handle[1];
22313b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22323b87356fSDan Williams 	dcr->windows = 0;
2233d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22343b87356fSDan Williams 
22353b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2236d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22373b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22383b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22393b87356fSDan Williams 			window_size);
22403b87356fSDan Williams 	dcr->region_index = 6+1;
22415dc68e55SDan Williams 	dcr_common_init(dcr);
22423b87356fSDan Williams 	dcr->serial_number = ~handle[2];
22433b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22443b87356fSDan Williams 	dcr->windows = 0;
2245d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22463b87356fSDan Williams 
22473b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2248d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22493b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22503b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22513b87356fSDan Williams 			window_size);
22523b87356fSDan Williams 	dcr->region_index = 7+1;
22535dc68e55SDan Williams 	dcr_common_init(dcr);
22543b87356fSDan Williams 	dcr->serial_number = ~handle[3];
22553b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22563b87356fSDan Williams 	dcr->windows = 0;
2257d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22583b87356fSDan Williams 
22596bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
22606bc75619SDan Williams 	bdw = nfit_buf + offset;
22616bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2262d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22636bc75619SDan Williams 	bdw->region_index = 0+1;
22646bc75619SDan Williams 	bdw->windows = 1;
22656bc75619SDan Williams 	bdw->offset = 0;
22666bc75619SDan Williams 	bdw->size = BDW_SIZE;
22676bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22686bc75619SDan Williams 	bdw->start_address = 0;
2269d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22706bc75619SDan Williams 
22716bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2272d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22736bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2274d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22756bc75619SDan Williams 	bdw->region_index = 1+1;
22766bc75619SDan Williams 	bdw->windows = 1;
22776bc75619SDan Williams 	bdw->offset = 0;
22786bc75619SDan Williams 	bdw->size = BDW_SIZE;
22796bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22806bc75619SDan Williams 	bdw->start_address = 0;
2281d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22826bc75619SDan Williams 
22836bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2284d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22856bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2286d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22876bc75619SDan Williams 	bdw->region_index = 2+1;
22886bc75619SDan Williams 	bdw->windows = 1;
22896bc75619SDan Williams 	bdw->offset = 0;
22906bc75619SDan Williams 	bdw->size = BDW_SIZE;
22916bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22926bc75619SDan Williams 	bdw->start_address = 0;
2293d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22946bc75619SDan Williams 
22956bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2296d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22976bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2298d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22996bc75619SDan Williams 	bdw->region_index = 3+1;
23006bc75619SDan Williams 	bdw->windows = 1;
23016bc75619SDan Williams 	bdw->offset = 0;
23026bc75619SDan Williams 	bdw->size = BDW_SIZE;
23036bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
23046bc75619SDan Williams 	bdw->start_address = 0;
2305d7d8464dSRoss Zwisler 	offset += bdw->header.length;
23066bc75619SDan Williams 
23079d27a87eSDan Williams 	/* flush0 (dimm0) */
23089d27a87eSDan Williams 	flush = nfit_buf + offset;
23099d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
231085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23119d27a87eSDan Williams 	flush->device_handle = handle[0];
231285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
231385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
231485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2315d7d8464dSRoss Zwisler 	offset += flush->header.length;
23169d27a87eSDan Williams 
23179d27a87eSDan Williams 	/* flush1 (dimm1) */
2318d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23199d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
232085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23219d27a87eSDan Williams 	flush->device_handle = handle[1];
232285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
232385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
232485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2325d7d8464dSRoss Zwisler 	offset += flush->header.length;
23269d27a87eSDan Williams 
23279d27a87eSDan Williams 	/* flush2 (dimm2) */
2328d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23299d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
233085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23319d27a87eSDan Williams 	flush->device_handle = handle[2];
233285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
233385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
233485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2335d7d8464dSRoss Zwisler 	offset += flush->header.length;
23369d27a87eSDan Williams 
23379d27a87eSDan Williams 	/* flush3 (dimm3) */
2338d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23399d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
234085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23419d27a87eSDan Williams 	flush->device_handle = handle[3];
234285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
234385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
234485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2345d7d8464dSRoss Zwisler 	offset += flush->header.length;
23469d27a87eSDan Williams 
2347f81e1d35SDave Jiang 	/* platform capabilities */
2348d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2349f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2350f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2351f81e1d35SDave Jiang 	pcap->highest_capability = 1;
23521273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2353d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2354f81e1d35SDave Jiang 
235520985164SVishal Verma 	if (t->setup_hotplug) {
23563b87356fSDan Williams 		/* dcr-descriptor4: blk */
235720985164SVishal Verma 		dcr = nfit_buf + offset;
235820985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2359d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
23603b87356fSDan Williams 		dcr->region_index = 8+1;
23615dc68e55SDan Williams 		dcr_common_init(dcr);
236220985164SVishal Verma 		dcr->serial_number = ~handle[4];
2363be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
236420985164SVishal Verma 		dcr->windows = 1;
236520985164SVishal Verma 		dcr->window_size = DCR_SIZE;
236620985164SVishal Verma 		dcr->command_offset = 0;
236720985164SVishal Verma 		dcr->command_size = 8;
236820985164SVishal Verma 		dcr->status_offset = 8;
236920985164SVishal Verma 		dcr->status_size = 4;
2370d7d8464dSRoss Zwisler 		offset += dcr->header.length;
237120985164SVishal Verma 
23723b87356fSDan Williams 		/* dcr-descriptor4: pmem */
23733b87356fSDan Williams 		dcr = nfit_buf + offset;
23743b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
23753b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
23763b87356fSDan Williams 				window_size);
23773b87356fSDan Williams 		dcr->region_index = 9+1;
23785dc68e55SDan Williams 		dcr_common_init(dcr);
23793b87356fSDan Williams 		dcr->serial_number = ~handle[4];
23803b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
23813b87356fSDan Williams 		dcr->windows = 0;
2382d7d8464dSRoss Zwisler 		offset += dcr->header.length;
23833b87356fSDan Williams 
238420985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
238520985164SVishal Verma 		bdw = nfit_buf + offset;
238620985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2387d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
23883b87356fSDan Williams 		bdw->region_index = 8+1;
238920985164SVishal Verma 		bdw->windows = 1;
239020985164SVishal Verma 		bdw->offset = 0;
239120985164SVishal Verma 		bdw->size = BDW_SIZE;
239220985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
239320985164SVishal Verma 		bdw->start_address = 0;
2394d7d8464dSRoss Zwisler 		offset += bdw->header.length;
239520985164SVishal Verma 
239620985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
239720985164SVishal Verma 		spa = nfit_buf + offset;
239820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
239920985164SVishal Verma 		spa->header.length = sizeof(*spa);
240020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
240120985164SVishal Verma 		spa->range_index = 10+1;
240220985164SVishal Verma 		spa->address = t->dcr_dma[4];
240320985164SVishal Verma 		spa->length = DCR_SIZE;
2404d7d8464dSRoss Zwisler 		offset += spa->header.length;
240520985164SVishal Verma 
240620985164SVishal Verma 		/*
240720985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
240820985164SVishal Verma 		 * does not actually alias the related block-data-window
240920985164SVishal Verma 		 * regions)
241020985164SVishal Verma 		 */
2411d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
241220985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
241320985164SVishal Verma 		spa->header.length = sizeof(*spa);
241420985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
241520985164SVishal Verma 		spa->range_index = 11+1;
241620985164SVishal Verma 		spa->address = t->spa_set_dma[2];
241720985164SVishal Verma 		spa->length = SPA0_SIZE;
2418d7d8464dSRoss Zwisler 		offset += spa->header.length;
241920985164SVishal Verma 
242020985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2421d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
242220985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
242320985164SVishal Verma 		spa->header.length = sizeof(*spa);
242420985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
242520985164SVishal Verma 		spa->range_index = 12+1;
242620985164SVishal Verma 		spa->address = t->dimm_dma[4];
242720985164SVishal Verma 		spa->length = DIMM_SIZE;
2428d7d8464dSRoss Zwisler 		offset += spa->header.length;
242920985164SVishal Verma 
243020985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
243120985164SVishal Verma 		memdev = nfit_buf + offset;
243220985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
243320985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
243420985164SVishal Verma 		memdev->device_handle = handle[4];
243520985164SVishal Verma 		memdev->physical_id = 4;
243620985164SVishal Verma 		memdev->region_id = 0;
243720985164SVishal Verma 		memdev->range_index = 10+1;
24383b87356fSDan Williams 		memdev->region_index = 8+1;
243920985164SVishal Verma 		memdev->region_size = 0;
244020985164SVishal Verma 		memdev->region_offset = 0;
244120985164SVishal Verma 		memdev->address = 0;
244220985164SVishal Verma 		memdev->interleave_index = 0;
244320985164SVishal Verma 		memdev->interleave_ways = 1;
2444d7d8464dSRoss Zwisler 		offset += memdev->header.length;
244520985164SVishal Verma 
2446d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2447d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
244820985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
244920985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
245020985164SVishal Verma 		memdev->device_handle = handle[4];
245120985164SVishal Verma 		memdev->physical_id = 4;
245220985164SVishal Verma 		memdev->region_id = 0;
245320985164SVishal Verma 		memdev->range_index = 11+1;
24543b87356fSDan Williams 		memdev->region_index = 9+1;
245520985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2456df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
245720985164SVishal Verma 		memdev->address = 0;
245820985164SVishal Verma 		memdev->interleave_index = 0;
245920985164SVishal Verma 		memdev->interleave_ways = 1;
2460ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2461d7d8464dSRoss Zwisler 		offset += memdev->header.length;
246220985164SVishal Verma 
24633b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2464d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
246520985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
246620985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
246720985164SVishal Verma 		memdev->device_handle = handle[4];
246820985164SVishal Verma 		memdev->physical_id = 4;
246920985164SVishal Verma 		memdev->region_id = 0;
247020985164SVishal Verma 		memdev->range_index = 12+1;
24713b87356fSDan Williams 		memdev->region_index = 8+1;
247220985164SVishal Verma 		memdev->region_size = 0;
247320985164SVishal Verma 		memdev->region_offset = 0;
247420985164SVishal Verma 		memdev->address = 0;
247520985164SVishal Verma 		memdev->interleave_index = 0;
247620985164SVishal Verma 		memdev->interleave_ways = 1;
2477d7d8464dSRoss Zwisler 		offset += memdev->header.length;
247820985164SVishal Verma 
247920985164SVishal Verma 		/* flush3 (dimm4) */
248020985164SVishal Verma 		flush = nfit_buf + offset;
248120985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
248285d3fa02SDan Williams 		flush->header.length = flush_hint_size;
248320985164SVishal Verma 		flush->device_handle = handle[4];
248485d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
248585d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
248685d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
248785d3fa02SDan Williams 				+ i * sizeof(u64);
2488d7d8464dSRoss Zwisler 		offset += flush->header.length;
24899741a559SRoss Zwisler 
24909741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
24919741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
249220985164SVishal Verma 	}
249320985164SVishal Verma 
24941526f9e2SRoss Zwisler 	t->nfit_filled = offset;
24951526f9e2SRoss Zwisler 
24969fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
24979fb1a190SDave Jiang 			SPA0_SIZE);
2498f471f1a7SDan Williams 
24996bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2500e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2501e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2502e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2503ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2504ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2505ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
25064cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2507e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2508e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2509e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2510e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
251110246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
2512d46e6a21SDan Williams 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_dsm_mask);
2513d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_dsm_mask);
2514d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_dsm_mask);
2515d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_dsm_mask);
2516bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2517bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2518bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2519bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2520bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2521674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
25223c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
25233c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25243c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
25253c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
25263c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25273c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
25283c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
25293c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2530926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2531926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2532ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
2533ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2534ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
2535ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25366bc75619SDan Williams }
25376bc75619SDan Williams 
25386bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
25396bc75619SDan Williams {
25406b577c9dSLinda Knippers 	size_t offset;
25416bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
25426bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
25436bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
25446bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2545d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25466bc75619SDan Williams 
25476b577c9dSLinda Knippers 	offset = 0;
25486bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
25496bc75619SDan Williams 	spa = nfit_buf + offset;
25506bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25516bc75619SDan Williams 	spa->header.length = sizeof(*spa);
25526bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
25536bc75619SDan Williams 	spa->range_index = 0+1;
25546bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
25556bc75619SDan Williams 	spa->length = SPA2_SIZE;
2556d7d8464dSRoss Zwisler 	offset += spa->header.length;
25576bc75619SDan Williams 
25587bfe97c7SDan Williams 	/* virtual cd region */
2559d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
25607bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25617bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
25627bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
25637bfe97c7SDan Williams 	spa->range_index = 0;
25647bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
25657bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2566d7d8464dSRoss Zwisler 	offset += spa->header.length;
25677bfe97c7SDan Williams 
25686bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
25696bc75619SDan Williams 	memdev = nfit_buf + offset;
25706bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
25716bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2572dafb1048SDan Williams 	memdev->device_handle = handle[5];
25736bc75619SDan Williams 	memdev->physical_id = 0;
25746bc75619SDan Williams 	memdev->region_id = 0;
25756bc75619SDan Williams 	memdev->range_index = 0+1;
25766bc75619SDan Williams 	memdev->region_index = 0+1;
25776bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
25786bc75619SDan Williams 	memdev->region_offset = 0;
25796bc75619SDan Williams 	memdev->address = 0;
25806bc75619SDan Williams 	memdev->interleave_index = 0;
25816bc75619SDan Williams 	memdev->interleave_ways = 1;
258258138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
258358138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2584f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2585d7d8464dSRoss Zwisler 	offset += memdev->header.length;
25866bc75619SDan Williams 
25876bc75619SDan Williams 	/* dcr-descriptor0 */
25886bc75619SDan Williams 	dcr = nfit_buf + offset;
25896bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
25903b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
25913b87356fSDan Williams 			window_size);
25926bc75619SDan Williams 	dcr->region_index = 0+1;
25935dc68e55SDan Williams 	dcr_common_init(dcr);
2594dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2595be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
25966bc75619SDan Williams 	dcr->windows = 0;
2597ac40b675SDan Williams 	offset += dcr->header.length;
2598d7d8464dSRoss Zwisler 
2599ac40b675SDan Williams 	memdev = nfit_buf + offset;
2600ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2601ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2602ac40b675SDan Williams 	memdev->device_handle = handle[6];
2603ac40b675SDan Williams 	memdev->physical_id = 0;
2604ac40b675SDan Williams 	memdev->region_id = 0;
2605ac40b675SDan Williams 	memdev->range_index = 0;
2606ac40b675SDan Williams 	memdev->region_index = 0+2;
2607ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2608ac40b675SDan Williams 	memdev->region_offset = 0;
2609ac40b675SDan Williams 	memdev->address = 0;
2610ac40b675SDan Williams 	memdev->interleave_index = 0;
2611ac40b675SDan Williams 	memdev->interleave_ways = 1;
2612ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2613d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2614ac40b675SDan Williams 
2615ac40b675SDan Williams 	/* dcr-descriptor1 */
2616ac40b675SDan Williams 	dcr = nfit_buf + offset;
2617ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2618ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2619ac40b675SDan Williams 			window_size);
2620ac40b675SDan Williams 	dcr->region_index = 0+2;
2621ac40b675SDan Williams 	dcr_common_init(dcr);
2622ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2623ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2624ac40b675SDan Williams 	dcr->windows = 0;
2625d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2626ac40b675SDan Williams 
26279741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
26289741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
26299741a559SRoss Zwisler 
26301526f9e2SRoss Zwisler 	t->nfit_filled = offset;
26311526f9e2SRoss Zwisler 
26329fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
26339fb1a190SDave Jiang 			SPA2_SIZE);
2634f471f1a7SDan Williams 
2635d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2636e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2637e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2638e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2639e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2640674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
26419484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
26429484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26439484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26446bc75619SDan Williams }
26456bc75619SDan Williams 
26466bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
26476bc75619SDan Williams 		void *iobuf, u64 len, int rw)
26486bc75619SDan Williams {
26496bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
26506bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
26516bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
26526bc75619SDan Williams 	unsigned int lane;
26536bc75619SDan Williams 
26546bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
26556bc75619SDan Williams 	if (rw)
265667a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
265767a3e8feSRoss Zwisler 	else {
265867a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
265967a3e8feSRoss Zwisler 
26605deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
26615deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
266267a3e8feSRoss Zwisler 	}
26636bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
26646bc75619SDan Williams 
26656bc75619SDan Williams 	return 0;
26666bc75619SDan Williams }
26676bc75619SDan Williams 
2668a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2669a7de92daSDan Williams 
2670a7de92daSDan Williams union acpi_object *result;
2671a7de92daSDan Williams 
2672a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
267394116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2674a7de92daSDan Williams {
2675a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2676a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2677a7de92daSDan Williams 
2678a7de92daSDan Williams 	return result;
2679a7de92daSDan Williams }
2680a7de92daSDan Williams 
2681a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2682a7de92daSDan Williams {
2683a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2684a7de92daSDan Williams 	if (!result)
2685a7de92daSDan Williams 		return -ENOMEM;
2686a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2687a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2688a7de92daSDan Williams 	result->buffer.length = size;
2689a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2690a7de92daSDan Williams 	memset(buf, 0, size);
2691a7de92daSDan Williams 	return 0;
2692a7de92daSDan Williams }
2693a7de92daSDan Williams 
2694a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2695a7de92daSDan Williams {
2696a7de92daSDan Williams 	int rc, cmd_rc;
2697a7de92daSDan Williams 	struct nvdimm *nvdimm;
2698a7de92daSDan Williams 	struct acpi_device *adev;
2699a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2700a7de92daSDan Williams 	struct nd_ars_record *record;
2701a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2702a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2703a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2704a7de92daSDan Williams 	union {
2705a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2706fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2707a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2708a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2709a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2710a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2711a7de92daSDan Williams 	} cmds;
2712a7de92daSDan Williams 
2713a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2714a7de92daSDan Williams 	if (!adev)
2715a7de92daSDan Williams 		return -ENOMEM;
2716a7de92daSDan Williams 	*adev = (struct acpi_device) {
2717a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2718a7de92daSDan Williams 		.dev = {
2719a7de92daSDan Williams 			.init_name = "test-adev",
2720a7de92daSDan Williams 		},
2721a7de92daSDan Williams 	};
2722a7de92daSDan Williams 
2723a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2724a7de92daSDan Williams 	if (!acpi_desc)
2725a7de92daSDan Williams 		return -ENOMEM;
2726a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2727a7de92daSDan Williams 		.nd_desc = {
2728a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2729a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2730a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
273110246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
273210246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2733a7de92daSDan Williams 			.module = THIS_MODULE,
2734a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2735a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
2736d46e6a21SDan Williams 		},
27379fb1a190SDave Jiang 		.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
27389fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_SET
27399fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
27409fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_GET,
2741a7de92daSDan Williams 		.dev = &adev->dev,
2742a7de92daSDan Williams 	};
2743a7de92daSDan Williams 
2744a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2745a7de92daSDan Williams 	if (!nfit_mem)
2746a7de92daSDan Williams 		return -ENOMEM;
2747a7de92daSDan Williams 
2748a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2749a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2750a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2751a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2752a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2753a7de92daSDan Williams 		.adev = adev,
2754a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2755a7de92daSDan Williams 		.dsm_mask = mask,
2756a7de92daSDan Williams 	};
2757a7de92daSDan Williams 
2758a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2759a7de92daSDan Williams 	if (!nvdimm)
2760a7de92daSDan Williams 		return -ENOMEM;
2761a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2762a7de92daSDan Williams 		.provider_data = nfit_mem,
2763a7de92daSDan Williams 		.cmd_mask = mask,
2764a7de92daSDan Williams 		.dev = {
2765a7de92daSDan Williams 			.init_name = "test-dimm",
2766a7de92daSDan Williams 		},
2767a7de92daSDan Williams 	};
2768a7de92daSDan Williams 
2769a7de92daSDan Williams 
2770a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2771a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2772a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2773a7de92daSDan Williams 		.status = 0,
2774a7de92daSDan Williams 		.config_size = SZ_128K,
2775a7de92daSDan Williams 		.max_xfer = SZ_4K,
2776a7de92daSDan Williams 	};
2777a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2778a7de92daSDan Williams 	if (rc)
2779a7de92daSDan Williams 		return rc;
2780a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2781a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2782a7de92daSDan Williams 
2783a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2784a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2785a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2786a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2787a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2788a7de92daSDan Williams 		return -EIO;
2789a7de92daSDan Williams 	}
2790a7de92daSDan Williams 
2791a7de92daSDan Williams 
2792a7de92daSDan Williams 	/* test ars_status with zero output */
2793a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2794a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2795a7de92daSDan Williams 		.out_length = 0,
2796a7de92daSDan Williams 	};
2797a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2798a7de92daSDan Williams 	if (rc)
2799a7de92daSDan Williams 		return rc;
2800a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2801a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2802a7de92daSDan Williams 
2803a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2804a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2805a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2806a7de92daSDan Williams 		return -EIO;
2807a7de92daSDan Williams 	}
2808a7de92daSDan Williams 
2809a7de92daSDan Williams 
2810a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2811a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2812a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2813a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2814a7de92daSDan Williams 	};
2815a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2816a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2817a7de92daSDan Williams 	if (rc)
2818a7de92daSDan Williams 		return rc;
2819a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2820a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2821a7de92daSDan Williams 
2822a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2823a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2824a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2825a7de92daSDan Williams 		return -EIO;
2826a7de92daSDan Williams 	}
2827a7de92daSDan Williams 
2828a7de92daSDan Williams 
2829a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2830a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2831a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2832a7de92daSDan Williams 		.out_length = cmd_size - 4,
2833a7de92daSDan Williams 	};
2834a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2835a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2836a7de92daSDan Williams 		.length = test_val,
2837a7de92daSDan Williams 	};
2838a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2839a7de92daSDan Williams 	if (rc)
2840a7de92daSDan Williams 		return rc;
2841a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2842a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2843a7de92daSDan Williams 
2844a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2845a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2846a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2847a7de92daSDan Williams 		return -EIO;
2848a7de92daSDan Williams 	}
2849a7de92daSDan Williams 
2850a7de92daSDan Williams 
2851a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2852a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2853a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2854a7de92daSDan Williams 		.out_length = cmd_size,
2855a7de92daSDan Williams 	};
2856a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2857a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2858a7de92daSDan Williams 		.length = test_val,
2859a7de92daSDan Williams 	};
2860a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2861a7de92daSDan Williams 	if (rc)
2862a7de92daSDan Williams 		return rc;
2863a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2864a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2865a7de92daSDan Williams 
2866a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2867a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2868a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2869a7de92daSDan Williams 		return -EIO;
2870a7de92daSDan Williams 	}
2871a7de92daSDan Williams 
2872a7de92daSDan Williams 
2873a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2874a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2875a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2876a7de92daSDan Williams 		.status = 1 << 16,
2877a7de92daSDan Williams 	};
2878a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2879a7de92daSDan Williams 	if (rc)
2880a7de92daSDan Williams 		return rc;
2881a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2882a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2883a7de92daSDan Williams 
2884a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2885a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2886a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2887a7de92daSDan Williams 		return -EIO;
2888a7de92daSDan Williams 	}
2889a7de92daSDan Williams 
2890fb2a1748SDan Williams 	/* test clear error */
2891fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2892fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2893fb2a1748SDan Williams 		.length = 512,
2894fb2a1748SDan Williams 		.cleared = 512,
2895fb2a1748SDan Williams 	};
2896fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2897fb2a1748SDan Williams 	if (rc)
2898fb2a1748SDan Williams 		return rc;
2899fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2900fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2901fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2902fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2903fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2904fb2a1748SDan Williams 		return -EIO;
2905fb2a1748SDan Williams 	}
2906fb2a1748SDan Williams 
2907a7de92daSDan Williams 	return 0;
2908a7de92daSDan Williams }
2909a7de92daSDan Williams 
29106bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
29116bc75619SDan Williams {
29126bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
29136bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
29146bc75619SDan Williams 	struct device *dev = &pdev->dev;
29156bc75619SDan Williams 	struct nfit_test *nfit_test;
2916231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2917c14a868aSDan Williams 	union acpi_object *obj;
29186bc75619SDan Williams 	int rc;
29196bc75619SDan Williams 
2920a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2921a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2922a7de92daSDan Williams 		if (rc)
2923a7de92daSDan Williams 			return rc;
2924a7de92daSDan Williams 	}
2925a7de92daSDan Williams 
29266bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
29276bc75619SDan Williams 
29286bc75619SDan Williams 	/* common alloc */
29296bc75619SDan Williams 	if (nfit_test->num_dcr) {
29306bc75619SDan Williams 		int num = nfit_test->num_dcr;
29316bc75619SDan Williams 
29326bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
29336bc75619SDan Williams 				GFP_KERNEL);
29346bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29356bc75619SDan Williams 				GFP_KERNEL);
29369d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
29379d27a87eSDan Williams 				GFP_KERNEL);
29389d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29399d27a87eSDan Williams 				GFP_KERNEL);
29406bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
29416bc75619SDan Williams 				GFP_KERNEL);
29426bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
29436bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29446bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
29456bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
29466bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
29476bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2948ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2949ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2950ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2951ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2952ed07c433SDan Williams 				GFP_KERNEL);
2953bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2954bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
29556bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
29566bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
29579d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2958bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2959bfbaa952SDave Jiang 				&& nfit_test->fw)
29606bc75619SDan Williams 			/* pass */;
29616bc75619SDan Williams 		else
29626bc75619SDan Williams 			return -ENOMEM;
29636bc75619SDan Williams 	}
29646bc75619SDan Williams 
29656bc75619SDan Williams 	if (nfit_test->num_pm) {
29666bc75619SDan Williams 		int num = nfit_test->num_pm;
29676bc75619SDan Williams 
29686bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
29696bc75619SDan Williams 				GFP_KERNEL);
29706bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
29716bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29726bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
29736bc75619SDan Williams 			/* pass */;
29746bc75619SDan Williams 		else
29756bc75619SDan Williams 			return -ENOMEM;
29766bc75619SDan Williams 	}
29776bc75619SDan Williams 
29786bc75619SDan Williams 	/* per-nfit specific alloc */
29796bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
29806bc75619SDan Williams 		return -ENOMEM;
29816bc75619SDan Williams 
29826bc75619SDan Williams 	nfit_test->setup(nfit_test);
29836bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2984a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
29856bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
29866bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2987a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2988bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2989a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
29906bc75619SDan Williams 
2991e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
29921526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
299358cd71b4SDan Williams 	if (rc)
299420985164SVishal Verma 		return rc;
299520985164SVishal Verma 
2996fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2997fbabd829SDan Williams 	if (rc)
2998fbabd829SDan Williams 		return rc;
2999fbabd829SDan Williams 
300020985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
300120985164SVishal Verma 		return 0;
300220985164SVishal Verma 
300320985164SVishal Verma 	nfit_test->setup_hotplug = 1;
300420985164SVishal Verma 	nfit_test->setup(nfit_test);
300520985164SVishal Verma 
3006c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3007c14a868aSDan Williams 	if (!obj)
3008c14a868aSDan Williams 		return -ENOMEM;
3009c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
3010c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
3011c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
3012c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
3013c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
3014231bf117SDan Williams 
3015231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
3016231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
3017231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
3018231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
3019231bf117SDan Williams 		int i;
3020231bf117SDan Williams 
3021af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
3022231bf117SDan Williams 			if (nfit_handle == handle[i])
3023231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
3024231bf117SDan Williams 						nfit_mem);
3025231bf117SDan Williams 	}
3026231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
30276bc75619SDan Williams 
30286bc75619SDan Williams 	return 0;
30296bc75619SDan Williams }
30306bc75619SDan Williams 
30316bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
30326bc75619SDan Williams {
30336bc75619SDan Williams 	return 0;
30346bc75619SDan Williams }
30356bc75619SDan Williams 
30366bc75619SDan Williams static void nfit_test_release(struct device *dev)
30376bc75619SDan Williams {
30386bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
30396bc75619SDan Williams 
30406bc75619SDan Williams 	kfree(nfit_test);
30416bc75619SDan Williams }
30426bc75619SDan Williams 
30436bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
30446bc75619SDan Williams 	{ KBUILD_MODNAME },
30456bc75619SDan Williams 	{ },
30466bc75619SDan Williams };
30476bc75619SDan Williams 
30486bc75619SDan Williams static struct platform_driver nfit_test_driver = {
30496bc75619SDan Williams 	.probe = nfit_test_probe,
30506bc75619SDan Williams 	.remove = nfit_test_remove,
30516bc75619SDan Williams 	.driver = {
30526bc75619SDan Williams 		.name = KBUILD_MODNAME,
30536bc75619SDan Williams 	},
30546bc75619SDan Williams 	.id_table = nfit_test_id,
30556bc75619SDan Williams };
30566bc75619SDan Williams 
30575d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
30585d8beee2SDan Williams 
30595d8beee2SDan Williams enum INJECT {
30605d8beee2SDan Williams 	INJECT_NONE,
30615d8beee2SDan Williams 	INJECT_SRC,
30625d8beee2SDan Williams 	INJECT_DST,
30635d8beee2SDan Williams };
30645d8beee2SDan Williams 
30655d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
30665d8beee2SDan Williams {
30675d8beee2SDan Williams 	size_t i;
30685d8beee2SDan Williams 
30695d8beee2SDan Williams 	memset(dst, 0xff, size);
30705d8beee2SDan Williams 	for (i = 0; i < size; i++)
30715d8beee2SDan Williams 		src[i] = (char) i;
30725d8beee2SDan Williams }
30735d8beee2SDan Williams 
30745d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
30755d8beee2SDan Williams 		size_t size, unsigned long rem)
30765d8beee2SDan Williams {
30775d8beee2SDan Williams 	size_t i;
30785d8beee2SDan Williams 
30795d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
30805d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
30815d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
30825d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
30835d8beee2SDan Williams 					(unsigned char) i);
30845d8beee2SDan Williams 			return false;
30855d8beee2SDan Williams 		}
30865d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
30875d8beee2SDan Williams 		if (dst[i] != 0xffU) {
30885d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
30895d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
30905d8beee2SDan Williams 			return false;
30915d8beee2SDan Williams 		}
30925d8beee2SDan Williams 	return true;
30935d8beee2SDan Williams }
30945d8beee2SDan Williams 
30955d8beee2SDan Williams void mcsafe_test(void)
30965d8beee2SDan Williams {
30975d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
30985d8beee2SDan Williams 	enum INJECT inj;
30995d8beee2SDan Williams 
31005d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
31015d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
31025d8beee2SDan Williams 	} else {
31035d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
31045d8beee2SDan Williams 		return;
31055d8beee2SDan Williams 	}
31065d8beee2SDan Williams 
31075d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
31085d8beee2SDan Williams 		int i;
31095d8beee2SDan Williams 
31105d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
31115d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
31125d8beee2SDan Williams 			unsigned long expect, rem;
31135d8beee2SDan Williams 			void *src, *dst;
31145d8beee2SDan Williams 			bool valid;
31155d8beee2SDan Williams 
31165d8beee2SDan Williams 			switch (inj) {
31175d8beee2SDan Williams 			case INJECT_NONE:
31185d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31195d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31205d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31215d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31225d8beee2SDan Williams 				expect = 0;
31235d8beee2SDan Williams 				break;
31245d8beee2SDan Williams 			case INJECT_SRC:
31255d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
31265d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31275d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31285d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31295d8beee2SDan Williams 				expect = 512 - i;
31305d8beee2SDan Williams 				break;
31315d8beee2SDan Williams 			case INJECT_DST:
31325d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31335d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
31345d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
31355d8beee2SDan Williams 				src = &mcsafe_buf[1024];
31365d8beee2SDan Williams 				expect = 512 - i;
31375d8beee2SDan Williams 				break;
31385d8beee2SDan Williams 			}
31395d8beee2SDan Williams 
31405d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
31415d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
31425d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
31435d8beee2SDan Williams 			if (rem == expect && valid)
31445d8beee2SDan Williams 				continue;
31455d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
31465d8beee2SDan Williams 					__func__,
31475d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
31485d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
31495d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
31505d8beee2SDan Williams 					expect);
31515d8beee2SDan Williams 		}
31525d8beee2SDan Williams 	}
31535d8beee2SDan Williams 
31545d8beee2SDan Williams 	mcsafe_inject_src(NULL);
31555d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
31565d8beee2SDan Williams }
31575d8beee2SDan Williams 
31586bc75619SDan Williams static __init int nfit_test_init(void)
31596bc75619SDan Williams {
31606bc75619SDan Williams 	int rc, i;
31616bc75619SDan Williams 
31620fb5c8dfSDan Williams 	pmem_test();
31630fb5c8dfSDan Williams 	libnvdimm_test();
31640fb5c8dfSDan Williams 	acpi_nfit_test();
31650fb5c8dfSDan Williams 	device_dax_test();
31665d8beee2SDan Williams 	mcsafe_test();
316792f6f2d7SVishal Verma 	dax_pmem_test();
316892f6f2d7SVishal Verma 	dax_pmem_core_test();
3169c0e71d60SJan Kara #ifdef CONFIG_DEV_DAX_PMEM_COMPAT
317092f6f2d7SVishal Verma 	dax_pmem_compat_test();
3171c0e71d60SJan Kara #endif
31720fb5c8dfSDan Williams 
3173a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3174231bf117SDan Williams 
31759fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
31769fb1a190SDave Jiang 	if (!nfit_wq)
31779fb1a190SDave Jiang 		return -ENOMEM;
31789fb1a190SDave Jiang 
3179a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
3180a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
3181a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
3182a7de92daSDan Williams 		goto err_register;
3183a7de92daSDan Williams 	}
31846bc75619SDan Williams 
3185e3f5df76SDan Williams 	nfit_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
3186e3f5df76SDan Williams 	if (!nfit_pool) {
3187e3f5df76SDan Williams 		rc = -ENOMEM;
3188e3f5df76SDan Williams 		goto err_register;
3189e3f5df76SDan Williams 	}
3190e3f5df76SDan Williams 
3191e3f5df76SDan Williams 	if (gen_pool_add(nfit_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
3192e3f5df76SDan Williams 		rc = -ENOMEM;
3193e3f5df76SDan Williams 		goto err_register;
3194e3f5df76SDan Williams 	}
3195e3f5df76SDan Williams 
31966bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
31976bc75619SDan Williams 		struct nfit_test *nfit_test;
31986bc75619SDan Williams 		struct platform_device *pdev;
31996bc75619SDan Williams 
32006bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
32016bc75619SDan Williams 		if (!nfit_test) {
32026bc75619SDan Williams 			rc = -ENOMEM;
32036bc75619SDan Williams 			goto err_register;
32046bc75619SDan Williams 		}
32056bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
32069fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
32076bc75619SDan Williams 		switch (i) {
32086bc75619SDan Williams 		case 0:
32096bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3210dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
32116bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
32126bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
32136bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
32146bc75619SDan Williams 			break;
32156bc75619SDan Williams 		case 1:
3216a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3217dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3218ac40b675SDan Williams 			nfit_test->num_dcr = 2;
32196bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
32206bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
32216bc75619SDan Williams 			break;
32226bc75619SDan Williams 		default:
32236bc75619SDan Williams 			rc = -EINVAL;
32246bc75619SDan Williams 			goto err_register;
32256bc75619SDan Williams 		}
32266bc75619SDan Williams 		pdev = &nfit_test->pdev;
32276bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
32286bc75619SDan Williams 		pdev->id = i;
32296bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
32306bc75619SDan Williams 		rc = platform_device_register(pdev);
32316bc75619SDan Williams 		if (rc) {
32326bc75619SDan Williams 			put_device(&pdev->dev);
32336bc75619SDan Williams 			goto err_register;
32346bc75619SDan Williams 		}
32358b06b884SDan Williams 		get_device(&pdev->dev);
32366bc75619SDan Williams 
32376bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
32386bc75619SDan Williams 		if (rc)
32396bc75619SDan Williams 			goto err_register;
32406bc75619SDan Williams 
32416bc75619SDan Williams 		instances[i] = nfit_test;
32429fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
32436bc75619SDan Williams 	}
32446bc75619SDan Williams 
32456bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
32466bc75619SDan Williams 	if (rc)
32476bc75619SDan Williams 		goto err_register;
32486bc75619SDan Williams 	return 0;
32496bc75619SDan Williams 
32506bc75619SDan Williams  err_register:
3251e3f5df76SDan Williams 	if (nfit_pool)
3252e3f5df76SDan Williams 		gen_pool_destroy(nfit_pool);
3253e3f5df76SDan Williams 
32549fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32556bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32566bc75619SDan Williams 		if (instances[i])
32576bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
32586bc75619SDan Williams 	nfit_test_teardown();
32598b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32608b06b884SDan Williams 		if (instances[i])
32618b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
32628b06b884SDan Williams 
32636bc75619SDan Williams 	return rc;
32646bc75619SDan Williams }
32656bc75619SDan Williams 
32666bc75619SDan Williams static __exit void nfit_test_exit(void)
32676bc75619SDan Williams {
32686bc75619SDan Williams 	int i;
32696bc75619SDan Williams 
32709fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
32719fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32726bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32736bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
32748b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
32756bc75619SDan Williams 	nfit_test_teardown();
32768b06b884SDan Williams 
3277e3f5df76SDan Williams 	gen_pool_destroy(nfit_pool);
3278e3f5df76SDan Williams 
32798b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32808b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3281231bf117SDan Williams 	class_destroy(nfit_test_dimm);
32826bc75619SDan Williams }
32836bc75619SDan Williams 
32846bc75619SDan Williams module_init(nfit_test_init);
32856bc75619SDan Williams module_exit(nfit_test_exit);
32866bc75619SDan Williams MODULE_LICENSE("GPL v2");
32876bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3288