xref: /openbmc/linux/tools/testing/nvdimm/test/nfit.c (revision 3c13e2ac747a37e683597d3d875f839f2bc150e1)
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 
143af31b04bSMasayoshi Mizuma static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
144af31b04bSMasayoshi Mizuma static int dimm_fail_cmd_code[ARRAY_SIZE(handle)];
145*3c13e2acSDave Jiang struct nfit_test_sec {
146*3c13e2acSDave Jiang 	u8 state;
147*3c13e2acSDave Jiang 	u8 passphrase[32];
148*3c13e2acSDave Jiang } dimm_sec_info[NUM_DCR];
14973606afdSDan Williams 
150b4d4702fSVishal Verma static const struct nd_intel_smart smart_def = {
151b4d4702fSVishal Verma 	.flags = ND_INTEL_SMART_HEALTH_VALID
152b4d4702fSVishal Verma 		| ND_INTEL_SMART_SPARES_VALID
153b4d4702fSVishal Verma 		| ND_INTEL_SMART_ALARM_VALID
154b4d4702fSVishal Verma 		| ND_INTEL_SMART_USED_VALID
155b4d4702fSVishal Verma 		| ND_INTEL_SMART_SHUTDOWN_VALID
156f1101766SDan Williams 		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
157b4d4702fSVishal Verma 		| ND_INTEL_SMART_MTEMP_VALID
158b4d4702fSVishal Verma 		| ND_INTEL_SMART_CTEMP_VALID,
159b4d4702fSVishal Verma 	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
160b4d4702fSVishal Verma 	.media_temperature = 23 * 16,
161b4d4702fSVishal Verma 	.ctrl_temperature = 25 * 16,
162b4d4702fSVishal Verma 	.pmic_temperature = 40 * 16,
163b4d4702fSVishal Verma 	.spares = 75,
164b4d4702fSVishal Verma 	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
165b4d4702fSVishal Verma 		| ND_INTEL_SMART_TEMP_TRIP,
166b4d4702fSVishal Verma 	.ait_status = 1,
167b4d4702fSVishal Verma 	.life_used = 5,
168b4d4702fSVishal Verma 	.shutdown_state = 0,
169f1101766SDan Williams 	.shutdown_count = 42,
170b4d4702fSVishal Verma 	.vendor_size = 0,
171b4d4702fSVishal Verma };
172b4d4702fSVishal Verma 
173bfbaa952SDave Jiang struct nfit_test_fw {
174bfbaa952SDave Jiang 	enum intel_fw_update_state state;
175bfbaa952SDave Jiang 	u32 context;
176bfbaa952SDave Jiang 	u64 version;
177bfbaa952SDave Jiang 	u32 size_received;
178bfbaa952SDave Jiang 	u64 end_time;
179bfbaa952SDave Jiang };
180bfbaa952SDave Jiang 
1816bc75619SDan Williams struct nfit_test {
1826bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1836bc75619SDan Williams 	struct platform_device pdev;
1846bc75619SDan Williams 	struct list_head resources;
1856bc75619SDan Williams 	void *nfit_buf;
1866bc75619SDan Williams 	dma_addr_t nfit_dma;
1876bc75619SDan Williams 	size_t nfit_size;
1881526f9e2SRoss Zwisler 	size_t nfit_filled;
189dafb1048SDan Williams 	int dcr_idx;
1906bc75619SDan Williams 	int num_dcr;
1916bc75619SDan Williams 	int num_pm;
1926bc75619SDan Williams 	void **dimm;
1936bc75619SDan Williams 	dma_addr_t *dimm_dma;
1949d27a87eSDan Williams 	void **flush;
1959d27a87eSDan Williams 	dma_addr_t *flush_dma;
1966bc75619SDan Williams 	void **label;
1976bc75619SDan Williams 	dma_addr_t *label_dma;
1986bc75619SDan Williams 	void **spa_set;
1996bc75619SDan Williams 	dma_addr_t *spa_set_dma;
2006bc75619SDan Williams 	struct nfit_test_dcr **dcr;
2016bc75619SDan Williams 	dma_addr_t *dcr_dma;
2026bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
2036bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
20420985164SVishal Verma 	int setup_hotplug;
205c14a868aSDan Williams 	union acpi_object **_fit;
206c14a868aSDan Williams 	dma_addr_t _fit_dma;
207f471f1a7SDan Williams 	struct ars_state {
208f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
209f471f1a7SDan Williams 		unsigned long deadline;
210f471f1a7SDan Williams 		spinlock_t lock;
211f471f1a7SDan Williams 	} ars_state;
212af31b04bSMasayoshi Mizuma 	struct device *dimm_dev[ARRAY_SIZE(handle)];
213ed07c433SDan Williams 	struct nd_intel_smart *smart;
214ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
2159fb1a190SDave Jiang 	struct badrange badrange;
2169fb1a190SDave Jiang 	struct work_struct work;
217bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
2186bc75619SDan Williams };
2196bc75619SDan Williams 
2209fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
2219fb1a190SDave Jiang 
2226bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
2236bc75619SDan Williams {
2246bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
2256bc75619SDan Williams 
2266bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
2276bc75619SDan Williams }
2286bc75619SDan Williams 
229bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
230bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
231bfbaa952SDave Jiang 		int idx)
232bfbaa952SDave Jiang {
233bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
234bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
235bfbaa952SDave Jiang 
236bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
237bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
238bfbaa952SDave Jiang 
239bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
240bfbaa952SDave Jiang 		return -EINVAL;
241bfbaa952SDave Jiang 
242bfbaa952SDave Jiang 	nd_cmd->status = 0;
243bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
244bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
245bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
246bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
247bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
248bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
249bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
250bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
251bfbaa952SDave Jiang 
252bfbaa952SDave Jiang 	return 0;
253bfbaa952SDave Jiang }
254bfbaa952SDave Jiang 
255bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
256bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
257bfbaa952SDave Jiang 		int idx)
258bfbaa952SDave Jiang {
259bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
260bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
261bfbaa952SDave Jiang 
262bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
263bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
264bfbaa952SDave Jiang 
265bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
266bfbaa952SDave Jiang 		return -EINVAL;
267bfbaa952SDave Jiang 
268bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
269bfbaa952SDave Jiang 		/* extended status, FW update in progress */
270bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
271bfbaa952SDave Jiang 		return 0;
272bfbaa952SDave Jiang 	}
273bfbaa952SDave Jiang 
274bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
275bfbaa952SDave Jiang 	fw->context++;
276bfbaa952SDave Jiang 	fw->size_received = 0;
277bfbaa952SDave Jiang 	nd_cmd->status = 0;
278bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
279bfbaa952SDave Jiang 
280bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
281bfbaa952SDave Jiang 
282bfbaa952SDave Jiang 	return 0;
283bfbaa952SDave Jiang }
284bfbaa952SDave Jiang 
285bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
286bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
287bfbaa952SDave Jiang 		int idx)
288bfbaa952SDave Jiang {
289bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
290bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
291bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
292bfbaa952SDave Jiang 
293bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
294bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
295bfbaa952SDave Jiang 
296bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
297bfbaa952SDave Jiang 		return -EINVAL;
298bfbaa952SDave Jiang 
299bfbaa952SDave Jiang 
300bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
301bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
302bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
303bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
304bfbaa952SDave Jiang 
305bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
306bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
307bfbaa952SDave Jiang 		*status = 0x5;
308bfbaa952SDave Jiang 		return 0;
309bfbaa952SDave Jiang 	}
310bfbaa952SDave Jiang 
311bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
312bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
313bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
314bfbaa952SDave Jiang 		*status = 0x10007;
315bfbaa952SDave Jiang 		return 0;
316bfbaa952SDave Jiang 	}
317bfbaa952SDave Jiang 
318bfbaa952SDave Jiang 	/*
319bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
320bfbaa952SDave Jiang 	 * check length is > max send length
321bfbaa952SDave Jiang 	 */
322bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
323bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
324bfbaa952SDave Jiang 		*status = 0x3;
325bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
326bfbaa952SDave Jiang 		return 0;
327bfbaa952SDave Jiang 	}
328bfbaa952SDave Jiang 
329bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
330bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
331bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
332bfbaa952SDave Jiang 	*status = 0;
333bfbaa952SDave Jiang 	return 0;
334bfbaa952SDave Jiang }
335bfbaa952SDave Jiang 
336bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
337bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
338bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
339bfbaa952SDave Jiang {
340bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
341bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
342bfbaa952SDave Jiang 
343bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
344bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
345bfbaa952SDave Jiang 
346bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
347bfbaa952SDave Jiang 		/* update already done, need cold boot */
348bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
349bfbaa952SDave Jiang 		return 0;
350bfbaa952SDave Jiang 	}
351bfbaa952SDave Jiang 
352bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
353bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
354bfbaa952SDave Jiang 
355bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
356bfbaa952SDave Jiang 	case 0: /* finish */
357bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
358bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
359bfbaa952SDave Jiang 					__func__, nd_cmd->context,
360bfbaa952SDave Jiang 					fw->context);
361bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
362bfbaa952SDave Jiang 			return 0;
363bfbaa952SDave Jiang 		}
364bfbaa952SDave Jiang 		nd_cmd->status = 0;
365bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
366bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
367bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
368bfbaa952SDave Jiang 		break;
369bfbaa952SDave Jiang 
370bfbaa952SDave Jiang 	case 1: /* abort */
371bfbaa952SDave Jiang 		fw->size_received = 0;
372bfbaa952SDave Jiang 		/* successfully aborted status */
373bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
374bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
375bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
376bfbaa952SDave Jiang 		break;
377bfbaa952SDave Jiang 
378bfbaa952SDave Jiang 	default: /* bad control flag */
379bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
380bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
381bfbaa952SDave Jiang 		return -EINVAL;
382bfbaa952SDave Jiang 	}
383bfbaa952SDave Jiang 
384bfbaa952SDave Jiang 	return 0;
385bfbaa952SDave Jiang }
386bfbaa952SDave Jiang 
387bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
388bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
389bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
390bfbaa952SDave Jiang {
391bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
392bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
393bfbaa952SDave Jiang 
394bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
395bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
396bfbaa952SDave Jiang 
397bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
398bfbaa952SDave Jiang 		return -EINVAL;
399bfbaa952SDave Jiang 
400bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
401bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
402bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
403bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
404bfbaa952SDave Jiang 		return 0;
405bfbaa952SDave Jiang 	}
406bfbaa952SDave Jiang 
407bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
408bfbaa952SDave Jiang 
409bfbaa952SDave Jiang 	switch (fw->state) {
410bfbaa952SDave Jiang 	case FW_STATE_NEW:
411bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
412bfbaa952SDave Jiang 		nd_cmd->status = 0;
413bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
414bfbaa952SDave Jiang 		break;
415bfbaa952SDave Jiang 
416bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
417bfbaa952SDave Jiang 		/* sequencing error */
418bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
419bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
420bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
421bfbaa952SDave Jiang 		break;
422bfbaa952SDave Jiang 
423bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
424bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
425bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
426bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
427bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
428bfbaa952SDave Jiang 			break;
429bfbaa952SDave Jiang 		}
430bfbaa952SDave Jiang 
431bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
432bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
433bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
434bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
435bfbaa952SDave Jiang 		nd_cmd->status = 0;
436bfbaa952SDave Jiang 		/* bogus test version */
437bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
438bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
439bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
440bfbaa952SDave Jiang 		break;
441bfbaa952SDave Jiang 
442bfbaa952SDave Jiang 	default: /* we should never get here */
443bfbaa952SDave Jiang 		return -EINVAL;
444bfbaa952SDave Jiang 	}
445bfbaa952SDave Jiang 
446bfbaa952SDave Jiang 	return 0;
447bfbaa952SDave Jiang }
448bfbaa952SDave Jiang 
44939c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4506bc75619SDan Williams 		unsigned int buf_len)
4516bc75619SDan Williams {
4526bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4536bc75619SDan Williams 		return -EINVAL;
45439c686b8SVishal Verma 
4556bc75619SDan Williams 	nd_cmd->status = 0;
4566bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4576bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
45839c686b8SVishal Verma 
45939c686b8SVishal Verma 	return 0;
4606bc75619SDan Williams }
46139c686b8SVishal Verma 
46239c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
46339c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
46439c686b8SVishal Verma {
4656bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
46639c686b8SVishal Verma 	int rc;
4676bc75619SDan Williams 
4686bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4696bc75619SDan Williams 		return -EINVAL;
4706bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4716bc75619SDan Williams 		return -EINVAL;
4726bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4736bc75619SDan Williams 		return -EINVAL;
4746bc75619SDan Williams 
4756bc75619SDan Williams 	nd_cmd->status = 0;
4766bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47739c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4786bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
47939c686b8SVishal Verma 
48039c686b8SVishal Verma 	return rc;
4816bc75619SDan Williams }
48239c686b8SVishal Verma 
48339c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
48439c686b8SVishal Verma 		unsigned int buf_len, void *label)
48539c686b8SVishal Verma {
4866bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4876bc75619SDan Williams 	u32 *status;
48839c686b8SVishal Verma 	int rc;
4896bc75619SDan Williams 
4906bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4916bc75619SDan Williams 		return -EINVAL;
4926bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4936bc75619SDan Williams 		return -EINVAL;
4946bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4956bc75619SDan Williams 		return -EINVAL;
4966bc75619SDan Williams 
49739c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4986bc75619SDan Williams 	*status = 0;
4996bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
50039c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
5016bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
50239c686b8SVishal Verma 
50339c686b8SVishal Verma 	return rc;
5046bc75619SDan Williams }
50539c686b8SVishal Verma 
506d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
507747ffe11SDan Williams 
50839c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
50939c686b8SVishal Verma 		unsigned int buf_len)
51039c686b8SVishal Verma {
5119fb1a190SDave Jiang 	int ars_recs;
5129fb1a190SDave Jiang 
51339c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
51439c686b8SVishal Verma 		return -EINVAL;
51539c686b8SVishal Verma 
5169fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5179fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5189fb1a190SDave Jiang 
519747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5209fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
52139c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
522d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
52339c686b8SVishal Verma 
52439c686b8SVishal Verma 	return 0;
52539c686b8SVishal Verma }
52639c686b8SVishal Verma 
5279fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5289fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
52939c686b8SVishal Verma {
530f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
531f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5329fb1a190SDave Jiang 	struct badrange_entry *be;
5339fb1a190SDave Jiang 	u64 end = addr + len - 1;
5349fb1a190SDave Jiang 	int i = 0;
535f471f1a7SDan Williams 
536f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
537f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
538f471f1a7SDan Williams 	ars_status->status = 0;
539f471f1a7SDan Williams 	ars_status->address = addr;
540f471f1a7SDan Williams 	ars_status->length = len;
541f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5429fb1a190SDave Jiang 
5439fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5449fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5459fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5469fb1a190SDave Jiang 		u64 rstart, rend;
5479fb1a190SDave Jiang 
5489fb1a190SDave Jiang 		/* skip entries outside the range */
5499fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5509fb1a190SDave Jiang 			continue;
5519fb1a190SDave Jiang 
5529fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5539fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5549fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
555f471f1a7SDan Williams 		ars_record->handle = 0;
5569fb1a190SDave Jiang 		ars_record->err_address = rstart;
5579fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5589fb1a190SDave Jiang 		i++;
5599fb1a190SDave Jiang 	}
5609fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5619fb1a190SDave Jiang 	ars_status->num_records = i;
5629fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5639fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
564f471f1a7SDan Williams }
565f471f1a7SDan Williams 
5669fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5679fb1a190SDave Jiang 		struct ars_state *ars_state,
568f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
569f471f1a7SDan Williams 		int *cmd_rc)
570f471f1a7SDan Williams {
571f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
57239c686b8SVishal Verma 		return -EINVAL;
57339c686b8SVishal Verma 
574f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
575f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
576f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
577f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
578f471f1a7SDan Williams 	} else {
579f471f1a7SDan Williams 		ars_start->status = 0;
580f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5819fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
582f471f1a7SDan Williams 				ars_start->length);
583f471f1a7SDan Williams 		*cmd_rc = 0;
584f471f1a7SDan Williams 	}
585f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
58639c686b8SVishal Verma 
58739c686b8SVishal Verma 	return 0;
58839c686b8SVishal Verma }
58939c686b8SVishal Verma 
590f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
591f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
592f471f1a7SDan Williams 		int *cmd_rc)
59339c686b8SVishal Verma {
594f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
59539c686b8SVishal Verma 		return -EINVAL;
59639c686b8SVishal Verma 
597f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
598f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
599f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
600f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
601f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
602f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
603f471f1a7SDan Williams 	} else {
604f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
605f471f1a7SDan Williams 				ars_state->ars_status->out_length);
606f471f1a7SDan Williams 		*cmd_rc = 0;
607f471f1a7SDan Williams 	}
608f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
60939c686b8SVishal Verma 	return 0;
61039c686b8SVishal Verma }
61139c686b8SVishal Verma 
6125e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6135e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
614d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
615d4f32367SDan Williams {
616d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
617d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
618d4f32367SDan Williams 		return -EINVAL;
619d4f32367SDan Williams 
620d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
621d4f32367SDan Williams 		return -EINVAL;
622d4f32367SDan Williams 
6235e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
624d4f32367SDan Williams 	clear_err->status = 0;
625d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
626d4f32367SDan Williams 	*cmd_rc = 0;
627d4f32367SDan Williams 	return 0;
628d4f32367SDan Williams }
629d4f32367SDan Williams 
63010246dc8SYasunori Goto struct region_search_spa {
63110246dc8SYasunori Goto 	u64 addr;
63210246dc8SYasunori Goto 	struct nd_region *region;
63310246dc8SYasunori Goto };
63410246dc8SYasunori Goto 
63510246dc8SYasunori Goto static int is_region_device(struct device *dev)
63610246dc8SYasunori Goto {
63710246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
63810246dc8SYasunori Goto }
63910246dc8SYasunori Goto 
64010246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
64110246dc8SYasunori Goto {
64210246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
64310246dc8SYasunori Goto 	struct nd_region *nd_region;
64410246dc8SYasunori Goto 	resource_size_t ndr_end;
64510246dc8SYasunori Goto 
64610246dc8SYasunori Goto 	if (!is_region_device(dev))
64710246dc8SYasunori Goto 		return 0;
64810246dc8SYasunori Goto 
64910246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
65010246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
65110246dc8SYasunori Goto 
65210246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
65310246dc8SYasunori Goto 		ctx->region = nd_region;
65410246dc8SYasunori Goto 		return 1;
65510246dc8SYasunori Goto 	}
65610246dc8SYasunori Goto 
65710246dc8SYasunori Goto 	return 0;
65810246dc8SYasunori Goto }
65910246dc8SYasunori Goto 
66010246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
66110246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
66210246dc8SYasunori Goto {
66310246dc8SYasunori Goto 	int ret;
66410246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
66510246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
66610246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
66710246dc8SYasunori Goto 	struct region_search_spa ctx = {
66810246dc8SYasunori Goto 		.addr = spa->spa,
66910246dc8SYasunori Goto 		.region = NULL,
67010246dc8SYasunori Goto 	};
67110246dc8SYasunori Goto 	u64 dpa;
67210246dc8SYasunori Goto 
67310246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
67410246dc8SYasunori Goto 				nfit_test_search_region_spa);
67510246dc8SYasunori Goto 
67610246dc8SYasunori Goto 	if (!ret)
67710246dc8SYasunori Goto 		return -ENODEV;
67810246dc8SYasunori Goto 
67910246dc8SYasunori Goto 	nd_region = ctx.region;
68010246dc8SYasunori Goto 
68110246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
68210246dc8SYasunori Goto 
68310246dc8SYasunori Goto 	/*
68410246dc8SYasunori Goto 	 * last dimm is selected for test
68510246dc8SYasunori Goto 	 */
68610246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
68710246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
68810246dc8SYasunori Goto 
68910246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
69010246dc8SYasunori Goto 	spa->num_nvdimms = 1;
69110246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
69210246dc8SYasunori Goto 
69310246dc8SYasunori Goto 	return 0;
69410246dc8SYasunori Goto }
69510246dc8SYasunori Goto 
69610246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
69710246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
69810246dc8SYasunori Goto {
69910246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
70010246dc8SYasunori Goto 		return -EINVAL;
70110246dc8SYasunori Goto 
70210246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
70310246dc8SYasunori Goto 		spa->status = 2;
70410246dc8SYasunori Goto 
70510246dc8SYasunori Goto 	return 0;
70610246dc8SYasunori Goto }
70710246dc8SYasunori Goto 
708ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
709ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
710baa51277SDan Williams {
711baa51277SDan Williams 	if (buf_len < sizeof(*smart))
712baa51277SDan Williams 		return -EINVAL;
713ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
714baa51277SDan Williams 	return 0;
715baa51277SDan Williams }
716baa51277SDan Williams 
717cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
718ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
719ed07c433SDan Williams 		unsigned int buf_len,
720ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
721baa51277SDan Williams {
722baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
723baa51277SDan Williams 		return -EINVAL;
724ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
725ed07c433SDan Williams 	return 0;
726ed07c433SDan Williams }
727ed07c433SDan Williams 
728ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
729ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
730ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
731ed07c433SDan Williams {
732ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
733ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
734ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
735ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
736ed07c433SDan Williams 			smart->ctrl_temperature);
737ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
738ed07c433SDan Williams 				&& smart->spares
739ed07c433SDan Williams 				<= thresh->spares)
740ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
741ed07c433SDan Williams 				&& smart->media_temperature
742ed07c433SDan Williams 				>= thresh->media_temperature)
743ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
744ed07c433SDan Williams 				&& smart->ctrl_temperature
7454cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7464cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7474cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
748ed07c433SDan Williams 		device_lock(bus_dev);
749ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
750ed07c433SDan Williams 		device_unlock(bus_dev);
751ed07c433SDan Williams 	}
752ed07c433SDan Williams }
753ed07c433SDan Williams 
754ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
755ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
756ed07c433SDan Williams 		unsigned int buf_len,
757ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
758ed07c433SDan Williams 		struct nd_intel_smart *smart,
759ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
760ed07c433SDan Williams {
761ed07c433SDan Williams 	unsigned int size;
762ed07c433SDan Williams 
763ed07c433SDan Williams 	size = sizeof(*in) - 4;
764ed07c433SDan Williams 	if (buf_len < size)
765ed07c433SDan Williams 		return -EINVAL;
766ed07c433SDan Williams 	memcpy(thresh->data, in, size);
767ed07c433SDan Williams 	in->status = 0;
768ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
769ed07c433SDan Williams 
770baa51277SDan Williams 	return 0;
771baa51277SDan Williams }
772baa51277SDan Williams 
7734cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7744cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7754cf260fcSVishal Verma 		unsigned int buf_len,
7764cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7774cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7784cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7794cf260fcSVishal Verma {
7804cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7814cf260fcSVishal Verma 		return -EINVAL;
7824cf260fcSVishal Verma 
783b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7844cf260fcSVishal Verma 		if (inj->mtemp_enable)
7854cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
786b4d4702fSVishal Verma 		else
787b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
788b4d4702fSVishal Verma 	}
789b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7904cf260fcSVishal Verma 		if (inj->spare_enable)
7914cf260fcSVishal Verma 			smart->spares = inj->spares;
792b4d4702fSVishal Verma 		else
793b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
794b4d4702fSVishal Verma 	}
795b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
7964cf260fcSVishal Verma 		if (inj->fatal_enable)
7974cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
798b4d4702fSVishal Verma 		else
799b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
800b4d4702fSVishal Verma 	}
801b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
8024cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
8034cf260fcSVishal Verma 			smart->shutdown_state = 1;
8044cf260fcSVishal Verma 			smart->shutdown_count++;
805b4d4702fSVishal Verma 		} else
806b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8074cf260fcSVishal Verma 	}
8084cf260fcSVishal Verma 	inj->status = 0;
8094cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8104cf260fcSVishal Verma 
8114cf260fcSVishal Verma 	return 0;
8124cf260fcSVishal Verma }
8134cf260fcSVishal Verma 
8149fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8159fb1a190SDave Jiang {
8169fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8179fb1a190SDave Jiang 
8189fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8199fb1a190SDave Jiang }
8209fb1a190SDave Jiang 
8219fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8229fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8239fb1a190SDave Jiang {
8249fb1a190SDave Jiang 	int rc;
8259fb1a190SDave Jiang 
82641cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8279fb1a190SDave Jiang 		rc = -EINVAL;
8289fb1a190SDave Jiang 		goto err;
8299fb1a190SDave Jiang 	}
8309fb1a190SDave Jiang 
8319fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8329fb1a190SDave Jiang 		rc = -EINVAL;
8339fb1a190SDave Jiang 		goto err;
8349fb1a190SDave Jiang 	}
8359fb1a190SDave Jiang 
8369fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8379fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8389fb1a190SDave Jiang 	if (rc < 0)
8399fb1a190SDave Jiang 		goto err;
8409fb1a190SDave Jiang 
8419fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8429fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8439fb1a190SDave Jiang 
8449fb1a190SDave Jiang 	err_inj->status = 0;
8459fb1a190SDave Jiang 	return 0;
8469fb1a190SDave Jiang 
8479fb1a190SDave Jiang err:
8489fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8499fb1a190SDave Jiang 	return rc;
8509fb1a190SDave Jiang }
8519fb1a190SDave Jiang 
8529fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8539fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8549fb1a190SDave Jiang {
8559fb1a190SDave Jiang 	int rc;
8569fb1a190SDave Jiang 
85741cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8589fb1a190SDave Jiang 		rc = -EINVAL;
8599fb1a190SDave Jiang 		goto err;
8609fb1a190SDave Jiang 	}
8619fb1a190SDave Jiang 
8629fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8639fb1a190SDave Jiang 		rc = -EINVAL;
8649fb1a190SDave Jiang 		goto err;
8659fb1a190SDave Jiang 	}
8669fb1a190SDave Jiang 
8679fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8689fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8699fb1a190SDave Jiang 
8709fb1a190SDave Jiang 	err_clr->status = 0;
8719fb1a190SDave Jiang 	return 0;
8729fb1a190SDave Jiang 
8739fb1a190SDave Jiang err:
8749fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8759fb1a190SDave Jiang 	return rc;
8769fb1a190SDave Jiang }
8779fb1a190SDave Jiang 
8789fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8799fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8809fb1a190SDave Jiang 		unsigned int buf_len)
8819fb1a190SDave Jiang {
8829fb1a190SDave Jiang 	struct badrange_entry *be;
8839fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8849fb1a190SDave Jiang 	int i = 0;
8859fb1a190SDave Jiang 
8869fb1a190SDave Jiang 	err_stat->status = 0;
8879fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8889fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8899fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8909fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8919fb1a190SDave Jiang 		i++;
8929fb1a190SDave Jiang 		if (i > max)
8939fb1a190SDave Jiang 			break;
8949fb1a190SDave Jiang 	}
8959fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8969fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8979fb1a190SDave Jiang 
8989fb1a190SDave Jiang 	return 0;
8999fb1a190SDave Jiang }
9009fb1a190SDave Jiang 
901674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
902674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
903674d8bdeSDave Jiang {
904674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
905674d8bdeSDave Jiang 
906674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
907674d8bdeSDave Jiang 		return -EINVAL;
908674d8bdeSDave Jiang 
909674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
910674d8bdeSDave Jiang 	case 0:
911674d8bdeSDave Jiang 		nd_cmd->status = 0;
912674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
913674d8bdeSDave Jiang 				__func__);
914674d8bdeSDave Jiang 		break;
915674d8bdeSDave Jiang 	case 1:
916674d8bdeSDave Jiang 		nd_cmd->status = 0;
917674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
918674d8bdeSDave Jiang 				__func__);
919674d8bdeSDave Jiang 		break;
920674d8bdeSDave Jiang 	default:
921674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
922674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
923674d8bdeSDave Jiang 		break;
924674d8bdeSDave Jiang 	}
925674d8bdeSDave Jiang 
926674d8bdeSDave Jiang 
927674d8bdeSDave Jiang 	return 0;
928674d8bdeSDave Jiang }
929674d8bdeSDave Jiang 
93039611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
93139611e83SDan Williams {
93239611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
93339611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
93439611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
93539611e83SDan Williams 		return -EIO;
93639611e83SDan Williams 	}
93739611e83SDan Williams 	return rc;
93839611e83SDan Williams }
93939611e83SDan Williams 
940*3c13e2acSDave Jiang static int nd_intel_test_cmd_security_status(struct nfit_test *t,
941*3c13e2acSDave Jiang 		struct nd_intel_get_security_state *nd_cmd,
942*3c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
943*3c13e2acSDave Jiang {
944*3c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
945*3c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
946*3c13e2acSDave Jiang 
947*3c13e2acSDave Jiang 	nd_cmd->status = 0;
948*3c13e2acSDave Jiang 	nd_cmd->state = sec->state;
949*3c13e2acSDave Jiang 	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);
950*3c13e2acSDave Jiang 
951*3c13e2acSDave Jiang 	return 0;
952*3c13e2acSDave Jiang }
953*3c13e2acSDave Jiang 
954*3c13e2acSDave Jiang static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
955*3c13e2acSDave Jiang 		struct nd_intel_unlock_unit *nd_cmd,
956*3c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
957*3c13e2acSDave Jiang {
958*3c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
959*3c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
960*3c13e2acSDave Jiang 
961*3c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
962*3c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
963*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
964*3c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
965*3c13e2acSDave Jiang 				sec->state);
966*3c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
967*3c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
968*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
969*3c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid passphrase\n");
970*3c13e2acSDave Jiang 	} else {
971*3c13e2acSDave Jiang 		nd_cmd->status = 0;
972*3c13e2acSDave Jiang 		sec->state = ND_INTEL_SEC_STATE_ENABLED;
973*3c13e2acSDave Jiang 		dev_dbg(dev, "Unit unlocked\n");
974*3c13e2acSDave Jiang 	}
975*3c13e2acSDave Jiang 
976*3c13e2acSDave Jiang 	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
977*3c13e2acSDave Jiang 	return 0;
978*3c13e2acSDave Jiang }
979*3c13e2acSDave Jiang 
980*3c13e2acSDave Jiang static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
981*3c13e2acSDave Jiang 		struct nd_intel_set_passphrase *nd_cmd,
982*3c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
983*3c13e2acSDave Jiang {
984*3c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
985*3c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
986*3c13e2acSDave Jiang 
987*3c13e2acSDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
988*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
989*3c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong security state\n");
990*3c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
991*3c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
992*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
993*3c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong passphrase\n");
994*3c13e2acSDave Jiang 	} else {
995*3c13e2acSDave Jiang 		memcpy(sec->passphrase, nd_cmd->new_pass,
996*3c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
997*3c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
998*3c13e2acSDave Jiang 		nd_cmd->status = 0;
999*3c13e2acSDave Jiang 		dev_dbg(dev, "passphrase updated\n");
1000*3c13e2acSDave Jiang 	}
1001*3c13e2acSDave Jiang 
1002*3c13e2acSDave Jiang 	return 0;
1003*3c13e2acSDave Jiang }
1004*3c13e2acSDave Jiang 
1005*3c13e2acSDave Jiang static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
1006*3c13e2acSDave Jiang 		struct nd_intel_freeze_lock *nd_cmd,
1007*3c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
1008*3c13e2acSDave Jiang {
1009*3c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
1010*3c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1011*3c13e2acSDave Jiang 
1012*3c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
1013*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1014*3c13e2acSDave Jiang 		dev_dbg(dev, "freeze lock: wrong security state\n");
1015*3c13e2acSDave Jiang 	} else {
1016*3c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
1017*3c13e2acSDave Jiang 		nd_cmd->status = 0;
1018*3c13e2acSDave Jiang 		dev_dbg(dev, "security frozen\n");
1019*3c13e2acSDave Jiang 	}
1020*3c13e2acSDave Jiang 
1021*3c13e2acSDave Jiang 	return 0;
1022*3c13e2acSDave Jiang }
1023*3c13e2acSDave Jiang 
1024*3c13e2acSDave Jiang static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
1025*3c13e2acSDave Jiang 		struct nd_intel_disable_passphrase *nd_cmd,
1026*3c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
1027*3c13e2acSDave Jiang {
1028*3c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
1029*3c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1030*3c13e2acSDave Jiang 
1031*3c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
1032*3c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
1033*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1034*3c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong security state\n");
1035*3c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
1036*3c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1037*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1038*3c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
1039*3c13e2acSDave Jiang 	} else {
1040*3c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1041*3c13e2acSDave Jiang 		sec->state = 0;
1042*3c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: done\n");
1043*3c13e2acSDave Jiang 	}
1044*3c13e2acSDave Jiang 
1045*3c13e2acSDave Jiang 	return 0;
1046*3c13e2acSDave Jiang }
1047*3c13e2acSDave Jiang 
1048*3c13e2acSDave Jiang static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
1049*3c13e2acSDave Jiang 		struct nd_intel_secure_erase *nd_cmd,
1050*3c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
1051*3c13e2acSDave Jiang {
1052*3c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
1053*3c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1054*3c13e2acSDave Jiang 
1055*3c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
1056*3c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
1057*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1058*3c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong security state\n");
1059*3c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
1060*3c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1061*3c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1062*3c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong passphrase\n");
1063*3c13e2acSDave Jiang 	} else {
1064*3c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1065*3c13e2acSDave Jiang 		sec->state = 0;
1066*3c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: done\n");
1067*3c13e2acSDave Jiang 	}
1068*3c13e2acSDave Jiang 
1069*3c13e2acSDave Jiang 	return 0;
1070*3c13e2acSDave Jiang }
1071*3c13e2acSDave Jiang 
1072bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
1073bfbaa952SDave Jiang {
1074bfbaa952SDave Jiang 	int i;
1075bfbaa952SDave Jiang 
1076bfbaa952SDave Jiang 	/* lookup per-dimm data */
1077bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1078bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1079bfbaa952SDave Jiang 			break;
1080bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
1081bfbaa952SDave Jiang 		return -ENXIO;
1082bfbaa952SDave Jiang 	return i;
1083bfbaa952SDave Jiang }
1084bfbaa952SDave Jiang 
108539c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
108639c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1087aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
108839c686b8SVishal Verma {
108939c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
109039c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
10916634fb06SDan Williams 	unsigned int func = cmd;
1092f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1093f471f1a7SDan Williams 
1094f471f1a7SDan Williams 	if (!cmd_rc)
1095f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1096f471f1a7SDan Williams 	*cmd_rc = 0;
109739c686b8SVishal Verma 
109839c686b8SVishal Verma 	if (nvdimm) {
109939c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1100e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
110139c686b8SVishal Verma 
11026634fb06SDan Williams 		if (!nfit_mem)
11036634fb06SDan Williams 			return -ENOTTY;
11046634fb06SDan Williams 
11056634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
11066634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
11076634fb06SDan Williams 
11086634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
11096634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
11106634fb06SDan Williams 			func = call_pkg->nd_command;
11116634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
11126634fb06SDan Williams 				return -ENOTTY;
1113bfbaa952SDave Jiang 
1114bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1115bfbaa952SDave Jiang 			if (i < 0)
1116bfbaa952SDave Jiang 				return i;
1117bfbaa952SDave Jiang 
1118bfbaa952SDave Jiang 			switch (func) {
1119*3c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
1120*3c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
1121*3c13e2acSDave Jiang 						buf, buf_len, i);
1122*3c13e2acSDave Jiang 				break;
1123*3c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
1124*3c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
1125*3c13e2acSDave Jiang 						buf, buf_len, i);
1126*3c13e2acSDave Jiang 				break;
1127*3c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
1128*3c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
1129*3c13e2acSDave Jiang 						buf, buf_len, i);
1130*3c13e2acSDave Jiang 				break;
1131*3c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
1132*3c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
1133*3c13e2acSDave Jiang 						buf, buf_len, i);
1134*3c13e2acSDave Jiang 				break;
1135*3c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
1136*3c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
1137*3c13e2acSDave Jiang 						buf, buf_len, i);
1138*3c13e2acSDave Jiang 				break;
1139*3c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
1140*3c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
1141*3c13e2acSDave Jiang 						buf, buf_len, i);
1142*3c13e2acSDave Jiang 				break;
1143674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
114439611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1145674d8bdeSDave Jiang 						buf, buf_len);
114639611e83SDan Williams 				break;
1147bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
114839611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
1149bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
115039611e83SDan Williams 				break;
1151bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
115239611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
1153bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
115439611e83SDan Williams 				break;
1155bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
115639611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
1157bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
115839611e83SDan Williams 				break;
1159bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
116039611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
1161bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
116239611e83SDan Williams 				break;
1163bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
116439611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
1165bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
116639611e83SDan Williams 				break;
1167bfbaa952SDave Jiang 			case ND_INTEL_SMART:
116839611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
1169bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
117039611e83SDan Williams 				break;
1171bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
117239611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1173bfbaa952SDave Jiang 						buf_len,
1174bfbaa952SDave Jiang 						&t->smart_threshold[i -
1175bfbaa952SDave Jiang 							t->dcr_idx]);
117639611e83SDan Williams 				break;
1177bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
117839611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1179bfbaa952SDave Jiang 						buf_len,
1180bfbaa952SDave Jiang 						&t->smart_threshold[i -
1181bfbaa952SDave Jiang 							t->dcr_idx],
1182bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
1183bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
118439611e83SDan Williams 				break;
11854cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
118639611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
11874cf260fcSVishal Verma 						buf_len,
11884cf260fcSVishal Verma 						&t->smart_threshold[i -
11894cf260fcSVishal Verma 							t->dcr_idx],
11904cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
11914cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
119239611e83SDan Williams 				break;
1193bfbaa952SDave Jiang 			default:
1194bfbaa952SDave Jiang 				return -ENOTTY;
1195bfbaa952SDave Jiang 			}
119639611e83SDan Williams 			return override_return_code(i, func, rc);
11976634fb06SDan Williams 		}
11986634fb06SDan Williams 
11996634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
12006634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
120139c686b8SVishal Verma 			return -ENOTTY;
120239c686b8SVishal Verma 
1203bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1204bfbaa952SDave Jiang 		if (i < 0)
1205bfbaa952SDave Jiang 			return i;
120673606afdSDan Williams 
12076634fb06SDan Williams 		switch (func) {
120839c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
120939c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
121039c686b8SVishal Verma 			break;
121139c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
121239c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1213dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
121439c686b8SVishal Verma 			break;
121539c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
121639c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1217dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
121839c686b8SVishal Verma 			break;
12196bc75619SDan Williams 		default:
12206bc75619SDan Williams 			return -ENOTTY;
12216bc75619SDan Williams 		}
122239611e83SDan Williams 		return override_return_code(i, func, rc);
122339c686b8SVishal Verma 	} else {
1224f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
122510246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
122610246dc8SYasunori Goto 
122710246dc8SYasunori Goto 		if (!nd_desc)
122810246dc8SYasunori Goto 			return -ENOTTY;
122910246dc8SYasunori Goto 
123010246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
123110246dc8SYasunori Goto 			func = call_pkg->nd_command;
123210246dc8SYasunori Goto 
123310246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
123410246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
123510246dc8SYasunori Goto 
123610246dc8SYasunori Goto 			switch (func) {
123710246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
123810246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
123910246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
124010246dc8SYasunori Goto 				return rc;
12419fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
12429fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
12439fb1a190SDave Jiang 					buf_len);
12449fb1a190SDave Jiang 				return rc;
12459fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
12469fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
12479fb1a190SDave Jiang 					buf_len);
12489fb1a190SDave Jiang 				return rc;
12499fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
12509fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
12519fb1a190SDave Jiang 					buf_len);
12529fb1a190SDave Jiang 				return rc;
125310246dc8SYasunori Goto 			default:
125410246dc8SYasunori Goto 				return -ENOTTY;
125510246dc8SYasunori Goto 			}
125610246dc8SYasunori Goto 		}
1257f471f1a7SDan Williams 
1258e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
125939c686b8SVishal Verma 			return -ENOTTY;
126039c686b8SVishal Verma 
12616634fb06SDan Williams 		switch (func) {
126239c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
126339c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
126439c686b8SVishal Verma 			break;
126539c686b8SVishal Verma 		case ND_CMD_ARS_START:
12669fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
12679fb1a190SDave Jiang 					buf_len, cmd_rc);
126839c686b8SVishal Verma 			break;
126939c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1270f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1271f471f1a7SDan Williams 					cmd_rc);
127239c686b8SVishal Verma 			break;
1273d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
12745e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1275d4f32367SDan Williams 			break;
127639c686b8SVishal Verma 		default:
127739c686b8SVishal Verma 			return -ENOTTY;
127839c686b8SVishal Verma 		}
127939c686b8SVishal Verma 	}
12806bc75619SDan Williams 
12816bc75619SDan Williams 	return rc;
12826bc75619SDan Williams }
12836bc75619SDan Williams 
12846bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
12856bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
12866bc75619SDan Williams 
12876bc75619SDan Williams static void release_nfit_res(void *data)
12886bc75619SDan Williams {
12896bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
12906bc75619SDan Williams 
12916bc75619SDan Williams 	spin_lock(&nfit_test_lock);
12926bc75619SDan Williams 	list_del(&nfit_res->list);
12936bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
12946bc75619SDan Williams 
12956bc75619SDan Williams 	vfree(nfit_res->buf);
12966bc75619SDan Williams 	kfree(nfit_res);
12976bc75619SDan Williams }
12986bc75619SDan Williams 
12996bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
13006bc75619SDan Williams 		void *buf)
13016bc75619SDan Williams {
13026bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
13036bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
13046bc75619SDan Williams 			GFP_KERNEL);
13056bc75619SDan Williams 	int rc;
13066bc75619SDan Williams 
1307bd4cd745SDan Williams 	if (!buf || !nfit_res)
13086bc75619SDan Williams 		goto err;
13096bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
13106bc75619SDan Williams 	if (rc)
13116bc75619SDan Williams 		goto err;
13126bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
13136bc75619SDan Williams 	memset(buf, 0, size);
13146bc75619SDan Williams 	nfit_res->dev = dev;
13156bc75619SDan Williams 	nfit_res->buf = buf;
1316bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1317bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1318bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1319bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1320bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
13216bc75619SDan Williams 	spin_lock(&nfit_test_lock);
13226bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
13236bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
13246bc75619SDan Williams 
13256bc75619SDan Williams 	return nfit_res->buf;
13266bc75619SDan Williams  err:
1327ee8520feSDan Williams 	if (buf)
13286bc75619SDan Williams 		vfree(buf);
13296bc75619SDan Williams 	kfree(nfit_res);
13306bc75619SDan Williams 	return NULL;
13316bc75619SDan Williams }
13326bc75619SDan Williams 
13336bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
13346bc75619SDan Williams {
13356bc75619SDan Williams 	void *buf = vmalloc(size);
13366bc75619SDan Williams 
13376bc75619SDan Williams 	*dma = (unsigned long) buf;
13386bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
13396bc75619SDan Williams }
13406bc75619SDan Williams 
13416bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
13426bc75619SDan Williams {
13436bc75619SDan Williams 	int i;
13446bc75619SDan Williams 
13456bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
13466bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
13476bc75619SDan Williams 		struct nfit_test *t = instances[i];
13486bc75619SDan Williams 
13496bc75619SDan Williams 		if (!t)
13506bc75619SDan Williams 			continue;
13516bc75619SDan Williams 		spin_lock(&nfit_test_lock);
13526bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1353bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1354bd4cd745SDan Williams 						+ resource_size(&n->res))) {
13556bc75619SDan Williams 				nfit_res = n;
13566bc75619SDan Williams 				break;
13576bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
13586bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1359bd4cd745SDan Williams 						+ resource_size(&n->res))) {
13606bc75619SDan Williams 				nfit_res = n;
13616bc75619SDan Williams 				break;
13626bc75619SDan Williams 			}
13636bc75619SDan Williams 		}
13646bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
13656bc75619SDan Williams 		if (nfit_res)
13666bc75619SDan Williams 			return nfit_res;
13676bc75619SDan Williams 	}
13686bc75619SDan Williams 
13696bc75619SDan Williams 	return NULL;
13706bc75619SDan Williams }
13716bc75619SDan Williams 
1372f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1373f471f1a7SDan Williams {
13749fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1375f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
13769fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1377f471f1a7SDan Williams 	if (!ars_state->ars_status)
1378f471f1a7SDan Williams 		return -ENOMEM;
1379f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1380f471f1a7SDan Williams 	return 0;
1381f471f1a7SDan Williams }
1382f471f1a7SDan Williams 
1383231bf117SDan Williams static void put_dimms(void *data)
1384231bf117SDan Williams {
1385718fda67SDan Williams 	struct nfit_test *t = data;
1386231bf117SDan Williams 	int i;
1387231bf117SDan Williams 
1388718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1389718fda67SDan Williams 		if (t->dimm_dev[i])
1390718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1391231bf117SDan Williams }
1392231bf117SDan Williams 
1393231bf117SDan Williams static struct class *nfit_test_dimm;
1394231bf117SDan Williams 
139573606afdSDan Williams static int dimm_name_to_id(struct device *dev)
139673606afdSDan Williams {
139773606afdSDan Williams 	int dimm;
139873606afdSDan Williams 
1399718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
140073606afdSDan Williams 		return -ENXIO;
140173606afdSDan Williams 	return dimm;
140273606afdSDan Williams }
140373606afdSDan Williams 
140473606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
140573606afdSDan Williams 		char *buf)
140673606afdSDan Williams {
140773606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
140873606afdSDan Williams 
140973606afdSDan Williams 	if (dimm < 0)
141073606afdSDan Williams 		return dimm;
141173606afdSDan Williams 
141219357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
141373606afdSDan Williams }
141473606afdSDan Williams DEVICE_ATTR_RO(handle);
141573606afdSDan Williams 
141673606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
141773606afdSDan Williams 		char *buf)
141873606afdSDan Williams {
141973606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
142073606afdSDan Williams 
142173606afdSDan Williams 	if (dimm < 0)
142273606afdSDan Williams 		return dimm;
142373606afdSDan Williams 
142473606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
142573606afdSDan Williams }
142673606afdSDan Williams 
142773606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
142873606afdSDan Williams 		const char *buf, size_t size)
142973606afdSDan Williams {
143073606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
143173606afdSDan Williams 	unsigned long val;
143273606afdSDan Williams 	ssize_t rc;
143373606afdSDan Williams 
143473606afdSDan Williams 	if (dimm < 0)
143573606afdSDan Williams 		return dimm;
143673606afdSDan Williams 
143773606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
143873606afdSDan Williams 	if (rc)
143973606afdSDan Williams 		return rc;
144073606afdSDan Williams 
144173606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
144273606afdSDan Williams 	return size;
144373606afdSDan Williams }
144473606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
144573606afdSDan Williams 
144655c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
144755c72ab6SDan Williams 		char *buf)
144855c72ab6SDan Williams {
144955c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
145055c72ab6SDan Williams 
145155c72ab6SDan Williams 	if (dimm < 0)
145255c72ab6SDan Williams 		return dimm;
145355c72ab6SDan Williams 
145455c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
145555c72ab6SDan Williams }
145655c72ab6SDan Williams 
145755c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
145855c72ab6SDan Williams 		const char *buf, size_t size)
145955c72ab6SDan Williams {
146055c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
146155c72ab6SDan Williams 	unsigned long val;
146255c72ab6SDan Williams 	ssize_t rc;
146355c72ab6SDan Williams 
146455c72ab6SDan Williams 	if (dimm < 0)
146555c72ab6SDan Williams 		return dimm;
146655c72ab6SDan Williams 
146755c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
146855c72ab6SDan Williams 	if (rc)
146955c72ab6SDan Williams 		return rc;
147055c72ab6SDan Williams 
147155c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
147255c72ab6SDan Williams 	return size;
147355c72ab6SDan Williams }
147455c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
147555c72ab6SDan Williams 
1476*3c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
1477*3c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
1478*3c13e2acSDave Jiang {
1479*3c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
1480*3c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1481*3c13e2acSDave Jiang 
1482*3c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
1483*3c13e2acSDave Jiang 	return size;
1484*3c13e2acSDave Jiang }
1485*3c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
1486*3c13e2acSDave Jiang 
148773606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
148873606afdSDan Williams 	&dev_attr_fail_cmd.attr,
148955c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
149073606afdSDan Williams 	&dev_attr_handle.attr,
1491*3c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
149273606afdSDan Williams 	NULL,
149373606afdSDan Williams };
149473606afdSDan Williams 
149573606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
149673606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
149773606afdSDan Williams };
149873606afdSDan Williams 
149973606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
150073606afdSDan Williams 	&nfit_test_dimm_attribute_group,
150173606afdSDan Williams 	NULL,
150273606afdSDan Williams };
150373606afdSDan Williams 
1504718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1505718fda67SDan Williams {
1506718fda67SDan Williams 	int i;
1507718fda67SDan Williams 
1508718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1509718fda67SDan Williams 		return -ENOMEM;
1510718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1511718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1512718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1513718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1514718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1515718fda67SDan Williams 		if (!t->dimm_dev[i])
1516718fda67SDan Williams 			return -ENOMEM;
1517718fda67SDan Williams 	}
1518718fda67SDan Williams 	return 0;
1519718fda67SDan Williams }
1520718fda67SDan Williams 
1521ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1522ed07c433SDan Williams {
1523ed07c433SDan Williams 	int i;
1524ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1525ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1526ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1527ed07c433SDan Williams 		.media_temperature = 40 * 16,
1528ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1529ed07c433SDan Williams 		.spares = 5,
1530ed07c433SDan Williams 	};
1531ed07c433SDan Williams 
1532ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1533b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1534ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1535ed07c433SDan Williams 				sizeof(smart_t_data));
1536ed07c433SDan Williams 	}
1537ed07c433SDan Williams }
1538ed07c433SDan Williams 
15396bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
15406bc75619SDan Williams {
15416b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
15426bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
15436bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
15443b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
15453b87356fSDan Williams 					window_size) * NUM_DCR
15469d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
154785d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1548f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1549f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
15506bc75619SDan Williams 	int i;
15516bc75619SDan Williams 
15526bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
15536bc75619SDan Williams 	if (!t->nfit_buf)
15546bc75619SDan Williams 		return -ENOMEM;
15556bc75619SDan Williams 	t->nfit_size = nfit_size;
15566bc75619SDan Williams 
1557ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
15586bc75619SDan Williams 	if (!t->spa_set[0])
15596bc75619SDan Williams 		return -ENOMEM;
15606bc75619SDan Williams 
1561ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
15626bc75619SDan Williams 	if (!t->spa_set[1])
15636bc75619SDan Williams 		return -ENOMEM;
15646bc75619SDan Williams 
1565ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
156620985164SVishal Verma 	if (!t->spa_set[2])
156720985164SVishal Verma 		return -ENOMEM;
156820985164SVishal Verma 
1569dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
15706bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
15716bc75619SDan Williams 		if (!t->dimm[i])
15726bc75619SDan Williams 			return -ENOMEM;
15736bc75619SDan Williams 
15746bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
15756bc75619SDan Williams 		if (!t->label[i])
15766bc75619SDan Williams 			return -ENOMEM;
15776bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
15789d27a87eSDan Williams 
15799d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
15809d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
158185d3fa02SDan Williams 				&t->flush_dma[i]);
15829d27a87eSDan Williams 		if (!t->flush[i])
15839d27a87eSDan Williams 			return -ENOMEM;
15846bc75619SDan Williams 	}
15856bc75619SDan Williams 
1586dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
15876bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
15886bc75619SDan Williams 		if (!t->dcr[i])
15896bc75619SDan Williams 			return -ENOMEM;
15906bc75619SDan Williams 	}
15916bc75619SDan Williams 
1592c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1593c14a868aSDan Williams 	if (!t->_fit)
1594c14a868aSDan Williams 		return -ENOMEM;
1595c14a868aSDan Williams 
1596718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1597231bf117SDan Williams 		return -ENOMEM;
1598ed07c433SDan Williams 	smart_init(t);
1599f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
16006bc75619SDan Williams }
16016bc75619SDan Williams 
16026bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
16036bc75619SDan Williams {
16047bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1605ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1606ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1607dafb1048SDan Williams 	int i;
16086bc75619SDan Williams 
16096bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
16106bc75619SDan Williams 	if (!t->nfit_buf)
16116bc75619SDan Williams 		return -ENOMEM;
16126bc75619SDan Williams 	t->nfit_size = nfit_size;
16136bc75619SDan Williams 
1614ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
16156bc75619SDan Williams 	if (!t->spa_set[0])
16166bc75619SDan Williams 		return -ENOMEM;
16176bc75619SDan Williams 
1618dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1619dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1620dafb1048SDan Williams 		if (!t->label[i])
1621dafb1048SDan Williams 			return -ENOMEM;
1622dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1623dafb1048SDan Williams 	}
1624dafb1048SDan Williams 
16257bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
16267bfe97c7SDan Williams 	if (!t->spa_set[1])
16277bfe97c7SDan Williams 		return -ENOMEM;
16287bfe97c7SDan Williams 
1629718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1630718fda67SDan Williams 		return -ENOMEM;
1631ed07c433SDan Williams 	smart_init(t);
1632f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
16336bc75619SDan Williams }
16346bc75619SDan Williams 
16355dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
16365dc68e55SDan Williams {
16375dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
16385dc68e55SDan Williams 	dcr->device_id = 0;
16395dc68e55SDan Williams 	dcr->revision_id = 1;
16405dc68e55SDan Williams 	dcr->valid_fields = 1;
16415dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
16425dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
16435dc68e55SDan Williams }
16445dc68e55SDan Williams 
16456bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
16466bc75619SDan Williams {
164785d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
164885d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
16496bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
16506bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
16516bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
16526bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
16536bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
16546bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
16559d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1656f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1657d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
16586bc75619SDan Williams 
16596bc75619SDan Williams 	/*
16606bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
16616bc75619SDan Williams 	 * does not actually alias the related block-data-window
16626bc75619SDan Williams 	 * regions)
16636bc75619SDan Williams 	 */
16646b577c9dSLinda Knippers 	spa = nfit_buf;
16656bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
16666bc75619SDan Williams 	spa->header.length = sizeof(*spa);
16676bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
16686bc75619SDan Williams 	spa->range_index = 0+1;
16696bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
16706bc75619SDan Williams 	spa->length = SPA0_SIZE;
1671d7d8464dSRoss Zwisler 	offset += spa->header.length;
16726bc75619SDan Williams 
16736bc75619SDan Williams 	/*
16746bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
16756bc75619SDan Williams 	 * does not actually alias the related block-data-window
16766bc75619SDan Williams 	 * regions)
16776bc75619SDan Williams 	 */
1678d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
16796bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
16806bc75619SDan Williams 	spa->header.length = sizeof(*spa);
16816bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
16826bc75619SDan Williams 	spa->range_index = 1+1;
16836bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
16846bc75619SDan Williams 	spa->length = SPA1_SIZE;
1685d7d8464dSRoss Zwisler 	offset += spa->header.length;
16866bc75619SDan Williams 
16876bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1688d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
16896bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
16906bc75619SDan Williams 	spa->header.length = sizeof(*spa);
16916bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
16926bc75619SDan Williams 	spa->range_index = 2+1;
16936bc75619SDan Williams 	spa->address = t->dcr_dma[0];
16946bc75619SDan Williams 	spa->length = DCR_SIZE;
1695d7d8464dSRoss Zwisler 	offset += spa->header.length;
16966bc75619SDan Williams 
16976bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1698d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
16996bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
17006bc75619SDan Williams 	spa->header.length = sizeof(*spa);
17016bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
17026bc75619SDan Williams 	spa->range_index = 3+1;
17036bc75619SDan Williams 	spa->address = t->dcr_dma[1];
17046bc75619SDan Williams 	spa->length = DCR_SIZE;
1705d7d8464dSRoss Zwisler 	offset += spa->header.length;
17066bc75619SDan Williams 
17076bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1708d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
17096bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
17106bc75619SDan Williams 	spa->header.length = sizeof(*spa);
17116bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
17126bc75619SDan Williams 	spa->range_index = 4+1;
17136bc75619SDan Williams 	spa->address = t->dcr_dma[2];
17146bc75619SDan Williams 	spa->length = DCR_SIZE;
1715d7d8464dSRoss Zwisler 	offset += spa->header.length;
17166bc75619SDan Williams 
17176bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1718d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
17196bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
17206bc75619SDan Williams 	spa->header.length = sizeof(*spa);
17216bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
17226bc75619SDan Williams 	spa->range_index = 5+1;
17236bc75619SDan Williams 	spa->address = t->dcr_dma[3];
17246bc75619SDan Williams 	spa->length = DCR_SIZE;
1725d7d8464dSRoss Zwisler 	offset += spa->header.length;
17266bc75619SDan Williams 
17276bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1728d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
17296bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
17306bc75619SDan Williams 	spa->header.length = sizeof(*spa);
17316bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
17326bc75619SDan Williams 	spa->range_index = 6+1;
17336bc75619SDan Williams 	spa->address = t->dimm_dma[0];
17346bc75619SDan Williams 	spa->length = DIMM_SIZE;
1735d7d8464dSRoss Zwisler 	offset += spa->header.length;
17366bc75619SDan Williams 
17376bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1738d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
17396bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
17406bc75619SDan Williams 	spa->header.length = sizeof(*spa);
17416bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
17426bc75619SDan Williams 	spa->range_index = 7+1;
17436bc75619SDan Williams 	spa->address = t->dimm_dma[1];
17446bc75619SDan Williams 	spa->length = DIMM_SIZE;
1745d7d8464dSRoss Zwisler 	offset += spa->header.length;
17466bc75619SDan Williams 
17476bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1748d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
17496bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
17506bc75619SDan Williams 	spa->header.length = sizeof(*spa);
17516bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
17526bc75619SDan Williams 	spa->range_index = 8+1;
17536bc75619SDan Williams 	spa->address = t->dimm_dma[2];
17546bc75619SDan Williams 	spa->length = DIMM_SIZE;
1755d7d8464dSRoss Zwisler 	offset += spa->header.length;
17566bc75619SDan Williams 
17576bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1758d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
17596bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
17606bc75619SDan Williams 	spa->header.length = sizeof(*spa);
17616bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
17626bc75619SDan Williams 	spa->range_index = 9+1;
17636bc75619SDan Williams 	spa->address = t->dimm_dma[3];
17646bc75619SDan Williams 	spa->length = DIMM_SIZE;
1765d7d8464dSRoss Zwisler 	offset += spa->header.length;
17666bc75619SDan Williams 
17676bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
17686bc75619SDan Williams 	memdev = nfit_buf + offset;
17696bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17706bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17716bc75619SDan Williams 	memdev->device_handle = handle[0];
17726bc75619SDan Williams 	memdev->physical_id = 0;
17736bc75619SDan Williams 	memdev->region_id = 0;
17746bc75619SDan Williams 	memdev->range_index = 0+1;
17753b87356fSDan Williams 	memdev->region_index = 4+1;
17766bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1777df06a2d5SDan Williams 	memdev->region_offset = 1;
17786bc75619SDan Williams 	memdev->address = 0;
17796bc75619SDan Williams 	memdev->interleave_index = 0;
17806bc75619SDan Williams 	memdev->interleave_ways = 2;
1781d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17826bc75619SDan Williams 
17836bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1784d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17856bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17866bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17876bc75619SDan Williams 	memdev->device_handle = handle[1];
17886bc75619SDan Williams 	memdev->physical_id = 1;
17896bc75619SDan Williams 	memdev->region_id = 0;
17906bc75619SDan Williams 	memdev->range_index = 0+1;
17913b87356fSDan Williams 	memdev->region_index = 5+1;
17926bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1793df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
17946bc75619SDan Williams 	memdev->address = 0;
17956bc75619SDan Williams 	memdev->interleave_index = 0;
17966bc75619SDan Williams 	memdev->interleave_ways = 2;
1797ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1798d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17996bc75619SDan Williams 
18006bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1801d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
18026bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
18036bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
18046bc75619SDan Williams 	memdev->device_handle = handle[0];
18056bc75619SDan Williams 	memdev->physical_id = 0;
18066bc75619SDan Williams 	memdev->region_id = 1;
18076bc75619SDan Williams 	memdev->range_index = 1+1;
18083b87356fSDan Williams 	memdev->region_index = 4+1;
18096bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1810df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
18116bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
18126bc75619SDan Williams 	memdev->interleave_index = 0;
18136bc75619SDan Williams 	memdev->interleave_ways = 4;
1814ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1815d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18166bc75619SDan Williams 
18176bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1818d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
18196bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
18206bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
18216bc75619SDan Williams 	memdev->device_handle = handle[1];
18226bc75619SDan Williams 	memdev->physical_id = 1;
18236bc75619SDan Williams 	memdev->region_id = 1;
18246bc75619SDan Williams 	memdev->range_index = 1+1;
18253b87356fSDan Williams 	memdev->region_index = 5+1;
18266bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1827df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
18286bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
18296bc75619SDan Williams 	memdev->interleave_index = 0;
18306bc75619SDan Williams 	memdev->interleave_ways = 4;
1831d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18326bc75619SDan Williams 
18336bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1834d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
18356bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
18366bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
18376bc75619SDan Williams 	memdev->device_handle = handle[2];
18386bc75619SDan Williams 	memdev->physical_id = 2;
18396bc75619SDan Williams 	memdev->region_id = 0;
18406bc75619SDan Williams 	memdev->range_index = 1+1;
18413b87356fSDan Williams 	memdev->region_index = 6+1;
18426bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1843df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
18446bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
18456bc75619SDan Williams 	memdev->interleave_index = 0;
18466bc75619SDan Williams 	memdev->interleave_ways = 4;
1847ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1848d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18496bc75619SDan Williams 
18506bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
1851d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
18526bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
18536bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
18546bc75619SDan Williams 	memdev->device_handle = handle[3];
18556bc75619SDan Williams 	memdev->physical_id = 3;
18566bc75619SDan Williams 	memdev->region_id = 0;
18576bc75619SDan Williams 	memdev->range_index = 1+1;
18583b87356fSDan Williams 	memdev->region_index = 7+1;
18596bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1860df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
18616bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
18626bc75619SDan Williams 	memdev->interleave_index = 0;
18636bc75619SDan Williams 	memdev->interleave_ways = 4;
1864d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18656bc75619SDan Williams 
18666bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
1867d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
18686bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
18696bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
18706bc75619SDan Williams 	memdev->device_handle = handle[0];
18716bc75619SDan Williams 	memdev->physical_id = 0;
18726bc75619SDan Williams 	memdev->region_id = 0;
18736bc75619SDan Williams 	memdev->range_index = 2+1;
18746bc75619SDan Williams 	memdev->region_index = 0+1;
18756bc75619SDan Williams 	memdev->region_size = 0;
18766bc75619SDan Williams 	memdev->region_offset = 0;
18776bc75619SDan Williams 	memdev->address = 0;
18786bc75619SDan Williams 	memdev->interleave_index = 0;
18796bc75619SDan Williams 	memdev->interleave_ways = 1;
1880d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18816bc75619SDan Williams 
18826bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
1883d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
18846bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
18856bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
18866bc75619SDan Williams 	memdev->device_handle = handle[1];
18876bc75619SDan Williams 	memdev->physical_id = 1;
18886bc75619SDan Williams 	memdev->region_id = 0;
18896bc75619SDan Williams 	memdev->range_index = 3+1;
18906bc75619SDan Williams 	memdev->region_index = 1+1;
18916bc75619SDan Williams 	memdev->region_size = 0;
18926bc75619SDan Williams 	memdev->region_offset = 0;
18936bc75619SDan Williams 	memdev->address = 0;
18946bc75619SDan Williams 	memdev->interleave_index = 0;
18956bc75619SDan Williams 	memdev->interleave_ways = 1;
1896d7d8464dSRoss Zwisler 	offset += memdev->header.length;
18976bc75619SDan Williams 
18986bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
1899d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19006bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19016bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19026bc75619SDan Williams 	memdev->device_handle = handle[2];
19036bc75619SDan Williams 	memdev->physical_id = 2;
19046bc75619SDan Williams 	memdev->region_id = 0;
19056bc75619SDan Williams 	memdev->range_index = 4+1;
19066bc75619SDan Williams 	memdev->region_index = 2+1;
19076bc75619SDan Williams 	memdev->region_size = 0;
19086bc75619SDan Williams 	memdev->region_offset = 0;
19096bc75619SDan Williams 	memdev->address = 0;
19106bc75619SDan Williams 	memdev->interleave_index = 0;
19116bc75619SDan Williams 	memdev->interleave_ways = 1;
1912d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19136bc75619SDan Williams 
19146bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
1915d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19166bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19176bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19186bc75619SDan Williams 	memdev->device_handle = handle[3];
19196bc75619SDan Williams 	memdev->physical_id = 3;
19206bc75619SDan Williams 	memdev->region_id = 0;
19216bc75619SDan Williams 	memdev->range_index = 5+1;
19226bc75619SDan Williams 	memdev->region_index = 3+1;
19236bc75619SDan Williams 	memdev->region_size = 0;
19246bc75619SDan Williams 	memdev->region_offset = 0;
19256bc75619SDan Williams 	memdev->address = 0;
19266bc75619SDan Williams 	memdev->interleave_index = 0;
19276bc75619SDan Williams 	memdev->interleave_ways = 1;
1928d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19296bc75619SDan Williams 
19306bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
1931d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19326bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19336bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19346bc75619SDan Williams 	memdev->device_handle = handle[0];
19356bc75619SDan Williams 	memdev->physical_id = 0;
19366bc75619SDan Williams 	memdev->region_id = 0;
19376bc75619SDan Williams 	memdev->range_index = 6+1;
19386bc75619SDan Williams 	memdev->region_index = 0+1;
19396bc75619SDan Williams 	memdev->region_size = 0;
19406bc75619SDan Williams 	memdev->region_offset = 0;
19416bc75619SDan Williams 	memdev->address = 0;
19426bc75619SDan Williams 	memdev->interleave_index = 0;
19436bc75619SDan Williams 	memdev->interleave_ways = 1;
1944d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19456bc75619SDan Williams 
19466bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
1947d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19486bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19496bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19506bc75619SDan Williams 	memdev->device_handle = handle[1];
19516bc75619SDan Williams 	memdev->physical_id = 1;
19526bc75619SDan Williams 	memdev->region_id = 0;
19536bc75619SDan Williams 	memdev->range_index = 7+1;
19546bc75619SDan Williams 	memdev->region_index = 1+1;
19556bc75619SDan Williams 	memdev->region_size = 0;
19566bc75619SDan Williams 	memdev->region_offset = 0;
19576bc75619SDan Williams 	memdev->address = 0;
19586bc75619SDan Williams 	memdev->interleave_index = 0;
19596bc75619SDan Williams 	memdev->interleave_ways = 1;
1960d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19616bc75619SDan Williams 
19626bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
1963d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19646bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19656bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19666bc75619SDan Williams 	memdev->device_handle = handle[2];
19676bc75619SDan Williams 	memdev->physical_id = 2;
19686bc75619SDan Williams 	memdev->region_id = 0;
19696bc75619SDan Williams 	memdev->range_index = 8+1;
19706bc75619SDan Williams 	memdev->region_index = 2+1;
19716bc75619SDan Williams 	memdev->region_size = 0;
19726bc75619SDan Williams 	memdev->region_offset = 0;
19736bc75619SDan Williams 	memdev->address = 0;
19746bc75619SDan Williams 	memdev->interleave_index = 0;
19756bc75619SDan Williams 	memdev->interleave_ways = 1;
1976d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19776bc75619SDan Williams 
19786bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
1979d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19806bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19816bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19826bc75619SDan Williams 	memdev->device_handle = handle[3];
19836bc75619SDan Williams 	memdev->physical_id = 3;
19846bc75619SDan Williams 	memdev->region_id = 0;
19856bc75619SDan Williams 	memdev->range_index = 9+1;
19866bc75619SDan Williams 	memdev->region_index = 3+1;
19876bc75619SDan Williams 	memdev->region_size = 0;
19886bc75619SDan Williams 	memdev->region_offset = 0;
19896bc75619SDan Williams 	memdev->address = 0;
19906bc75619SDan Williams 	memdev->interleave_index = 0;
19916bc75619SDan Williams 	memdev->interleave_ways = 1;
1992ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1993d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19946bc75619SDan Williams 
19953b87356fSDan Williams 	/* dcr-descriptor0: blk */
19966bc75619SDan Williams 	dcr = nfit_buf + offset;
19976bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1998d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
19996bc75619SDan Williams 	dcr->region_index = 0+1;
20005dc68e55SDan Williams 	dcr_common_init(dcr);
20016bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2002be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
20036bc75619SDan Williams 	dcr->windows = 1;
20046bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
20056bc75619SDan Williams 	dcr->command_offset = 0;
20066bc75619SDan Williams 	dcr->command_size = 8;
20076bc75619SDan Williams 	dcr->status_offset = 8;
20086bc75619SDan Williams 	dcr->status_size = 4;
2009d7d8464dSRoss Zwisler 	offset += dcr->header.length;
20106bc75619SDan Williams 
20113b87356fSDan Williams 	/* dcr-descriptor1: blk */
2012d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
20136bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2014d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
20156bc75619SDan Williams 	dcr->region_index = 1+1;
20165dc68e55SDan Williams 	dcr_common_init(dcr);
20176bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2018be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
20196bc75619SDan Williams 	dcr->windows = 1;
20206bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
20216bc75619SDan Williams 	dcr->command_offset = 0;
20226bc75619SDan Williams 	dcr->command_size = 8;
20236bc75619SDan Williams 	dcr->status_offset = 8;
20246bc75619SDan Williams 	dcr->status_size = 4;
2025d7d8464dSRoss Zwisler 	offset += dcr->header.length;
20266bc75619SDan Williams 
20273b87356fSDan Williams 	/* dcr-descriptor2: blk */
2028d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
20296bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2030d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
20316bc75619SDan Williams 	dcr->region_index = 2+1;
20325dc68e55SDan Williams 	dcr_common_init(dcr);
20336bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2034be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
20356bc75619SDan Williams 	dcr->windows = 1;
20366bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
20376bc75619SDan Williams 	dcr->command_offset = 0;
20386bc75619SDan Williams 	dcr->command_size = 8;
20396bc75619SDan Williams 	dcr->status_offset = 8;
20406bc75619SDan Williams 	dcr->status_size = 4;
2041d7d8464dSRoss Zwisler 	offset += dcr->header.length;
20426bc75619SDan Williams 
20433b87356fSDan Williams 	/* dcr-descriptor3: blk */
2044d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
20456bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2046d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
20476bc75619SDan Williams 	dcr->region_index = 3+1;
20485dc68e55SDan Williams 	dcr_common_init(dcr);
20496bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2050be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
20516bc75619SDan Williams 	dcr->windows = 1;
20526bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
20536bc75619SDan Williams 	dcr->command_offset = 0;
20546bc75619SDan Williams 	dcr->command_size = 8;
20556bc75619SDan Williams 	dcr->status_offset = 8;
20566bc75619SDan Williams 	dcr->status_size = 4;
2057d7d8464dSRoss Zwisler 	offset += dcr->header.length;
20586bc75619SDan Williams 
20593b87356fSDan Williams 	/* dcr-descriptor0: pmem */
20603b87356fSDan Williams 	dcr = nfit_buf + offset;
20613b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20623b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
20633b87356fSDan Williams 			window_size);
20643b87356fSDan Williams 	dcr->region_index = 4+1;
20655dc68e55SDan Williams 	dcr_common_init(dcr);
20663b87356fSDan Williams 	dcr->serial_number = ~handle[0];
20673b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
20683b87356fSDan Williams 	dcr->windows = 0;
2069d7d8464dSRoss Zwisler 	offset += dcr->header.length;
20703b87356fSDan Williams 
20713b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2072d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
20733b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20743b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
20753b87356fSDan Williams 			window_size);
20763b87356fSDan Williams 	dcr->region_index = 5+1;
20775dc68e55SDan Williams 	dcr_common_init(dcr);
20783b87356fSDan Williams 	dcr->serial_number = ~handle[1];
20793b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
20803b87356fSDan Williams 	dcr->windows = 0;
2081d7d8464dSRoss Zwisler 	offset += dcr->header.length;
20823b87356fSDan Williams 
20833b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2084d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
20853b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20863b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
20873b87356fSDan Williams 			window_size);
20883b87356fSDan Williams 	dcr->region_index = 6+1;
20895dc68e55SDan Williams 	dcr_common_init(dcr);
20903b87356fSDan Williams 	dcr->serial_number = ~handle[2];
20913b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
20923b87356fSDan Williams 	dcr->windows = 0;
2093d7d8464dSRoss Zwisler 	offset += dcr->header.length;
20943b87356fSDan Williams 
20953b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2096d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
20973b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20983b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
20993b87356fSDan Williams 			window_size);
21003b87356fSDan Williams 	dcr->region_index = 7+1;
21015dc68e55SDan Williams 	dcr_common_init(dcr);
21023b87356fSDan Williams 	dcr->serial_number = ~handle[3];
21033b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
21043b87356fSDan Williams 	dcr->windows = 0;
2105d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21063b87356fSDan Williams 
21076bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
21086bc75619SDan Williams 	bdw = nfit_buf + offset;
21096bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2110d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
21116bc75619SDan Williams 	bdw->region_index = 0+1;
21126bc75619SDan Williams 	bdw->windows = 1;
21136bc75619SDan Williams 	bdw->offset = 0;
21146bc75619SDan Williams 	bdw->size = BDW_SIZE;
21156bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
21166bc75619SDan Williams 	bdw->start_address = 0;
2117d7d8464dSRoss Zwisler 	offset += bdw->header.length;
21186bc75619SDan Williams 
21196bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2120d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
21216bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2122d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
21236bc75619SDan Williams 	bdw->region_index = 1+1;
21246bc75619SDan Williams 	bdw->windows = 1;
21256bc75619SDan Williams 	bdw->offset = 0;
21266bc75619SDan Williams 	bdw->size = BDW_SIZE;
21276bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
21286bc75619SDan Williams 	bdw->start_address = 0;
2129d7d8464dSRoss Zwisler 	offset += bdw->header.length;
21306bc75619SDan Williams 
21316bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2132d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
21336bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2134d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
21356bc75619SDan Williams 	bdw->region_index = 2+1;
21366bc75619SDan Williams 	bdw->windows = 1;
21376bc75619SDan Williams 	bdw->offset = 0;
21386bc75619SDan Williams 	bdw->size = BDW_SIZE;
21396bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
21406bc75619SDan Williams 	bdw->start_address = 0;
2141d7d8464dSRoss Zwisler 	offset += bdw->header.length;
21426bc75619SDan Williams 
21436bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2144d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
21456bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2146d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
21476bc75619SDan Williams 	bdw->region_index = 3+1;
21486bc75619SDan Williams 	bdw->windows = 1;
21496bc75619SDan Williams 	bdw->offset = 0;
21506bc75619SDan Williams 	bdw->size = BDW_SIZE;
21516bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
21526bc75619SDan Williams 	bdw->start_address = 0;
2153d7d8464dSRoss Zwisler 	offset += bdw->header.length;
21546bc75619SDan Williams 
21559d27a87eSDan Williams 	/* flush0 (dimm0) */
21569d27a87eSDan Williams 	flush = nfit_buf + offset;
21579d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
215885d3fa02SDan Williams 	flush->header.length = flush_hint_size;
21599d27a87eSDan Williams 	flush->device_handle = handle[0];
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[0] + i * sizeof(u64);
2163d7d8464dSRoss Zwisler 	offset += flush->header.length;
21649d27a87eSDan Williams 
21659d27a87eSDan Williams 	/* flush1 (dimm1) */
2166d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
21679d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
216885d3fa02SDan Williams 	flush->header.length = flush_hint_size;
21699d27a87eSDan Williams 	flush->device_handle = handle[1];
217085d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
217185d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
217285d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2173d7d8464dSRoss Zwisler 	offset += flush->header.length;
21749d27a87eSDan Williams 
21759d27a87eSDan Williams 	/* flush2 (dimm2) */
2176d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
21779d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
217885d3fa02SDan Williams 	flush->header.length = flush_hint_size;
21799d27a87eSDan Williams 	flush->device_handle = handle[2];
218085d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
218185d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
218285d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2183d7d8464dSRoss Zwisler 	offset += flush->header.length;
21849d27a87eSDan Williams 
21859d27a87eSDan Williams 	/* flush3 (dimm3) */
2186d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
21879d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
218885d3fa02SDan Williams 	flush->header.length = flush_hint_size;
21899d27a87eSDan Williams 	flush->device_handle = handle[3];
219085d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
219185d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
219285d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2193d7d8464dSRoss Zwisler 	offset += flush->header.length;
21949d27a87eSDan Williams 
2195f81e1d35SDave Jiang 	/* platform capabilities */
2196d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2197f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2198f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2199f81e1d35SDave Jiang 	pcap->highest_capability = 1;
22001273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2201d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2202f81e1d35SDave Jiang 
220320985164SVishal Verma 	if (t->setup_hotplug) {
22043b87356fSDan Williams 		/* dcr-descriptor4: blk */
220520985164SVishal Verma 		dcr = nfit_buf + offset;
220620985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2207d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
22083b87356fSDan Williams 		dcr->region_index = 8+1;
22095dc68e55SDan Williams 		dcr_common_init(dcr);
221020985164SVishal Verma 		dcr->serial_number = ~handle[4];
2211be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
221220985164SVishal Verma 		dcr->windows = 1;
221320985164SVishal Verma 		dcr->window_size = DCR_SIZE;
221420985164SVishal Verma 		dcr->command_offset = 0;
221520985164SVishal Verma 		dcr->command_size = 8;
221620985164SVishal Verma 		dcr->status_offset = 8;
221720985164SVishal Verma 		dcr->status_size = 4;
2218d7d8464dSRoss Zwisler 		offset += dcr->header.length;
221920985164SVishal Verma 
22203b87356fSDan Williams 		/* dcr-descriptor4: pmem */
22213b87356fSDan Williams 		dcr = nfit_buf + offset;
22223b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22233b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
22243b87356fSDan Williams 				window_size);
22253b87356fSDan Williams 		dcr->region_index = 9+1;
22265dc68e55SDan Williams 		dcr_common_init(dcr);
22273b87356fSDan Williams 		dcr->serial_number = ~handle[4];
22283b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
22293b87356fSDan Williams 		dcr->windows = 0;
2230d7d8464dSRoss Zwisler 		offset += dcr->header.length;
22313b87356fSDan Williams 
223220985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
223320985164SVishal Verma 		bdw = nfit_buf + offset;
223420985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2235d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
22363b87356fSDan Williams 		bdw->region_index = 8+1;
223720985164SVishal Verma 		bdw->windows = 1;
223820985164SVishal Verma 		bdw->offset = 0;
223920985164SVishal Verma 		bdw->size = BDW_SIZE;
224020985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
224120985164SVishal Verma 		bdw->start_address = 0;
2242d7d8464dSRoss Zwisler 		offset += bdw->header.length;
224320985164SVishal Verma 
224420985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
224520985164SVishal Verma 		spa = nfit_buf + offset;
224620985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
224720985164SVishal Verma 		spa->header.length = sizeof(*spa);
224820985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
224920985164SVishal Verma 		spa->range_index = 10+1;
225020985164SVishal Verma 		spa->address = t->dcr_dma[4];
225120985164SVishal Verma 		spa->length = DCR_SIZE;
2252d7d8464dSRoss Zwisler 		offset += spa->header.length;
225320985164SVishal Verma 
225420985164SVishal Verma 		/*
225520985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
225620985164SVishal Verma 		 * does not actually alias the related block-data-window
225720985164SVishal Verma 		 * regions)
225820985164SVishal Verma 		 */
2259d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
226020985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
226120985164SVishal Verma 		spa->header.length = sizeof(*spa);
226220985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
226320985164SVishal Verma 		spa->range_index = 11+1;
226420985164SVishal Verma 		spa->address = t->spa_set_dma[2];
226520985164SVishal Verma 		spa->length = SPA0_SIZE;
2266d7d8464dSRoss Zwisler 		offset += spa->header.length;
226720985164SVishal Verma 
226820985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2269d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
227020985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
227120985164SVishal Verma 		spa->header.length = sizeof(*spa);
227220985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
227320985164SVishal Verma 		spa->range_index = 12+1;
227420985164SVishal Verma 		spa->address = t->dimm_dma[4];
227520985164SVishal Verma 		spa->length = DIMM_SIZE;
2276d7d8464dSRoss Zwisler 		offset += spa->header.length;
227720985164SVishal Verma 
227820985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
227920985164SVishal Verma 		memdev = nfit_buf + offset;
228020985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
228120985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
228220985164SVishal Verma 		memdev->device_handle = handle[4];
228320985164SVishal Verma 		memdev->physical_id = 4;
228420985164SVishal Verma 		memdev->region_id = 0;
228520985164SVishal Verma 		memdev->range_index = 10+1;
22863b87356fSDan Williams 		memdev->region_index = 8+1;
228720985164SVishal Verma 		memdev->region_size = 0;
228820985164SVishal Verma 		memdev->region_offset = 0;
228920985164SVishal Verma 		memdev->address = 0;
229020985164SVishal Verma 		memdev->interleave_index = 0;
229120985164SVishal Verma 		memdev->interleave_ways = 1;
2292d7d8464dSRoss Zwisler 		offset += memdev->header.length;
229320985164SVishal Verma 
2294d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2295d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
229620985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
229720985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
229820985164SVishal Verma 		memdev->device_handle = handle[4];
229920985164SVishal Verma 		memdev->physical_id = 4;
230020985164SVishal Verma 		memdev->region_id = 0;
230120985164SVishal Verma 		memdev->range_index = 11+1;
23023b87356fSDan Williams 		memdev->region_index = 9+1;
230320985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2304df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
230520985164SVishal Verma 		memdev->address = 0;
230620985164SVishal Verma 		memdev->interleave_index = 0;
230720985164SVishal Verma 		memdev->interleave_ways = 1;
2308ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2309d7d8464dSRoss Zwisler 		offset += memdev->header.length;
231020985164SVishal Verma 
23113b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2312d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
231320985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
231420985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
231520985164SVishal Verma 		memdev->device_handle = handle[4];
231620985164SVishal Verma 		memdev->physical_id = 4;
231720985164SVishal Verma 		memdev->region_id = 0;
231820985164SVishal Verma 		memdev->range_index = 12+1;
23193b87356fSDan Williams 		memdev->region_index = 8+1;
232020985164SVishal Verma 		memdev->region_size = 0;
232120985164SVishal Verma 		memdev->region_offset = 0;
232220985164SVishal Verma 		memdev->address = 0;
232320985164SVishal Verma 		memdev->interleave_index = 0;
232420985164SVishal Verma 		memdev->interleave_ways = 1;
2325d7d8464dSRoss Zwisler 		offset += memdev->header.length;
232620985164SVishal Verma 
232720985164SVishal Verma 		/* flush3 (dimm4) */
232820985164SVishal Verma 		flush = nfit_buf + offset;
232920985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
233085d3fa02SDan Williams 		flush->header.length = flush_hint_size;
233120985164SVishal Verma 		flush->device_handle = handle[4];
233285d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
233385d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
233485d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
233585d3fa02SDan Williams 				+ i * sizeof(u64);
2336d7d8464dSRoss Zwisler 		offset += flush->header.length;
23379741a559SRoss Zwisler 
23389741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
23399741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
234020985164SVishal Verma 	}
234120985164SVishal Verma 
23421526f9e2SRoss Zwisler 	t->nfit_filled = offset;
23431526f9e2SRoss Zwisler 
23449fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
23459fb1a190SDave Jiang 			SPA0_SIZE);
2346f471f1a7SDan Williams 
23476bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2348e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2349e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2350e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2351ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2352ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2353ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
23544cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2355e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2356e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2357e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2358e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
235910246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
236010246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
23619fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
23629fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
23639fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2364bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2365bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2366bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2367bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2368bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2369674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
2370*3c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
2371*3c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2372*3c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
2373*3c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
2374*3c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2375*3c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
2376*3c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
2377*3c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
23786bc75619SDan Williams }
23796bc75619SDan Williams 
23806bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
23816bc75619SDan Williams {
23826b577c9dSLinda Knippers 	size_t offset;
23836bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
23846bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
23856bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
23866bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2387d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
23886bc75619SDan Williams 
23896b577c9dSLinda Knippers 	offset = 0;
23906bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
23916bc75619SDan Williams 	spa = nfit_buf + offset;
23926bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
23936bc75619SDan Williams 	spa->header.length = sizeof(*spa);
23946bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
23956bc75619SDan Williams 	spa->range_index = 0+1;
23966bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
23976bc75619SDan Williams 	spa->length = SPA2_SIZE;
2398d7d8464dSRoss Zwisler 	offset += spa->header.length;
23996bc75619SDan Williams 
24007bfe97c7SDan Williams 	/* virtual cd region */
2401d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
24027bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
24037bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
24047bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
24057bfe97c7SDan Williams 	spa->range_index = 0;
24067bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
24077bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2408d7d8464dSRoss Zwisler 	offset += spa->header.length;
24097bfe97c7SDan Williams 
24106bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
24116bc75619SDan Williams 	memdev = nfit_buf + offset;
24126bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
24136bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2414dafb1048SDan Williams 	memdev->device_handle = handle[5];
24156bc75619SDan Williams 	memdev->physical_id = 0;
24166bc75619SDan Williams 	memdev->region_id = 0;
24176bc75619SDan Williams 	memdev->range_index = 0+1;
24186bc75619SDan Williams 	memdev->region_index = 0+1;
24196bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
24206bc75619SDan Williams 	memdev->region_offset = 0;
24216bc75619SDan Williams 	memdev->address = 0;
24226bc75619SDan Williams 	memdev->interleave_index = 0;
24236bc75619SDan Williams 	memdev->interleave_ways = 1;
242458138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
242558138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2426f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2427d7d8464dSRoss Zwisler 	offset += memdev->header.length;
24286bc75619SDan Williams 
24296bc75619SDan Williams 	/* dcr-descriptor0 */
24306bc75619SDan Williams 	dcr = nfit_buf + offset;
24316bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24323b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24333b87356fSDan Williams 			window_size);
24346bc75619SDan Williams 	dcr->region_index = 0+1;
24355dc68e55SDan Williams 	dcr_common_init(dcr);
2436dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2437be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
24386bc75619SDan Williams 	dcr->windows = 0;
2439ac40b675SDan Williams 	offset += dcr->header.length;
2440d7d8464dSRoss Zwisler 
2441ac40b675SDan Williams 	memdev = nfit_buf + offset;
2442ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2443ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2444ac40b675SDan Williams 	memdev->device_handle = handle[6];
2445ac40b675SDan Williams 	memdev->physical_id = 0;
2446ac40b675SDan Williams 	memdev->region_id = 0;
2447ac40b675SDan Williams 	memdev->range_index = 0;
2448ac40b675SDan Williams 	memdev->region_index = 0+2;
2449ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2450ac40b675SDan Williams 	memdev->region_offset = 0;
2451ac40b675SDan Williams 	memdev->address = 0;
2452ac40b675SDan Williams 	memdev->interleave_index = 0;
2453ac40b675SDan Williams 	memdev->interleave_ways = 1;
2454ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2455d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2456ac40b675SDan Williams 
2457ac40b675SDan Williams 	/* dcr-descriptor1 */
2458ac40b675SDan Williams 	dcr = nfit_buf + offset;
2459ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2460ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2461ac40b675SDan Williams 			window_size);
2462ac40b675SDan Williams 	dcr->region_index = 0+2;
2463ac40b675SDan Williams 	dcr_common_init(dcr);
2464ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2465ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2466ac40b675SDan Williams 	dcr->windows = 0;
2467d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2468ac40b675SDan Williams 
24699741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
24709741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
24719741a559SRoss Zwisler 
24721526f9e2SRoss Zwisler 	t->nfit_filled = offset;
24731526f9e2SRoss Zwisler 
24749fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
24759fb1a190SDave Jiang 			SPA2_SIZE);
2476f471f1a7SDan Williams 
2477d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2478e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2479e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2480e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2481e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2482674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
24839484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
24849484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
24859484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
24866bc75619SDan Williams }
24876bc75619SDan Williams 
24886bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
24896bc75619SDan Williams 		void *iobuf, u64 len, int rw)
24906bc75619SDan Williams {
24916bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
24926bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
24936bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
24946bc75619SDan Williams 	unsigned int lane;
24956bc75619SDan Williams 
24966bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
24976bc75619SDan Williams 	if (rw)
249867a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
249967a3e8feSRoss Zwisler 	else {
250067a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
250167a3e8feSRoss Zwisler 
25025deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
25035deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
250467a3e8feSRoss Zwisler 	}
25056bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
25066bc75619SDan Williams 
25076bc75619SDan Williams 	return 0;
25086bc75619SDan Williams }
25096bc75619SDan Williams 
2510a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2511a7de92daSDan Williams 
2512a7de92daSDan Williams union acpi_object *result;
2513a7de92daSDan Williams 
2514a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
251594116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2516a7de92daSDan Williams {
2517a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2518a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2519a7de92daSDan Williams 
2520a7de92daSDan Williams 	return result;
2521a7de92daSDan Williams }
2522a7de92daSDan Williams 
2523a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2524a7de92daSDan Williams {
2525a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2526a7de92daSDan Williams 	if (!result)
2527a7de92daSDan Williams 		return -ENOMEM;
2528a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2529a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2530a7de92daSDan Williams 	result->buffer.length = size;
2531a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2532a7de92daSDan Williams 	memset(buf, 0, size);
2533a7de92daSDan Williams 	return 0;
2534a7de92daSDan Williams }
2535a7de92daSDan Williams 
2536a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2537a7de92daSDan Williams {
2538a7de92daSDan Williams 	int rc, cmd_rc;
2539a7de92daSDan Williams 	struct nvdimm *nvdimm;
2540a7de92daSDan Williams 	struct acpi_device *adev;
2541a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2542a7de92daSDan Williams 	struct nd_ars_record *record;
2543a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2544a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2545a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2546a7de92daSDan Williams 	union {
2547a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2548fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2549a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2550a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2551a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2552a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2553a7de92daSDan Williams 	} cmds;
2554a7de92daSDan Williams 
2555a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2556a7de92daSDan Williams 	if (!adev)
2557a7de92daSDan Williams 		return -ENOMEM;
2558a7de92daSDan Williams 	*adev = (struct acpi_device) {
2559a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2560a7de92daSDan Williams 		.dev = {
2561a7de92daSDan Williams 			.init_name = "test-adev",
2562a7de92daSDan Williams 		},
2563a7de92daSDan Williams 	};
2564a7de92daSDan Williams 
2565a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2566a7de92daSDan Williams 	if (!acpi_desc)
2567a7de92daSDan Williams 		return -ENOMEM;
2568a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2569a7de92daSDan Williams 		.nd_desc = {
2570a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2571a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2572a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
257310246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
257410246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2575a7de92daSDan Williams 			.module = THIS_MODULE,
2576a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2577a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
25789fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
25799fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
25809fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
25819fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2582a7de92daSDan Williams 		},
2583a7de92daSDan Williams 		.dev = &adev->dev,
2584a7de92daSDan Williams 	};
2585a7de92daSDan Williams 
2586a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2587a7de92daSDan Williams 	if (!nfit_mem)
2588a7de92daSDan Williams 		return -ENOMEM;
2589a7de92daSDan Williams 
2590a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2591a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2592a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2593a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2594a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2595a7de92daSDan Williams 		.adev = adev,
2596a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2597a7de92daSDan Williams 		.dsm_mask = mask,
2598a7de92daSDan Williams 	};
2599a7de92daSDan Williams 
2600a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2601a7de92daSDan Williams 	if (!nvdimm)
2602a7de92daSDan Williams 		return -ENOMEM;
2603a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2604a7de92daSDan Williams 		.provider_data = nfit_mem,
2605a7de92daSDan Williams 		.cmd_mask = mask,
2606a7de92daSDan Williams 		.dev = {
2607a7de92daSDan Williams 			.init_name = "test-dimm",
2608a7de92daSDan Williams 		},
2609a7de92daSDan Williams 	};
2610a7de92daSDan Williams 
2611a7de92daSDan Williams 
2612a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2613a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2614a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2615a7de92daSDan Williams 		.status = 0,
2616a7de92daSDan Williams 		.config_size = SZ_128K,
2617a7de92daSDan Williams 		.max_xfer = SZ_4K,
2618a7de92daSDan Williams 	};
2619a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2620a7de92daSDan Williams 	if (rc)
2621a7de92daSDan Williams 		return rc;
2622a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2623a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2624a7de92daSDan Williams 
2625a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2626a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2627a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2628a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2629a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2630a7de92daSDan Williams 		return -EIO;
2631a7de92daSDan Williams 	}
2632a7de92daSDan Williams 
2633a7de92daSDan Williams 
2634a7de92daSDan Williams 	/* test ars_status with zero output */
2635a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2636a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2637a7de92daSDan Williams 		.out_length = 0,
2638a7de92daSDan Williams 	};
2639a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2640a7de92daSDan Williams 	if (rc)
2641a7de92daSDan Williams 		return rc;
2642a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2643a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2644a7de92daSDan Williams 
2645a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2646a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2647a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2648a7de92daSDan Williams 		return -EIO;
2649a7de92daSDan Williams 	}
2650a7de92daSDan Williams 
2651a7de92daSDan Williams 
2652a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2653a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2654a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2655a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2656a7de92daSDan Williams 	};
2657a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2658a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2659a7de92daSDan Williams 	if (rc)
2660a7de92daSDan Williams 		return rc;
2661a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2662a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2663a7de92daSDan Williams 
2664a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2665a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2666a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2667a7de92daSDan Williams 		return -EIO;
2668a7de92daSDan Williams 	}
2669a7de92daSDan Williams 
2670a7de92daSDan Williams 
2671a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2672a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2673a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2674a7de92daSDan Williams 		.out_length = cmd_size - 4,
2675a7de92daSDan Williams 	};
2676a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2677a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2678a7de92daSDan Williams 		.length = test_val,
2679a7de92daSDan Williams 	};
2680a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2681a7de92daSDan Williams 	if (rc)
2682a7de92daSDan Williams 		return rc;
2683a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2684a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2685a7de92daSDan Williams 
2686a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2687a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2688a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2689a7de92daSDan Williams 		return -EIO;
2690a7de92daSDan Williams 	}
2691a7de92daSDan Williams 
2692a7de92daSDan Williams 
2693a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2694a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2695a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2696a7de92daSDan Williams 		.out_length = cmd_size,
2697a7de92daSDan Williams 	};
2698a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2699a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2700a7de92daSDan Williams 		.length = test_val,
2701a7de92daSDan Williams 	};
2702a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2703a7de92daSDan Williams 	if (rc)
2704a7de92daSDan Williams 		return rc;
2705a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2706a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2707a7de92daSDan Williams 
2708a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2709a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2710a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2711a7de92daSDan Williams 		return -EIO;
2712a7de92daSDan Williams 	}
2713a7de92daSDan Williams 
2714a7de92daSDan Williams 
2715a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2716a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2717a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2718a7de92daSDan Williams 		.status = 1 << 16,
2719a7de92daSDan Williams 	};
2720a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2721a7de92daSDan Williams 	if (rc)
2722a7de92daSDan Williams 		return rc;
2723a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2724a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2725a7de92daSDan Williams 
2726a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2727a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2728a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2729a7de92daSDan Williams 		return -EIO;
2730a7de92daSDan Williams 	}
2731a7de92daSDan Williams 
2732fb2a1748SDan Williams 	/* test clear error */
2733fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2734fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2735fb2a1748SDan Williams 		.length = 512,
2736fb2a1748SDan Williams 		.cleared = 512,
2737fb2a1748SDan Williams 	};
2738fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2739fb2a1748SDan Williams 	if (rc)
2740fb2a1748SDan Williams 		return rc;
2741fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2742fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2743fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2744fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2745fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2746fb2a1748SDan Williams 		return -EIO;
2747fb2a1748SDan Williams 	}
2748fb2a1748SDan Williams 
2749a7de92daSDan Williams 	return 0;
2750a7de92daSDan Williams }
2751a7de92daSDan Williams 
27526bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
27536bc75619SDan Williams {
27546bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
27556bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
27566bc75619SDan Williams 	struct device *dev = &pdev->dev;
27576bc75619SDan Williams 	struct nfit_test *nfit_test;
2758231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2759c14a868aSDan Williams 	union acpi_object *obj;
27606bc75619SDan Williams 	int rc;
27616bc75619SDan Williams 
2762a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2763a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2764a7de92daSDan Williams 		if (rc)
2765a7de92daSDan Williams 			return rc;
2766a7de92daSDan Williams 	}
2767a7de92daSDan Williams 
27686bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
27696bc75619SDan Williams 
27706bc75619SDan Williams 	/* common alloc */
27716bc75619SDan Williams 	if (nfit_test->num_dcr) {
27726bc75619SDan Williams 		int num = nfit_test->num_dcr;
27736bc75619SDan Williams 
27746bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
27756bc75619SDan Williams 				GFP_KERNEL);
27766bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
27776bc75619SDan Williams 				GFP_KERNEL);
27789d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
27799d27a87eSDan Williams 				GFP_KERNEL);
27809d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
27819d27a87eSDan Williams 				GFP_KERNEL);
27826bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
27836bc75619SDan Williams 				GFP_KERNEL);
27846bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
27856bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
27866bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
27876bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
27886bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
27896bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2790ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2791ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2792ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2793ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2794ed07c433SDan Williams 				GFP_KERNEL);
2795bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2796bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
27976bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
27986bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
27999d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2800bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2801bfbaa952SDave Jiang 				&& nfit_test->fw)
28026bc75619SDan Williams 			/* pass */;
28036bc75619SDan Williams 		else
28046bc75619SDan Williams 			return -ENOMEM;
28056bc75619SDan Williams 	}
28066bc75619SDan Williams 
28076bc75619SDan Williams 	if (nfit_test->num_pm) {
28086bc75619SDan Williams 		int num = nfit_test->num_pm;
28096bc75619SDan Williams 
28106bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
28116bc75619SDan Williams 				GFP_KERNEL);
28126bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
28136bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
28146bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
28156bc75619SDan Williams 			/* pass */;
28166bc75619SDan Williams 		else
28176bc75619SDan Williams 			return -ENOMEM;
28186bc75619SDan Williams 	}
28196bc75619SDan Williams 
28206bc75619SDan Williams 	/* per-nfit specific alloc */
28216bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
28226bc75619SDan Williams 		return -ENOMEM;
28236bc75619SDan Williams 
28246bc75619SDan Williams 	nfit_test->setup(nfit_test);
28256bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2826a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
28276bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
28286bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2829a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2830bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2831a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
28326bc75619SDan Williams 
2833e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
28341526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
283558cd71b4SDan Williams 	if (rc)
283620985164SVishal Verma 		return rc;
283720985164SVishal Verma 
2838fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2839fbabd829SDan Williams 	if (rc)
2840fbabd829SDan Williams 		return rc;
2841fbabd829SDan Williams 
284220985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
284320985164SVishal Verma 		return 0;
284420985164SVishal Verma 
284520985164SVishal Verma 	nfit_test->setup_hotplug = 1;
284620985164SVishal Verma 	nfit_test->setup(nfit_test);
284720985164SVishal Verma 
2848c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2849c14a868aSDan Williams 	if (!obj)
2850c14a868aSDan Williams 		return -ENOMEM;
2851c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2852c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2853c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2854c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2855c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2856231bf117SDan Williams 
2857231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2858231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
2859231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2860231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2861231bf117SDan Williams 		int i;
2862231bf117SDan Williams 
2863af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
2864231bf117SDan Williams 			if (nfit_handle == handle[i])
2865231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
2866231bf117SDan Williams 						nfit_mem);
2867231bf117SDan Williams 	}
2868231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
28696bc75619SDan Williams 
28706bc75619SDan Williams 	return 0;
28716bc75619SDan Williams }
28726bc75619SDan Williams 
28736bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
28746bc75619SDan Williams {
28756bc75619SDan Williams 	return 0;
28766bc75619SDan Williams }
28776bc75619SDan Williams 
28786bc75619SDan Williams static void nfit_test_release(struct device *dev)
28796bc75619SDan Williams {
28806bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
28816bc75619SDan Williams 
28826bc75619SDan Williams 	kfree(nfit_test);
28836bc75619SDan Williams }
28846bc75619SDan Williams 
28856bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
28866bc75619SDan Williams 	{ KBUILD_MODNAME },
28876bc75619SDan Williams 	{ },
28886bc75619SDan Williams };
28896bc75619SDan Williams 
28906bc75619SDan Williams static struct platform_driver nfit_test_driver = {
28916bc75619SDan Williams 	.probe = nfit_test_probe,
28926bc75619SDan Williams 	.remove = nfit_test_remove,
28936bc75619SDan Williams 	.driver = {
28946bc75619SDan Williams 		.name = KBUILD_MODNAME,
28956bc75619SDan Williams 	},
28966bc75619SDan Williams 	.id_table = nfit_test_id,
28976bc75619SDan Williams };
28986bc75619SDan Williams 
28995d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
29005d8beee2SDan Williams 
29015d8beee2SDan Williams enum INJECT {
29025d8beee2SDan Williams 	INJECT_NONE,
29035d8beee2SDan Williams 	INJECT_SRC,
29045d8beee2SDan Williams 	INJECT_DST,
29055d8beee2SDan Williams };
29065d8beee2SDan Williams 
29075d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
29085d8beee2SDan Williams {
29095d8beee2SDan Williams 	size_t i;
29105d8beee2SDan Williams 
29115d8beee2SDan Williams 	memset(dst, 0xff, size);
29125d8beee2SDan Williams 	for (i = 0; i < size; i++)
29135d8beee2SDan Williams 		src[i] = (char) i;
29145d8beee2SDan Williams }
29155d8beee2SDan Williams 
29165d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
29175d8beee2SDan Williams 		size_t size, unsigned long rem)
29185d8beee2SDan Williams {
29195d8beee2SDan Williams 	size_t i;
29205d8beee2SDan Williams 
29215d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
29225d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
29235d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
29245d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
29255d8beee2SDan Williams 					(unsigned char) i);
29265d8beee2SDan Williams 			return false;
29275d8beee2SDan Williams 		}
29285d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
29295d8beee2SDan Williams 		if (dst[i] != 0xffU) {
29305d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
29315d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
29325d8beee2SDan Williams 			return false;
29335d8beee2SDan Williams 		}
29345d8beee2SDan Williams 	return true;
29355d8beee2SDan Williams }
29365d8beee2SDan Williams 
29375d8beee2SDan Williams void mcsafe_test(void)
29385d8beee2SDan Williams {
29395d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
29405d8beee2SDan Williams 	enum INJECT inj;
29415d8beee2SDan Williams 
29425d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
29435d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
29445d8beee2SDan Williams 	} else {
29455d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
29465d8beee2SDan Williams 		return;
29475d8beee2SDan Williams 	}
29485d8beee2SDan Williams 
29495d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
29505d8beee2SDan Williams 		int i;
29515d8beee2SDan Williams 
29525d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
29535d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
29545d8beee2SDan Williams 			unsigned long expect, rem;
29555d8beee2SDan Williams 			void *src, *dst;
29565d8beee2SDan Williams 			bool valid;
29575d8beee2SDan Williams 
29585d8beee2SDan Williams 			switch (inj) {
29595d8beee2SDan Williams 			case INJECT_NONE:
29605d8beee2SDan Williams 				mcsafe_inject_src(NULL);
29615d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
29625d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
29635d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
29645d8beee2SDan Williams 				expect = 0;
29655d8beee2SDan Williams 				break;
29665d8beee2SDan Williams 			case INJECT_SRC:
29675d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
29685d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
29695d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
29705d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
29715d8beee2SDan Williams 				expect = 512 - i;
29725d8beee2SDan Williams 				break;
29735d8beee2SDan Williams 			case INJECT_DST:
29745d8beee2SDan Williams 				mcsafe_inject_src(NULL);
29755d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
29765d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
29775d8beee2SDan Williams 				src = &mcsafe_buf[1024];
29785d8beee2SDan Williams 				expect = 512 - i;
29795d8beee2SDan Williams 				break;
29805d8beee2SDan Williams 			}
29815d8beee2SDan Williams 
29825d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
29835d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
29845d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
29855d8beee2SDan Williams 			if (rem == expect && valid)
29865d8beee2SDan Williams 				continue;
29875d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
29885d8beee2SDan Williams 					__func__,
29895d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
29905d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
29915d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
29925d8beee2SDan Williams 					expect);
29935d8beee2SDan Williams 		}
29945d8beee2SDan Williams 	}
29955d8beee2SDan Williams 
29965d8beee2SDan Williams 	mcsafe_inject_src(NULL);
29975d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
29985d8beee2SDan Williams }
29995d8beee2SDan Williams 
30006bc75619SDan Williams static __init int nfit_test_init(void)
30016bc75619SDan Williams {
30026bc75619SDan Williams 	int rc, i;
30036bc75619SDan Williams 
30040fb5c8dfSDan Williams 	pmem_test();
30050fb5c8dfSDan Williams 	libnvdimm_test();
30060fb5c8dfSDan Williams 	acpi_nfit_test();
30070fb5c8dfSDan Williams 	device_dax_test();
30085d8beee2SDan Williams 	mcsafe_test();
30090fb5c8dfSDan Williams 
3010a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3011231bf117SDan Williams 
30129fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
30139fb1a190SDave Jiang 	if (!nfit_wq)
30149fb1a190SDave Jiang 		return -ENOMEM;
30159fb1a190SDave Jiang 
3016a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
3017a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
3018a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
3019a7de92daSDan Williams 		goto err_register;
3020a7de92daSDan Williams 	}
30216bc75619SDan Williams 
30226bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
30236bc75619SDan Williams 		struct nfit_test *nfit_test;
30246bc75619SDan Williams 		struct platform_device *pdev;
30256bc75619SDan Williams 
30266bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
30276bc75619SDan Williams 		if (!nfit_test) {
30286bc75619SDan Williams 			rc = -ENOMEM;
30296bc75619SDan Williams 			goto err_register;
30306bc75619SDan Williams 		}
30316bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
30329fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
30336bc75619SDan Williams 		switch (i) {
30346bc75619SDan Williams 		case 0:
30356bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3036dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
30376bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
30386bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
30396bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
30406bc75619SDan Williams 			break;
30416bc75619SDan Williams 		case 1:
3042a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3043dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3044ac40b675SDan Williams 			nfit_test->num_dcr = 2;
30456bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
30466bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
30476bc75619SDan Williams 			break;
30486bc75619SDan Williams 		default:
30496bc75619SDan Williams 			rc = -EINVAL;
30506bc75619SDan Williams 			goto err_register;
30516bc75619SDan Williams 		}
30526bc75619SDan Williams 		pdev = &nfit_test->pdev;
30536bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
30546bc75619SDan Williams 		pdev->id = i;
30556bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
30566bc75619SDan Williams 		rc = platform_device_register(pdev);
30576bc75619SDan Williams 		if (rc) {
30586bc75619SDan Williams 			put_device(&pdev->dev);
30596bc75619SDan Williams 			goto err_register;
30606bc75619SDan Williams 		}
30618b06b884SDan Williams 		get_device(&pdev->dev);
30626bc75619SDan Williams 
30636bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
30646bc75619SDan Williams 		if (rc)
30656bc75619SDan Williams 			goto err_register;
30666bc75619SDan Williams 
30676bc75619SDan Williams 		instances[i] = nfit_test;
30689fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
30696bc75619SDan Williams 	}
30706bc75619SDan Williams 
30716bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
30726bc75619SDan Williams 	if (rc)
30736bc75619SDan Williams 		goto err_register;
30746bc75619SDan Williams 	return 0;
30756bc75619SDan Williams 
30766bc75619SDan Williams  err_register:
30779fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
30786bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
30796bc75619SDan Williams 		if (instances[i])
30806bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
30816bc75619SDan Williams 	nfit_test_teardown();
30828b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
30838b06b884SDan Williams 		if (instances[i])
30848b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
30858b06b884SDan Williams 
30866bc75619SDan Williams 	return rc;
30876bc75619SDan Williams }
30886bc75619SDan Williams 
30896bc75619SDan Williams static __exit void nfit_test_exit(void)
30906bc75619SDan Williams {
30916bc75619SDan Williams 	int i;
30926bc75619SDan Williams 
30939fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
30949fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
30956bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
30966bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
30978b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
30986bc75619SDan Williams 	nfit_test_teardown();
30998b06b884SDan Williams 
31008b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
31018b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3102231bf117SDan Williams 	class_destroy(nfit_test_dimm);
31036bc75619SDan Williams }
31046bc75619SDan Williams 
31056bc75619SDan Williams module_init(nfit_test_init);
31066bc75619SDan Williams module_exit(nfit_test_exit);
31076bc75619SDan Williams MODULE_LICENSE("GPL v2");
31086bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3109