xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 0d47c4dfe5431abd04951a65845b3d989a704f63)
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 
1195*0d47c4dfSDan Williams static void nfit_ctl_dbg(struct acpi_nfit_desc *acpi_desc,
1196*0d47c4dfSDan Williams 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1197*0d47c4dfSDan Williams 		unsigned int len)
1198*0d47c4dfSDan Williams {
1199*0d47c4dfSDan Williams 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
1200*0d47c4dfSDan Williams 	unsigned int func = cmd;
1201*0d47c4dfSDan Williams 	unsigned int family = 0;
1202*0d47c4dfSDan Williams 
1203*0d47c4dfSDan Williams 	if (cmd == ND_CMD_CALL) {
1204*0d47c4dfSDan Williams 		struct nd_cmd_pkg *pkg = buf;
1205*0d47c4dfSDan Williams 
1206*0d47c4dfSDan Williams 		len = pkg->nd_size_in;
1207*0d47c4dfSDan Williams 		family = pkg->nd_family;
1208*0d47c4dfSDan Williams 		buf = pkg->nd_payload;
1209*0d47c4dfSDan Williams 		func = pkg->nd_command;
1210*0d47c4dfSDan Williams 	}
1211*0d47c4dfSDan Williams 	dev_dbg(&t->pdev.dev, "%s family: %d cmd: %d: func: %d input length: %d\n",
1212*0d47c4dfSDan Williams 			nvdimm ? nvdimm_name(nvdimm) : "bus", family, cmd, func,
1213*0d47c4dfSDan Williams 			len);
1214*0d47c4dfSDan Williams 	print_hex_dump_debug("nvdimm in  ", DUMP_PREFIX_OFFSET, 16, 4,
1215*0d47c4dfSDan Williams 			buf, min(len, 256u), true);
1216*0d47c4dfSDan Williams }
1217*0d47c4dfSDan Williams 
121839c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
121939c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1220aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
122139c686b8SVishal Verma {
122239c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
122339c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
12246634fb06SDan Williams 	unsigned int func = cmd;
1225f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1226f471f1a7SDan Williams 
1227f471f1a7SDan Williams 	if (!cmd_rc)
1228f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1229f471f1a7SDan Williams 	*cmd_rc = 0;
123039c686b8SVishal Verma 
1231*0d47c4dfSDan Williams 	nfit_ctl_dbg(acpi_desc, nvdimm, cmd, buf, buf_len);
1232*0d47c4dfSDan Williams 
123339c686b8SVishal Verma 	if (nvdimm) {
123439c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1235e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
123639c686b8SVishal Verma 
12376634fb06SDan Williams 		if (!nfit_mem)
12386634fb06SDan Williams 			return -ENOTTY;
12396634fb06SDan Williams 
12406634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
12416634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
12426634fb06SDan Williams 
12436634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
12446634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
12456634fb06SDan Williams 			func = call_pkg->nd_command;
12466634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
12476634fb06SDan Williams 				return -ENOTTY;
1248bfbaa952SDave Jiang 
1249bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1250bfbaa952SDave Jiang 			if (i < 0)
1251bfbaa952SDave Jiang 				return i;
125224770658SDan Williams 			if (i >= NUM_DCR) {
125324770658SDan Williams 				dev_WARN_ONCE(&t->pdev.dev, 1,
125424770658SDan Williams 						"ND_CMD_CALL only valid for nfit_test0\n");
125524770658SDan Williams 				return -EINVAL;
125624770658SDan Williams 			}
1257bfbaa952SDave Jiang 
1258bfbaa952SDave Jiang 			switch (func) {
12593c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
12603c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
12613c13e2acSDave Jiang 						buf, buf_len, i);
12623c13e2acSDave Jiang 				break;
12633c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
12643c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
12653c13e2acSDave Jiang 						buf, buf_len, i);
12663c13e2acSDave Jiang 				break;
12673c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
12683c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
12693c13e2acSDave Jiang 						buf, buf_len, i);
12703c13e2acSDave Jiang 				break;
12713c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
12723c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
12733c13e2acSDave Jiang 						buf, buf_len, i);
12743c13e2acSDave Jiang 				break;
12753c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
12763c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
12773c13e2acSDave Jiang 						buf, buf_len, i);
12783c13e2acSDave Jiang 				break;
12793c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
12803c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
12813c13e2acSDave Jiang 						buf, buf_len, i);
12823c13e2acSDave Jiang 				break;
1283926f7480SDave Jiang 			case NVDIMM_INTEL_OVERWRITE:
1284926f7480SDave Jiang 				rc = nd_intel_test_cmd_overwrite(t,
128524770658SDan Williams 						buf, buf_len, i);
1286926f7480SDave Jiang 				break;
1287926f7480SDave Jiang 			case NVDIMM_INTEL_QUERY_OVERWRITE:
1288926f7480SDave Jiang 				rc = nd_intel_test_cmd_query_overwrite(t,
128924770658SDan Williams 						buf, buf_len, i);
1290926f7480SDave Jiang 				break;
1291ecaa4a97SDave Jiang 			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
1292ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_set_pass(t,
1293ecaa4a97SDave Jiang 						buf, buf_len, i);
1294ecaa4a97SDave Jiang 				break;
1295ecaa4a97SDave Jiang 			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
1296ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_secure_erase(t,
1297ecaa4a97SDave Jiang 						buf, buf_len, i);
1298ecaa4a97SDave Jiang 				break;
1299674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
130039611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1301674d8bdeSDave Jiang 						buf, buf_len);
130239611e83SDan Williams 				break;
1303bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
130439611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
130524770658SDan Williams 						buf_len, i);
130639611e83SDan Williams 				break;
1307bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
130839611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
130924770658SDan Williams 						buf_len, i);
131039611e83SDan Williams 				break;
1311bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
131239611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
131324770658SDan Williams 						buf_len, i);
131439611e83SDan Williams 				break;
1315bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
131639611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
131724770658SDan Williams 						buf_len, i);
131839611e83SDan Williams 				break;
1319bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
132039611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
132124770658SDan Williams 						buf_len, i);
132239611e83SDan Williams 				break;
1323bfbaa952SDave Jiang 			case ND_INTEL_SMART:
132439611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
132524770658SDan Williams 						&t->smart[i]);
132639611e83SDan Williams 				break;
1327bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
132839611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1329bfbaa952SDave Jiang 						buf_len,
133024770658SDan Williams 						&t->smart_threshold[i]);
133139611e83SDan Williams 				break;
1332bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
133339611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1334bfbaa952SDave Jiang 						buf_len,
133524770658SDan Williams 						&t->smart_threshold[i],
133624770658SDan Williams 						&t->smart[i],
1337bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
133839611e83SDan Williams 				break;
13394cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
134039611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
13414cf260fcSVishal Verma 						buf_len,
134224770658SDan Williams 						&t->smart_threshold[i],
134324770658SDan Williams 						&t->smart[i],
13444cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
134539611e83SDan Williams 				break;
1346bfbaa952SDave Jiang 			default:
1347bfbaa952SDave Jiang 				return -ENOTTY;
1348bfbaa952SDave Jiang 			}
134939611e83SDan Williams 			return override_return_code(i, func, rc);
13506634fb06SDan Williams 		}
13516634fb06SDan Williams 
13526634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
13536634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
135439c686b8SVishal Verma 			return -ENOTTY;
135539c686b8SVishal Verma 
1356bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1357bfbaa952SDave Jiang 		if (i < 0)
1358bfbaa952SDave Jiang 			return i;
135973606afdSDan Williams 
13606634fb06SDan Williams 		switch (func) {
136139c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
136239c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
136339c686b8SVishal Verma 			break;
136439c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
136539c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1366dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
136739c686b8SVishal Verma 			break;
136839c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
136939c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1370dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
137139c686b8SVishal Verma 			break;
13726bc75619SDan Williams 		default:
13736bc75619SDan Williams 			return -ENOTTY;
13746bc75619SDan Williams 		}
137539611e83SDan Williams 		return override_return_code(i, func, rc);
137639c686b8SVishal Verma 	} else {
1377f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
137810246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
137910246dc8SYasunori Goto 
138010246dc8SYasunori Goto 		if (!nd_desc)
138110246dc8SYasunori Goto 			return -ENOTTY;
138210246dc8SYasunori Goto 
138310246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
138410246dc8SYasunori Goto 			func = call_pkg->nd_command;
138510246dc8SYasunori Goto 
138610246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
138710246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
138810246dc8SYasunori Goto 
138910246dc8SYasunori Goto 			switch (func) {
139010246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
139110246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
139210246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
139310246dc8SYasunori Goto 				return rc;
13949fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
13959fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
13969fb1a190SDave Jiang 					buf_len);
13979fb1a190SDave Jiang 				return rc;
13989fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
13999fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
14009fb1a190SDave Jiang 					buf_len);
14019fb1a190SDave Jiang 				return rc;
14029fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
14039fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
14049fb1a190SDave Jiang 					buf_len);
14059fb1a190SDave Jiang 				return rc;
140610246dc8SYasunori Goto 			default:
140710246dc8SYasunori Goto 				return -ENOTTY;
140810246dc8SYasunori Goto 			}
140910246dc8SYasunori Goto 		}
1410f471f1a7SDan Williams 
1411e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
141239c686b8SVishal Verma 			return -ENOTTY;
141339c686b8SVishal Verma 
14146634fb06SDan Williams 		switch (func) {
141539c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
141639c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
141739c686b8SVishal Verma 			break;
141839c686b8SVishal Verma 		case ND_CMD_ARS_START:
14199fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
14209fb1a190SDave Jiang 					buf_len, cmd_rc);
142139c686b8SVishal Verma 			break;
142239c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1423f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1424f471f1a7SDan Williams 					cmd_rc);
142539c686b8SVishal Verma 			break;
1426d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
14275e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1428d4f32367SDan Williams 			break;
142939c686b8SVishal Verma 		default:
143039c686b8SVishal Verma 			return -ENOTTY;
143139c686b8SVishal Verma 		}
143239c686b8SVishal Verma 	}
14336bc75619SDan Williams 
14346bc75619SDan Williams 	return rc;
14356bc75619SDan Williams }
14366bc75619SDan Williams 
14376bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
14386bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
14396bc75619SDan Williams 
14406bc75619SDan Williams static void release_nfit_res(void *data)
14416bc75619SDan Williams {
14426bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
14436bc75619SDan Williams 
14446bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14456bc75619SDan Williams 	list_del(&nfit_res->list);
14466bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14476bc75619SDan Williams 
1448e3f5df76SDan Williams 	if (resource_size(&nfit_res->res) >= DIMM_SIZE)
1449e3f5df76SDan Williams 		gen_pool_free(nfit_pool, nfit_res->res.start,
1450e3f5df76SDan Williams 				resource_size(&nfit_res->res));
14516bc75619SDan Williams 	vfree(nfit_res->buf);
14526bc75619SDan Williams 	kfree(nfit_res);
14536bc75619SDan Williams }
14546bc75619SDan Williams 
14556bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
14566bc75619SDan Williams 		void *buf)
14576bc75619SDan Williams {
14586bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
14596bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
14606bc75619SDan Williams 			GFP_KERNEL);
14616bc75619SDan Williams 	int rc;
14626bc75619SDan Williams 
1463e3f5df76SDan Williams 	if (!buf || !nfit_res || !*dma)
14646bc75619SDan Williams 		goto err;
14656bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
14666bc75619SDan Williams 	if (rc)
14676bc75619SDan Williams 		goto err;
14686bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
14696bc75619SDan Williams 	memset(buf, 0, size);
14706bc75619SDan Williams 	nfit_res->dev = dev;
14716bc75619SDan Williams 	nfit_res->buf = buf;
1472bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1473bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1474bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1475bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1476bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
14776bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14786bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
14796bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14806bc75619SDan Williams 
14816bc75619SDan Williams 	return nfit_res->buf;
14826bc75619SDan Williams  err:
1483e3f5df76SDan Williams 	if (*dma && size >= DIMM_SIZE)
1484e3f5df76SDan Williams 		gen_pool_free(nfit_pool, *dma, size);
1485ee8520feSDan Williams 	if (buf)
14866bc75619SDan Williams 		vfree(buf);
14876bc75619SDan Williams 	kfree(nfit_res);
14886bc75619SDan Williams 	return NULL;
14896bc75619SDan Williams }
14906bc75619SDan Williams 
14916bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
14926bc75619SDan Williams {
1493e3f5df76SDan Williams 	struct genpool_data_align data = {
1494e3f5df76SDan Williams 		.align = SZ_128M,
1495e3f5df76SDan Williams 	};
14966bc75619SDan Williams 	void *buf = vmalloc(size);
14976bc75619SDan Williams 
1498e3f5df76SDan Williams 	if (size >= DIMM_SIZE)
1499e3f5df76SDan Williams 		*dma = gen_pool_alloc_algo(nfit_pool, size,
1500e3f5df76SDan Williams 				gen_pool_first_fit_align, &data);
1501e3f5df76SDan Williams 	else
15026bc75619SDan Williams 		*dma = (unsigned long) buf;
15036bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
15046bc75619SDan Williams }
15056bc75619SDan Williams 
15066bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
15076bc75619SDan Williams {
15086bc75619SDan Williams 	int i;
15096bc75619SDan Williams 
15106bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
15116bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
15126bc75619SDan Williams 		struct nfit_test *t = instances[i];
15136bc75619SDan Williams 
15146bc75619SDan Williams 		if (!t)
15156bc75619SDan Williams 			continue;
15166bc75619SDan Williams 		spin_lock(&nfit_test_lock);
15176bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1518bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1519bd4cd745SDan Williams 						+ resource_size(&n->res))) {
15206bc75619SDan Williams 				nfit_res = n;
15216bc75619SDan Williams 				break;
15226bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
15236bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1524bd4cd745SDan Williams 						+ resource_size(&n->res))) {
15256bc75619SDan Williams 				nfit_res = n;
15266bc75619SDan Williams 				break;
15276bc75619SDan Williams 			}
15286bc75619SDan Williams 		}
15296bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
15306bc75619SDan Williams 		if (nfit_res)
15316bc75619SDan Williams 			return nfit_res;
15326bc75619SDan Williams 	}
15336bc75619SDan Williams 
15346bc75619SDan Williams 	return NULL;
15356bc75619SDan Williams }
15366bc75619SDan Williams 
1537f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1538f471f1a7SDan Williams {
15399fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1540f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
15419fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1542f471f1a7SDan Williams 	if (!ars_state->ars_status)
1543f471f1a7SDan Williams 		return -ENOMEM;
1544f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1545f471f1a7SDan Williams 	return 0;
1546f471f1a7SDan Williams }
1547f471f1a7SDan Williams 
1548231bf117SDan Williams static void put_dimms(void *data)
1549231bf117SDan Williams {
1550718fda67SDan Williams 	struct nfit_test *t = data;
1551231bf117SDan Williams 	int i;
1552231bf117SDan Williams 
1553718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1554718fda67SDan Williams 		if (t->dimm_dev[i])
1555718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1556231bf117SDan Williams }
1557231bf117SDan Williams 
1558231bf117SDan Williams static struct class *nfit_test_dimm;
1559231bf117SDan Williams 
156073606afdSDan Williams static int dimm_name_to_id(struct device *dev)
156173606afdSDan Williams {
156273606afdSDan Williams 	int dimm;
156373606afdSDan Williams 
1564718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
156573606afdSDan Williams 		return -ENXIO;
156673606afdSDan Williams 	return dimm;
156773606afdSDan Williams }
156873606afdSDan Williams 
156973606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
157073606afdSDan Williams 		char *buf)
157173606afdSDan Williams {
157273606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
157373606afdSDan Williams 
157473606afdSDan Williams 	if (dimm < 0)
157573606afdSDan Williams 		return dimm;
157673606afdSDan Williams 
157719357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
157873606afdSDan Williams }
157973606afdSDan Williams DEVICE_ATTR_RO(handle);
158073606afdSDan Williams 
158173606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
158273606afdSDan Williams 		char *buf)
158373606afdSDan Williams {
158473606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
158573606afdSDan Williams 
158673606afdSDan Williams 	if (dimm < 0)
158773606afdSDan Williams 		return dimm;
158873606afdSDan Williams 
158973606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
159073606afdSDan Williams }
159173606afdSDan Williams 
159273606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
159373606afdSDan Williams 		const char *buf, size_t size)
159473606afdSDan Williams {
159573606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
159673606afdSDan Williams 	unsigned long val;
159773606afdSDan Williams 	ssize_t rc;
159873606afdSDan Williams 
159973606afdSDan Williams 	if (dimm < 0)
160073606afdSDan Williams 		return dimm;
160173606afdSDan Williams 
160273606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
160373606afdSDan Williams 	if (rc)
160473606afdSDan Williams 		return rc;
160573606afdSDan Williams 
160673606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
160773606afdSDan Williams 	return size;
160873606afdSDan Williams }
160973606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
161073606afdSDan Williams 
161155c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
161255c72ab6SDan Williams 		char *buf)
161355c72ab6SDan Williams {
161455c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
161555c72ab6SDan Williams 
161655c72ab6SDan Williams 	if (dimm < 0)
161755c72ab6SDan Williams 		return dimm;
161855c72ab6SDan Williams 
161955c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
162055c72ab6SDan Williams }
162155c72ab6SDan Williams 
162255c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
162355c72ab6SDan Williams 		const char *buf, size_t size)
162455c72ab6SDan Williams {
162555c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
162655c72ab6SDan Williams 	unsigned long val;
162755c72ab6SDan Williams 	ssize_t rc;
162855c72ab6SDan Williams 
162955c72ab6SDan Williams 	if (dimm < 0)
163055c72ab6SDan Williams 		return dimm;
163155c72ab6SDan Williams 
163255c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
163355c72ab6SDan Williams 	if (rc)
163455c72ab6SDan Williams 		return rc;
163555c72ab6SDan Williams 
163655c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
163755c72ab6SDan Williams 	return size;
163855c72ab6SDan Williams }
163955c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
164055c72ab6SDan Williams 
16413c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
16423c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
16433c13e2acSDave Jiang {
16443c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
16453c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
16463c13e2acSDave Jiang 
16473c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
16483c13e2acSDave Jiang 	return size;
16493c13e2acSDave Jiang }
16503c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
16513c13e2acSDave Jiang 
165273606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
165373606afdSDan Williams 	&dev_attr_fail_cmd.attr,
165455c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
165573606afdSDan Williams 	&dev_attr_handle.attr,
16563c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
165773606afdSDan Williams 	NULL,
165873606afdSDan Williams };
165973606afdSDan Williams 
166073606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
166173606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
166273606afdSDan Williams };
166373606afdSDan Williams 
166473606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
166573606afdSDan Williams 	&nfit_test_dimm_attribute_group,
166673606afdSDan Williams 	NULL,
166773606afdSDan Williams };
166873606afdSDan Williams 
1669718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1670718fda67SDan Williams {
1671718fda67SDan Williams 	int i;
1672718fda67SDan Williams 
1673718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1674718fda67SDan Williams 		return -ENOMEM;
1675718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1676718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1677718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1678718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1679718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1680718fda67SDan Williams 		if (!t->dimm_dev[i])
1681718fda67SDan Williams 			return -ENOMEM;
1682718fda67SDan Williams 	}
1683718fda67SDan Williams 	return 0;
1684718fda67SDan Williams }
1685718fda67SDan Williams 
1686ecaa4a97SDave Jiang static void security_init(struct nfit_test *t)
1687ecaa4a97SDave Jiang {
1688ecaa4a97SDave Jiang 	int i;
1689ecaa4a97SDave Jiang 
1690ecaa4a97SDave Jiang 	for (i = 0; i < t->num_dcr; i++) {
1691ecaa4a97SDave Jiang 		struct nfit_test_sec *sec = &dimm_sec_info[i];
1692ecaa4a97SDave Jiang 
1693ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1694ecaa4a97SDave Jiang 	}
1695ecaa4a97SDave Jiang }
1696ecaa4a97SDave Jiang 
1697ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1698ed07c433SDan Williams {
1699ed07c433SDan Williams 	int i;
1700ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1701ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1702ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1703ed07c433SDan Williams 		.media_temperature = 40 * 16,
1704ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1705ed07c433SDan Williams 		.spares = 5,
1706ed07c433SDan Williams 	};
1707ed07c433SDan Williams 
1708ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1709b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1710ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1711ed07c433SDan Williams 				sizeof(smart_t_data));
1712ed07c433SDan Williams 	}
1713ed07c433SDan Williams }
1714ed07c433SDan Williams 
17156bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
17166bc75619SDan Williams {
17176b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
17186bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
17196bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
17203b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
17213b87356fSDan Williams 					window_size) * NUM_DCR
17229d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
172385d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1724f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1725f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
17266bc75619SDan Williams 	int i;
17276bc75619SDan Williams 
17286bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17296bc75619SDan Williams 	if (!t->nfit_buf)
17306bc75619SDan Williams 		return -ENOMEM;
17316bc75619SDan Williams 	t->nfit_size = nfit_size;
17326bc75619SDan Williams 
1733ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
17346bc75619SDan Williams 	if (!t->spa_set[0])
17356bc75619SDan Williams 		return -ENOMEM;
17366bc75619SDan Williams 
1737ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
17386bc75619SDan Williams 	if (!t->spa_set[1])
17396bc75619SDan Williams 		return -ENOMEM;
17406bc75619SDan Williams 
1741ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
174220985164SVishal Verma 	if (!t->spa_set[2])
174320985164SVishal Verma 		return -ENOMEM;
174420985164SVishal Verma 
1745dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17466bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
17476bc75619SDan Williams 		if (!t->dimm[i])
17486bc75619SDan Williams 			return -ENOMEM;
17496bc75619SDan Williams 
17506bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
17516bc75619SDan Williams 		if (!t->label[i])
17526bc75619SDan Williams 			return -ENOMEM;
17536bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
17549d27a87eSDan Williams 
17559d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
17569d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
175785d3fa02SDan Williams 				&t->flush_dma[i]);
17589d27a87eSDan Williams 		if (!t->flush[i])
17599d27a87eSDan Williams 			return -ENOMEM;
17606bc75619SDan Williams 	}
17616bc75619SDan Williams 
1762dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17636bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
17646bc75619SDan Williams 		if (!t->dcr[i])
17656bc75619SDan Williams 			return -ENOMEM;
17666bc75619SDan Williams 	}
17676bc75619SDan Williams 
1768c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1769c14a868aSDan Williams 	if (!t->_fit)
1770c14a868aSDan Williams 		return -ENOMEM;
1771c14a868aSDan Williams 
1772718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1773231bf117SDan Williams 		return -ENOMEM;
1774ed07c433SDan Williams 	smart_init(t);
1775ecaa4a97SDave Jiang 	security_init(t);
1776f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17776bc75619SDan Williams }
17786bc75619SDan Williams 
17796bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
17806bc75619SDan Williams {
17817bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1782ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1783ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1784dafb1048SDan Williams 	int i;
17856bc75619SDan Williams 
17866bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17876bc75619SDan Williams 	if (!t->nfit_buf)
17886bc75619SDan Williams 		return -ENOMEM;
17896bc75619SDan Williams 	t->nfit_size = nfit_size;
17906bc75619SDan Williams 
1791ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
17926bc75619SDan Williams 	if (!t->spa_set[0])
17936bc75619SDan Williams 		return -ENOMEM;
17946bc75619SDan Williams 
1795dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1796dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1797dafb1048SDan Williams 		if (!t->label[i])
1798dafb1048SDan Williams 			return -ENOMEM;
1799dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1800dafb1048SDan Williams 	}
1801dafb1048SDan Williams 
18027bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
18037bfe97c7SDan Williams 	if (!t->spa_set[1])
18047bfe97c7SDan Williams 		return -ENOMEM;
18057bfe97c7SDan Williams 
1806718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1807718fda67SDan Williams 		return -ENOMEM;
1808ed07c433SDan Williams 	smart_init(t);
1809f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
18106bc75619SDan Williams }
18116bc75619SDan Williams 
18125dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
18135dc68e55SDan Williams {
18145dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
18155dc68e55SDan Williams 	dcr->device_id = 0;
18165dc68e55SDan Williams 	dcr->revision_id = 1;
18175dc68e55SDan Williams 	dcr->valid_fields = 1;
18185dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
18195dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
18205dc68e55SDan Williams }
18215dc68e55SDan Williams 
18226bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
18236bc75619SDan Williams {
182485d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
182585d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
18266bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
18276bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
18286bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
18296bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
18306bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
18316bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
18329d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1833f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1834d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
18356bc75619SDan Williams 
18366bc75619SDan Williams 	/*
18376bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
18386bc75619SDan Williams 	 * does not actually alias the related block-data-window
18396bc75619SDan Williams 	 * regions)
18406bc75619SDan Williams 	 */
18416b577c9dSLinda Knippers 	spa = nfit_buf;
18426bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18436bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18446bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18456bc75619SDan Williams 	spa->range_index = 0+1;
18466bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
18476bc75619SDan Williams 	spa->length = SPA0_SIZE;
1848d7d8464dSRoss Zwisler 	offset += spa->header.length;
18496bc75619SDan Williams 
18506bc75619SDan Williams 	/*
18516bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
18526bc75619SDan Williams 	 * does not actually alias the related block-data-window
18536bc75619SDan Williams 	 * regions)
18546bc75619SDan Williams 	 */
1855d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18566bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18576bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18586bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18596bc75619SDan Williams 	spa->range_index = 1+1;
18606bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
18616bc75619SDan Williams 	spa->length = SPA1_SIZE;
1862d7d8464dSRoss Zwisler 	offset += spa->header.length;
18636bc75619SDan Williams 
18646bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1865d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18666bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18676bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18686bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18696bc75619SDan Williams 	spa->range_index = 2+1;
18706bc75619SDan Williams 	spa->address = t->dcr_dma[0];
18716bc75619SDan Williams 	spa->length = DCR_SIZE;
1872d7d8464dSRoss Zwisler 	offset += spa->header.length;
18736bc75619SDan Williams 
18746bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1875d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18766bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18776bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18786bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18796bc75619SDan Williams 	spa->range_index = 3+1;
18806bc75619SDan Williams 	spa->address = t->dcr_dma[1];
18816bc75619SDan Williams 	spa->length = DCR_SIZE;
1882d7d8464dSRoss Zwisler 	offset += spa->header.length;
18836bc75619SDan Williams 
18846bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1885d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18866bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18876bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18886bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18896bc75619SDan Williams 	spa->range_index = 4+1;
18906bc75619SDan Williams 	spa->address = t->dcr_dma[2];
18916bc75619SDan Williams 	spa->length = DCR_SIZE;
1892d7d8464dSRoss Zwisler 	offset += spa->header.length;
18936bc75619SDan Williams 
18946bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1895d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18966bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18976bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18986bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18996bc75619SDan Williams 	spa->range_index = 5+1;
19006bc75619SDan Williams 	spa->address = t->dcr_dma[3];
19016bc75619SDan Williams 	spa->length = DCR_SIZE;
1902d7d8464dSRoss Zwisler 	offset += spa->header.length;
19036bc75619SDan Williams 
19046bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1905d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19066bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19076bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19086bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19096bc75619SDan Williams 	spa->range_index = 6+1;
19106bc75619SDan Williams 	spa->address = t->dimm_dma[0];
19116bc75619SDan Williams 	spa->length = DIMM_SIZE;
1912d7d8464dSRoss Zwisler 	offset += spa->header.length;
19136bc75619SDan Williams 
19146bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1915d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19166bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19176bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19186bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19196bc75619SDan Williams 	spa->range_index = 7+1;
19206bc75619SDan Williams 	spa->address = t->dimm_dma[1];
19216bc75619SDan Williams 	spa->length = DIMM_SIZE;
1922d7d8464dSRoss Zwisler 	offset += spa->header.length;
19236bc75619SDan Williams 
19246bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1925d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19266bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19276bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19286bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19296bc75619SDan Williams 	spa->range_index = 8+1;
19306bc75619SDan Williams 	spa->address = t->dimm_dma[2];
19316bc75619SDan Williams 	spa->length = DIMM_SIZE;
1932d7d8464dSRoss Zwisler 	offset += spa->header.length;
19336bc75619SDan Williams 
19346bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1935d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
19366bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
19376bc75619SDan Williams 	spa->header.length = sizeof(*spa);
19386bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
19396bc75619SDan Williams 	spa->range_index = 9+1;
19406bc75619SDan Williams 	spa->address = t->dimm_dma[3];
19416bc75619SDan Williams 	spa->length = DIMM_SIZE;
1942d7d8464dSRoss Zwisler 	offset += spa->header.length;
19436bc75619SDan Williams 
19446bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
19456bc75619SDan Williams 	memdev = nfit_buf + offset;
19466bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19476bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19486bc75619SDan Williams 	memdev->device_handle = handle[0];
19496bc75619SDan Williams 	memdev->physical_id = 0;
19506bc75619SDan Williams 	memdev->region_id = 0;
19516bc75619SDan Williams 	memdev->range_index = 0+1;
19523b87356fSDan Williams 	memdev->region_index = 4+1;
19536bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1954df06a2d5SDan Williams 	memdev->region_offset = 1;
19556bc75619SDan Williams 	memdev->address = 0;
19566bc75619SDan Williams 	memdev->interleave_index = 0;
19576bc75619SDan Williams 	memdev->interleave_ways = 2;
1958d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19596bc75619SDan Williams 
19606bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1961d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19626bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19636bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19646bc75619SDan Williams 	memdev->device_handle = handle[1];
19656bc75619SDan Williams 	memdev->physical_id = 1;
19666bc75619SDan Williams 	memdev->region_id = 0;
19676bc75619SDan Williams 	memdev->range_index = 0+1;
19683b87356fSDan Williams 	memdev->region_index = 5+1;
19696bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1970df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
19716bc75619SDan Williams 	memdev->address = 0;
19726bc75619SDan Williams 	memdev->interleave_index = 0;
19736bc75619SDan Williams 	memdev->interleave_ways = 2;
1974ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1975d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19766bc75619SDan Williams 
19776bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1978d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19796bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19806bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19816bc75619SDan Williams 	memdev->device_handle = handle[0];
19826bc75619SDan Williams 	memdev->physical_id = 0;
19836bc75619SDan Williams 	memdev->region_id = 1;
19846bc75619SDan Williams 	memdev->range_index = 1+1;
19853b87356fSDan Williams 	memdev->region_index = 4+1;
19866bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1987df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
19886bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19896bc75619SDan Williams 	memdev->interleave_index = 0;
19906bc75619SDan Williams 	memdev->interleave_ways = 4;
1991ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1992d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19936bc75619SDan Williams 
19946bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1995d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19966bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19976bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19986bc75619SDan Williams 	memdev->device_handle = handle[1];
19996bc75619SDan Williams 	memdev->physical_id = 1;
20006bc75619SDan Williams 	memdev->region_id = 1;
20016bc75619SDan Williams 	memdev->range_index = 1+1;
20023b87356fSDan Williams 	memdev->region_index = 5+1;
20036bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2004df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
20056bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
20066bc75619SDan Williams 	memdev->interleave_index = 0;
20076bc75619SDan Williams 	memdev->interleave_ways = 4;
2008d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20096bc75619SDan Williams 
20106bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
2011d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20126bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20136bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20146bc75619SDan Williams 	memdev->device_handle = handle[2];
20156bc75619SDan Williams 	memdev->physical_id = 2;
20166bc75619SDan Williams 	memdev->region_id = 0;
20176bc75619SDan Williams 	memdev->range_index = 1+1;
20183b87356fSDan Williams 	memdev->region_index = 6+1;
20196bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2020df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
20216bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
20226bc75619SDan Williams 	memdev->interleave_index = 0;
20236bc75619SDan Williams 	memdev->interleave_ways = 4;
2024ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2025d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20266bc75619SDan Williams 
20276bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
2028d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20296bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20306bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20316bc75619SDan Williams 	memdev->device_handle = handle[3];
20326bc75619SDan Williams 	memdev->physical_id = 3;
20336bc75619SDan Williams 	memdev->region_id = 0;
20346bc75619SDan Williams 	memdev->range_index = 1+1;
20353b87356fSDan Williams 	memdev->region_index = 7+1;
20366bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2037df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
20386bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
20396bc75619SDan Williams 	memdev->interleave_index = 0;
20406bc75619SDan Williams 	memdev->interleave_ways = 4;
2041d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20426bc75619SDan Williams 
20436bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
2044d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20456bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20466bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20476bc75619SDan Williams 	memdev->device_handle = handle[0];
20486bc75619SDan Williams 	memdev->physical_id = 0;
20496bc75619SDan Williams 	memdev->region_id = 0;
20506bc75619SDan Williams 	memdev->range_index = 2+1;
20516bc75619SDan Williams 	memdev->region_index = 0+1;
20526bc75619SDan Williams 	memdev->region_size = 0;
20536bc75619SDan Williams 	memdev->region_offset = 0;
20546bc75619SDan Williams 	memdev->address = 0;
20556bc75619SDan Williams 	memdev->interleave_index = 0;
20566bc75619SDan Williams 	memdev->interleave_ways = 1;
2057d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20586bc75619SDan Williams 
20596bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
2060d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20616bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20626bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20636bc75619SDan Williams 	memdev->device_handle = handle[1];
20646bc75619SDan Williams 	memdev->physical_id = 1;
20656bc75619SDan Williams 	memdev->region_id = 0;
20666bc75619SDan Williams 	memdev->range_index = 3+1;
20676bc75619SDan Williams 	memdev->region_index = 1+1;
20686bc75619SDan Williams 	memdev->region_size = 0;
20696bc75619SDan Williams 	memdev->region_offset = 0;
20706bc75619SDan Williams 	memdev->address = 0;
20716bc75619SDan Williams 	memdev->interleave_index = 0;
20726bc75619SDan Williams 	memdev->interleave_ways = 1;
2073d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20746bc75619SDan Williams 
20756bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
2076d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20776bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20786bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20796bc75619SDan Williams 	memdev->device_handle = handle[2];
20806bc75619SDan Williams 	memdev->physical_id = 2;
20816bc75619SDan Williams 	memdev->region_id = 0;
20826bc75619SDan Williams 	memdev->range_index = 4+1;
20836bc75619SDan Williams 	memdev->region_index = 2+1;
20846bc75619SDan Williams 	memdev->region_size = 0;
20856bc75619SDan Williams 	memdev->region_offset = 0;
20866bc75619SDan Williams 	memdev->address = 0;
20876bc75619SDan Williams 	memdev->interleave_index = 0;
20886bc75619SDan Williams 	memdev->interleave_ways = 1;
2089d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20906bc75619SDan Williams 
20916bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
2092d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20936bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20946bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20956bc75619SDan Williams 	memdev->device_handle = handle[3];
20966bc75619SDan Williams 	memdev->physical_id = 3;
20976bc75619SDan Williams 	memdev->region_id = 0;
20986bc75619SDan Williams 	memdev->range_index = 5+1;
20996bc75619SDan Williams 	memdev->region_index = 3+1;
21006bc75619SDan Williams 	memdev->region_size = 0;
21016bc75619SDan Williams 	memdev->region_offset = 0;
21026bc75619SDan Williams 	memdev->address = 0;
21036bc75619SDan Williams 	memdev->interleave_index = 0;
21046bc75619SDan Williams 	memdev->interleave_ways = 1;
2105d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21066bc75619SDan Williams 
21076bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
2108d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21096bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21106bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21116bc75619SDan Williams 	memdev->device_handle = handle[0];
21126bc75619SDan Williams 	memdev->physical_id = 0;
21136bc75619SDan Williams 	memdev->region_id = 0;
21146bc75619SDan Williams 	memdev->range_index = 6+1;
21156bc75619SDan Williams 	memdev->region_index = 0+1;
21166bc75619SDan Williams 	memdev->region_size = 0;
21176bc75619SDan Williams 	memdev->region_offset = 0;
21186bc75619SDan Williams 	memdev->address = 0;
21196bc75619SDan Williams 	memdev->interleave_index = 0;
21206bc75619SDan Williams 	memdev->interleave_ways = 1;
2121d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21226bc75619SDan Williams 
21236bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
2124d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21256bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21266bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21276bc75619SDan Williams 	memdev->device_handle = handle[1];
21286bc75619SDan Williams 	memdev->physical_id = 1;
21296bc75619SDan Williams 	memdev->region_id = 0;
21306bc75619SDan Williams 	memdev->range_index = 7+1;
21316bc75619SDan Williams 	memdev->region_index = 1+1;
21326bc75619SDan Williams 	memdev->region_size = 0;
21336bc75619SDan Williams 	memdev->region_offset = 0;
21346bc75619SDan Williams 	memdev->address = 0;
21356bc75619SDan Williams 	memdev->interleave_index = 0;
21366bc75619SDan Williams 	memdev->interleave_ways = 1;
2137d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21386bc75619SDan Williams 
21396bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
2140d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21416bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21426bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21436bc75619SDan Williams 	memdev->device_handle = handle[2];
21446bc75619SDan Williams 	memdev->physical_id = 2;
21456bc75619SDan Williams 	memdev->region_id = 0;
21466bc75619SDan Williams 	memdev->range_index = 8+1;
21476bc75619SDan Williams 	memdev->region_index = 2+1;
21486bc75619SDan Williams 	memdev->region_size = 0;
21496bc75619SDan Williams 	memdev->region_offset = 0;
21506bc75619SDan Williams 	memdev->address = 0;
21516bc75619SDan Williams 	memdev->interleave_index = 0;
21526bc75619SDan Williams 	memdev->interleave_ways = 1;
2153d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21546bc75619SDan Williams 
21556bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
2156d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21576bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21586bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21596bc75619SDan Williams 	memdev->device_handle = handle[3];
21606bc75619SDan Williams 	memdev->physical_id = 3;
21616bc75619SDan Williams 	memdev->region_id = 0;
21626bc75619SDan Williams 	memdev->range_index = 9+1;
21636bc75619SDan Williams 	memdev->region_index = 3+1;
21646bc75619SDan Williams 	memdev->region_size = 0;
21656bc75619SDan Williams 	memdev->region_offset = 0;
21666bc75619SDan Williams 	memdev->address = 0;
21676bc75619SDan Williams 	memdev->interleave_index = 0;
21686bc75619SDan Williams 	memdev->interleave_ways = 1;
2169ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2170d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21716bc75619SDan Williams 
21723b87356fSDan Williams 	/* dcr-descriptor0: blk */
21736bc75619SDan Williams 	dcr = nfit_buf + offset;
21746bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2175d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21766bc75619SDan Williams 	dcr->region_index = 0+1;
21775dc68e55SDan Williams 	dcr_common_init(dcr);
21786bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2179be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21806bc75619SDan Williams 	dcr->windows = 1;
21816bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21826bc75619SDan Williams 	dcr->command_offset = 0;
21836bc75619SDan Williams 	dcr->command_size = 8;
21846bc75619SDan Williams 	dcr->status_offset = 8;
21856bc75619SDan Williams 	dcr->status_size = 4;
2186d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21876bc75619SDan Williams 
21883b87356fSDan Williams 	/* dcr-descriptor1: blk */
2189d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21906bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2191d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21926bc75619SDan Williams 	dcr->region_index = 1+1;
21935dc68e55SDan Williams 	dcr_common_init(dcr);
21946bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2195be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21966bc75619SDan Williams 	dcr->windows = 1;
21976bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21986bc75619SDan Williams 	dcr->command_offset = 0;
21996bc75619SDan Williams 	dcr->command_size = 8;
22006bc75619SDan Williams 	dcr->status_offset = 8;
22016bc75619SDan Williams 	dcr->status_size = 4;
2202d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22036bc75619SDan Williams 
22043b87356fSDan Williams 	/* dcr-descriptor2: blk */
2205d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22066bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2207d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
22086bc75619SDan Williams 	dcr->region_index = 2+1;
22095dc68e55SDan Williams 	dcr_common_init(dcr);
22106bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2211be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
22126bc75619SDan Williams 	dcr->windows = 1;
22136bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
22146bc75619SDan Williams 	dcr->command_offset = 0;
22156bc75619SDan Williams 	dcr->command_size = 8;
22166bc75619SDan Williams 	dcr->status_offset = 8;
22176bc75619SDan Williams 	dcr->status_size = 4;
2218d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22196bc75619SDan Williams 
22203b87356fSDan Williams 	/* dcr-descriptor3: blk */
2221d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22226bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2223d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
22246bc75619SDan Williams 	dcr->region_index = 3+1;
22255dc68e55SDan Williams 	dcr_common_init(dcr);
22266bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2227be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
22286bc75619SDan Williams 	dcr->windows = 1;
22296bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
22306bc75619SDan Williams 	dcr->command_offset = 0;
22316bc75619SDan Williams 	dcr->command_size = 8;
22326bc75619SDan Williams 	dcr->status_offset = 8;
22336bc75619SDan Williams 	dcr->status_size = 4;
2234d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22356bc75619SDan Williams 
22363b87356fSDan Williams 	/* dcr-descriptor0: pmem */
22373b87356fSDan Williams 	dcr = nfit_buf + offset;
22383b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22393b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22403b87356fSDan Williams 			window_size);
22413b87356fSDan Williams 	dcr->region_index = 4+1;
22425dc68e55SDan Williams 	dcr_common_init(dcr);
22433b87356fSDan Williams 	dcr->serial_number = ~handle[0];
22443b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22453b87356fSDan Williams 	dcr->windows = 0;
2246d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22473b87356fSDan Williams 
22483b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2249d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22503b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22513b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22523b87356fSDan Williams 			window_size);
22533b87356fSDan Williams 	dcr->region_index = 5+1;
22545dc68e55SDan Williams 	dcr_common_init(dcr);
22553b87356fSDan Williams 	dcr->serial_number = ~handle[1];
22563b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22573b87356fSDan Williams 	dcr->windows = 0;
2258d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22593b87356fSDan Williams 
22603b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2261d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22623b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22633b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22643b87356fSDan Williams 			window_size);
22653b87356fSDan Williams 	dcr->region_index = 6+1;
22665dc68e55SDan Williams 	dcr_common_init(dcr);
22673b87356fSDan Williams 	dcr->serial_number = ~handle[2];
22683b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22693b87356fSDan Williams 	dcr->windows = 0;
2270d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22713b87356fSDan Williams 
22723b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2273d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22743b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22753b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22763b87356fSDan Williams 			window_size);
22773b87356fSDan Williams 	dcr->region_index = 7+1;
22785dc68e55SDan Williams 	dcr_common_init(dcr);
22793b87356fSDan Williams 	dcr->serial_number = ~handle[3];
22803b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22813b87356fSDan Williams 	dcr->windows = 0;
2282d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22833b87356fSDan Williams 
22846bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
22856bc75619SDan Williams 	bdw = nfit_buf + offset;
22866bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2287d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22886bc75619SDan Williams 	bdw->region_index = 0+1;
22896bc75619SDan Williams 	bdw->windows = 1;
22906bc75619SDan Williams 	bdw->offset = 0;
22916bc75619SDan Williams 	bdw->size = BDW_SIZE;
22926bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22936bc75619SDan Williams 	bdw->start_address = 0;
2294d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22956bc75619SDan Williams 
22966bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2297d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22986bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2299d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
23006bc75619SDan Williams 	bdw->region_index = 1+1;
23016bc75619SDan Williams 	bdw->windows = 1;
23026bc75619SDan Williams 	bdw->offset = 0;
23036bc75619SDan Williams 	bdw->size = BDW_SIZE;
23046bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
23056bc75619SDan Williams 	bdw->start_address = 0;
2306d7d8464dSRoss Zwisler 	offset += bdw->header.length;
23076bc75619SDan Williams 
23086bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2309d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
23106bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2311d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
23126bc75619SDan Williams 	bdw->region_index = 2+1;
23136bc75619SDan Williams 	bdw->windows = 1;
23146bc75619SDan Williams 	bdw->offset = 0;
23156bc75619SDan Williams 	bdw->size = BDW_SIZE;
23166bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
23176bc75619SDan Williams 	bdw->start_address = 0;
2318d7d8464dSRoss Zwisler 	offset += bdw->header.length;
23196bc75619SDan Williams 
23206bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2321d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
23226bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2323d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
23246bc75619SDan Williams 	bdw->region_index = 3+1;
23256bc75619SDan Williams 	bdw->windows = 1;
23266bc75619SDan Williams 	bdw->offset = 0;
23276bc75619SDan Williams 	bdw->size = BDW_SIZE;
23286bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
23296bc75619SDan Williams 	bdw->start_address = 0;
2330d7d8464dSRoss Zwisler 	offset += bdw->header.length;
23316bc75619SDan Williams 
23329d27a87eSDan Williams 	/* flush0 (dimm0) */
23339d27a87eSDan Williams 	flush = nfit_buf + offset;
23349d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
233585d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23369d27a87eSDan Williams 	flush->device_handle = handle[0];
233785d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
233885d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
233985d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2340d7d8464dSRoss Zwisler 	offset += flush->header.length;
23419d27a87eSDan Williams 
23429d27a87eSDan Williams 	/* flush1 (dimm1) */
2343d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23449d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
234585d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23469d27a87eSDan Williams 	flush->device_handle = handle[1];
234785d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
234885d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
234985d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2350d7d8464dSRoss Zwisler 	offset += flush->header.length;
23519d27a87eSDan Williams 
23529d27a87eSDan Williams 	/* flush2 (dimm2) */
2353d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23549d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
235585d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23569d27a87eSDan Williams 	flush->device_handle = handle[2];
235785d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
235885d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
235985d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2360d7d8464dSRoss Zwisler 	offset += flush->header.length;
23619d27a87eSDan Williams 
23629d27a87eSDan Williams 	/* flush3 (dimm3) */
2363d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23649d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
236585d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23669d27a87eSDan Williams 	flush->device_handle = handle[3];
236785d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
236885d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
236985d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2370d7d8464dSRoss Zwisler 	offset += flush->header.length;
23719d27a87eSDan Williams 
2372f81e1d35SDave Jiang 	/* platform capabilities */
2373d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2374f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2375f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2376f81e1d35SDave Jiang 	pcap->highest_capability = 1;
23771273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2378d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2379f81e1d35SDave Jiang 
238020985164SVishal Verma 	if (t->setup_hotplug) {
23813b87356fSDan Williams 		/* dcr-descriptor4: blk */
238220985164SVishal Verma 		dcr = nfit_buf + offset;
238320985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2384d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
23853b87356fSDan Williams 		dcr->region_index = 8+1;
23865dc68e55SDan Williams 		dcr_common_init(dcr);
238720985164SVishal Verma 		dcr->serial_number = ~handle[4];
2388be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
238920985164SVishal Verma 		dcr->windows = 1;
239020985164SVishal Verma 		dcr->window_size = DCR_SIZE;
239120985164SVishal Verma 		dcr->command_offset = 0;
239220985164SVishal Verma 		dcr->command_size = 8;
239320985164SVishal Verma 		dcr->status_offset = 8;
239420985164SVishal Verma 		dcr->status_size = 4;
2395d7d8464dSRoss Zwisler 		offset += dcr->header.length;
239620985164SVishal Verma 
23973b87356fSDan Williams 		/* dcr-descriptor4: pmem */
23983b87356fSDan Williams 		dcr = nfit_buf + offset;
23993b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24003b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
24013b87356fSDan Williams 				window_size);
24023b87356fSDan Williams 		dcr->region_index = 9+1;
24035dc68e55SDan Williams 		dcr_common_init(dcr);
24043b87356fSDan Williams 		dcr->serial_number = ~handle[4];
24053b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
24063b87356fSDan Williams 		dcr->windows = 0;
2407d7d8464dSRoss Zwisler 		offset += dcr->header.length;
24083b87356fSDan Williams 
240920985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
241020985164SVishal Verma 		bdw = nfit_buf + offset;
241120985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2412d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
24133b87356fSDan Williams 		bdw->region_index = 8+1;
241420985164SVishal Verma 		bdw->windows = 1;
241520985164SVishal Verma 		bdw->offset = 0;
241620985164SVishal Verma 		bdw->size = BDW_SIZE;
241720985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
241820985164SVishal Verma 		bdw->start_address = 0;
2419d7d8464dSRoss Zwisler 		offset += bdw->header.length;
242020985164SVishal Verma 
242120985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
242220985164SVishal Verma 		spa = nfit_buf + offset;
242320985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
242420985164SVishal Verma 		spa->header.length = sizeof(*spa);
242520985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
242620985164SVishal Verma 		spa->range_index = 10+1;
242720985164SVishal Verma 		spa->address = t->dcr_dma[4];
242820985164SVishal Verma 		spa->length = DCR_SIZE;
2429d7d8464dSRoss Zwisler 		offset += spa->header.length;
243020985164SVishal Verma 
243120985164SVishal Verma 		/*
243220985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
243320985164SVishal Verma 		 * does not actually alias the related block-data-window
243420985164SVishal Verma 		 * regions)
243520985164SVishal Verma 		 */
2436d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
243720985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
243820985164SVishal Verma 		spa->header.length = sizeof(*spa);
243920985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
244020985164SVishal Verma 		spa->range_index = 11+1;
244120985164SVishal Verma 		spa->address = t->spa_set_dma[2];
244220985164SVishal Verma 		spa->length = SPA0_SIZE;
2443d7d8464dSRoss Zwisler 		offset += spa->header.length;
244420985164SVishal Verma 
244520985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2446d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
244720985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
244820985164SVishal Verma 		spa->header.length = sizeof(*spa);
244920985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
245020985164SVishal Verma 		spa->range_index = 12+1;
245120985164SVishal Verma 		spa->address = t->dimm_dma[4];
245220985164SVishal Verma 		spa->length = DIMM_SIZE;
2453d7d8464dSRoss Zwisler 		offset += spa->header.length;
245420985164SVishal Verma 
245520985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
245620985164SVishal Verma 		memdev = nfit_buf + offset;
245720985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
245820985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
245920985164SVishal Verma 		memdev->device_handle = handle[4];
246020985164SVishal Verma 		memdev->physical_id = 4;
246120985164SVishal Verma 		memdev->region_id = 0;
246220985164SVishal Verma 		memdev->range_index = 10+1;
24633b87356fSDan Williams 		memdev->region_index = 8+1;
246420985164SVishal Verma 		memdev->region_size = 0;
246520985164SVishal Verma 		memdev->region_offset = 0;
246620985164SVishal Verma 		memdev->address = 0;
246720985164SVishal Verma 		memdev->interleave_index = 0;
246820985164SVishal Verma 		memdev->interleave_ways = 1;
2469d7d8464dSRoss Zwisler 		offset += memdev->header.length;
247020985164SVishal Verma 
2471d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2472d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
247320985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
247420985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
247520985164SVishal Verma 		memdev->device_handle = handle[4];
247620985164SVishal Verma 		memdev->physical_id = 4;
247720985164SVishal Verma 		memdev->region_id = 0;
247820985164SVishal Verma 		memdev->range_index = 11+1;
24793b87356fSDan Williams 		memdev->region_index = 9+1;
248020985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2481df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
248220985164SVishal Verma 		memdev->address = 0;
248320985164SVishal Verma 		memdev->interleave_index = 0;
248420985164SVishal Verma 		memdev->interleave_ways = 1;
2485ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2486d7d8464dSRoss Zwisler 		offset += memdev->header.length;
248720985164SVishal Verma 
24883b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2489d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
249020985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
249120985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
249220985164SVishal Verma 		memdev->device_handle = handle[4];
249320985164SVishal Verma 		memdev->physical_id = 4;
249420985164SVishal Verma 		memdev->region_id = 0;
249520985164SVishal Verma 		memdev->range_index = 12+1;
24963b87356fSDan Williams 		memdev->region_index = 8+1;
249720985164SVishal Verma 		memdev->region_size = 0;
249820985164SVishal Verma 		memdev->region_offset = 0;
249920985164SVishal Verma 		memdev->address = 0;
250020985164SVishal Verma 		memdev->interleave_index = 0;
250120985164SVishal Verma 		memdev->interleave_ways = 1;
2502d7d8464dSRoss Zwisler 		offset += memdev->header.length;
250320985164SVishal Verma 
250420985164SVishal Verma 		/* flush3 (dimm4) */
250520985164SVishal Verma 		flush = nfit_buf + offset;
250620985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
250785d3fa02SDan Williams 		flush->header.length = flush_hint_size;
250820985164SVishal Verma 		flush->device_handle = handle[4];
250985d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
251085d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
251185d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
251285d3fa02SDan Williams 				+ i * sizeof(u64);
2513d7d8464dSRoss Zwisler 		offset += flush->header.length;
25149741a559SRoss Zwisler 
25159741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
25169741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
251720985164SVishal Verma 	}
251820985164SVishal Verma 
25191526f9e2SRoss Zwisler 	t->nfit_filled = offset;
25201526f9e2SRoss Zwisler 
25219fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
25229fb1a190SDave Jiang 			SPA0_SIZE);
2523f471f1a7SDan Williams 
25246bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2525e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2526e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2527e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2528ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2529ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2530ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
25314cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2532e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2533e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2534e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2535e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
253610246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
2537d46e6a21SDan Williams 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_dsm_mask);
2538d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_dsm_mask);
2539d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_dsm_mask);
2540d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_dsm_mask);
2541bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2542bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2543bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2544bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2545bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2546674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
25473c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
25483c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25493c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
25503c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
25513c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25523c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
25533c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
25543c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2555926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2556926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2557ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
2558ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2559ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
2560ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25616bc75619SDan Williams }
25626bc75619SDan Williams 
25636bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
25646bc75619SDan Williams {
25656b577c9dSLinda Knippers 	size_t offset;
25666bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
25676bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
25686bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
25696bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2570d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25716bc75619SDan Williams 
25726b577c9dSLinda Knippers 	offset = 0;
25736bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
25746bc75619SDan Williams 	spa = nfit_buf + offset;
25756bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25766bc75619SDan Williams 	spa->header.length = sizeof(*spa);
25776bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
25786bc75619SDan Williams 	spa->range_index = 0+1;
25796bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
25806bc75619SDan Williams 	spa->length = SPA2_SIZE;
2581d7d8464dSRoss Zwisler 	offset += spa->header.length;
25826bc75619SDan Williams 
25837bfe97c7SDan Williams 	/* virtual cd region */
2584d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
25857bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25867bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
25877bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
25887bfe97c7SDan Williams 	spa->range_index = 0;
25897bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
25907bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2591d7d8464dSRoss Zwisler 	offset += spa->header.length;
25927bfe97c7SDan Williams 
25936bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
25946bc75619SDan Williams 	memdev = nfit_buf + offset;
25956bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
25966bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2597dafb1048SDan Williams 	memdev->device_handle = handle[5];
25986bc75619SDan Williams 	memdev->physical_id = 0;
25996bc75619SDan Williams 	memdev->region_id = 0;
26006bc75619SDan Williams 	memdev->range_index = 0+1;
26016bc75619SDan Williams 	memdev->region_index = 0+1;
26026bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
26036bc75619SDan Williams 	memdev->region_offset = 0;
26046bc75619SDan Williams 	memdev->address = 0;
26056bc75619SDan Williams 	memdev->interleave_index = 0;
26066bc75619SDan Williams 	memdev->interleave_ways = 1;
260758138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
260858138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2609f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2610d7d8464dSRoss Zwisler 	offset += memdev->header.length;
26116bc75619SDan Williams 
26126bc75619SDan Williams 	/* dcr-descriptor0 */
26136bc75619SDan Williams 	dcr = nfit_buf + offset;
26146bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
26153b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
26163b87356fSDan Williams 			window_size);
26176bc75619SDan Williams 	dcr->region_index = 0+1;
26185dc68e55SDan Williams 	dcr_common_init(dcr);
2619dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2620be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
26216bc75619SDan Williams 	dcr->windows = 0;
2622ac40b675SDan Williams 	offset += dcr->header.length;
2623d7d8464dSRoss Zwisler 
2624ac40b675SDan Williams 	memdev = nfit_buf + offset;
2625ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2626ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2627ac40b675SDan Williams 	memdev->device_handle = handle[6];
2628ac40b675SDan Williams 	memdev->physical_id = 0;
2629ac40b675SDan Williams 	memdev->region_id = 0;
2630ac40b675SDan Williams 	memdev->range_index = 0;
2631ac40b675SDan Williams 	memdev->region_index = 0+2;
2632ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2633ac40b675SDan Williams 	memdev->region_offset = 0;
2634ac40b675SDan Williams 	memdev->address = 0;
2635ac40b675SDan Williams 	memdev->interleave_index = 0;
2636ac40b675SDan Williams 	memdev->interleave_ways = 1;
2637ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2638d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2639ac40b675SDan Williams 
2640ac40b675SDan Williams 	/* dcr-descriptor1 */
2641ac40b675SDan Williams 	dcr = nfit_buf + offset;
2642ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2643ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2644ac40b675SDan Williams 			window_size);
2645ac40b675SDan Williams 	dcr->region_index = 0+2;
2646ac40b675SDan Williams 	dcr_common_init(dcr);
2647ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2648ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2649ac40b675SDan Williams 	dcr->windows = 0;
2650d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2651ac40b675SDan Williams 
26529741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
26539741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
26549741a559SRoss Zwisler 
26551526f9e2SRoss Zwisler 	t->nfit_filled = offset;
26561526f9e2SRoss Zwisler 
26579fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
26589fb1a190SDave Jiang 			SPA2_SIZE);
2659f471f1a7SDan Williams 
2660d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2661e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2662e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2663e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2664e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2665674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
26669484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
26679484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26689484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26696bc75619SDan Williams }
26706bc75619SDan Williams 
26716bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
26726bc75619SDan Williams 		void *iobuf, u64 len, int rw)
26736bc75619SDan Williams {
26746bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
26756bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
26766bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
26776bc75619SDan Williams 	unsigned int lane;
26786bc75619SDan Williams 
26796bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
26806bc75619SDan Williams 	if (rw)
268167a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
268267a3e8feSRoss Zwisler 	else {
268367a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
268467a3e8feSRoss Zwisler 
26855deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
26865deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
268767a3e8feSRoss Zwisler 	}
26886bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
26896bc75619SDan Williams 
26906bc75619SDan Williams 	return 0;
26916bc75619SDan Williams }
26926bc75619SDan Williams 
2693a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2694a7de92daSDan Williams 
2695a7de92daSDan Williams union acpi_object *result;
2696a7de92daSDan Williams 
2697a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
269894116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2699a7de92daSDan Williams {
2700a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2701a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2702a7de92daSDan Williams 
2703a7de92daSDan Williams 	return result;
2704a7de92daSDan Williams }
2705a7de92daSDan Williams 
2706a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2707a7de92daSDan Williams {
2708a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2709a7de92daSDan Williams 	if (!result)
2710a7de92daSDan Williams 		return -ENOMEM;
2711a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2712a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2713a7de92daSDan Williams 	result->buffer.length = size;
2714a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2715a7de92daSDan Williams 	memset(buf, 0, size);
2716a7de92daSDan Williams 	return 0;
2717a7de92daSDan Williams }
2718a7de92daSDan Williams 
2719a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2720a7de92daSDan Williams {
2721a7de92daSDan Williams 	int rc, cmd_rc;
2722a7de92daSDan Williams 	struct nvdimm *nvdimm;
2723a7de92daSDan Williams 	struct acpi_device *adev;
2724a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2725a7de92daSDan Williams 	struct nd_ars_record *record;
2726a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2727a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2728a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2729a7de92daSDan Williams 	union {
2730a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2731fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2732a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2733a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2734a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2735a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2736a7de92daSDan Williams 	} cmds;
2737a7de92daSDan Williams 
2738a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2739a7de92daSDan Williams 	if (!adev)
2740a7de92daSDan Williams 		return -ENOMEM;
2741a7de92daSDan Williams 	*adev = (struct acpi_device) {
2742a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2743a7de92daSDan Williams 		.dev = {
2744a7de92daSDan Williams 			.init_name = "test-adev",
2745a7de92daSDan Williams 		},
2746a7de92daSDan Williams 	};
2747a7de92daSDan Williams 
2748a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2749a7de92daSDan Williams 	if (!acpi_desc)
2750a7de92daSDan Williams 		return -ENOMEM;
2751a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2752a7de92daSDan Williams 		.nd_desc = {
2753a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2754a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2755a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
275610246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
275710246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2758a7de92daSDan Williams 			.module = THIS_MODULE,
2759a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2760a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
2761d46e6a21SDan Williams 		},
27629fb1a190SDave Jiang 		.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
27639fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_SET
27649fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
27659fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_GET,
2766a7de92daSDan Williams 		.dev = &adev->dev,
2767a7de92daSDan Williams 	};
2768a7de92daSDan Williams 
2769a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2770a7de92daSDan Williams 	if (!nfit_mem)
2771a7de92daSDan Williams 		return -ENOMEM;
2772a7de92daSDan Williams 
2773a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2774a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2775a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2776a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2777a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2778a7de92daSDan Williams 		.adev = adev,
2779a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2780a7de92daSDan Williams 		.dsm_mask = mask,
2781a7de92daSDan Williams 	};
2782a7de92daSDan Williams 
2783a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2784a7de92daSDan Williams 	if (!nvdimm)
2785a7de92daSDan Williams 		return -ENOMEM;
2786a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2787a7de92daSDan Williams 		.provider_data = nfit_mem,
2788a7de92daSDan Williams 		.cmd_mask = mask,
2789a7de92daSDan Williams 		.dev = {
2790a7de92daSDan Williams 			.init_name = "test-dimm",
2791a7de92daSDan Williams 		},
2792a7de92daSDan Williams 	};
2793a7de92daSDan Williams 
2794a7de92daSDan Williams 
2795a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2796a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2797a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2798a7de92daSDan Williams 		.status = 0,
2799a7de92daSDan Williams 		.config_size = SZ_128K,
2800a7de92daSDan Williams 		.max_xfer = SZ_4K,
2801a7de92daSDan Williams 	};
2802a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2803a7de92daSDan Williams 	if (rc)
2804a7de92daSDan Williams 		return rc;
2805a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2806a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2807a7de92daSDan Williams 
2808a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2809a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2810a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2811a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2812a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2813a7de92daSDan Williams 		return -EIO;
2814a7de92daSDan Williams 	}
2815a7de92daSDan Williams 
2816a7de92daSDan Williams 
2817a7de92daSDan Williams 	/* test ars_status with zero output */
2818a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2819a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2820a7de92daSDan Williams 		.out_length = 0,
2821a7de92daSDan Williams 	};
2822a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2823a7de92daSDan Williams 	if (rc)
2824a7de92daSDan Williams 		return rc;
2825a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2826a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2827a7de92daSDan Williams 
2828a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2829a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2830a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2831a7de92daSDan Williams 		return -EIO;
2832a7de92daSDan Williams 	}
2833a7de92daSDan Williams 
2834a7de92daSDan Williams 
2835a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2836a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2837a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2838a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2839a7de92daSDan Williams 	};
2840a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2841a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2842a7de92daSDan Williams 	if (rc)
2843a7de92daSDan Williams 		return rc;
2844a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2845a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2846a7de92daSDan Williams 
2847a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2848a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2849a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2850a7de92daSDan Williams 		return -EIO;
2851a7de92daSDan Williams 	}
2852a7de92daSDan Williams 
2853a7de92daSDan Williams 
2854a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2855a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2856a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2857a7de92daSDan Williams 		.out_length = cmd_size - 4,
2858a7de92daSDan Williams 	};
2859a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2860a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2861a7de92daSDan Williams 		.length = test_val,
2862a7de92daSDan Williams 	};
2863a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2864a7de92daSDan Williams 	if (rc)
2865a7de92daSDan Williams 		return rc;
2866a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2867a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2868a7de92daSDan Williams 
2869a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2870a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2871a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2872a7de92daSDan Williams 		return -EIO;
2873a7de92daSDan Williams 	}
2874a7de92daSDan Williams 
2875a7de92daSDan Williams 
2876a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2877a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2878a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2879a7de92daSDan Williams 		.out_length = cmd_size,
2880a7de92daSDan Williams 	};
2881a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2882a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2883a7de92daSDan Williams 		.length = test_val,
2884a7de92daSDan Williams 	};
2885a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2886a7de92daSDan Williams 	if (rc)
2887a7de92daSDan Williams 		return rc;
2888a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2889a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2890a7de92daSDan Williams 
2891a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2892a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2893a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2894a7de92daSDan Williams 		return -EIO;
2895a7de92daSDan Williams 	}
2896a7de92daSDan Williams 
2897a7de92daSDan Williams 
2898a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2899a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2900a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2901a7de92daSDan Williams 		.status = 1 << 16,
2902a7de92daSDan Williams 	};
2903a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2904a7de92daSDan Williams 	if (rc)
2905a7de92daSDan Williams 		return rc;
2906a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2907a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2908a7de92daSDan Williams 
2909a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2910a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2911a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2912a7de92daSDan Williams 		return -EIO;
2913a7de92daSDan Williams 	}
2914a7de92daSDan Williams 
2915fb2a1748SDan Williams 	/* test clear error */
2916fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2917fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2918fb2a1748SDan Williams 		.length = 512,
2919fb2a1748SDan Williams 		.cleared = 512,
2920fb2a1748SDan Williams 	};
2921fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2922fb2a1748SDan Williams 	if (rc)
2923fb2a1748SDan Williams 		return rc;
2924fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2925fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2926fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2927fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2928fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2929fb2a1748SDan Williams 		return -EIO;
2930fb2a1748SDan Williams 	}
2931fb2a1748SDan Williams 
2932a7de92daSDan Williams 	return 0;
2933a7de92daSDan Williams }
2934a7de92daSDan Williams 
29356bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
29366bc75619SDan Williams {
29376bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
29386bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
29396bc75619SDan Williams 	struct device *dev = &pdev->dev;
29406bc75619SDan Williams 	struct nfit_test *nfit_test;
2941231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2942c14a868aSDan Williams 	union acpi_object *obj;
29436bc75619SDan Williams 	int rc;
29446bc75619SDan Williams 
2945a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2946a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2947a7de92daSDan Williams 		if (rc)
2948a7de92daSDan Williams 			return rc;
2949a7de92daSDan Williams 	}
2950a7de92daSDan Williams 
29516bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
29526bc75619SDan Williams 
29536bc75619SDan Williams 	/* common alloc */
29546bc75619SDan Williams 	if (nfit_test->num_dcr) {
29556bc75619SDan Williams 		int num = nfit_test->num_dcr;
29566bc75619SDan Williams 
29576bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
29586bc75619SDan Williams 				GFP_KERNEL);
29596bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29606bc75619SDan Williams 				GFP_KERNEL);
29619d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
29629d27a87eSDan Williams 				GFP_KERNEL);
29639d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29649d27a87eSDan Williams 				GFP_KERNEL);
29656bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
29666bc75619SDan Williams 				GFP_KERNEL);
29676bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
29686bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29696bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
29706bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
29716bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
29726bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2973ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2974ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2975ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2976ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2977ed07c433SDan Williams 				GFP_KERNEL);
2978bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2979bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
29806bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
29816bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
29829d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2983bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2984bfbaa952SDave Jiang 				&& nfit_test->fw)
29856bc75619SDan Williams 			/* pass */;
29866bc75619SDan Williams 		else
29876bc75619SDan Williams 			return -ENOMEM;
29886bc75619SDan Williams 	}
29896bc75619SDan Williams 
29906bc75619SDan Williams 	if (nfit_test->num_pm) {
29916bc75619SDan Williams 		int num = nfit_test->num_pm;
29926bc75619SDan Williams 
29936bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
29946bc75619SDan Williams 				GFP_KERNEL);
29956bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
29966bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29976bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
29986bc75619SDan Williams 			/* pass */;
29996bc75619SDan Williams 		else
30006bc75619SDan Williams 			return -ENOMEM;
30016bc75619SDan Williams 	}
30026bc75619SDan Williams 
30036bc75619SDan Williams 	/* per-nfit specific alloc */
30046bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
30056bc75619SDan Williams 		return -ENOMEM;
30066bc75619SDan Williams 
30076bc75619SDan Williams 	nfit_test->setup(nfit_test);
30086bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
3009a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
30106bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
30116bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
3012a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
3013bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
3014a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
30156bc75619SDan Williams 
3016e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
30171526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
301858cd71b4SDan Williams 	if (rc)
301920985164SVishal Verma 		return rc;
302020985164SVishal Verma 
3021fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
3022fbabd829SDan Williams 	if (rc)
3023fbabd829SDan Williams 		return rc;
3024fbabd829SDan Williams 
302520985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
302620985164SVishal Verma 		return 0;
302720985164SVishal Verma 
302820985164SVishal Verma 	nfit_test->setup_hotplug = 1;
302920985164SVishal Verma 	nfit_test->setup(nfit_test);
303020985164SVishal Verma 
3031c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3032c14a868aSDan Williams 	if (!obj)
3033c14a868aSDan Williams 		return -ENOMEM;
3034c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
3035c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
3036c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
3037c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
3038c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
3039231bf117SDan Williams 
3040231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
3041231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
3042231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
3043231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
3044231bf117SDan Williams 		int i;
3045231bf117SDan Williams 
3046af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
3047231bf117SDan Williams 			if (nfit_handle == handle[i])
3048231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
3049231bf117SDan Williams 						nfit_mem);
3050231bf117SDan Williams 	}
3051231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
30526bc75619SDan Williams 
30536bc75619SDan Williams 	return 0;
30546bc75619SDan Williams }
30556bc75619SDan Williams 
30566bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
30576bc75619SDan Williams {
30586bc75619SDan Williams 	return 0;
30596bc75619SDan Williams }
30606bc75619SDan Williams 
30616bc75619SDan Williams static void nfit_test_release(struct device *dev)
30626bc75619SDan Williams {
30636bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
30646bc75619SDan Williams 
30656bc75619SDan Williams 	kfree(nfit_test);
30666bc75619SDan Williams }
30676bc75619SDan Williams 
30686bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
30696bc75619SDan Williams 	{ KBUILD_MODNAME },
30706bc75619SDan Williams 	{ },
30716bc75619SDan Williams };
30726bc75619SDan Williams 
30736bc75619SDan Williams static struct platform_driver nfit_test_driver = {
30746bc75619SDan Williams 	.probe = nfit_test_probe,
30756bc75619SDan Williams 	.remove = nfit_test_remove,
30766bc75619SDan Williams 	.driver = {
30776bc75619SDan Williams 		.name = KBUILD_MODNAME,
30786bc75619SDan Williams 	},
30796bc75619SDan Williams 	.id_table = nfit_test_id,
30806bc75619SDan Williams };
30816bc75619SDan Williams 
30825d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
30835d8beee2SDan Williams 
30845d8beee2SDan Williams enum INJECT {
30855d8beee2SDan Williams 	INJECT_NONE,
30865d8beee2SDan Williams 	INJECT_SRC,
30875d8beee2SDan Williams 	INJECT_DST,
30885d8beee2SDan Williams };
30895d8beee2SDan Williams 
30905d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
30915d8beee2SDan Williams {
30925d8beee2SDan Williams 	size_t i;
30935d8beee2SDan Williams 
30945d8beee2SDan Williams 	memset(dst, 0xff, size);
30955d8beee2SDan Williams 	for (i = 0; i < size; i++)
30965d8beee2SDan Williams 		src[i] = (char) i;
30975d8beee2SDan Williams }
30985d8beee2SDan Williams 
30995d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
31005d8beee2SDan Williams 		size_t size, unsigned long rem)
31015d8beee2SDan Williams {
31025d8beee2SDan Williams 	size_t i;
31035d8beee2SDan Williams 
31045d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
31055d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
31065d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
31075d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
31085d8beee2SDan Williams 					(unsigned char) i);
31095d8beee2SDan Williams 			return false;
31105d8beee2SDan Williams 		}
31115d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
31125d8beee2SDan Williams 		if (dst[i] != 0xffU) {
31135d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
31145d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
31155d8beee2SDan Williams 			return false;
31165d8beee2SDan Williams 		}
31175d8beee2SDan Williams 	return true;
31185d8beee2SDan Williams }
31195d8beee2SDan Williams 
31205d8beee2SDan Williams void mcsafe_test(void)
31215d8beee2SDan Williams {
31225d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
31235d8beee2SDan Williams 	enum INJECT inj;
31245d8beee2SDan Williams 
31255d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
31265d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
31275d8beee2SDan Williams 	} else {
31285d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
31295d8beee2SDan Williams 		return;
31305d8beee2SDan Williams 	}
31315d8beee2SDan Williams 
31325d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
31335d8beee2SDan Williams 		int i;
31345d8beee2SDan Williams 
31355d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
31365d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
31375d8beee2SDan Williams 			unsigned long expect, rem;
31385d8beee2SDan Williams 			void *src, *dst;
31395d8beee2SDan Williams 			bool valid;
31405d8beee2SDan Williams 
31415d8beee2SDan Williams 			switch (inj) {
31425d8beee2SDan Williams 			case INJECT_NONE:
31435d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31445d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31455d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31465d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31475d8beee2SDan Williams 				expect = 0;
31485d8beee2SDan Williams 				break;
31495d8beee2SDan Williams 			case INJECT_SRC:
31505d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
31515d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31525d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31535d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31545d8beee2SDan Williams 				expect = 512 - i;
31555d8beee2SDan Williams 				break;
31565d8beee2SDan Williams 			case INJECT_DST:
31575d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31585d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
31595d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
31605d8beee2SDan Williams 				src = &mcsafe_buf[1024];
31615d8beee2SDan Williams 				expect = 512 - i;
31625d8beee2SDan Williams 				break;
31635d8beee2SDan Williams 			}
31645d8beee2SDan Williams 
31655d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
31665d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
31675d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
31685d8beee2SDan Williams 			if (rem == expect && valid)
31695d8beee2SDan Williams 				continue;
31705d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
31715d8beee2SDan Williams 					__func__,
31725d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
31735d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
31745d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
31755d8beee2SDan Williams 					expect);
31765d8beee2SDan Williams 		}
31775d8beee2SDan Williams 	}
31785d8beee2SDan Williams 
31795d8beee2SDan Williams 	mcsafe_inject_src(NULL);
31805d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
31815d8beee2SDan Williams }
31825d8beee2SDan Williams 
31836bc75619SDan Williams static __init int nfit_test_init(void)
31846bc75619SDan Williams {
31856bc75619SDan Williams 	int rc, i;
31866bc75619SDan Williams 
31870fb5c8dfSDan Williams 	pmem_test();
31880fb5c8dfSDan Williams 	libnvdimm_test();
31890fb5c8dfSDan Williams 	acpi_nfit_test();
31900fb5c8dfSDan Williams 	device_dax_test();
31915d8beee2SDan Williams 	mcsafe_test();
319292f6f2d7SVishal Verma 	dax_pmem_test();
319392f6f2d7SVishal Verma 	dax_pmem_core_test();
3194c0e71d60SJan Kara #ifdef CONFIG_DEV_DAX_PMEM_COMPAT
319592f6f2d7SVishal Verma 	dax_pmem_compat_test();
3196c0e71d60SJan Kara #endif
31970fb5c8dfSDan Williams 
3198a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3199231bf117SDan Williams 
32009fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
32019fb1a190SDave Jiang 	if (!nfit_wq)
32029fb1a190SDave Jiang 		return -ENOMEM;
32039fb1a190SDave Jiang 
3204a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
3205a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
3206a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
3207a7de92daSDan Williams 		goto err_register;
3208a7de92daSDan Williams 	}
32096bc75619SDan Williams 
3210e3f5df76SDan Williams 	nfit_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
3211e3f5df76SDan Williams 	if (!nfit_pool) {
3212e3f5df76SDan Williams 		rc = -ENOMEM;
3213e3f5df76SDan Williams 		goto err_register;
3214e3f5df76SDan Williams 	}
3215e3f5df76SDan Williams 
3216e3f5df76SDan Williams 	if (gen_pool_add(nfit_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
3217e3f5df76SDan Williams 		rc = -ENOMEM;
3218e3f5df76SDan Williams 		goto err_register;
3219e3f5df76SDan Williams 	}
3220e3f5df76SDan Williams 
32216bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
32226bc75619SDan Williams 		struct nfit_test *nfit_test;
32236bc75619SDan Williams 		struct platform_device *pdev;
32246bc75619SDan Williams 
32256bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
32266bc75619SDan Williams 		if (!nfit_test) {
32276bc75619SDan Williams 			rc = -ENOMEM;
32286bc75619SDan Williams 			goto err_register;
32296bc75619SDan Williams 		}
32306bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
32319fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
32326bc75619SDan Williams 		switch (i) {
32336bc75619SDan Williams 		case 0:
32346bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3235dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
32366bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
32376bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
32386bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
32396bc75619SDan Williams 			break;
32406bc75619SDan Williams 		case 1:
3241a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3242dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3243ac40b675SDan Williams 			nfit_test->num_dcr = 2;
32446bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
32456bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
32466bc75619SDan Williams 			break;
32476bc75619SDan Williams 		default:
32486bc75619SDan Williams 			rc = -EINVAL;
32496bc75619SDan Williams 			goto err_register;
32506bc75619SDan Williams 		}
32516bc75619SDan Williams 		pdev = &nfit_test->pdev;
32526bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
32536bc75619SDan Williams 		pdev->id = i;
32546bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
32556bc75619SDan Williams 		rc = platform_device_register(pdev);
32566bc75619SDan Williams 		if (rc) {
32576bc75619SDan Williams 			put_device(&pdev->dev);
32586bc75619SDan Williams 			goto err_register;
32596bc75619SDan Williams 		}
32608b06b884SDan Williams 		get_device(&pdev->dev);
32616bc75619SDan Williams 
32626bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
32636bc75619SDan Williams 		if (rc)
32646bc75619SDan Williams 			goto err_register;
32656bc75619SDan Williams 
32666bc75619SDan Williams 		instances[i] = nfit_test;
32679fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
32686bc75619SDan Williams 	}
32696bc75619SDan Williams 
32706bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
32716bc75619SDan Williams 	if (rc)
32726bc75619SDan Williams 		goto err_register;
32736bc75619SDan Williams 	return 0;
32746bc75619SDan Williams 
32756bc75619SDan Williams  err_register:
3276e3f5df76SDan Williams 	if (nfit_pool)
3277e3f5df76SDan Williams 		gen_pool_destroy(nfit_pool);
3278e3f5df76SDan Williams 
32799fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32806bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32816bc75619SDan Williams 		if (instances[i])
32826bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
32836bc75619SDan Williams 	nfit_test_teardown();
32848b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32858b06b884SDan Williams 		if (instances[i])
32868b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
32878b06b884SDan Williams 
32886bc75619SDan Williams 	return rc;
32896bc75619SDan Williams }
32906bc75619SDan Williams 
32916bc75619SDan Williams static __exit void nfit_test_exit(void)
32926bc75619SDan Williams {
32936bc75619SDan Williams 	int i;
32946bc75619SDan Williams 
32959fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
32969fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32976bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32986bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
32998b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
33006bc75619SDan Williams 	nfit_test_teardown();
33018b06b884SDan Williams 
3302e3f5df76SDan Williams 	gen_pool_destroy(nfit_pool);
3303e3f5df76SDan Williams 
33048b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
33058b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3306231bf117SDan Williams 	class_destroy(nfit_test_dimm);
33076bc75619SDan Williams }
33086bc75619SDan Williams 
33096bc75619SDan Williams module_init(nfit_test_init);
33106bc75619SDan Williams module_exit(nfit_test_exit);
33116bc75619SDan Williams MODULE_LICENSE("GPL v2");
33126bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3313