xref: /linux/tools/testing/nvdimm/test/nfit.c (revision f110176633d74bbac1f80ab9b9c6b83ea3e1cc23)
16bc75619SDan Williams /*
26bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
36bc75619SDan Williams  *
46bc75619SDan Williams  * This program is free software; you can redistribute it and/or modify
56bc75619SDan Williams  * it under the terms of version 2 of the GNU General Public License as
66bc75619SDan Williams  * published by the Free Software Foundation.
76bc75619SDan Williams  *
86bc75619SDan Williams  * This program is distributed in the hope that it will be useful, but
96bc75619SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
106bc75619SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116bc75619SDan Williams  * General Public License for more details.
126bc75619SDan Williams  */
136bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
146bc75619SDan Williams #include <linux/platform_device.h>
156bc75619SDan Williams #include <linux/dma-mapping.h>
16d8d378faSDan Williams #include <linux/workqueue.h>
176bc75619SDan Williams #include <linux/libnvdimm.h>
186bc75619SDan Williams #include <linux/vmalloc.h>
196bc75619SDan Williams #include <linux/device.h>
206bc75619SDan Williams #include <linux/module.h>
2120985164SVishal Verma #include <linux/mutex.h>
226bc75619SDan Williams #include <linux/ndctl.h>
236bc75619SDan Williams #include <linux/sizes.h>
2420985164SVishal Verma #include <linux/list.h>
256bc75619SDan Williams #include <linux/slab.h>
26a7de92daSDan Williams #include <nd-core.h>
270ead1118SDan Williams #include <intel.h>
286bc75619SDan Williams #include <nfit.h>
296bc75619SDan Williams #include <nd.h>
306bc75619SDan Williams #include "nfit_test.h"
310fb5c8dfSDan Williams #include "../watermark.h"
326bc75619SDan Williams 
335d8beee2SDan Williams #include <asm/mcsafe_test.h>
345d8beee2SDan Williams 
356bc75619SDan Williams /*
366bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
376bc75619SDan Williams  *
386bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
396bc75619SDan Williams  *
406bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
416bc75619SDan Williams  *           +----------+--------------+----------+---------+
426bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
436bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
446bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
456bc75619SDan Williams  *    |      +----------+--------------v----------v         v
466bc75619SDan Williams  * +--+---+                            |                    |
476bc75619SDan Williams  * | cpu0 |                                    region1
486bc75619SDan Williams  * +--+---+                            |                    |
496bc75619SDan Williams  *    |      +-------------------------^----------^         ^
506bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
516bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
526bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
536bc75619SDan Williams  *           +-------------------------+----------+-+-------+
546bc75619SDan Williams  *
5520985164SVishal Verma  * +--+---+
5620985164SVishal Verma  * | cpu1 |
5720985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5820985164SVishal Verma  *    |      +----------------------------------------------+
5920985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
6020985164SVishal Verma  * | imc0 +--+----------------------------------------------+
6120985164SVishal Verma  * +------+
6220985164SVishal Verma  *
6320985164SVishal Verma  *
646bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
656bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
666bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
676bc75619SDan Williams  *
686bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
696bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
706bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
716bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
726bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
736bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
746bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
756bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
766bc75619SDan Williams  *    names that can be assigned to a namespace.
776bc75619SDan Williams  *
786bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
796bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
806bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
816bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
826bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
836bc75619SDan Williams  *    "blk5.0".
846bc75619SDan Williams  *
856bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
866bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
876bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
886bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
896bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
906bc75619SDan Williams  *
916bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
926bc75619SDan Williams  *
936bc75619SDan Williams  *  region2
946bc75619SDan Williams  * +---------------------+
956bc75619SDan Williams  * |---------------------|
966bc75619SDan Williams  * ||       pm2.0       ||
976bc75619SDan Williams  * |---------------------|
986bc75619SDan Williams  * +---------------------+
996bc75619SDan Williams  *
1006bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
1016bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
1026bc75619SDan Williams  *    reference an NVDIMM.
1036bc75619SDan Williams  */
1046bc75619SDan Williams enum {
10520985164SVishal Verma 	NUM_PM  = 3,
10620985164SVishal Verma 	NUM_DCR = 5,
10785d3fa02SDan Williams 	NUM_HINTS = 8,
1086bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1096bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1109741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1119741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1126bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1136bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1147bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1156bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1166bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1176bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1186bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1196bc75619SDan Williams 	DCR_SIZE = 12,
1206bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1216bc75619SDan Williams };
1226bc75619SDan Williams 
1236bc75619SDan Williams struct nfit_test_dcr {
1246bc75619SDan Williams 	__le64 bdw_addr;
1256bc75619SDan Williams 	__le32 bdw_status;
1266bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1276bc75619SDan Williams };
1286bc75619SDan Williams 
1296bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1306bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1316bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1326bc75619SDan Williams 
133dafb1048SDan Williams static u32 handle[] = {
1346bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1356bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1366bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1376bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13820985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
139dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
140ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1416bc75619SDan Williams };
1426bc75619SDan Williams 
14373606afdSDan Williams static unsigned long dimm_fail_cmd_flags[NUM_DCR];
14455c72ab6SDan Williams static int dimm_fail_cmd_code[NUM_DCR];
14573606afdSDan Williams 
146b4d4702fSVishal Verma static const struct nd_intel_smart smart_def = {
147b4d4702fSVishal Verma 	.flags = ND_INTEL_SMART_HEALTH_VALID
148b4d4702fSVishal Verma 		| ND_INTEL_SMART_SPARES_VALID
149b4d4702fSVishal Verma 		| ND_INTEL_SMART_ALARM_VALID
150b4d4702fSVishal Verma 		| ND_INTEL_SMART_USED_VALID
151b4d4702fSVishal Verma 		| ND_INTEL_SMART_SHUTDOWN_VALID
152*f1101766SDan Williams 		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
153b4d4702fSVishal Verma 		| ND_INTEL_SMART_MTEMP_VALID
154b4d4702fSVishal Verma 		| ND_INTEL_SMART_CTEMP_VALID,
155b4d4702fSVishal Verma 	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
156b4d4702fSVishal Verma 	.media_temperature = 23 * 16,
157b4d4702fSVishal Verma 	.ctrl_temperature = 25 * 16,
158b4d4702fSVishal Verma 	.pmic_temperature = 40 * 16,
159b4d4702fSVishal Verma 	.spares = 75,
160b4d4702fSVishal Verma 	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
161b4d4702fSVishal Verma 		| ND_INTEL_SMART_TEMP_TRIP,
162b4d4702fSVishal Verma 	.ait_status = 1,
163b4d4702fSVishal Verma 	.life_used = 5,
164b4d4702fSVishal Verma 	.shutdown_state = 0,
165*f1101766SDan Williams 	.shutdown_count = 42,
166b4d4702fSVishal Verma 	.vendor_size = 0,
167b4d4702fSVishal Verma };
168b4d4702fSVishal Verma 
169bfbaa952SDave Jiang struct nfit_test_fw {
170bfbaa952SDave Jiang 	enum intel_fw_update_state state;
171bfbaa952SDave Jiang 	u32 context;
172bfbaa952SDave Jiang 	u64 version;
173bfbaa952SDave Jiang 	u32 size_received;
174bfbaa952SDave Jiang 	u64 end_time;
175bfbaa952SDave Jiang };
176bfbaa952SDave Jiang 
1776bc75619SDan Williams struct nfit_test {
1786bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1796bc75619SDan Williams 	struct platform_device pdev;
1806bc75619SDan Williams 	struct list_head resources;
1816bc75619SDan Williams 	void *nfit_buf;
1826bc75619SDan Williams 	dma_addr_t nfit_dma;
1836bc75619SDan Williams 	size_t nfit_size;
1841526f9e2SRoss Zwisler 	size_t nfit_filled;
185dafb1048SDan Williams 	int dcr_idx;
1866bc75619SDan Williams 	int num_dcr;
1876bc75619SDan Williams 	int num_pm;
1886bc75619SDan Williams 	void **dimm;
1896bc75619SDan Williams 	dma_addr_t *dimm_dma;
1909d27a87eSDan Williams 	void **flush;
1919d27a87eSDan Williams 	dma_addr_t *flush_dma;
1926bc75619SDan Williams 	void **label;
1936bc75619SDan Williams 	dma_addr_t *label_dma;
1946bc75619SDan Williams 	void **spa_set;
1956bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1966bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1976bc75619SDan Williams 	dma_addr_t *dcr_dma;
1986bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1996bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
20020985164SVishal Verma 	int setup_hotplug;
201c14a868aSDan Williams 	union acpi_object **_fit;
202c14a868aSDan Williams 	dma_addr_t _fit_dma;
203f471f1a7SDan Williams 	struct ars_state {
204f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
205f471f1a7SDan Williams 		unsigned long deadline;
206f471f1a7SDan Williams 		spinlock_t lock;
207f471f1a7SDan Williams 	} ars_state;
208231bf117SDan Williams 	struct device *dimm_dev[NUM_DCR];
209ed07c433SDan Williams 	struct nd_intel_smart *smart;
210ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
2119fb1a190SDave Jiang 	struct badrange badrange;
2129fb1a190SDave Jiang 	struct work_struct work;
213bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
2146bc75619SDan Williams };
2156bc75619SDan Williams 
2169fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
2179fb1a190SDave Jiang 
2186bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
2196bc75619SDan Williams {
2206bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
2216bc75619SDan Williams 
2226bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
2236bc75619SDan Williams }
2246bc75619SDan Williams 
225bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
226bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
227bfbaa952SDave Jiang 		int idx)
228bfbaa952SDave Jiang {
229bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
230bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
231bfbaa952SDave Jiang 
232bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
233bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
234bfbaa952SDave Jiang 
235bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
236bfbaa952SDave Jiang 		return -EINVAL;
237bfbaa952SDave Jiang 
238bfbaa952SDave Jiang 	nd_cmd->status = 0;
239bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
240bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
241bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
242bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
243bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
244bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
245bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
246bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
247bfbaa952SDave Jiang 
248bfbaa952SDave Jiang 	return 0;
249bfbaa952SDave Jiang }
250bfbaa952SDave Jiang 
251bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
252bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
253bfbaa952SDave Jiang 		int idx)
254bfbaa952SDave Jiang {
255bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
256bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
257bfbaa952SDave Jiang 
258bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
259bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
260bfbaa952SDave Jiang 
261bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
262bfbaa952SDave Jiang 		return -EINVAL;
263bfbaa952SDave Jiang 
264bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
265bfbaa952SDave Jiang 		/* extended status, FW update in progress */
266bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
267bfbaa952SDave Jiang 		return 0;
268bfbaa952SDave Jiang 	}
269bfbaa952SDave Jiang 
270bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
271bfbaa952SDave Jiang 	fw->context++;
272bfbaa952SDave Jiang 	fw->size_received = 0;
273bfbaa952SDave Jiang 	nd_cmd->status = 0;
274bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
275bfbaa952SDave Jiang 
276bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
277bfbaa952SDave Jiang 
278bfbaa952SDave Jiang 	return 0;
279bfbaa952SDave Jiang }
280bfbaa952SDave Jiang 
281bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
282bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
283bfbaa952SDave Jiang 		int idx)
284bfbaa952SDave Jiang {
285bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
286bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
287bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
288bfbaa952SDave Jiang 
289bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
290bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
291bfbaa952SDave Jiang 
292bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
293bfbaa952SDave Jiang 		return -EINVAL;
294bfbaa952SDave Jiang 
295bfbaa952SDave Jiang 
296bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
297bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
298bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
299bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
300bfbaa952SDave Jiang 
301bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
302bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
303bfbaa952SDave Jiang 		*status = 0x5;
304bfbaa952SDave Jiang 		return 0;
305bfbaa952SDave Jiang 	}
306bfbaa952SDave Jiang 
307bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
308bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
309bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
310bfbaa952SDave Jiang 		*status = 0x10007;
311bfbaa952SDave Jiang 		return 0;
312bfbaa952SDave Jiang 	}
313bfbaa952SDave Jiang 
314bfbaa952SDave Jiang 	/*
315bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
316bfbaa952SDave Jiang 	 * check length is > max send length
317bfbaa952SDave Jiang 	 */
318bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
319bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
320bfbaa952SDave Jiang 		*status = 0x3;
321bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
322bfbaa952SDave Jiang 		return 0;
323bfbaa952SDave Jiang 	}
324bfbaa952SDave Jiang 
325bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
326bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
327bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
328bfbaa952SDave Jiang 	*status = 0;
329bfbaa952SDave Jiang 	return 0;
330bfbaa952SDave Jiang }
331bfbaa952SDave Jiang 
332bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
333bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
334bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
335bfbaa952SDave Jiang {
336bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
337bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
338bfbaa952SDave Jiang 
339bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
340bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
341bfbaa952SDave Jiang 
342bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
343bfbaa952SDave Jiang 		/* update already done, need cold boot */
344bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
345bfbaa952SDave Jiang 		return 0;
346bfbaa952SDave Jiang 	}
347bfbaa952SDave Jiang 
348bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
349bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
350bfbaa952SDave Jiang 
351bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
352bfbaa952SDave Jiang 	case 0: /* finish */
353bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
354bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
355bfbaa952SDave Jiang 					__func__, nd_cmd->context,
356bfbaa952SDave Jiang 					fw->context);
357bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
358bfbaa952SDave Jiang 			return 0;
359bfbaa952SDave Jiang 		}
360bfbaa952SDave Jiang 		nd_cmd->status = 0;
361bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
362bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
363bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
364bfbaa952SDave Jiang 		break;
365bfbaa952SDave Jiang 
366bfbaa952SDave Jiang 	case 1: /* abort */
367bfbaa952SDave Jiang 		fw->size_received = 0;
368bfbaa952SDave Jiang 		/* successfully aborted status */
369bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
370bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
371bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
372bfbaa952SDave Jiang 		break;
373bfbaa952SDave Jiang 
374bfbaa952SDave Jiang 	default: /* bad control flag */
375bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
376bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
377bfbaa952SDave Jiang 		return -EINVAL;
378bfbaa952SDave Jiang 	}
379bfbaa952SDave Jiang 
380bfbaa952SDave Jiang 	return 0;
381bfbaa952SDave Jiang }
382bfbaa952SDave Jiang 
383bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
384bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
385bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
386bfbaa952SDave Jiang {
387bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
388bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
389bfbaa952SDave Jiang 
390bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
391bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
392bfbaa952SDave Jiang 
393bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
394bfbaa952SDave Jiang 		return -EINVAL;
395bfbaa952SDave Jiang 
396bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
397bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
398bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
399bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
400bfbaa952SDave Jiang 		return 0;
401bfbaa952SDave Jiang 	}
402bfbaa952SDave Jiang 
403bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
404bfbaa952SDave Jiang 
405bfbaa952SDave Jiang 	switch (fw->state) {
406bfbaa952SDave Jiang 	case FW_STATE_NEW:
407bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
408bfbaa952SDave Jiang 		nd_cmd->status = 0;
409bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
410bfbaa952SDave Jiang 		break;
411bfbaa952SDave Jiang 
412bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
413bfbaa952SDave Jiang 		/* sequencing error */
414bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
415bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
416bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
417bfbaa952SDave Jiang 		break;
418bfbaa952SDave Jiang 
419bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
420bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
421bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
422bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
423bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
424bfbaa952SDave Jiang 			break;
425bfbaa952SDave Jiang 		}
426bfbaa952SDave Jiang 
427bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
428bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
429bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
430bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
431bfbaa952SDave Jiang 		nd_cmd->status = 0;
432bfbaa952SDave Jiang 		/* bogus test version */
433bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
434bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
435bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
436bfbaa952SDave Jiang 		break;
437bfbaa952SDave Jiang 
438bfbaa952SDave Jiang 	default: /* we should never get here */
439bfbaa952SDave Jiang 		return -EINVAL;
440bfbaa952SDave Jiang 	}
441bfbaa952SDave Jiang 
442bfbaa952SDave Jiang 	return 0;
443bfbaa952SDave Jiang }
444bfbaa952SDave Jiang 
44539c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4466bc75619SDan Williams 		unsigned int buf_len)
4476bc75619SDan Williams {
4486bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4496bc75619SDan Williams 		return -EINVAL;
45039c686b8SVishal Verma 
4516bc75619SDan Williams 	nd_cmd->status = 0;
4526bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4536bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
45439c686b8SVishal Verma 
45539c686b8SVishal Verma 	return 0;
4566bc75619SDan Williams }
45739c686b8SVishal Verma 
45839c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
45939c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
46039c686b8SVishal Verma {
4616bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
46239c686b8SVishal Verma 	int rc;
4636bc75619SDan Williams 
4646bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4656bc75619SDan Williams 		return -EINVAL;
4666bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4676bc75619SDan Williams 		return -EINVAL;
4686bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4696bc75619SDan Williams 		return -EINVAL;
4706bc75619SDan Williams 
4716bc75619SDan Williams 	nd_cmd->status = 0;
4726bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47339c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4746bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
47539c686b8SVishal Verma 
47639c686b8SVishal Verma 	return rc;
4776bc75619SDan Williams }
47839c686b8SVishal Verma 
47939c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
48039c686b8SVishal Verma 		unsigned int buf_len, void *label)
48139c686b8SVishal Verma {
4826bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4836bc75619SDan Williams 	u32 *status;
48439c686b8SVishal Verma 	int rc;
4856bc75619SDan Williams 
4866bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4876bc75619SDan Williams 		return -EINVAL;
4886bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4896bc75619SDan Williams 		return -EINVAL;
4906bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4916bc75619SDan Williams 		return -EINVAL;
4926bc75619SDan Williams 
49339c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4946bc75619SDan Williams 	*status = 0;
4956bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
49639c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
4976bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
49839c686b8SVishal Verma 
49939c686b8SVishal Verma 	return rc;
5006bc75619SDan Williams }
50139c686b8SVishal Verma 
502d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
503747ffe11SDan Williams 
50439c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
50539c686b8SVishal Verma 		unsigned int buf_len)
50639c686b8SVishal Verma {
5079fb1a190SDave Jiang 	int ars_recs;
5089fb1a190SDave Jiang 
50939c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
51039c686b8SVishal Verma 		return -EINVAL;
51139c686b8SVishal Verma 
5129fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5139fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5149fb1a190SDave Jiang 
515747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5169fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
51739c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
518d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
51939c686b8SVishal Verma 
52039c686b8SVishal Verma 	return 0;
52139c686b8SVishal Verma }
52239c686b8SVishal Verma 
5239fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5249fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
52539c686b8SVishal Verma {
526f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
527f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5289fb1a190SDave Jiang 	struct badrange_entry *be;
5299fb1a190SDave Jiang 	u64 end = addr + len - 1;
5309fb1a190SDave Jiang 	int i = 0;
531f471f1a7SDan Williams 
532f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
533f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
534f471f1a7SDan Williams 	ars_status->status = 0;
535f471f1a7SDan Williams 	ars_status->address = addr;
536f471f1a7SDan Williams 	ars_status->length = len;
537f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5389fb1a190SDave Jiang 
5399fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5409fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5419fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5429fb1a190SDave Jiang 		u64 rstart, rend;
5439fb1a190SDave Jiang 
5449fb1a190SDave Jiang 		/* skip entries outside the range */
5459fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5469fb1a190SDave Jiang 			continue;
5479fb1a190SDave Jiang 
5489fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5499fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5509fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
551f471f1a7SDan Williams 		ars_record->handle = 0;
5529fb1a190SDave Jiang 		ars_record->err_address = rstart;
5539fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5549fb1a190SDave Jiang 		i++;
5559fb1a190SDave Jiang 	}
5569fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5579fb1a190SDave Jiang 	ars_status->num_records = i;
5589fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5599fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
560f471f1a7SDan Williams }
561f471f1a7SDan Williams 
5629fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5639fb1a190SDave Jiang 		struct ars_state *ars_state,
564f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
565f471f1a7SDan Williams 		int *cmd_rc)
566f471f1a7SDan Williams {
567f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
56839c686b8SVishal Verma 		return -EINVAL;
56939c686b8SVishal Verma 
570f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
571f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
572f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
573f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
574f471f1a7SDan Williams 	} else {
575f471f1a7SDan Williams 		ars_start->status = 0;
576f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5779fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
578f471f1a7SDan Williams 				ars_start->length);
579f471f1a7SDan Williams 		*cmd_rc = 0;
580f471f1a7SDan Williams 	}
581f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
58239c686b8SVishal Verma 
58339c686b8SVishal Verma 	return 0;
58439c686b8SVishal Verma }
58539c686b8SVishal Verma 
586f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
587f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
588f471f1a7SDan Williams 		int *cmd_rc)
58939c686b8SVishal Verma {
590f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
59139c686b8SVishal Verma 		return -EINVAL;
59239c686b8SVishal Verma 
593f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
594f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
595f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
596f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
597f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
598f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
599f471f1a7SDan Williams 	} else {
600f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
601f471f1a7SDan Williams 				ars_state->ars_status->out_length);
602f471f1a7SDan Williams 		*cmd_rc = 0;
603f471f1a7SDan Williams 	}
604f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
60539c686b8SVishal Verma 	return 0;
60639c686b8SVishal Verma }
60739c686b8SVishal Verma 
6085e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6095e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
610d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
611d4f32367SDan Williams {
612d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
613d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
614d4f32367SDan Williams 		return -EINVAL;
615d4f32367SDan Williams 
616d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
617d4f32367SDan Williams 		return -EINVAL;
618d4f32367SDan Williams 
6195e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
620d4f32367SDan Williams 	clear_err->status = 0;
621d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
622d4f32367SDan Williams 	*cmd_rc = 0;
623d4f32367SDan Williams 	return 0;
624d4f32367SDan Williams }
625d4f32367SDan Williams 
62610246dc8SYasunori Goto struct region_search_spa {
62710246dc8SYasunori Goto 	u64 addr;
62810246dc8SYasunori Goto 	struct nd_region *region;
62910246dc8SYasunori Goto };
63010246dc8SYasunori Goto 
63110246dc8SYasunori Goto static int is_region_device(struct device *dev)
63210246dc8SYasunori Goto {
63310246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
63410246dc8SYasunori Goto }
63510246dc8SYasunori Goto 
63610246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
63710246dc8SYasunori Goto {
63810246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
63910246dc8SYasunori Goto 	struct nd_region *nd_region;
64010246dc8SYasunori Goto 	resource_size_t ndr_end;
64110246dc8SYasunori Goto 
64210246dc8SYasunori Goto 	if (!is_region_device(dev))
64310246dc8SYasunori Goto 		return 0;
64410246dc8SYasunori Goto 
64510246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
64610246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
64710246dc8SYasunori Goto 
64810246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
64910246dc8SYasunori Goto 		ctx->region = nd_region;
65010246dc8SYasunori Goto 		return 1;
65110246dc8SYasunori Goto 	}
65210246dc8SYasunori Goto 
65310246dc8SYasunori Goto 	return 0;
65410246dc8SYasunori Goto }
65510246dc8SYasunori Goto 
65610246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
65710246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
65810246dc8SYasunori Goto {
65910246dc8SYasunori Goto 	int ret;
66010246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
66110246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
66210246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
66310246dc8SYasunori Goto 	struct region_search_spa ctx = {
66410246dc8SYasunori Goto 		.addr = spa->spa,
66510246dc8SYasunori Goto 		.region = NULL,
66610246dc8SYasunori Goto 	};
66710246dc8SYasunori Goto 	u64 dpa;
66810246dc8SYasunori Goto 
66910246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
67010246dc8SYasunori Goto 				nfit_test_search_region_spa);
67110246dc8SYasunori Goto 
67210246dc8SYasunori Goto 	if (!ret)
67310246dc8SYasunori Goto 		return -ENODEV;
67410246dc8SYasunori Goto 
67510246dc8SYasunori Goto 	nd_region = ctx.region;
67610246dc8SYasunori Goto 
67710246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
67810246dc8SYasunori Goto 
67910246dc8SYasunori Goto 	/*
68010246dc8SYasunori Goto 	 * last dimm is selected for test
68110246dc8SYasunori Goto 	 */
68210246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
68310246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
68410246dc8SYasunori Goto 
68510246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
68610246dc8SYasunori Goto 	spa->num_nvdimms = 1;
68710246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
68810246dc8SYasunori Goto 
68910246dc8SYasunori Goto 	return 0;
69010246dc8SYasunori Goto }
69110246dc8SYasunori Goto 
69210246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
69310246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
69410246dc8SYasunori Goto {
69510246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
69610246dc8SYasunori Goto 		return -EINVAL;
69710246dc8SYasunori Goto 
69810246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
69910246dc8SYasunori Goto 		spa->status = 2;
70010246dc8SYasunori Goto 
70110246dc8SYasunori Goto 	return 0;
70210246dc8SYasunori Goto }
70310246dc8SYasunori Goto 
704ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
705ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
706baa51277SDan Williams {
707baa51277SDan Williams 	if (buf_len < sizeof(*smart))
708baa51277SDan Williams 		return -EINVAL;
709ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
710baa51277SDan Williams 	return 0;
711baa51277SDan Williams }
712baa51277SDan Williams 
713cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
714ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
715ed07c433SDan Williams 		unsigned int buf_len,
716ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
717baa51277SDan Williams {
718baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
719baa51277SDan Williams 		return -EINVAL;
720ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
721ed07c433SDan Williams 	return 0;
722ed07c433SDan Williams }
723ed07c433SDan Williams 
724ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
725ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
726ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
727ed07c433SDan Williams {
728ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
729ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
730ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
731ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
732ed07c433SDan Williams 			smart->ctrl_temperature);
733ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
734ed07c433SDan Williams 				&& smart->spares
735ed07c433SDan Williams 				<= thresh->spares)
736ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
737ed07c433SDan Williams 				&& smart->media_temperature
738ed07c433SDan Williams 				>= thresh->media_temperature)
739ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
740ed07c433SDan Williams 				&& smart->ctrl_temperature
7414cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7424cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7434cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
744ed07c433SDan Williams 		device_lock(bus_dev);
745ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
746ed07c433SDan Williams 		device_unlock(bus_dev);
747ed07c433SDan Williams 	}
748ed07c433SDan Williams }
749ed07c433SDan Williams 
750ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
751ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
752ed07c433SDan Williams 		unsigned int buf_len,
753ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
754ed07c433SDan Williams 		struct nd_intel_smart *smart,
755ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
756ed07c433SDan Williams {
757ed07c433SDan Williams 	unsigned int size;
758ed07c433SDan Williams 
759ed07c433SDan Williams 	size = sizeof(*in) - 4;
760ed07c433SDan Williams 	if (buf_len < size)
761ed07c433SDan Williams 		return -EINVAL;
762ed07c433SDan Williams 	memcpy(thresh->data, in, size);
763ed07c433SDan Williams 	in->status = 0;
764ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
765ed07c433SDan Williams 
766baa51277SDan Williams 	return 0;
767baa51277SDan Williams }
768baa51277SDan Williams 
7694cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7704cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7714cf260fcSVishal Verma 		unsigned int buf_len,
7724cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7734cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7744cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7754cf260fcSVishal Verma {
7764cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7774cf260fcSVishal Verma 		return -EINVAL;
7784cf260fcSVishal Verma 
779b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7804cf260fcSVishal Verma 		if (inj->mtemp_enable)
7814cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
782b4d4702fSVishal Verma 		else
783b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
784b4d4702fSVishal Verma 	}
785b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7864cf260fcSVishal Verma 		if (inj->spare_enable)
7874cf260fcSVishal Verma 			smart->spares = inj->spares;
788b4d4702fSVishal Verma 		else
789b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
790b4d4702fSVishal Verma 	}
791b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
7924cf260fcSVishal Verma 		if (inj->fatal_enable)
7934cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
794b4d4702fSVishal Verma 		else
795b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
796b4d4702fSVishal Verma 	}
797b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
7984cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
7994cf260fcSVishal Verma 			smart->shutdown_state = 1;
8004cf260fcSVishal Verma 			smart->shutdown_count++;
801b4d4702fSVishal Verma 		} else
802b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8034cf260fcSVishal Verma 	}
8044cf260fcSVishal Verma 	inj->status = 0;
8054cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8064cf260fcSVishal Verma 
8074cf260fcSVishal Verma 	return 0;
8084cf260fcSVishal Verma }
8094cf260fcSVishal Verma 
8109fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8119fb1a190SDave Jiang {
8129fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8139fb1a190SDave Jiang 
8149fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8159fb1a190SDave Jiang }
8169fb1a190SDave Jiang 
8179fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8189fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8199fb1a190SDave Jiang {
8209fb1a190SDave Jiang 	int rc;
8219fb1a190SDave Jiang 
82241cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8239fb1a190SDave Jiang 		rc = -EINVAL;
8249fb1a190SDave Jiang 		goto err;
8259fb1a190SDave Jiang 	}
8269fb1a190SDave Jiang 
8279fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8289fb1a190SDave Jiang 		rc = -EINVAL;
8299fb1a190SDave Jiang 		goto err;
8309fb1a190SDave Jiang 	}
8319fb1a190SDave Jiang 
8329fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8339fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8349fb1a190SDave Jiang 	if (rc < 0)
8359fb1a190SDave Jiang 		goto err;
8369fb1a190SDave Jiang 
8379fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8389fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8399fb1a190SDave Jiang 
8409fb1a190SDave Jiang 	err_inj->status = 0;
8419fb1a190SDave Jiang 	return 0;
8429fb1a190SDave Jiang 
8439fb1a190SDave Jiang err:
8449fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8459fb1a190SDave Jiang 	return rc;
8469fb1a190SDave Jiang }
8479fb1a190SDave Jiang 
8489fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8499fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8509fb1a190SDave Jiang {
8519fb1a190SDave Jiang 	int rc;
8529fb1a190SDave Jiang 
85341cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8549fb1a190SDave Jiang 		rc = -EINVAL;
8559fb1a190SDave Jiang 		goto err;
8569fb1a190SDave Jiang 	}
8579fb1a190SDave Jiang 
8589fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8599fb1a190SDave Jiang 		rc = -EINVAL;
8609fb1a190SDave Jiang 		goto err;
8619fb1a190SDave Jiang 	}
8629fb1a190SDave Jiang 
8639fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8649fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8659fb1a190SDave Jiang 
8669fb1a190SDave Jiang 	err_clr->status = 0;
8679fb1a190SDave Jiang 	return 0;
8689fb1a190SDave Jiang 
8699fb1a190SDave Jiang err:
8709fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8719fb1a190SDave Jiang 	return rc;
8729fb1a190SDave Jiang }
8739fb1a190SDave Jiang 
8749fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8759fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8769fb1a190SDave Jiang 		unsigned int buf_len)
8779fb1a190SDave Jiang {
8789fb1a190SDave Jiang 	struct badrange_entry *be;
8799fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8809fb1a190SDave Jiang 	int i = 0;
8819fb1a190SDave Jiang 
8829fb1a190SDave Jiang 	err_stat->status = 0;
8839fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8849fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8859fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8869fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8879fb1a190SDave Jiang 		i++;
8889fb1a190SDave Jiang 		if (i > max)
8899fb1a190SDave Jiang 			break;
8909fb1a190SDave Jiang 	}
8919fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8929fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8939fb1a190SDave Jiang 
8949fb1a190SDave Jiang 	return 0;
8959fb1a190SDave Jiang }
8969fb1a190SDave Jiang 
897674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
898674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
899674d8bdeSDave Jiang {
900674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
901674d8bdeSDave Jiang 
902674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
903674d8bdeSDave Jiang 		return -EINVAL;
904674d8bdeSDave Jiang 
905674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
906674d8bdeSDave Jiang 	case 0:
907674d8bdeSDave Jiang 		nd_cmd->status = 0;
908674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
909674d8bdeSDave Jiang 				__func__);
910674d8bdeSDave Jiang 		break;
911674d8bdeSDave Jiang 	case 1:
912674d8bdeSDave Jiang 		nd_cmd->status = 0;
913674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
914674d8bdeSDave Jiang 				__func__);
915674d8bdeSDave Jiang 		break;
916674d8bdeSDave Jiang 	default:
917674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
918674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
919674d8bdeSDave Jiang 		break;
920674d8bdeSDave Jiang 	}
921674d8bdeSDave Jiang 
922674d8bdeSDave Jiang 
923674d8bdeSDave Jiang 	return 0;
924674d8bdeSDave Jiang }
925674d8bdeSDave Jiang 
92639611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
92739611e83SDan Williams {
92839611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
92939611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
93039611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
93139611e83SDan Williams 		return -EIO;
93239611e83SDan Williams 	}
93339611e83SDan Williams 	return rc;
93439611e83SDan Williams }
93539611e83SDan Williams 
936bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
937bfbaa952SDave Jiang {
938bfbaa952SDave Jiang 	int i;
939bfbaa952SDave Jiang 
940bfbaa952SDave Jiang 	/* lookup per-dimm data */
941bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
942bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
943bfbaa952SDave Jiang 			break;
944bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
945bfbaa952SDave Jiang 		return -ENXIO;
946bfbaa952SDave Jiang 	return i;
947bfbaa952SDave Jiang }
948bfbaa952SDave Jiang 
94939c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
95039c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
951aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
95239c686b8SVishal Verma {
95339c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
95439c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
9556634fb06SDan Williams 	unsigned int func = cmd;
956f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
957f471f1a7SDan Williams 
958f471f1a7SDan Williams 	if (!cmd_rc)
959f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
960f471f1a7SDan Williams 	*cmd_rc = 0;
96139c686b8SVishal Verma 
96239c686b8SVishal Verma 	if (nvdimm) {
96339c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
964e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
96539c686b8SVishal Verma 
9666634fb06SDan Williams 		if (!nfit_mem)
9676634fb06SDan Williams 			return -ENOTTY;
9686634fb06SDan Williams 
9696634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
9706634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
9716634fb06SDan Williams 
9726634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
9736634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
9746634fb06SDan Williams 			func = call_pkg->nd_command;
9756634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
9766634fb06SDan Williams 				return -ENOTTY;
977bfbaa952SDave Jiang 
978bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
979bfbaa952SDave Jiang 			if (i < 0)
980bfbaa952SDave Jiang 				return i;
981bfbaa952SDave Jiang 
982bfbaa952SDave Jiang 			switch (func) {
983674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
98439611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
985674d8bdeSDave Jiang 						buf, buf_len);
98639611e83SDan Williams 				break;
987bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
98839611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
989bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
99039611e83SDan Williams 				break;
991bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
99239611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
993bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
99439611e83SDan Williams 				break;
995bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
99639611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
997bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
99839611e83SDan Williams 				break;
999bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
100039611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
1001bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
100239611e83SDan Williams 				break;
1003bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
100439611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
1005bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
100639611e83SDan Williams 				break;
1007bfbaa952SDave Jiang 			case ND_INTEL_SMART:
100839611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
1009bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
101039611e83SDan Williams 				break;
1011bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
101239611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1013bfbaa952SDave Jiang 						buf_len,
1014bfbaa952SDave Jiang 						&t->smart_threshold[i -
1015bfbaa952SDave Jiang 							t->dcr_idx]);
101639611e83SDan Williams 				break;
1017bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
101839611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1019bfbaa952SDave Jiang 						buf_len,
1020bfbaa952SDave Jiang 						&t->smart_threshold[i -
1021bfbaa952SDave Jiang 							t->dcr_idx],
1022bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
1023bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
102439611e83SDan Williams 				break;
10254cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
102639611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
10274cf260fcSVishal Verma 						buf_len,
10284cf260fcSVishal Verma 						&t->smart_threshold[i -
10294cf260fcSVishal Verma 							t->dcr_idx],
10304cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
10314cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
103239611e83SDan Williams 				break;
1033bfbaa952SDave Jiang 			default:
1034bfbaa952SDave Jiang 				return -ENOTTY;
1035bfbaa952SDave Jiang 			}
103639611e83SDan Williams 			return override_return_code(i, func, rc);
10376634fb06SDan Williams 		}
10386634fb06SDan Williams 
10396634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
10406634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
104139c686b8SVishal Verma 			return -ENOTTY;
104239c686b8SVishal Verma 
1043bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1044bfbaa952SDave Jiang 		if (i < 0)
1045bfbaa952SDave Jiang 			return i;
104673606afdSDan Williams 
10476634fb06SDan Williams 		switch (func) {
104839c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
104939c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
105039c686b8SVishal Verma 			break;
105139c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
105239c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1053dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
105439c686b8SVishal Verma 			break;
105539c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
105639c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1057dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
105839c686b8SVishal Verma 			break;
10596bc75619SDan Williams 		default:
10606bc75619SDan Williams 			return -ENOTTY;
10616bc75619SDan Williams 		}
106239611e83SDan Williams 		return override_return_code(i, func, rc);
106339c686b8SVishal Verma 	} else {
1064f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
106510246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
106610246dc8SYasunori Goto 
106710246dc8SYasunori Goto 		if (!nd_desc)
106810246dc8SYasunori Goto 			return -ENOTTY;
106910246dc8SYasunori Goto 
107010246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
107110246dc8SYasunori Goto 			func = call_pkg->nd_command;
107210246dc8SYasunori Goto 
107310246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
107410246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
107510246dc8SYasunori Goto 
107610246dc8SYasunori Goto 			switch (func) {
107710246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
107810246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
107910246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
108010246dc8SYasunori Goto 				return rc;
10819fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
10829fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
10839fb1a190SDave Jiang 					buf_len);
10849fb1a190SDave Jiang 				return rc;
10859fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
10869fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
10879fb1a190SDave Jiang 					buf_len);
10889fb1a190SDave Jiang 				return rc;
10899fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
10909fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
10919fb1a190SDave Jiang 					buf_len);
10929fb1a190SDave Jiang 				return rc;
109310246dc8SYasunori Goto 			default:
109410246dc8SYasunori Goto 				return -ENOTTY;
109510246dc8SYasunori Goto 			}
109610246dc8SYasunori Goto 		}
1097f471f1a7SDan Williams 
1098e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
109939c686b8SVishal Verma 			return -ENOTTY;
110039c686b8SVishal Verma 
11016634fb06SDan Williams 		switch (func) {
110239c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
110339c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
110439c686b8SVishal Verma 			break;
110539c686b8SVishal Verma 		case ND_CMD_ARS_START:
11069fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
11079fb1a190SDave Jiang 					buf_len, cmd_rc);
110839c686b8SVishal Verma 			break;
110939c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1110f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1111f471f1a7SDan Williams 					cmd_rc);
111239c686b8SVishal Verma 			break;
1113d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
11145e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1115d4f32367SDan Williams 			break;
111639c686b8SVishal Verma 		default:
111739c686b8SVishal Verma 			return -ENOTTY;
111839c686b8SVishal Verma 		}
111939c686b8SVishal Verma 	}
11206bc75619SDan Williams 
11216bc75619SDan Williams 	return rc;
11226bc75619SDan Williams }
11236bc75619SDan Williams 
11246bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
11256bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
11266bc75619SDan Williams 
11276bc75619SDan Williams static void release_nfit_res(void *data)
11286bc75619SDan Williams {
11296bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
11306bc75619SDan Williams 
11316bc75619SDan Williams 	spin_lock(&nfit_test_lock);
11326bc75619SDan Williams 	list_del(&nfit_res->list);
11336bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
11346bc75619SDan Williams 
11356bc75619SDan Williams 	vfree(nfit_res->buf);
11366bc75619SDan Williams 	kfree(nfit_res);
11376bc75619SDan Williams }
11386bc75619SDan Williams 
11396bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
11406bc75619SDan Williams 		void *buf)
11416bc75619SDan Williams {
11426bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
11436bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
11446bc75619SDan Williams 			GFP_KERNEL);
11456bc75619SDan Williams 	int rc;
11466bc75619SDan Williams 
1147bd4cd745SDan Williams 	if (!buf || !nfit_res)
11486bc75619SDan Williams 		goto err;
11496bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
11506bc75619SDan Williams 	if (rc)
11516bc75619SDan Williams 		goto err;
11526bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
11536bc75619SDan Williams 	memset(buf, 0, size);
11546bc75619SDan Williams 	nfit_res->dev = dev;
11556bc75619SDan Williams 	nfit_res->buf = buf;
1156bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1157bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1158bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1159bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1160bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
11616bc75619SDan Williams 	spin_lock(&nfit_test_lock);
11626bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
11636bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
11646bc75619SDan Williams 
11656bc75619SDan Williams 	return nfit_res->buf;
11666bc75619SDan Williams  err:
1167ee8520feSDan Williams 	if (buf)
11686bc75619SDan Williams 		vfree(buf);
11696bc75619SDan Williams 	kfree(nfit_res);
11706bc75619SDan Williams 	return NULL;
11716bc75619SDan Williams }
11726bc75619SDan Williams 
11736bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
11746bc75619SDan Williams {
11756bc75619SDan Williams 	void *buf = vmalloc(size);
11766bc75619SDan Williams 
11776bc75619SDan Williams 	*dma = (unsigned long) buf;
11786bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
11796bc75619SDan Williams }
11806bc75619SDan Williams 
11816bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
11826bc75619SDan Williams {
11836bc75619SDan Williams 	int i;
11846bc75619SDan Williams 
11856bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
11866bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
11876bc75619SDan Williams 		struct nfit_test *t = instances[i];
11886bc75619SDan Williams 
11896bc75619SDan Williams 		if (!t)
11906bc75619SDan Williams 			continue;
11916bc75619SDan Williams 		spin_lock(&nfit_test_lock);
11926bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1193bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1194bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11956bc75619SDan Williams 				nfit_res = n;
11966bc75619SDan Williams 				break;
11976bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
11986bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1199bd4cd745SDan Williams 						+ resource_size(&n->res))) {
12006bc75619SDan Williams 				nfit_res = n;
12016bc75619SDan Williams 				break;
12026bc75619SDan Williams 			}
12036bc75619SDan Williams 		}
12046bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
12056bc75619SDan Williams 		if (nfit_res)
12066bc75619SDan Williams 			return nfit_res;
12076bc75619SDan Williams 	}
12086bc75619SDan Williams 
12096bc75619SDan Williams 	return NULL;
12106bc75619SDan Williams }
12116bc75619SDan Williams 
1212f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1213f471f1a7SDan Williams {
12149fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1215f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
12169fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1217f471f1a7SDan Williams 	if (!ars_state->ars_status)
1218f471f1a7SDan Williams 		return -ENOMEM;
1219f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1220f471f1a7SDan Williams 	return 0;
1221f471f1a7SDan Williams }
1222f471f1a7SDan Williams 
1223231bf117SDan Williams static void put_dimms(void *data)
1224231bf117SDan Williams {
1225718fda67SDan Williams 	struct nfit_test *t = data;
1226231bf117SDan Williams 	int i;
1227231bf117SDan Williams 
1228718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1229718fda67SDan Williams 		if (t->dimm_dev[i])
1230718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1231231bf117SDan Williams }
1232231bf117SDan Williams 
1233231bf117SDan Williams static struct class *nfit_test_dimm;
1234231bf117SDan Williams 
123573606afdSDan Williams static int dimm_name_to_id(struct device *dev)
123673606afdSDan Williams {
123773606afdSDan Williams 	int dimm;
123873606afdSDan Williams 
1239718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
124073606afdSDan Williams 		return -ENXIO;
124173606afdSDan Williams 	return dimm;
124273606afdSDan Williams }
124373606afdSDan Williams 
124473606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
124573606afdSDan Williams 		char *buf)
124673606afdSDan Williams {
124773606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
124873606afdSDan Williams 
124973606afdSDan Williams 	if (dimm < 0)
125073606afdSDan Williams 		return dimm;
125173606afdSDan Williams 
125219357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
125373606afdSDan Williams }
125473606afdSDan Williams DEVICE_ATTR_RO(handle);
125573606afdSDan Williams 
125673606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
125773606afdSDan Williams 		char *buf)
125873606afdSDan Williams {
125973606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
126073606afdSDan Williams 
126173606afdSDan Williams 	if (dimm < 0)
126273606afdSDan Williams 		return dimm;
126373606afdSDan Williams 
126473606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
126573606afdSDan Williams }
126673606afdSDan Williams 
126773606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
126873606afdSDan Williams 		const char *buf, size_t size)
126973606afdSDan Williams {
127073606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
127173606afdSDan Williams 	unsigned long val;
127273606afdSDan Williams 	ssize_t rc;
127373606afdSDan Williams 
127473606afdSDan Williams 	if (dimm < 0)
127573606afdSDan Williams 		return dimm;
127673606afdSDan Williams 
127773606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
127873606afdSDan Williams 	if (rc)
127973606afdSDan Williams 		return rc;
128073606afdSDan Williams 
128173606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
128273606afdSDan Williams 	return size;
128373606afdSDan Williams }
128473606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
128573606afdSDan Williams 
128655c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
128755c72ab6SDan Williams 		char *buf)
128855c72ab6SDan Williams {
128955c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
129055c72ab6SDan Williams 
129155c72ab6SDan Williams 	if (dimm < 0)
129255c72ab6SDan Williams 		return dimm;
129355c72ab6SDan Williams 
129455c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
129555c72ab6SDan Williams }
129655c72ab6SDan Williams 
129755c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
129855c72ab6SDan Williams 		const char *buf, size_t size)
129955c72ab6SDan Williams {
130055c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
130155c72ab6SDan Williams 	unsigned long val;
130255c72ab6SDan Williams 	ssize_t rc;
130355c72ab6SDan Williams 
130455c72ab6SDan Williams 	if (dimm < 0)
130555c72ab6SDan Williams 		return dimm;
130655c72ab6SDan Williams 
130755c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
130855c72ab6SDan Williams 	if (rc)
130955c72ab6SDan Williams 		return rc;
131055c72ab6SDan Williams 
131155c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
131255c72ab6SDan Williams 	return size;
131355c72ab6SDan Williams }
131455c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
131555c72ab6SDan Williams 
131673606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
131773606afdSDan Williams 	&dev_attr_fail_cmd.attr,
131855c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
131973606afdSDan Williams 	&dev_attr_handle.attr,
132073606afdSDan Williams 	NULL,
132173606afdSDan Williams };
132273606afdSDan Williams 
132373606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
132473606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
132573606afdSDan Williams };
132673606afdSDan Williams 
132773606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
132873606afdSDan Williams 	&nfit_test_dimm_attribute_group,
132973606afdSDan Williams 	NULL,
133073606afdSDan Williams };
133173606afdSDan Williams 
1332718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1333718fda67SDan Williams {
1334718fda67SDan Williams 	int i;
1335718fda67SDan Williams 
1336718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1337718fda67SDan Williams 		return -ENOMEM;
1338718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1339718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1340718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1341718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1342718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1343718fda67SDan Williams 		if (!t->dimm_dev[i])
1344718fda67SDan Williams 			return -ENOMEM;
1345718fda67SDan Williams 	}
1346718fda67SDan Williams 	return 0;
1347718fda67SDan Williams }
1348718fda67SDan Williams 
1349ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1350ed07c433SDan Williams {
1351ed07c433SDan Williams 	int i;
1352ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1353ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1354ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1355ed07c433SDan Williams 		.media_temperature = 40 * 16,
1356ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1357ed07c433SDan Williams 		.spares = 5,
1358ed07c433SDan Williams 	};
1359ed07c433SDan Williams 
1360ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1361b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1362ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1363ed07c433SDan Williams 				sizeof(smart_t_data));
1364ed07c433SDan Williams 	}
1365ed07c433SDan Williams }
1366ed07c433SDan Williams 
13676bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
13686bc75619SDan Williams {
13696b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
13706bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
13716bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
13723b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
13733b87356fSDan Williams 					window_size) * NUM_DCR
13749d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
137585d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1376f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1377f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
13786bc75619SDan Williams 	int i;
13796bc75619SDan Williams 
13806bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
13816bc75619SDan Williams 	if (!t->nfit_buf)
13826bc75619SDan Williams 		return -ENOMEM;
13836bc75619SDan Williams 	t->nfit_size = nfit_size;
13846bc75619SDan Williams 
1385ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
13866bc75619SDan Williams 	if (!t->spa_set[0])
13876bc75619SDan Williams 		return -ENOMEM;
13886bc75619SDan Williams 
1389ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
13906bc75619SDan Williams 	if (!t->spa_set[1])
13916bc75619SDan Williams 		return -ENOMEM;
13926bc75619SDan Williams 
1393ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
139420985164SVishal Verma 	if (!t->spa_set[2])
139520985164SVishal Verma 		return -ENOMEM;
139620985164SVishal Verma 
1397dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
13986bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
13996bc75619SDan Williams 		if (!t->dimm[i])
14006bc75619SDan Williams 			return -ENOMEM;
14016bc75619SDan Williams 
14026bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
14036bc75619SDan Williams 		if (!t->label[i])
14046bc75619SDan Williams 			return -ENOMEM;
14056bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
14069d27a87eSDan Williams 
14079d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
14089d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
140985d3fa02SDan Williams 				&t->flush_dma[i]);
14109d27a87eSDan Williams 		if (!t->flush[i])
14119d27a87eSDan Williams 			return -ENOMEM;
14126bc75619SDan Williams 	}
14136bc75619SDan Williams 
1414dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
14156bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
14166bc75619SDan Williams 		if (!t->dcr[i])
14176bc75619SDan Williams 			return -ENOMEM;
14186bc75619SDan Williams 	}
14196bc75619SDan Williams 
1420c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1421c14a868aSDan Williams 	if (!t->_fit)
1422c14a868aSDan Williams 		return -ENOMEM;
1423c14a868aSDan Williams 
1424718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1425231bf117SDan Williams 		return -ENOMEM;
1426ed07c433SDan Williams 	smart_init(t);
1427f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
14286bc75619SDan Williams }
14296bc75619SDan Williams 
14306bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
14316bc75619SDan Williams {
14327bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1433ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1434ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1435dafb1048SDan Williams 	int i;
14366bc75619SDan Williams 
14376bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
14386bc75619SDan Williams 	if (!t->nfit_buf)
14396bc75619SDan Williams 		return -ENOMEM;
14406bc75619SDan Williams 	t->nfit_size = nfit_size;
14416bc75619SDan Williams 
1442ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
14436bc75619SDan Williams 	if (!t->spa_set[0])
14446bc75619SDan Williams 		return -ENOMEM;
14456bc75619SDan Williams 
1446dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1447dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1448dafb1048SDan Williams 		if (!t->label[i])
1449dafb1048SDan Williams 			return -ENOMEM;
1450dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1451dafb1048SDan Williams 	}
1452dafb1048SDan Williams 
14537bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
14547bfe97c7SDan Williams 	if (!t->spa_set[1])
14557bfe97c7SDan Williams 		return -ENOMEM;
14567bfe97c7SDan Williams 
1457718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1458718fda67SDan Williams 		return -ENOMEM;
1459ed07c433SDan Williams 	smart_init(t);
1460f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
14616bc75619SDan Williams }
14626bc75619SDan Williams 
14635dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
14645dc68e55SDan Williams {
14655dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
14665dc68e55SDan Williams 	dcr->device_id = 0;
14675dc68e55SDan Williams 	dcr->revision_id = 1;
14685dc68e55SDan Williams 	dcr->valid_fields = 1;
14695dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
14705dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
14715dc68e55SDan Williams }
14725dc68e55SDan Williams 
14736bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
14746bc75619SDan Williams {
147585d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
147685d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
14776bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
14786bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
14796bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
14806bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
14816bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
14826bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
14839d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1484f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1485d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
14866bc75619SDan Williams 
14876bc75619SDan Williams 	/*
14886bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
14896bc75619SDan Williams 	 * does not actually alias the related block-data-window
14906bc75619SDan Williams 	 * regions)
14916bc75619SDan Williams 	 */
14926b577c9dSLinda Knippers 	spa = nfit_buf;
14936bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14946bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14956bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
14966bc75619SDan Williams 	spa->range_index = 0+1;
14976bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
14986bc75619SDan Williams 	spa->length = SPA0_SIZE;
1499d7d8464dSRoss Zwisler 	offset += spa->header.length;
15006bc75619SDan Williams 
15016bc75619SDan Williams 	/*
15026bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
15036bc75619SDan Williams 	 * does not actually alias the related block-data-window
15046bc75619SDan Williams 	 * regions)
15056bc75619SDan Williams 	 */
1506d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15076bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15086bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15096bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
15106bc75619SDan Williams 	spa->range_index = 1+1;
15116bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
15126bc75619SDan Williams 	spa->length = SPA1_SIZE;
1513d7d8464dSRoss Zwisler 	offset += spa->header.length;
15146bc75619SDan Williams 
15156bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1516d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15176bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15186bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15196bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15206bc75619SDan Williams 	spa->range_index = 2+1;
15216bc75619SDan Williams 	spa->address = t->dcr_dma[0];
15226bc75619SDan Williams 	spa->length = DCR_SIZE;
1523d7d8464dSRoss Zwisler 	offset += spa->header.length;
15246bc75619SDan Williams 
15256bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1526d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15276bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15286bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15296bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15306bc75619SDan Williams 	spa->range_index = 3+1;
15316bc75619SDan Williams 	spa->address = t->dcr_dma[1];
15326bc75619SDan Williams 	spa->length = DCR_SIZE;
1533d7d8464dSRoss Zwisler 	offset += spa->header.length;
15346bc75619SDan Williams 
15356bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1536d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15376bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15386bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15396bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15406bc75619SDan Williams 	spa->range_index = 4+1;
15416bc75619SDan Williams 	spa->address = t->dcr_dma[2];
15426bc75619SDan Williams 	spa->length = DCR_SIZE;
1543d7d8464dSRoss Zwisler 	offset += spa->header.length;
15446bc75619SDan Williams 
15456bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1546d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15476bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15486bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15496bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15506bc75619SDan Williams 	spa->range_index = 5+1;
15516bc75619SDan Williams 	spa->address = t->dcr_dma[3];
15526bc75619SDan Williams 	spa->length = DCR_SIZE;
1553d7d8464dSRoss Zwisler 	offset += spa->header.length;
15546bc75619SDan Williams 
15556bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1556d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15576bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15586bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15596bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15606bc75619SDan Williams 	spa->range_index = 6+1;
15616bc75619SDan Williams 	spa->address = t->dimm_dma[0];
15626bc75619SDan Williams 	spa->length = DIMM_SIZE;
1563d7d8464dSRoss Zwisler 	offset += spa->header.length;
15646bc75619SDan Williams 
15656bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1566d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15676bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15686bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15696bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15706bc75619SDan Williams 	spa->range_index = 7+1;
15716bc75619SDan Williams 	spa->address = t->dimm_dma[1];
15726bc75619SDan Williams 	spa->length = DIMM_SIZE;
1573d7d8464dSRoss Zwisler 	offset += spa->header.length;
15746bc75619SDan Williams 
15756bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1576d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15776bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15786bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15796bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15806bc75619SDan Williams 	spa->range_index = 8+1;
15816bc75619SDan Williams 	spa->address = t->dimm_dma[2];
15826bc75619SDan Williams 	spa->length = DIMM_SIZE;
1583d7d8464dSRoss Zwisler 	offset += spa->header.length;
15846bc75619SDan Williams 
15856bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1586d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15876bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15886bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15896bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15906bc75619SDan Williams 	spa->range_index = 9+1;
15916bc75619SDan Williams 	spa->address = t->dimm_dma[3];
15926bc75619SDan Williams 	spa->length = DIMM_SIZE;
1593d7d8464dSRoss Zwisler 	offset += spa->header.length;
15946bc75619SDan Williams 
15956bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
15966bc75619SDan Williams 	memdev = nfit_buf + offset;
15976bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15986bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15996bc75619SDan Williams 	memdev->device_handle = handle[0];
16006bc75619SDan Williams 	memdev->physical_id = 0;
16016bc75619SDan Williams 	memdev->region_id = 0;
16026bc75619SDan Williams 	memdev->range_index = 0+1;
16033b87356fSDan Williams 	memdev->region_index = 4+1;
16046bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1605df06a2d5SDan Williams 	memdev->region_offset = 1;
16066bc75619SDan Williams 	memdev->address = 0;
16076bc75619SDan Williams 	memdev->interleave_index = 0;
16086bc75619SDan Williams 	memdev->interleave_ways = 2;
1609d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16106bc75619SDan Williams 
16116bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1612d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16136bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16146bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16156bc75619SDan Williams 	memdev->device_handle = handle[1];
16166bc75619SDan Williams 	memdev->physical_id = 1;
16176bc75619SDan Williams 	memdev->region_id = 0;
16186bc75619SDan Williams 	memdev->range_index = 0+1;
16193b87356fSDan Williams 	memdev->region_index = 5+1;
16206bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1621df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
16226bc75619SDan Williams 	memdev->address = 0;
16236bc75619SDan Williams 	memdev->interleave_index = 0;
16246bc75619SDan Williams 	memdev->interleave_ways = 2;
1625ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1626d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16276bc75619SDan Williams 
16286bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1629d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16306bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16316bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16326bc75619SDan Williams 	memdev->device_handle = handle[0];
16336bc75619SDan Williams 	memdev->physical_id = 0;
16346bc75619SDan Williams 	memdev->region_id = 1;
16356bc75619SDan Williams 	memdev->range_index = 1+1;
16363b87356fSDan Williams 	memdev->region_index = 4+1;
16376bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1638df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
16396bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16406bc75619SDan Williams 	memdev->interleave_index = 0;
16416bc75619SDan Williams 	memdev->interleave_ways = 4;
1642ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1643d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16446bc75619SDan Williams 
16456bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1646d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16476bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16486bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16496bc75619SDan Williams 	memdev->device_handle = handle[1];
16506bc75619SDan Williams 	memdev->physical_id = 1;
16516bc75619SDan Williams 	memdev->region_id = 1;
16526bc75619SDan Williams 	memdev->range_index = 1+1;
16533b87356fSDan Williams 	memdev->region_index = 5+1;
16546bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1655df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
16566bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16576bc75619SDan Williams 	memdev->interleave_index = 0;
16586bc75619SDan Williams 	memdev->interleave_ways = 4;
1659d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16606bc75619SDan Williams 
16616bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1662d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16636bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16646bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16656bc75619SDan Williams 	memdev->device_handle = handle[2];
16666bc75619SDan Williams 	memdev->physical_id = 2;
16676bc75619SDan Williams 	memdev->region_id = 0;
16686bc75619SDan Williams 	memdev->range_index = 1+1;
16693b87356fSDan Williams 	memdev->region_index = 6+1;
16706bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1671df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
16726bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16736bc75619SDan Williams 	memdev->interleave_index = 0;
16746bc75619SDan Williams 	memdev->interleave_ways = 4;
1675ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1676d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16776bc75619SDan Williams 
16786bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
1679d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16806bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16816bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16826bc75619SDan Williams 	memdev->device_handle = handle[3];
16836bc75619SDan Williams 	memdev->physical_id = 3;
16846bc75619SDan Williams 	memdev->region_id = 0;
16856bc75619SDan Williams 	memdev->range_index = 1+1;
16863b87356fSDan Williams 	memdev->region_index = 7+1;
16876bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1688df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
16896bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16906bc75619SDan Williams 	memdev->interleave_index = 0;
16916bc75619SDan Williams 	memdev->interleave_ways = 4;
1692d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16936bc75619SDan Williams 
16946bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
1695d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16966bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16976bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16986bc75619SDan Williams 	memdev->device_handle = handle[0];
16996bc75619SDan Williams 	memdev->physical_id = 0;
17006bc75619SDan Williams 	memdev->region_id = 0;
17016bc75619SDan Williams 	memdev->range_index = 2+1;
17026bc75619SDan Williams 	memdev->region_index = 0+1;
17036bc75619SDan Williams 	memdev->region_size = 0;
17046bc75619SDan Williams 	memdev->region_offset = 0;
17056bc75619SDan Williams 	memdev->address = 0;
17066bc75619SDan Williams 	memdev->interleave_index = 0;
17076bc75619SDan Williams 	memdev->interleave_ways = 1;
1708d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17096bc75619SDan Williams 
17106bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
1711d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17126bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17136bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17146bc75619SDan Williams 	memdev->device_handle = handle[1];
17156bc75619SDan Williams 	memdev->physical_id = 1;
17166bc75619SDan Williams 	memdev->region_id = 0;
17176bc75619SDan Williams 	memdev->range_index = 3+1;
17186bc75619SDan Williams 	memdev->region_index = 1+1;
17196bc75619SDan Williams 	memdev->region_size = 0;
17206bc75619SDan Williams 	memdev->region_offset = 0;
17216bc75619SDan Williams 	memdev->address = 0;
17226bc75619SDan Williams 	memdev->interleave_index = 0;
17236bc75619SDan Williams 	memdev->interleave_ways = 1;
1724d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17256bc75619SDan Williams 
17266bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
1727d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17286bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17296bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17306bc75619SDan Williams 	memdev->device_handle = handle[2];
17316bc75619SDan Williams 	memdev->physical_id = 2;
17326bc75619SDan Williams 	memdev->region_id = 0;
17336bc75619SDan Williams 	memdev->range_index = 4+1;
17346bc75619SDan Williams 	memdev->region_index = 2+1;
17356bc75619SDan Williams 	memdev->region_size = 0;
17366bc75619SDan Williams 	memdev->region_offset = 0;
17376bc75619SDan Williams 	memdev->address = 0;
17386bc75619SDan Williams 	memdev->interleave_index = 0;
17396bc75619SDan Williams 	memdev->interleave_ways = 1;
1740d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17416bc75619SDan Williams 
17426bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
1743d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17446bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17456bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17466bc75619SDan Williams 	memdev->device_handle = handle[3];
17476bc75619SDan Williams 	memdev->physical_id = 3;
17486bc75619SDan Williams 	memdev->region_id = 0;
17496bc75619SDan Williams 	memdev->range_index = 5+1;
17506bc75619SDan Williams 	memdev->region_index = 3+1;
17516bc75619SDan Williams 	memdev->region_size = 0;
17526bc75619SDan Williams 	memdev->region_offset = 0;
17536bc75619SDan Williams 	memdev->address = 0;
17546bc75619SDan Williams 	memdev->interleave_index = 0;
17556bc75619SDan Williams 	memdev->interleave_ways = 1;
1756d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17576bc75619SDan Williams 
17586bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
1759d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17606bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17616bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17626bc75619SDan Williams 	memdev->device_handle = handle[0];
17636bc75619SDan Williams 	memdev->physical_id = 0;
17646bc75619SDan Williams 	memdev->region_id = 0;
17656bc75619SDan Williams 	memdev->range_index = 6+1;
17666bc75619SDan Williams 	memdev->region_index = 0+1;
17676bc75619SDan Williams 	memdev->region_size = 0;
17686bc75619SDan Williams 	memdev->region_offset = 0;
17696bc75619SDan Williams 	memdev->address = 0;
17706bc75619SDan Williams 	memdev->interleave_index = 0;
17716bc75619SDan Williams 	memdev->interleave_ways = 1;
1772d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17736bc75619SDan Williams 
17746bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
1775d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17766bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17776bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17786bc75619SDan Williams 	memdev->device_handle = handle[1];
17796bc75619SDan Williams 	memdev->physical_id = 1;
17806bc75619SDan Williams 	memdev->region_id = 0;
17816bc75619SDan Williams 	memdev->range_index = 7+1;
17826bc75619SDan Williams 	memdev->region_index = 1+1;
17836bc75619SDan Williams 	memdev->region_size = 0;
17846bc75619SDan Williams 	memdev->region_offset = 0;
17856bc75619SDan Williams 	memdev->address = 0;
17866bc75619SDan Williams 	memdev->interleave_index = 0;
17876bc75619SDan Williams 	memdev->interleave_ways = 1;
1788d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17896bc75619SDan Williams 
17906bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
1791d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17926bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17936bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17946bc75619SDan Williams 	memdev->device_handle = handle[2];
17956bc75619SDan Williams 	memdev->physical_id = 2;
17966bc75619SDan Williams 	memdev->region_id = 0;
17976bc75619SDan Williams 	memdev->range_index = 8+1;
17986bc75619SDan Williams 	memdev->region_index = 2+1;
17996bc75619SDan Williams 	memdev->region_size = 0;
18006bc75619SDan Williams 	memdev->region_offset = 0;
18016bc75619SDan Williams 	memdev->address = 0;
18026bc75619SDan Williams 	memdev->interleave_index = 0;
18036bc75619SDan Williams 	memdev->interleave_ways = 1;
1804d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18056bc75619SDan Williams 
18066bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
1807d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
18086bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
18096bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
18106bc75619SDan Williams 	memdev->device_handle = handle[3];
18116bc75619SDan Williams 	memdev->physical_id = 3;
18126bc75619SDan Williams 	memdev->region_id = 0;
18136bc75619SDan Williams 	memdev->range_index = 9+1;
18146bc75619SDan Williams 	memdev->region_index = 3+1;
18156bc75619SDan Williams 	memdev->region_size = 0;
18166bc75619SDan Williams 	memdev->region_offset = 0;
18176bc75619SDan Williams 	memdev->address = 0;
18186bc75619SDan Williams 	memdev->interleave_index = 0;
18196bc75619SDan Williams 	memdev->interleave_ways = 1;
1820ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1821d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18226bc75619SDan Williams 
18233b87356fSDan Williams 	/* dcr-descriptor0: blk */
18246bc75619SDan Williams 	dcr = nfit_buf + offset;
18256bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1826d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18276bc75619SDan Williams 	dcr->region_index = 0+1;
18285dc68e55SDan Williams 	dcr_common_init(dcr);
18296bc75619SDan Williams 	dcr->serial_number = ~handle[0];
1830be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18316bc75619SDan Williams 	dcr->windows = 1;
18326bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18336bc75619SDan Williams 	dcr->command_offset = 0;
18346bc75619SDan Williams 	dcr->command_size = 8;
18356bc75619SDan Williams 	dcr->status_offset = 8;
18366bc75619SDan Williams 	dcr->status_size = 4;
1837d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18386bc75619SDan Williams 
18393b87356fSDan Williams 	/* dcr-descriptor1: blk */
1840d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18416bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1842d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18436bc75619SDan Williams 	dcr->region_index = 1+1;
18445dc68e55SDan Williams 	dcr_common_init(dcr);
18456bc75619SDan Williams 	dcr->serial_number = ~handle[1];
1846be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18476bc75619SDan Williams 	dcr->windows = 1;
18486bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18496bc75619SDan Williams 	dcr->command_offset = 0;
18506bc75619SDan Williams 	dcr->command_size = 8;
18516bc75619SDan Williams 	dcr->status_offset = 8;
18526bc75619SDan Williams 	dcr->status_size = 4;
1853d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18546bc75619SDan Williams 
18553b87356fSDan Williams 	/* dcr-descriptor2: blk */
1856d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18576bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1858d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18596bc75619SDan Williams 	dcr->region_index = 2+1;
18605dc68e55SDan Williams 	dcr_common_init(dcr);
18616bc75619SDan Williams 	dcr->serial_number = ~handle[2];
1862be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18636bc75619SDan Williams 	dcr->windows = 1;
18646bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18656bc75619SDan Williams 	dcr->command_offset = 0;
18666bc75619SDan Williams 	dcr->command_size = 8;
18676bc75619SDan Williams 	dcr->status_offset = 8;
18686bc75619SDan Williams 	dcr->status_size = 4;
1869d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18706bc75619SDan Williams 
18713b87356fSDan Williams 	/* dcr-descriptor3: blk */
1872d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18736bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1874d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18756bc75619SDan Williams 	dcr->region_index = 3+1;
18765dc68e55SDan Williams 	dcr_common_init(dcr);
18776bc75619SDan Williams 	dcr->serial_number = ~handle[3];
1878be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18796bc75619SDan Williams 	dcr->windows = 1;
18806bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18816bc75619SDan Williams 	dcr->command_offset = 0;
18826bc75619SDan Williams 	dcr->command_size = 8;
18836bc75619SDan Williams 	dcr->status_offset = 8;
18846bc75619SDan Williams 	dcr->status_size = 4;
1885d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18866bc75619SDan Williams 
18873b87356fSDan Williams 	/* dcr-descriptor0: pmem */
18883b87356fSDan Williams 	dcr = nfit_buf + offset;
18893b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18903b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18913b87356fSDan Williams 			window_size);
18923b87356fSDan Williams 	dcr->region_index = 4+1;
18935dc68e55SDan Williams 	dcr_common_init(dcr);
18943b87356fSDan Williams 	dcr->serial_number = ~handle[0];
18953b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18963b87356fSDan Williams 	dcr->windows = 0;
1897d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18983b87356fSDan Williams 
18993b87356fSDan Williams 	/* dcr-descriptor1: pmem */
1900d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
19013b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
19023b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
19033b87356fSDan Williams 			window_size);
19043b87356fSDan Williams 	dcr->region_index = 5+1;
19055dc68e55SDan Williams 	dcr_common_init(dcr);
19063b87356fSDan Williams 	dcr->serial_number = ~handle[1];
19073b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
19083b87356fSDan Williams 	dcr->windows = 0;
1909d7d8464dSRoss Zwisler 	offset += dcr->header.length;
19103b87356fSDan Williams 
19113b87356fSDan Williams 	/* dcr-descriptor2: pmem */
1912d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
19133b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
19143b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
19153b87356fSDan Williams 			window_size);
19163b87356fSDan Williams 	dcr->region_index = 6+1;
19175dc68e55SDan Williams 	dcr_common_init(dcr);
19183b87356fSDan Williams 	dcr->serial_number = ~handle[2];
19193b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
19203b87356fSDan Williams 	dcr->windows = 0;
1921d7d8464dSRoss Zwisler 	offset += dcr->header.length;
19223b87356fSDan Williams 
19233b87356fSDan Williams 	/* dcr-descriptor3: pmem */
1924d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
19253b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
19263b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
19273b87356fSDan Williams 			window_size);
19283b87356fSDan Williams 	dcr->region_index = 7+1;
19295dc68e55SDan Williams 	dcr_common_init(dcr);
19303b87356fSDan Williams 	dcr->serial_number = ~handle[3];
19313b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
19323b87356fSDan Williams 	dcr->windows = 0;
1933d7d8464dSRoss Zwisler 	offset += dcr->header.length;
19343b87356fSDan Williams 
19356bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
19366bc75619SDan Williams 	bdw = nfit_buf + offset;
19376bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1938d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19396bc75619SDan Williams 	bdw->region_index = 0+1;
19406bc75619SDan Williams 	bdw->windows = 1;
19416bc75619SDan Williams 	bdw->offset = 0;
19426bc75619SDan Williams 	bdw->size = BDW_SIZE;
19436bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19446bc75619SDan Williams 	bdw->start_address = 0;
1945d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19466bc75619SDan Williams 
19476bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
1948d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19496bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1950d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19516bc75619SDan Williams 	bdw->region_index = 1+1;
19526bc75619SDan Williams 	bdw->windows = 1;
19536bc75619SDan Williams 	bdw->offset = 0;
19546bc75619SDan Williams 	bdw->size = BDW_SIZE;
19556bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19566bc75619SDan Williams 	bdw->start_address = 0;
1957d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19586bc75619SDan Williams 
19596bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
1960d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19616bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1962d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19636bc75619SDan Williams 	bdw->region_index = 2+1;
19646bc75619SDan Williams 	bdw->windows = 1;
19656bc75619SDan Williams 	bdw->offset = 0;
19666bc75619SDan Williams 	bdw->size = BDW_SIZE;
19676bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19686bc75619SDan Williams 	bdw->start_address = 0;
1969d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19706bc75619SDan Williams 
19716bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
1972d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19736bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1974d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19756bc75619SDan Williams 	bdw->region_index = 3+1;
19766bc75619SDan Williams 	bdw->windows = 1;
19776bc75619SDan Williams 	bdw->offset = 0;
19786bc75619SDan Williams 	bdw->size = BDW_SIZE;
19796bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19806bc75619SDan Williams 	bdw->start_address = 0;
1981d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19826bc75619SDan Williams 
19839d27a87eSDan Williams 	/* flush0 (dimm0) */
19849d27a87eSDan Williams 	flush = nfit_buf + offset;
19859d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
198685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19879d27a87eSDan Williams 	flush->device_handle = handle[0];
198885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
198985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
199085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
1991d7d8464dSRoss Zwisler 	offset += flush->header.length;
19929d27a87eSDan Williams 
19939d27a87eSDan Williams 	/* flush1 (dimm1) */
1994d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19959d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
199685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19979d27a87eSDan Williams 	flush->device_handle = handle[1];
199885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
199985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
200085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2001d7d8464dSRoss Zwisler 	offset += flush->header.length;
20029d27a87eSDan Williams 
20039d27a87eSDan Williams 	/* flush2 (dimm2) */
2004d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
20059d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
200685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
20079d27a87eSDan Williams 	flush->device_handle = handle[2];
200885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
200985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
201085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2011d7d8464dSRoss Zwisler 	offset += flush->header.length;
20129d27a87eSDan Williams 
20139d27a87eSDan Williams 	/* flush3 (dimm3) */
2014d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
20159d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
201685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
20179d27a87eSDan Williams 	flush->device_handle = handle[3];
201885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
201985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
202085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2021d7d8464dSRoss Zwisler 	offset += flush->header.length;
20229d27a87eSDan Williams 
2023f81e1d35SDave Jiang 	/* platform capabilities */
2024d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2025f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2026f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2027f81e1d35SDave Jiang 	pcap->highest_capability = 1;
20281273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2029d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2030f81e1d35SDave Jiang 
203120985164SVishal Verma 	if (t->setup_hotplug) {
20323b87356fSDan Williams 		/* dcr-descriptor4: blk */
203320985164SVishal Verma 		dcr = nfit_buf + offset;
203420985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2035d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
20363b87356fSDan Williams 		dcr->region_index = 8+1;
20375dc68e55SDan Williams 		dcr_common_init(dcr);
203820985164SVishal Verma 		dcr->serial_number = ~handle[4];
2039be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
204020985164SVishal Verma 		dcr->windows = 1;
204120985164SVishal Verma 		dcr->window_size = DCR_SIZE;
204220985164SVishal Verma 		dcr->command_offset = 0;
204320985164SVishal Verma 		dcr->command_size = 8;
204420985164SVishal Verma 		dcr->status_offset = 8;
204520985164SVishal Verma 		dcr->status_size = 4;
2046d7d8464dSRoss Zwisler 		offset += dcr->header.length;
204720985164SVishal Verma 
20483b87356fSDan Williams 		/* dcr-descriptor4: pmem */
20493b87356fSDan Williams 		dcr = nfit_buf + offset;
20503b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20513b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
20523b87356fSDan Williams 				window_size);
20533b87356fSDan Williams 		dcr->region_index = 9+1;
20545dc68e55SDan Williams 		dcr_common_init(dcr);
20553b87356fSDan Williams 		dcr->serial_number = ~handle[4];
20563b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
20573b87356fSDan Williams 		dcr->windows = 0;
2058d7d8464dSRoss Zwisler 		offset += dcr->header.length;
20593b87356fSDan Williams 
206020985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
206120985164SVishal Verma 		bdw = nfit_buf + offset;
206220985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2063d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
20643b87356fSDan Williams 		bdw->region_index = 8+1;
206520985164SVishal Verma 		bdw->windows = 1;
206620985164SVishal Verma 		bdw->offset = 0;
206720985164SVishal Verma 		bdw->size = BDW_SIZE;
206820985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
206920985164SVishal Verma 		bdw->start_address = 0;
2070d7d8464dSRoss Zwisler 		offset += bdw->header.length;
207120985164SVishal Verma 
207220985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
207320985164SVishal Verma 		spa = nfit_buf + offset;
207420985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
207520985164SVishal Verma 		spa->header.length = sizeof(*spa);
207620985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
207720985164SVishal Verma 		spa->range_index = 10+1;
207820985164SVishal Verma 		spa->address = t->dcr_dma[4];
207920985164SVishal Verma 		spa->length = DCR_SIZE;
2080d7d8464dSRoss Zwisler 		offset += spa->header.length;
208120985164SVishal Verma 
208220985164SVishal Verma 		/*
208320985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
208420985164SVishal Verma 		 * does not actually alias the related block-data-window
208520985164SVishal Verma 		 * regions)
208620985164SVishal Verma 		 */
2087d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
208820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
208920985164SVishal Verma 		spa->header.length = sizeof(*spa);
209020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
209120985164SVishal Verma 		spa->range_index = 11+1;
209220985164SVishal Verma 		spa->address = t->spa_set_dma[2];
209320985164SVishal Verma 		spa->length = SPA0_SIZE;
2094d7d8464dSRoss Zwisler 		offset += spa->header.length;
209520985164SVishal Verma 
209620985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2097d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
209820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
209920985164SVishal Verma 		spa->header.length = sizeof(*spa);
210020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
210120985164SVishal Verma 		spa->range_index = 12+1;
210220985164SVishal Verma 		spa->address = t->dimm_dma[4];
210320985164SVishal Verma 		spa->length = DIMM_SIZE;
2104d7d8464dSRoss Zwisler 		offset += spa->header.length;
210520985164SVishal Verma 
210620985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
210720985164SVishal Verma 		memdev = nfit_buf + offset;
210820985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
210920985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
211020985164SVishal Verma 		memdev->device_handle = handle[4];
211120985164SVishal Verma 		memdev->physical_id = 4;
211220985164SVishal Verma 		memdev->region_id = 0;
211320985164SVishal Verma 		memdev->range_index = 10+1;
21143b87356fSDan Williams 		memdev->region_index = 8+1;
211520985164SVishal Verma 		memdev->region_size = 0;
211620985164SVishal Verma 		memdev->region_offset = 0;
211720985164SVishal Verma 		memdev->address = 0;
211820985164SVishal Verma 		memdev->interleave_index = 0;
211920985164SVishal Verma 		memdev->interleave_ways = 1;
2120d7d8464dSRoss Zwisler 		offset += memdev->header.length;
212120985164SVishal Verma 
2122d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2123d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
212420985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
212520985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
212620985164SVishal Verma 		memdev->device_handle = handle[4];
212720985164SVishal Verma 		memdev->physical_id = 4;
212820985164SVishal Verma 		memdev->region_id = 0;
212920985164SVishal Verma 		memdev->range_index = 11+1;
21303b87356fSDan Williams 		memdev->region_index = 9+1;
213120985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2132df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
213320985164SVishal Verma 		memdev->address = 0;
213420985164SVishal Verma 		memdev->interleave_index = 0;
213520985164SVishal Verma 		memdev->interleave_ways = 1;
2136ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2137d7d8464dSRoss Zwisler 		offset += memdev->header.length;
213820985164SVishal Verma 
21393b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2140d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
214120985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
214220985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
214320985164SVishal Verma 		memdev->device_handle = handle[4];
214420985164SVishal Verma 		memdev->physical_id = 4;
214520985164SVishal Verma 		memdev->region_id = 0;
214620985164SVishal Verma 		memdev->range_index = 12+1;
21473b87356fSDan Williams 		memdev->region_index = 8+1;
214820985164SVishal Verma 		memdev->region_size = 0;
214920985164SVishal Verma 		memdev->region_offset = 0;
215020985164SVishal Verma 		memdev->address = 0;
215120985164SVishal Verma 		memdev->interleave_index = 0;
215220985164SVishal Verma 		memdev->interleave_ways = 1;
2153d7d8464dSRoss Zwisler 		offset += memdev->header.length;
215420985164SVishal Verma 
215520985164SVishal Verma 		/* flush3 (dimm4) */
215620985164SVishal Verma 		flush = nfit_buf + offset;
215720985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
215885d3fa02SDan Williams 		flush->header.length = flush_hint_size;
215920985164SVishal Verma 		flush->device_handle = handle[4];
216085d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
216185d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
216285d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
216385d3fa02SDan Williams 				+ i * sizeof(u64);
2164d7d8464dSRoss Zwisler 		offset += flush->header.length;
21659741a559SRoss Zwisler 
21669741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
21679741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
216820985164SVishal Verma 	}
216920985164SVishal Verma 
21701526f9e2SRoss Zwisler 	t->nfit_filled = offset;
21711526f9e2SRoss Zwisler 
21729fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
21739fb1a190SDave Jiang 			SPA0_SIZE);
2174f471f1a7SDan Williams 
21756bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2176e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2177e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2178e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2179ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2180ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2181ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
21824cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2183e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2184e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2185e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2186e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
218710246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
218810246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
21899fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
21909fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
21919fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2192bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2193bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2194bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2195bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2196bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2197674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
21986bc75619SDan Williams }
21996bc75619SDan Williams 
22006bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
22016bc75619SDan Williams {
22026b577c9dSLinda Knippers 	size_t offset;
22036bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
22046bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
22056bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
22066bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2207d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
22086bc75619SDan Williams 
22096b577c9dSLinda Knippers 	offset = 0;
22106bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
22116bc75619SDan Williams 	spa = nfit_buf + offset;
22126bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
22136bc75619SDan Williams 	spa->header.length = sizeof(*spa);
22146bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
22156bc75619SDan Williams 	spa->range_index = 0+1;
22166bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
22176bc75619SDan Williams 	spa->length = SPA2_SIZE;
2218d7d8464dSRoss Zwisler 	offset += spa->header.length;
22196bc75619SDan Williams 
22207bfe97c7SDan Williams 	/* virtual cd region */
2221d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
22227bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
22237bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
22247bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
22257bfe97c7SDan Williams 	spa->range_index = 0;
22267bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
22277bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2228d7d8464dSRoss Zwisler 	offset += spa->header.length;
22297bfe97c7SDan Williams 
22306bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
22316bc75619SDan Williams 	memdev = nfit_buf + offset;
22326bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22336bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2234dafb1048SDan Williams 	memdev->device_handle = handle[5];
22356bc75619SDan Williams 	memdev->physical_id = 0;
22366bc75619SDan Williams 	memdev->region_id = 0;
22376bc75619SDan Williams 	memdev->range_index = 0+1;
22386bc75619SDan Williams 	memdev->region_index = 0+1;
22396bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
22406bc75619SDan Williams 	memdev->region_offset = 0;
22416bc75619SDan Williams 	memdev->address = 0;
22426bc75619SDan Williams 	memdev->interleave_index = 0;
22436bc75619SDan Williams 	memdev->interleave_ways = 1;
224458138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
224558138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2246f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2247d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22486bc75619SDan Williams 
22496bc75619SDan Williams 	/* dcr-descriptor0 */
22506bc75619SDan Williams 	dcr = nfit_buf + offset;
22516bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22523b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22533b87356fSDan Williams 			window_size);
22546bc75619SDan Williams 	dcr->region_index = 0+1;
22555dc68e55SDan Williams 	dcr_common_init(dcr);
2256dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2257be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
22586bc75619SDan Williams 	dcr->windows = 0;
2259ac40b675SDan Williams 	offset += dcr->header.length;
2260d7d8464dSRoss Zwisler 
2261ac40b675SDan Williams 	memdev = nfit_buf + offset;
2262ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2263ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2264ac40b675SDan Williams 	memdev->device_handle = handle[6];
2265ac40b675SDan Williams 	memdev->physical_id = 0;
2266ac40b675SDan Williams 	memdev->region_id = 0;
2267ac40b675SDan Williams 	memdev->range_index = 0;
2268ac40b675SDan Williams 	memdev->region_index = 0+2;
2269ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2270ac40b675SDan Williams 	memdev->region_offset = 0;
2271ac40b675SDan Williams 	memdev->address = 0;
2272ac40b675SDan Williams 	memdev->interleave_index = 0;
2273ac40b675SDan Williams 	memdev->interleave_ways = 1;
2274ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2275d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2276ac40b675SDan Williams 
2277ac40b675SDan Williams 	/* dcr-descriptor1 */
2278ac40b675SDan Williams 	dcr = nfit_buf + offset;
2279ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2280ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2281ac40b675SDan Williams 			window_size);
2282ac40b675SDan Williams 	dcr->region_index = 0+2;
2283ac40b675SDan Williams 	dcr_common_init(dcr);
2284ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2285ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2286ac40b675SDan Williams 	dcr->windows = 0;
2287d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2288ac40b675SDan Williams 
22899741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
22909741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
22919741a559SRoss Zwisler 
22921526f9e2SRoss Zwisler 	t->nfit_filled = offset;
22931526f9e2SRoss Zwisler 
22949fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
22959fb1a190SDave Jiang 			SPA2_SIZE);
2296f471f1a7SDan Williams 
2297d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2298e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2299e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2300e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2301e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2302674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
23039484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
23049484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
23059484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
23066bc75619SDan Williams }
23076bc75619SDan Williams 
23086bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
23096bc75619SDan Williams 		void *iobuf, u64 len, int rw)
23106bc75619SDan Williams {
23116bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
23126bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
23136bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
23146bc75619SDan Williams 	unsigned int lane;
23156bc75619SDan Williams 
23166bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
23176bc75619SDan Williams 	if (rw)
231867a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
231967a3e8feSRoss Zwisler 	else {
232067a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
232167a3e8feSRoss Zwisler 
23225deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
23235deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
232467a3e8feSRoss Zwisler 	}
23256bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
23266bc75619SDan Williams 
23276bc75619SDan Williams 	return 0;
23286bc75619SDan Williams }
23296bc75619SDan Williams 
2330a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2331a7de92daSDan Williams 
2332a7de92daSDan Williams union acpi_object *result;
2333a7de92daSDan Williams 
2334a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
233594116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2336a7de92daSDan Williams {
2337a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2338a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2339a7de92daSDan Williams 
2340a7de92daSDan Williams 	return result;
2341a7de92daSDan Williams }
2342a7de92daSDan Williams 
2343a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2344a7de92daSDan Williams {
2345a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2346a7de92daSDan Williams 	if (!result)
2347a7de92daSDan Williams 		return -ENOMEM;
2348a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2349a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2350a7de92daSDan Williams 	result->buffer.length = size;
2351a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2352a7de92daSDan Williams 	memset(buf, 0, size);
2353a7de92daSDan Williams 	return 0;
2354a7de92daSDan Williams }
2355a7de92daSDan Williams 
2356a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2357a7de92daSDan Williams {
2358a7de92daSDan Williams 	int rc, cmd_rc;
2359a7de92daSDan Williams 	struct nvdimm *nvdimm;
2360a7de92daSDan Williams 	struct acpi_device *adev;
2361a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2362a7de92daSDan Williams 	struct nd_ars_record *record;
2363a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2364a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2365a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2366a7de92daSDan Williams 	union {
2367a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2368fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2369a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2370a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2371a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2372a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2373a7de92daSDan Williams 	} cmds;
2374a7de92daSDan Williams 
2375a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2376a7de92daSDan Williams 	if (!adev)
2377a7de92daSDan Williams 		return -ENOMEM;
2378a7de92daSDan Williams 	*adev = (struct acpi_device) {
2379a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2380a7de92daSDan Williams 		.dev = {
2381a7de92daSDan Williams 			.init_name = "test-adev",
2382a7de92daSDan Williams 		},
2383a7de92daSDan Williams 	};
2384a7de92daSDan Williams 
2385a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2386a7de92daSDan Williams 	if (!acpi_desc)
2387a7de92daSDan Williams 		return -ENOMEM;
2388a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2389a7de92daSDan Williams 		.nd_desc = {
2390a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2391a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2392a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
239310246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
239410246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2395a7de92daSDan Williams 			.module = THIS_MODULE,
2396a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2397a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
23989fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
23999fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
24009fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
24019fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2402a7de92daSDan Williams 		},
2403a7de92daSDan Williams 		.dev = &adev->dev,
2404a7de92daSDan Williams 	};
2405a7de92daSDan Williams 
2406a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2407a7de92daSDan Williams 	if (!nfit_mem)
2408a7de92daSDan Williams 		return -ENOMEM;
2409a7de92daSDan Williams 
2410a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2411a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2412a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2413a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2414a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2415a7de92daSDan Williams 		.adev = adev,
2416a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2417a7de92daSDan Williams 		.dsm_mask = mask,
2418a7de92daSDan Williams 	};
2419a7de92daSDan Williams 
2420a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2421a7de92daSDan Williams 	if (!nvdimm)
2422a7de92daSDan Williams 		return -ENOMEM;
2423a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2424a7de92daSDan Williams 		.provider_data = nfit_mem,
2425a7de92daSDan Williams 		.cmd_mask = mask,
2426a7de92daSDan Williams 		.dev = {
2427a7de92daSDan Williams 			.init_name = "test-dimm",
2428a7de92daSDan Williams 		},
2429a7de92daSDan Williams 	};
2430a7de92daSDan Williams 
2431a7de92daSDan Williams 
2432a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2433a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2434a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2435a7de92daSDan Williams 		.status = 0,
2436a7de92daSDan Williams 		.config_size = SZ_128K,
2437a7de92daSDan Williams 		.max_xfer = SZ_4K,
2438a7de92daSDan Williams 	};
2439a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2440a7de92daSDan Williams 	if (rc)
2441a7de92daSDan Williams 		return rc;
2442a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2443a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2444a7de92daSDan Williams 
2445a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2446a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2447a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2448a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2449a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2450a7de92daSDan Williams 		return -EIO;
2451a7de92daSDan Williams 	}
2452a7de92daSDan Williams 
2453a7de92daSDan Williams 
2454a7de92daSDan Williams 	/* test ars_status with zero output */
2455a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2456a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2457a7de92daSDan Williams 		.out_length = 0,
2458a7de92daSDan Williams 	};
2459a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2460a7de92daSDan Williams 	if (rc)
2461a7de92daSDan Williams 		return rc;
2462a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2463a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2464a7de92daSDan Williams 
2465a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2466a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2467a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2468a7de92daSDan Williams 		return -EIO;
2469a7de92daSDan Williams 	}
2470a7de92daSDan Williams 
2471a7de92daSDan Williams 
2472a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2473a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2474a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2475a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2476a7de92daSDan Williams 	};
2477a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2478a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2479a7de92daSDan Williams 	if (rc)
2480a7de92daSDan Williams 		return rc;
2481a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2482a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2483a7de92daSDan Williams 
2484a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2485a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2486a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2487a7de92daSDan Williams 		return -EIO;
2488a7de92daSDan Williams 	}
2489a7de92daSDan Williams 
2490a7de92daSDan Williams 
2491a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2492a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2493a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2494a7de92daSDan Williams 		.out_length = cmd_size - 4,
2495a7de92daSDan Williams 	};
2496a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2497a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2498a7de92daSDan Williams 		.length = test_val,
2499a7de92daSDan Williams 	};
2500a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2501a7de92daSDan Williams 	if (rc)
2502a7de92daSDan Williams 		return rc;
2503a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2504a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2505a7de92daSDan Williams 
2506a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2507a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2508a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2509a7de92daSDan Williams 		return -EIO;
2510a7de92daSDan Williams 	}
2511a7de92daSDan Williams 
2512a7de92daSDan Williams 
2513a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2514a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2515a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2516a7de92daSDan Williams 		.out_length = cmd_size,
2517a7de92daSDan Williams 	};
2518a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2519a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2520a7de92daSDan Williams 		.length = test_val,
2521a7de92daSDan Williams 	};
2522a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2523a7de92daSDan Williams 	if (rc)
2524a7de92daSDan Williams 		return rc;
2525a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2526a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2527a7de92daSDan Williams 
2528a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2529a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2530a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2531a7de92daSDan Williams 		return -EIO;
2532a7de92daSDan Williams 	}
2533a7de92daSDan Williams 
2534a7de92daSDan Williams 
2535a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2536a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2537a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2538a7de92daSDan Williams 		.status = 1 << 16,
2539a7de92daSDan Williams 	};
2540a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2541a7de92daSDan Williams 	if (rc)
2542a7de92daSDan Williams 		return rc;
2543a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2544a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2545a7de92daSDan Williams 
2546a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2547a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2548a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2549a7de92daSDan Williams 		return -EIO;
2550a7de92daSDan Williams 	}
2551a7de92daSDan Williams 
2552fb2a1748SDan Williams 	/* test clear error */
2553fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2554fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2555fb2a1748SDan Williams 		.length = 512,
2556fb2a1748SDan Williams 		.cleared = 512,
2557fb2a1748SDan Williams 	};
2558fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2559fb2a1748SDan Williams 	if (rc)
2560fb2a1748SDan Williams 		return rc;
2561fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2562fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2563fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2564fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2565fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2566fb2a1748SDan Williams 		return -EIO;
2567fb2a1748SDan Williams 	}
2568fb2a1748SDan Williams 
2569a7de92daSDan Williams 	return 0;
2570a7de92daSDan Williams }
2571a7de92daSDan Williams 
25726bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
25736bc75619SDan Williams {
25746bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
25756bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25766bc75619SDan Williams 	struct device *dev = &pdev->dev;
25776bc75619SDan Williams 	struct nfit_test *nfit_test;
2578231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2579c14a868aSDan Williams 	union acpi_object *obj;
25806bc75619SDan Williams 	int rc;
25816bc75619SDan Williams 
2582a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2583a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2584a7de92daSDan Williams 		if (rc)
2585a7de92daSDan Williams 			return rc;
2586a7de92daSDan Williams 	}
2587a7de92daSDan Williams 
25886bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
25896bc75619SDan Williams 
25906bc75619SDan Williams 	/* common alloc */
25916bc75619SDan Williams 	if (nfit_test->num_dcr) {
25926bc75619SDan Williams 		int num = nfit_test->num_dcr;
25936bc75619SDan Williams 
25946bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
25956bc75619SDan Williams 				GFP_KERNEL);
25966bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
25976bc75619SDan Williams 				GFP_KERNEL);
25989d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
25999d27a87eSDan Williams 				GFP_KERNEL);
26009d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
26019d27a87eSDan Williams 				GFP_KERNEL);
26026bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
26036bc75619SDan Williams 				GFP_KERNEL);
26046bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
26056bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
26066bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
26076bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
26086bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
26096bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2610ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2611ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2612ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2613ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2614ed07c433SDan Williams 				GFP_KERNEL);
2615bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2616bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
26176bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
26186bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
26199d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2620bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2621bfbaa952SDave Jiang 				&& nfit_test->fw)
26226bc75619SDan Williams 			/* pass */;
26236bc75619SDan Williams 		else
26246bc75619SDan Williams 			return -ENOMEM;
26256bc75619SDan Williams 	}
26266bc75619SDan Williams 
26276bc75619SDan Williams 	if (nfit_test->num_pm) {
26286bc75619SDan Williams 		int num = nfit_test->num_pm;
26296bc75619SDan Williams 
26306bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
26316bc75619SDan Williams 				GFP_KERNEL);
26326bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
26336bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
26346bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
26356bc75619SDan Williams 			/* pass */;
26366bc75619SDan Williams 		else
26376bc75619SDan Williams 			return -ENOMEM;
26386bc75619SDan Williams 	}
26396bc75619SDan Williams 
26406bc75619SDan Williams 	/* per-nfit specific alloc */
26416bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
26426bc75619SDan Williams 		return -ENOMEM;
26436bc75619SDan Williams 
26446bc75619SDan Williams 	nfit_test->setup(nfit_test);
26456bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2646a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
26476bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
26486bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2649a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2650bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2651a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
26526bc75619SDan Williams 
2653e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
26541526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
265558cd71b4SDan Williams 	if (rc)
265620985164SVishal Verma 		return rc;
265720985164SVishal Verma 
2658fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2659fbabd829SDan Williams 	if (rc)
2660fbabd829SDan Williams 		return rc;
2661fbabd829SDan Williams 
266220985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
266320985164SVishal Verma 		return 0;
266420985164SVishal Verma 
266520985164SVishal Verma 	nfit_test->setup_hotplug = 1;
266620985164SVishal Verma 	nfit_test->setup(nfit_test);
266720985164SVishal Verma 
2668c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2669c14a868aSDan Williams 	if (!obj)
2670c14a868aSDan Williams 		return -ENOMEM;
2671c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2672c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2673c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2674c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2675c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2676231bf117SDan Williams 
2677231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2678231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
2679231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2680231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2681231bf117SDan Williams 		int i;
2682231bf117SDan Williams 
2683231bf117SDan Williams 		for (i = 0; i < NUM_DCR; i++)
2684231bf117SDan Williams 			if (nfit_handle == handle[i])
2685231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
2686231bf117SDan Williams 						nfit_mem);
2687231bf117SDan Williams 	}
2688231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
26896bc75619SDan Williams 
26906bc75619SDan Williams 	return 0;
26916bc75619SDan Williams }
26926bc75619SDan Williams 
26936bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
26946bc75619SDan Williams {
26956bc75619SDan Williams 	return 0;
26966bc75619SDan Williams }
26976bc75619SDan Williams 
26986bc75619SDan Williams static void nfit_test_release(struct device *dev)
26996bc75619SDan Williams {
27006bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
27016bc75619SDan Williams 
27026bc75619SDan Williams 	kfree(nfit_test);
27036bc75619SDan Williams }
27046bc75619SDan Williams 
27056bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
27066bc75619SDan Williams 	{ KBUILD_MODNAME },
27076bc75619SDan Williams 	{ },
27086bc75619SDan Williams };
27096bc75619SDan Williams 
27106bc75619SDan Williams static struct platform_driver nfit_test_driver = {
27116bc75619SDan Williams 	.probe = nfit_test_probe,
27126bc75619SDan Williams 	.remove = nfit_test_remove,
27136bc75619SDan Williams 	.driver = {
27146bc75619SDan Williams 		.name = KBUILD_MODNAME,
27156bc75619SDan Williams 	},
27166bc75619SDan Williams 	.id_table = nfit_test_id,
27176bc75619SDan Williams };
27186bc75619SDan Williams 
27195d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
27205d8beee2SDan Williams 
27215d8beee2SDan Williams enum INJECT {
27225d8beee2SDan Williams 	INJECT_NONE,
27235d8beee2SDan Williams 	INJECT_SRC,
27245d8beee2SDan Williams 	INJECT_DST,
27255d8beee2SDan Williams };
27265d8beee2SDan Williams 
27275d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
27285d8beee2SDan Williams {
27295d8beee2SDan Williams 	size_t i;
27305d8beee2SDan Williams 
27315d8beee2SDan Williams 	memset(dst, 0xff, size);
27325d8beee2SDan Williams 	for (i = 0; i < size; i++)
27335d8beee2SDan Williams 		src[i] = (char) i;
27345d8beee2SDan Williams }
27355d8beee2SDan Williams 
27365d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
27375d8beee2SDan Williams 		size_t size, unsigned long rem)
27385d8beee2SDan Williams {
27395d8beee2SDan Williams 	size_t i;
27405d8beee2SDan Williams 
27415d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
27425d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
27435d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
27445d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
27455d8beee2SDan Williams 					(unsigned char) i);
27465d8beee2SDan Williams 			return false;
27475d8beee2SDan Williams 		}
27485d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
27495d8beee2SDan Williams 		if (dst[i] != 0xffU) {
27505d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
27515d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
27525d8beee2SDan Williams 			return false;
27535d8beee2SDan Williams 		}
27545d8beee2SDan Williams 	return true;
27555d8beee2SDan Williams }
27565d8beee2SDan Williams 
27575d8beee2SDan Williams void mcsafe_test(void)
27585d8beee2SDan Williams {
27595d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
27605d8beee2SDan Williams 	enum INJECT inj;
27615d8beee2SDan Williams 
27625d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
27635d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
27645d8beee2SDan Williams 	} else {
27655d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
27665d8beee2SDan Williams 		return;
27675d8beee2SDan Williams 	}
27685d8beee2SDan Williams 
27695d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
27705d8beee2SDan Williams 		int i;
27715d8beee2SDan Williams 
27725d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
27735d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
27745d8beee2SDan Williams 			unsigned long expect, rem;
27755d8beee2SDan Williams 			void *src, *dst;
27765d8beee2SDan Williams 			bool valid;
27775d8beee2SDan Williams 
27785d8beee2SDan Williams 			switch (inj) {
27795d8beee2SDan Williams 			case INJECT_NONE:
27805d8beee2SDan Williams 				mcsafe_inject_src(NULL);
27815d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
27825d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
27835d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
27845d8beee2SDan Williams 				expect = 0;
27855d8beee2SDan Williams 				break;
27865d8beee2SDan Williams 			case INJECT_SRC:
27875d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
27885d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
27895d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
27905d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
27915d8beee2SDan Williams 				expect = 512 - i;
27925d8beee2SDan Williams 				break;
27935d8beee2SDan Williams 			case INJECT_DST:
27945d8beee2SDan Williams 				mcsafe_inject_src(NULL);
27955d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
27965d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
27975d8beee2SDan Williams 				src = &mcsafe_buf[1024];
27985d8beee2SDan Williams 				expect = 512 - i;
27995d8beee2SDan Williams 				break;
28005d8beee2SDan Williams 			}
28015d8beee2SDan Williams 
28025d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
28035d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
28045d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
28055d8beee2SDan Williams 			if (rem == expect && valid)
28065d8beee2SDan Williams 				continue;
28075d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
28085d8beee2SDan Williams 					__func__,
28095d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
28105d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
28115d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
28125d8beee2SDan Williams 					expect);
28135d8beee2SDan Williams 		}
28145d8beee2SDan Williams 	}
28155d8beee2SDan Williams 
28165d8beee2SDan Williams 	mcsafe_inject_src(NULL);
28175d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
28185d8beee2SDan Williams }
28195d8beee2SDan Williams 
28206bc75619SDan Williams static __init int nfit_test_init(void)
28216bc75619SDan Williams {
28226bc75619SDan Williams 	int rc, i;
28236bc75619SDan Williams 
28240fb5c8dfSDan Williams 	pmem_test();
28250fb5c8dfSDan Williams 	libnvdimm_test();
28260fb5c8dfSDan Williams 	acpi_nfit_test();
28270fb5c8dfSDan Williams 	device_dax_test();
28285d8beee2SDan Williams 	mcsafe_test();
28290fb5c8dfSDan Williams 
2830a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
2831231bf117SDan Williams 
28329fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
28339fb1a190SDave Jiang 	if (!nfit_wq)
28349fb1a190SDave Jiang 		return -ENOMEM;
28359fb1a190SDave Jiang 
2836a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
2837a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
2838a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
2839a7de92daSDan Williams 		goto err_register;
2840a7de92daSDan Williams 	}
28416bc75619SDan Williams 
28426bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
28436bc75619SDan Williams 		struct nfit_test *nfit_test;
28446bc75619SDan Williams 		struct platform_device *pdev;
28456bc75619SDan Williams 
28466bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
28476bc75619SDan Williams 		if (!nfit_test) {
28486bc75619SDan Williams 			rc = -ENOMEM;
28496bc75619SDan Williams 			goto err_register;
28506bc75619SDan Williams 		}
28516bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
28529fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
28536bc75619SDan Williams 		switch (i) {
28546bc75619SDan Williams 		case 0:
28556bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
2856dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
28576bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
28586bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
28596bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
28606bc75619SDan Williams 			break;
28616bc75619SDan Williams 		case 1:
2862a117699cSYasunori Goto 			nfit_test->num_pm = 2;
2863dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
2864ac40b675SDan Williams 			nfit_test->num_dcr = 2;
28656bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
28666bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
28676bc75619SDan Williams 			break;
28686bc75619SDan Williams 		default:
28696bc75619SDan Williams 			rc = -EINVAL;
28706bc75619SDan Williams 			goto err_register;
28716bc75619SDan Williams 		}
28726bc75619SDan Williams 		pdev = &nfit_test->pdev;
28736bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
28746bc75619SDan Williams 		pdev->id = i;
28756bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
28766bc75619SDan Williams 		rc = platform_device_register(pdev);
28776bc75619SDan Williams 		if (rc) {
28786bc75619SDan Williams 			put_device(&pdev->dev);
28796bc75619SDan Williams 			goto err_register;
28806bc75619SDan Williams 		}
28818b06b884SDan Williams 		get_device(&pdev->dev);
28826bc75619SDan Williams 
28836bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
28846bc75619SDan Williams 		if (rc)
28856bc75619SDan Williams 			goto err_register;
28866bc75619SDan Williams 
28876bc75619SDan Williams 		instances[i] = nfit_test;
28889fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
28896bc75619SDan Williams 	}
28906bc75619SDan Williams 
28916bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
28926bc75619SDan Williams 	if (rc)
28936bc75619SDan Williams 		goto err_register;
28946bc75619SDan Williams 	return 0;
28956bc75619SDan Williams 
28966bc75619SDan Williams  err_register:
28979fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
28986bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
28996bc75619SDan Williams 		if (instances[i])
29006bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
29016bc75619SDan Williams 	nfit_test_teardown();
29028b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
29038b06b884SDan Williams 		if (instances[i])
29048b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
29058b06b884SDan Williams 
29066bc75619SDan Williams 	return rc;
29076bc75619SDan Williams }
29086bc75619SDan Williams 
29096bc75619SDan Williams static __exit void nfit_test_exit(void)
29106bc75619SDan Williams {
29116bc75619SDan Williams 	int i;
29126bc75619SDan Williams 
29139fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
29149fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
29156bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
29166bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
29178b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
29186bc75619SDan Williams 	nfit_test_teardown();
29198b06b884SDan Williams 
29208b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
29218b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
2922231bf117SDan Williams 	class_destroy(nfit_test_dimm);
29236bc75619SDan Williams }
29246bc75619SDan Williams 
29256bc75619SDan Williams module_init(nfit_test_init);
29266bc75619SDan Williams module_exit(nfit_test_exit);
29276bc75619SDan Williams MODULE_LICENSE("GPL v2");
29286bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
2929