xref: /openbmc/linux/tools/testing/nvdimm/test/nfit.c (revision ec6347bb43395cb92126788a1a5b25302543f815)
15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26bc75619SDan Williams /*
36bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
46bc75619SDan Williams  */
56bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
66bc75619SDan Williams #include <linux/platform_device.h>
76bc75619SDan Williams #include <linux/dma-mapping.h>
8d8d378faSDan Williams #include <linux/workqueue.h>
96bc75619SDan Williams #include <linux/libnvdimm.h>
10e3f5df76SDan Williams #include <linux/genalloc.h>
116bc75619SDan Williams #include <linux/vmalloc.h>
126bc75619SDan Williams #include <linux/device.h>
136bc75619SDan Williams #include <linux/module.h>
1420985164SVishal Verma #include <linux/mutex.h>
156bc75619SDan Williams #include <linux/ndctl.h>
166bc75619SDan Williams #include <linux/sizes.h>
1720985164SVishal Verma #include <linux/list.h>
186bc75619SDan Williams #include <linux/slab.h>
19a7de92daSDan Williams #include <nd-core.h>
200ead1118SDan Williams #include <intel.h>
216bc75619SDan Williams #include <nfit.h>
226bc75619SDan Williams #include <nd.h>
236bc75619SDan Williams #include "nfit_test.h"
240fb5c8dfSDan Williams #include "../watermark.h"
256bc75619SDan Williams 
26*ec6347bbSDan Williams #include <asm/copy_mc_test.h>
27*ec6347bbSDan Williams #include <asm/mce.h>
285d8beee2SDan Williams 
296bc75619SDan Williams /*
306bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
316bc75619SDan Williams  *
326bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
336bc75619SDan Williams  *
346bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
356bc75619SDan Williams  *           +----------+--------------+----------+---------+
366bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
376bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
386bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
396bc75619SDan Williams  *    |      +----------+--------------v----------v         v
406bc75619SDan Williams  * +--+---+                            |                    |
416bc75619SDan Williams  * | cpu0 |                                    region1
426bc75619SDan Williams  * +--+---+                            |                    |
436bc75619SDan Williams  *    |      +-------------------------^----------^         ^
446bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
456bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
466bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
476bc75619SDan Williams  *           +-------------------------+----------+-+-------+
486bc75619SDan Williams  *
4920985164SVishal Verma  * +--+---+
5020985164SVishal Verma  * | cpu1 |
5120985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5220985164SVishal Verma  *    |      +----------------------------------------------+
5320985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5420985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5520985164SVishal Verma  * +------+
5620985164SVishal Verma  *
5720985164SVishal Verma  *
586bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
596bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
606bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
616bc75619SDan Williams  *
626bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
636bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
646bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
656bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
666bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
676bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
686bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
696bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
706bc75619SDan Williams  *    names that can be assigned to a namespace.
716bc75619SDan Williams  *
726bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
736bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
746bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
756bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
766bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
776bc75619SDan Williams  *    "blk5.0".
786bc75619SDan Williams  *
796bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
806bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
816bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
826bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
836bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
846bc75619SDan Williams  *
856bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
866bc75619SDan Williams  *
876bc75619SDan Williams  *  region2
886bc75619SDan Williams  * +---------------------+
896bc75619SDan Williams  * |---------------------|
906bc75619SDan Williams  * ||       pm2.0       ||
916bc75619SDan Williams  * |---------------------|
926bc75619SDan Williams  * +---------------------+
936bc75619SDan Williams  *
946bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
956bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
966bc75619SDan Williams  *    reference an NVDIMM.
976bc75619SDan Williams  */
986bc75619SDan Williams enum {
9920985164SVishal Verma 	NUM_PM  = 3,
10020985164SVishal Verma 	NUM_DCR = 5,
10185d3fa02SDan Williams 	NUM_HINTS = 8,
1026bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1036bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1049741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1059741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1066bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1076bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1087bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1096bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1106bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1116bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1126bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1136bc75619SDan Williams 	DCR_SIZE = 12,
1146bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1156bc75619SDan Williams };
1166bc75619SDan Williams 
1176bc75619SDan Williams struct nfit_test_dcr {
1186bc75619SDan Williams 	__le64 bdw_addr;
1196bc75619SDan Williams 	__le32 bdw_status;
1206bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1216bc75619SDan Williams };
1226bc75619SDan Williams 
1236bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1246bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1256bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1266bc75619SDan Williams 
127dafb1048SDan Williams static u32 handle[] = {
1286bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1296bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1306bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1316bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13220985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
133dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
134ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1356bc75619SDan Williams };
1366bc75619SDan Williams 
137af31b04bSMasayoshi Mizuma static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
138af31b04bSMasayoshi Mizuma static int dimm_fail_cmd_code[ARRAY_SIZE(handle)];
1393c13e2acSDave Jiang struct nfit_test_sec {
1403c13e2acSDave Jiang 	u8 state;
141ecaa4a97SDave Jiang 	u8 ext_state;
1422170a0d5SDave Jiang 	u8 old_state;
1433c13e2acSDave Jiang 	u8 passphrase[32];
144ecaa4a97SDave Jiang 	u8 master_passphrase[32];
145926f7480SDave Jiang 	u64 overwrite_end_time;
1463c13e2acSDave Jiang } dimm_sec_info[NUM_DCR];
14773606afdSDan Williams 
148b4d4702fSVishal Verma static const struct nd_intel_smart smart_def = {
149b4d4702fSVishal Verma 	.flags = ND_INTEL_SMART_HEALTH_VALID
150b4d4702fSVishal Verma 		| ND_INTEL_SMART_SPARES_VALID
151b4d4702fSVishal Verma 		| ND_INTEL_SMART_ALARM_VALID
152b4d4702fSVishal Verma 		| ND_INTEL_SMART_USED_VALID
153b4d4702fSVishal Verma 		| ND_INTEL_SMART_SHUTDOWN_VALID
154f1101766SDan Williams 		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
155b4d4702fSVishal Verma 		| ND_INTEL_SMART_MTEMP_VALID
156b4d4702fSVishal Verma 		| ND_INTEL_SMART_CTEMP_VALID,
157b4d4702fSVishal Verma 	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
158b4d4702fSVishal Verma 	.media_temperature = 23 * 16,
159b4d4702fSVishal Verma 	.ctrl_temperature = 25 * 16,
160b4d4702fSVishal Verma 	.pmic_temperature = 40 * 16,
161b4d4702fSVishal Verma 	.spares = 75,
162b4d4702fSVishal Verma 	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
163b4d4702fSVishal Verma 		| ND_INTEL_SMART_TEMP_TRIP,
164b4d4702fSVishal Verma 	.ait_status = 1,
165b4d4702fSVishal Verma 	.life_used = 5,
166b4d4702fSVishal Verma 	.shutdown_state = 0,
167f1101766SDan Williams 	.shutdown_count = 42,
168b4d4702fSVishal Verma 	.vendor_size = 0,
169b4d4702fSVishal Verma };
170b4d4702fSVishal Verma 
171bfbaa952SDave Jiang struct nfit_test_fw {
172bfbaa952SDave Jiang 	enum intel_fw_update_state state;
173bfbaa952SDave Jiang 	u32 context;
174bfbaa952SDave Jiang 	u64 version;
175bfbaa952SDave Jiang 	u32 size_received;
176bfbaa952SDave Jiang 	u64 end_time;
177916566aeSDan Williams 	bool armed;
178916566aeSDan Williams 	bool missed_activate;
179916566aeSDan Williams 	unsigned long last_activate;
180bfbaa952SDave Jiang };
181bfbaa952SDave Jiang 
1826bc75619SDan Williams struct nfit_test {
1836bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1846bc75619SDan Williams 	struct platform_device pdev;
1856bc75619SDan Williams 	struct list_head resources;
1866bc75619SDan Williams 	void *nfit_buf;
1876bc75619SDan Williams 	dma_addr_t nfit_dma;
1886bc75619SDan Williams 	size_t nfit_size;
1891526f9e2SRoss Zwisler 	size_t nfit_filled;
190dafb1048SDan Williams 	int dcr_idx;
1916bc75619SDan Williams 	int num_dcr;
1926bc75619SDan Williams 	int num_pm;
1936bc75619SDan Williams 	void **dimm;
1946bc75619SDan Williams 	dma_addr_t *dimm_dma;
1959d27a87eSDan Williams 	void **flush;
1969d27a87eSDan Williams 	dma_addr_t *flush_dma;
1976bc75619SDan Williams 	void **label;
1986bc75619SDan Williams 	dma_addr_t *label_dma;
1996bc75619SDan Williams 	void **spa_set;
2006bc75619SDan Williams 	dma_addr_t *spa_set_dma;
2016bc75619SDan Williams 	struct nfit_test_dcr **dcr;
2026bc75619SDan Williams 	dma_addr_t *dcr_dma;
2036bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
2046bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
20520985164SVishal Verma 	int setup_hotplug;
206c14a868aSDan Williams 	union acpi_object **_fit;
207c14a868aSDan Williams 	dma_addr_t _fit_dma;
208f471f1a7SDan Williams 	struct ars_state {
209f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
210f471f1a7SDan Williams 		unsigned long deadline;
211f471f1a7SDan Williams 		spinlock_t lock;
212f471f1a7SDan Williams 	} ars_state;
213af31b04bSMasayoshi Mizuma 	struct device *dimm_dev[ARRAY_SIZE(handle)];
214ed07c433SDan Williams 	struct nd_intel_smart *smart;
215ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
2169fb1a190SDave Jiang 	struct badrange badrange;
2179fb1a190SDave Jiang 	struct work_struct work;
218bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
2196bc75619SDan Williams };
2206bc75619SDan Williams 
2219fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
2229fb1a190SDave Jiang 
223e3f5df76SDan Williams static struct gen_pool *nfit_pool;
224e3f5df76SDan Williams 
225037c8489SDave Jiang static const char zero_key[NVDIMM_PASSPHRASE_LEN];
226037c8489SDave Jiang 
2276bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
2286bc75619SDan Williams {
2296bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
2306bc75619SDan Williams 
2316bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
2326bc75619SDan Williams }
2336bc75619SDan Williams 
234bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
235bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
236bfbaa952SDave Jiang 		int idx)
237bfbaa952SDave Jiang {
238bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
239bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
240bfbaa952SDave Jiang 
241bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
242bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
243bfbaa952SDave Jiang 
244bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
245bfbaa952SDave Jiang 		return -EINVAL;
246bfbaa952SDave Jiang 
247bfbaa952SDave Jiang 	nd_cmd->status = 0;
248bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
249bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
250bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
251bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
252bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
253bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
254bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
255bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
256bfbaa952SDave Jiang 
257bfbaa952SDave Jiang 	return 0;
258bfbaa952SDave Jiang }
259bfbaa952SDave Jiang 
260bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
261bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
262bfbaa952SDave Jiang 		int idx)
263bfbaa952SDave Jiang {
264bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
265bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
266bfbaa952SDave Jiang 
267bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
268bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
269bfbaa952SDave Jiang 
270bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
271bfbaa952SDave Jiang 		return -EINVAL;
272bfbaa952SDave Jiang 
273bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
274bfbaa952SDave Jiang 		/* extended status, FW update in progress */
275bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
276bfbaa952SDave Jiang 		return 0;
277bfbaa952SDave Jiang 	}
278bfbaa952SDave Jiang 
279bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
280bfbaa952SDave Jiang 	fw->context++;
281bfbaa952SDave Jiang 	fw->size_received = 0;
282bfbaa952SDave Jiang 	nd_cmd->status = 0;
283bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
284bfbaa952SDave Jiang 
285bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
286bfbaa952SDave Jiang 
287bfbaa952SDave Jiang 	return 0;
288bfbaa952SDave Jiang }
289bfbaa952SDave Jiang 
290bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
291bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
292bfbaa952SDave Jiang 		int idx)
293bfbaa952SDave Jiang {
294bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
295bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
296bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
297bfbaa952SDave Jiang 
298bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
299bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
300bfbaa952SDave Jiang 
301bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
302bfbaa952SDave Jiang 		return -EINVAL;
303bfbaa952SDave Jiang 
304bfbaa952SDave Jiang 
305bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
306bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
307bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
308bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
309bfbaa952SDave Jiang 
310bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
311bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
312bfbaa952SDave Jiang 		*status = 0x5;
313bfbaa952SDave Jiang 		return 0;
314bfbaa952SDave Jiang 	}
315bfbaa952SDave Jiang 
316bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
317bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
318bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
319bfbaa952SDave Jiang 		*status = 0x10007;
320bfbaa952SDave Jiang 		return 0;
321bfbaa952SDave Jiang 	}
322bfbaa952SDave Jiang 
323bfbaa952SDave Jiang 	/*
324bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
325bfbaa952SDave Jiang 	 * check length is > max send length
326bfbaa952SDave Jiang 	 */
327bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
328bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
329bfbaa952SDave Jiang 		*status = 0x3;
330bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
331bfbaa952SDave Jiang 		return 0;
332bfbaa952SDave Jiang 	}
333bfbaa952SDave Jiang 
334bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
335bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
336bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
337bfbaa952SDave Jiang 	*status = 0;
338bfbaa952SDave Jiang 	return 0;
339bfbaa952SDave Jiang }
340bfbaa952SDave Jiang 
341bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
342bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
343bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
344bfbaa952SDave Jiang {
345bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
346bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
347bfbaa952SDave Jiang 
348bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
349bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
350bfbaa952SDave Jiang 
351bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
352916566aeSDan Williams 		/* update already done, need activation */
353bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
354bfbaa952SDave Jiang 		return 0;
355bfbaa952SDave Jiang 	}
356bfbaa952SDave Jiang 
357bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
358bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
359bfbaa952SDave Jiang 
360bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
361bfbaa952SDave Jiang 	case 0: /* finish */
362bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
363bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
364bfbaa952SDave Jiang 					__func__, nd_cmd->context,
365bfbaa952SDave Jiang 					fw->context);
366bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
367bfbaa952SDave Jiang 			return 0;
368bfbaa952SDave Jiang 		}
369bfbaa952SDave Jiang 		nd_cmd->status = 0;
370bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
371bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
372bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
373bfbaa952SDave Jiang 		break;
374bfbaa952SDave Jiang 
375bfbaa952SDave Jiang 	case 1: /* abort */
376bfbaa952SDave Jiang 		fw->size_received = 0;
377bfbaa952SDave Jiang 		/* successfully aborted status */
378bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
379bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
380bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
381bfbaa952SDave Jiang 		break;
382bfbaa952SDave Jiang 
383bfbaa952SDave Jiang 	default: /* bad control flag */
384bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
385bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
386bfbaa952SDave Jiang 		return -EINVAL;
387bfbaa952SDave Jiang 	}
388bfbaa952SDave Jiang 
389bfbaa952SDave Jiang 	return 0;
390bfbaa952SDave Jiang }
391bfbaa952SDave Jiang 
392bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
393bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
394bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
395bfbaa952SDave Jiang {
396bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
397bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
398bfbaa952SDave Jiang 
399bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
400bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
401bfbaa952SDave Jiang 
402bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
403bfbaa952SDave Jiang 		return -EINVAL;
404bfbaa952SDave Jiang 
405bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
406bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
407bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
408bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
409bfbaa952SDave Jiang 		return 0;
410bfbaa952SDave Jiang 	}
411bfbaa952SDave Jiang 
412bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
413bfbaa952SDave Jiang 
414bfbaa952SDave Jiang 	switch (fw->state) {
415bfbaa952SDave Jiang 	case FW_STATE_NEW:
416bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
417bfbaa952SDave Jiang 		nd_cmd->status = 0;
418bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
419bfbaa952SDave Jiang 		break;
420bfbaa952SDave Jiang 
421bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
422bfbaa952SDave Jiang 		/* sequencing error */
423bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
424bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
425bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
426bfbaa952SDave Jiang 		break;
427bfbaa952SDave Jiang 
428bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
429bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
430bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
431bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
432bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
433bfbaa952SDave Jiang 			break;
434bfbaa952SDave Jiang 		}
435bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
436bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
437916566aeSDan Williams 		fw->missed_activate = false;
4385518ba4eSDan Williams 		/* fall through */
439bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
440bfbaa952SDave Jiang 		nd_cmd->status = 0;
441bfbaa952SDave Jiang 		/* bogus test version */
442bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
443bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
444bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
445bfbaa952SDave Jiang 		break;
446bfbaa952SDave Jiang 
447bfbaa952SDave Jiang 	default: /* we should never get here */
448bfbaa952SDave Jiang 		return -EINVAL;
449bfbaa952SDave Jiang 	}
450bfbaa952SDave Jiang 
451bfbaa952SDave Jiang 	return 0;
452bfbaa952SDave Jiang }
453bfbaa952SDave Jiang 
45439c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4556bc75619SDan Williams 		unsigned int buf_len)
4566bc75619SDan Williams {
4576bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4586bc75619SDan Williams 		return -EINVAL;
45939c686b8SVishal Verma 
4606bc75619SDan Williams 	nd_cmd->status = 0;
4616bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4626bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
46339c686b8SVishal Verma 
46439c686b8SVishal Verma 	return 0;
4656bc75619SDan Williams }
46639c686b8SVishal Verma 
46739c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
46839c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
46939c686b8SVishal Verma {
4706bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
47139c686b8SVishal Verma 	int rc;
4726bc75619SDan Williams 
4736bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4746bc75619SDan Williams 		return -EINVAL;
4756bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4766bc75619SDan Williams 		return -EINVAL;
4776bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4786bc75619SDan Williams 		return -EINVAL;
4796bc75619SDan Williams 
4806bc75619SDan Williams 	nd_cmd->status = 0;
4816bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
48239c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4836bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
48439c686b8SVishal Verma 
48539c686b8SVishal Verma 	return rc;
4866bc75619SDan Williams }
48739c686b8SVishal Verma 
48839c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
48939c686b8SVishal Verma 		unsigned int buf_len, void *label)
49039c686b8SVishal Verma {
4916bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4926bc75619SDan Williams 	u32 *status;
49339c686b8SVishal Verma 	int rc;
4946bc75619SDan Williams 
4956bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4966bc75619SDan Williams 		return -EINVAL;
4976bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4986bc75619SDan Williams 		return -EINVAL;
4996bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
5006bc75619SDan Williams 		return -EINVAL;
5016bc75619SDan Williams 
50239c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
5036bc75619SDan Williams 	*status = 0;
5046bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
50539c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
5066bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
50739c686b8SVishal Verma 
50839c686b8SVishal Verma 	return rc;
5096bc75619SDan Williams }
51039c686b8SVishal Verma 
511d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
512747ffe11SDan Williams 
51339c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
51439c686b8SVishal Verma 		unsigned int buf_len)
51539c686b8SVishal Verma {
5169fb1a190SDave Jiang 	int ars_recs;
5179fb1a190SDave Jiang 
51839c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
51939c686b8SVishal Verma 		return -EINVAL;
52039c686b8SVishal Verma 
5219fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5229fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5239fb1a190SDave Jiang 
524747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5259fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
52639c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
527d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
52839c686b8SVishal Verma 
52939c686b8SVishal Verma 	return 0;
53039c686b8SVishal Verma }
53139c686b8SVishal Verma 
5329fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5339fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
53439c686b8SVishal Verma {
535f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
536f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5379fb1a190SDave Jiang 	struct badrange_entry *be;
5389fb1a190SDave Jiang 	u64 end = addr + len - 1;
5399fb1a190SDave Jiang 	int i = 0;
540f471f1a7SDan Williams 
541f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
542f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
543f471f1a7SDan Williams 	ars_status->status = 0;
544f471f1a7SDan Williams 	ars_status->address = addr;
545f471f1a7SDan Williams 	ars_status->length = len;
546f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5479fb1a190SDave Jiang 
5489fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5499fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5509fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5519fb1a190SDave Jiang 		u64 rstart, rend;
5529fb1a190SDave Jiang 
5539fb1a190SDave Jiang 		/* skip entries outside the range */
5549fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5559fb1a190SDave Jiang 			continue;
5569fb1a190SDave Jiang 
5579fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5589fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5599fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
560f471f1a7SDan Williams 		ars_record->handle = 0;
5619fb1a190SDave Jiang 		ars_record->err_address = rstart;
5629fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5639fb1a190SDave Jiang 		i++;
5649fb1a190SDave Jiang 	}
5659fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5669fb1a190SDave Jiang 	ars_status->num_records = i;
5679fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5689fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
569f471f1a7SDan Williams }
570f471f1a7SDan Williams 
5719fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5729fb1a190SDave Jiang 		struct ars_state *ars_state,
573f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
574f471f1a7SDan Williams 		int *cmd_rc)
575f471f1a7SDan Williams {
576f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
57739c686b8SVishal Verma 		return -EINVAL;
57839c686b8SVishal Verma 
579f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
580f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
581f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
582f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
583f471f1a7SDan Williams 	} else {
584f471f1a7SDan Williams 		ars_start->status = 0;
585f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5869fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
587f471f1a7SDan Williams 				ars_start->length);
588f471f1a7SDan Williams 		*cmd_rc = 0;
589f471f1a7SDan Williams 	}
590f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
59139c686b8SVishal Verma 
59239c686b8SVishal Verma 	return 0;
59339c686b8SVishal Verma }
59439c686b8SVishal Verma 
595f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
596f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
597f471f1a7SDan Williams 		int *cmd_rc)
59839c686b8SVishal Verma {
599f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
60039c686b8SVishal Verma 		return -EINVAL;
60139c686b8SVishal Verma 
602f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
603f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
604f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
605f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
606f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
607f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
608f471f1a7SDan Williams 	} else {
609f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
610f471f1a7SDan Williams 				ars_state->ars_status->out_length);
611f471f1a7SDan Williams 		*cmd_rc = 0;
612f471f1a7SDan Williams 	}
613f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
61439c686b8SVishal Verma 	return 0;
61539c686b8SVishal Verma }
61639c686b8SVishal Verma 
6175e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6185e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
619d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
620d4f32367SDan Williams {
621d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
622d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
623d4f32367SDan Williams 		return -EINVAL;
624d4f32367SDan Williams 
625d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
626d4f32367SDan Williams 		return -EINVAL;
627d4f32367SDan Williams 
6285e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
629d4f32367SDan Williams 	clear_err->status = 0;
630d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
631d4f32367SDan Williams 	*cmd_rc = 0;
632d4f32367SDan Williams 	return 0;
633d4f32367SDan Williams }
634d4f32367SDan Williams 
63510246dc8SYasunori Goto struct region_search_spa {
63610246dc8SYasunori Goto 	u64 addr;
63710246dc8SYasunori Goto 	struct nd_region *region;
63810246dc8SYasunori Goto };
63910246dc8SYasunori Goto 
64010246dc8SYasunori Goto static int is_region_device(struct device *dev)
64110246dc8SYasunori Goto {
64210246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
64310246dc8SYasunori Goto }
64410246dc8SYasunori Goto 
64510246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
64610246dc8SYasunori Goto {
64710246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
64810246dc8SYasunori Goto 	struct nd_region *nd_region;
64910246dc8SYasunori Goto 	resource_size_t ndr_end;
65010246dc8SYasunori Goto 
65110246dc8SYasunori Goto 	if (!is_region_device(dev))
65210246dc8SYasunori Goto 		return 0;
65310246dc8SYasunori Goto 
65410246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
65510246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
65610246dc8SYasunori Goto 
65710246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
65810246dc8SYasunori Goto 		ctx->region = nd_region;
65910246dc8SYasunori Goto 		return 1;
66010246dc8SYasunori Goto 	}
66110246dc8SYasunori Goto 
66210246dc8SYasunori Goto 	return 0;
66310246dc8SYasunori Goto }
66410246dc8SYasunori Goto 
66510246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
66610246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
66710246dc8SYasunori Goto {
66810246dc8SYasunori Goto 	int ret;
66910246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
67010246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
67110246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
67210246dc8SYasunori Goto 	struct region_search_spa ctx = {
67310246dc8SYasunori Goto 		.addr = spa->spa,
67410246dc8SYasunori Goto 		.region = NULL,
67510246dc8SYasunori Goto 	};
67610246dc8SYasunori Goto 	u64 dpa;
67710246dc8SYasunori Goto 
67810246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
67910246dc8SYasunori Goto 				nfit_test_search_region_spa);
68010246dc8SYasunori Goto 
68110246dc8SYasunori Goto 	if (!ret)
68210246dc8SYasunori Goto 		return -ENODEV;
68310246dc8SYasunori Goto 
68410246dc8SYasunori Goto 	nd_region = ctx.region;
68510246dc8SYasunori Goto 
68610246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
68710246dc8SYasunori Goto 
68810246dc8SYasunori Goto 	/*
68910246dc8SYasunori Goto 	 * last dimm is selected for test
69010246dc8SYasunori Goto 	 */
69110246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
69210246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
69310246dc8SYasunori Goto 
69410246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
69510246dc8SYasunori Goto 	spa->num_nvdimms = 1;
69610246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
69710246dc8SYasunori Goto 
69810246dc8SYasunori Goto 	return 0;
69910246dc8SYasunori Goto }
70010246dc8SYasunori Goto 
70110246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
70210246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
70310246dc8SYasunori Goto {
70410246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
70510246dc8SYasunori Goto 		return -EINVAL;
70610246dc8SYasunori Goto 
70710246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
70810246dc8SYasunori Goto 		spa->status = 2;
70910246dc8SYasunori Goto 
71010246dc8SYasunori Goto 	return 0;
71110246dc8SYasunori Goto }
71210246dc8SYasunori Goto 
713ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
714ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
715baa51277SDan Williams {
716baa51277SDan Williams 	if (buf_len < sizeof(*smart))
717baa51277SDan Williams 		return -EINVAL;
718ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
719baa51277SDan Williams 	return 0;
720baa51277SDan Williams }
721baa51277SDan Williams 
722cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
723ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
724ed07c433SDan Williams 		unsigned int buf_len,
725ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
726baa51277SDan Williams {
727baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
728baa51277SDan Williams 		return -EINVAL;
729ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
730ed07c433SDan Williams 	return 0;
731ed07c433SDan Williams }
732ed07c433SDan Williams 
733ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
734ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
735ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
736ed07c433SDan Williams {
737ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
738ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
739ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
740ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
741ed07c433SDan Williams 			smart->ctrl_temperature);
742ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
743ed07c433SDan Williams 				&& smart->spares
744ed07c433SDan Williams 				<= thresh->spares)
745ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
746ed07c433SDan Williams 				&& smart->media_temperature
747ed07c433SDan Williams 				>= thresh->media_temperature)
748ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
749ed07c433SDan Williams 				&& smart->ctrl_temperature
7504cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7514cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7524cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
753ed07c433SDan Williams 		device_lock(bus_dev);
754ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
755ed07c433SDan Williams 		device_unlock(bus_dev);
756ed07c433SDan Williams 	}
757ed07c433SDan Williams }
758ed07c433SDan Williams 
759ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
760ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
761ed07c433SDan Williams 		unsigned int buf_len,
762ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
763ed07c433SDan Williams 		struct nd_intel_smart *smart,
764ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
765ed07c433SDan Williams {
766ed07c433SDan Williams 	unsigned int size;
767ed07c433SDan Williams 
768ed07c433SDan Williams 	size = sizeof(*in) - 4;
769ed07c433SDan Williams 	if (buf_len < size)
770ed07c433SDan Williams 		return -EINVAL;
771ed07c433SDan Williams 	memcpy(thresh->data, in, size);
772ed07c433SDan Williams 	in->status = 0;
773ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
774ed07c433SDan Williams 
775baa51277SDan Williams 	return 0;
776baa51277SDan Williams }
777baa51277SDan Williams 
7784cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7794cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7804cf260fcSVishal Verma 		unsigned int buf_len,
7814cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7824cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7834cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7844cf260fcSVishal Verma {
7854cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7864cf260fcSVishal Verma 		return -EINVAL;
7874cf260fcSVishal Verma 
788b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7894cf260fcSVishal Verma 		if (inj->mtemp_enable)
7904cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
791b4d4702fSVishal Verma 		else
792b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
793b4d4702fSVishal Verma 	}
794b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7954cf260fcSVishal Verma 		if (inj->spare_enable)
7964cf260fcSVishal Verma 			smart->spares = inj->spares;
797b4d4702fSVishal Verma 		else
798b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
799b4d4702fSVishal Verma 	}
800b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
8014cf260fcSVishal Verma 		if (inj->fatal_enable)
8024cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
803b4d4702fSVishal Verma 		else
804b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
805b4d4702fSVishal Verma 	}
806b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
8074cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
8084cf260fcSVishal Verma 			smart->shutdown_state = 1;
8094cf260fcSVishal Verma 			smart->shutdown_count++;
810b4d4702fSVishal Verma 		} else
811b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8124cf260fcSVishal Verma 	}
8134cf260fcSVishal Verma 	inj->status = 0;
8144cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8154cf260fcSVishal Verma 
8164cf260fcSVishal Verma 	return 0;
8174cf260fcSVishal Verma }
8184cf260fcSVishal Verma 
8199fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8209fb1a190SDave Jiang {
8219fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8229fb1a190SDave Jiang 
8239fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8249fb1a190SDave Jiang }
8259fb1a190SDave Jiang 
8269fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8279fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8289fb1a190SDave Jiang {
8299fb1a190SDave Jiang 	int rc;
8309fb1a190SDave Jiang 
83141cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8329fb1a190SDave Jiang 		rc = -EINVAL;
8339fb1a190SDave Jiang 		goto err;
8349fb1a190SDave Jiang 	}
8359fb1a190SDave Jiang 
8369fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8379fb1a190SDave Jiang 		rc = -EINVAL;
8389fb1a190SDave Jiang 		goto err;
8399fb1a190SDave Jiang 	}
8409fb1a190SDave Jiang 
8419fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8429fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8439fb1a190SDave Jiang 	if (rc < 0)
8449fb1a190SDave Jiang 		goto err;
8459fb1a190SDave Jiang 
8469fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8479fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8489fb1a190SDave Jiang 
8499fb1a190SDave Jiang 	err_inj->status = 0;
8509fb1a190SDave Jiang 	return 0;
8519fb1a190SDave Jiang 
8529fb1a190SDave Jiang err:
8539fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8549fb1a190SDave Jiang 	return rc;
8559fb1a190SDave Jiang }
8569fb1a190SDave Jiang 
8579fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8589fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8599fb1a190SDave Jiang {
8609fb1a190SDave Jiang 	int rc;
8619fb1a190SDave Jiang 
86241cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8639fb1a190SDave Jiang 		rc = -EINVAL;
8649fb1a190SDave Jiang 		goto err;
8659fb1a190SDave Jiang 	}
8669fb1a190SDave Jiang 
8679fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8689fb1a190SDave Jiang 		rc = -EINVAL;
8699fb1a190SDave Jiang 		goto err;
8709fb1a190SDave Jiang 	}
8719fb1a190SDave Jiang 
8729fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8739fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8749fb1a190SDave Jiang 
8759fb1a190SDave Jiang 	err_clr->status = 0;
8769fb1a190SDave Jiang 	return 0;
8779fb1a190SDave Jiang 
8789fb1a190SDave Jiang err:
8799fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8809fb1a190SDave Jiang 	return rc;
8819fb1a190SDave Jiang }
8829fb1a190SDave Jiang 
8839fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8849fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8859fb1a190SDave Jiang 		unsigned int buf_len)
8869fb1a190SDave Jiang {
8879fb1a190SDave Jiang 	struct badrange_entry *be;
8889fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8899fb1a190SDave Jiang 	int i = 0;
8909fb1a190SDave Jiang 
8919fb1a190SDave Jiang 	err_stat->status = 0;
8929fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8939fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8949fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8959fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8969fb1a190SDave Jiang 		i++;
8979fb1a190SDave Jiang 		if (i > max)
8989fb1a190SDave Jiang 			break;
8999fb1a190SDave Jiang 	}
9009fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
9019fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
9029fb1a190SDave Jiang 
9039fb1a190SDave Jiang 	return 0;
9049fb1a190SDave Jiang }
9059fb1a190SDave Jiang 
906674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
907674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
908674d8bdeSDave Jiang {
909674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
910674d8bdeSDave Jiang 
911674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
912674d8bdeSDave Jiang 		return -EINVAL;
913674d8bdeSDave Jiang 
914674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
915674d8bdeSDave Jiang 	case 0:
916674d8bdeSDave Jiang 		nd_cmd->status = 0;
917674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
918674d8bdeSDave Jiang 				__func__);
919674d8bdeSDave Jiang 		break;
920674d8bdeSDave Jiang 	case 1:
921674d8bdeSDave Jiang 		nd_cmd->status = 0;
922674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
923674d8bdeSDave Jiang 				__func__);
924674d8bdeSDave Jiang 		break;
925674d8bdeSDave Jiang 	default:
926674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
927674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
928674d8bdeSDave Jiang 		break;
929674d8bdeSDave Jiang 	}
930674d8bdeSDave Jiang 
931674d8bdeSDave Jiang 
932674d8bdeSDave Jiang 	return 0;
933674d8bdeSDave Jiang }
934674d8bdeSDave Jiang 
93539611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
93639611e83SDan Williams {
93739611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
93839611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
93939611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
94039611e83SDan Williams 		return -EIO;
94139611e83SDan Williams 	}
94239611e83SDan Williams 	return rc;
94339611e83SDan Williams }
94439611e83SDan Williams 
9453c13e2acSDave Jiang static int nd_intel_test_cmd_security_status(struct nfit_test *t,
9463c13e2acSDave Jiang 		struct nd_intel_get_security_state *nd_cmd,
9473c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9483c13e2acSDave Jiang {
9493c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9503c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9513c13e2acSDave Jiang 
9523c13e2acSDave Jiang 	nd_cmd->status = 0;
9533c13e2acSDave Jiang 	nd_cmd->state = sec->state;
954ecaa4a97SDave Jiang 	nd_cmd->extended_state = sec->ext_state;
9553c13e2acSDave Jiang 	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);
9563c13e2acSDave Jiang 
9573c13e2acSDave Jiang 	return 0;
9583c13e2acSDave Jiang }
9593c13e2acSDave Jiang 
9603c13e2acSDave Jiang static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
9613c13e2acSDave Jiang 		struct nd_intel_unlock_unit *nd_cmd,
9623c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9633c13e2acSDave Jiang {
9643c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9653c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9663c13e2acSDave Jiang 
9673c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
9683c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
9693c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9703c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
9713c13e2acSDave Jiang 				sec->state);
9723c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
9733c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9743c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9753c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid passphrase\n");
9763c13e2acSDave Jiang 	} else {
9773c13e2acSDave Jiang 		nd_cmd->status = 0;
9783c13e2acSDave Jiang 		sec->state = ND_INTEL_SEC_STATE_ENABLED;
9793c13e2acSDave Jiang 		dev_dbg(dev, "Unit unlocked\n");
9803c13e2acSDave Jiang 	}
9813c13e2acSDave Jiang 
9823c13e2acSDave Jiang 	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
9833c13e2acSDave Jiang 	return 0;
9843c13e2acSDave Jiang }
9853c13e2acSDave Jiang 
9863c13e2acSDave Jiang static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
9873c13e2acSDave Jiang 		struct nd_intel_set_passphrase *nd_cmd,
9883c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9893c13e2acSDave Jiang {
9903c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9913c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9923c13e2acSDave Jiang 
9933c13e2acSDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
9943c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9953c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong security state\n");
9963c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
9973c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9983c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9993c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong passphrase\n");
10003c13e2acSDave Jiang 	} else {
10013c13e2acSDave Jiang 		memcpy(sec->passphrase, nd_cmd->new_pass,
10023c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
10033c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
10043c13e2acSDave Jiang 		nd_cmd->status = 0;
10053c13e2acSDave Jiang 		dev_dbg(dev, "passphrase updated\n");
10063c13e2acSDave Jiang 	}
10073c13e2acSDave Jiang 
10083c13e2acSDave Jiang 	return 0;
10093c13e2acSDave Jiang }
10103c13e2acSDave Jiang 
10113c13e2acSDave Jiang static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
10123c13e2acSDave Jiang 		struct nd_intel_freeze_lock *nd_cmd,
10133c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10143c13e2acSDave Jiang {
10153c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10163c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10173c13e2acSDave Jiang 
10183c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
10193c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10203c13e2acSDave Jiang 		dev_dbg(dev, "freeze lock: wrong security state\n");
10213c13e2acSDave Jiang 	} else {
10223c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
10233c13e2acSDave Jiang 		nd_cmd->status = 0;
10243c13e2acSDave Jiang 		dev_dbg(dev, "security frozen\n");
10253c13e2acSDave Jiang 	}
10263c13e2acSDave Jiang 
10273c13e2acSDave Jiang 	return 0;
10283c13e2acSDave Jiang }
10293c13e2acSDave Jiang 
10303c13e2acSDave Jiang static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
10313c13e2acSDave Jiang 		struct nd_intel_disable_passphrase *nd_cmd,
10323c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10333c13e2acSDave Jiang {
10343c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10353c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10363c13e2acSDave Jiang 
10373c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
10383c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
10393c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10403c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong security state\n");
10413c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10423c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10433c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10443c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
10453c13e2acSDave Jiang 	} else {
10463c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10473c13e2acSDave Jiang 		sec->state = 0;
10483c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: done\n");
10493c13e2acSDave Jiang 	}
10503c13e2acSDave Jiang 
10513c13e2acSDave Jiang 	return 0;
10523c13e2acSDave Jiang }
10533c13e2acSDave Jiang 
10543c13e2acSDave Jiang static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
10553c13e2acSDave Jiang 		struct nd_intel_secure_erase *nd_cmd,
10563c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10573c13e2acSDave Jiang {
10583c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10593c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10603c13e2acSDave Jiang 
1061037c8489SDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
10623c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10633c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong security state\n");
10643c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10653c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10663c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10673c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong passphrase\n");
10683c13e2acSDave Jiang 	} else {
1069037c8489SDave Jiang 		if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)
1070037c8489SDave Jiang 				&& (memcmp(nd_cmd->passphrase, zero_key,
1071037c8489SDave Jiang 					ND_INTEL_PASSPHRASE_SIZE) != 0)) {
1072037c8489SDave Jiang 			dev_dbg(dev, "invalid zero key\n");
1073037c8489SDave Jiang 			return 0;
1074037c8489SDave Jiang 		}
10753c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1076ecaa4a97SDave Jiang 		memset(sec->master_passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10773c13e2acSDave Jiang 		sec->state = 0;
1078ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
10793c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: done\n");
10803c13e2acSDave Jiang 	}
10813c13e2acSDave Jiang 
10823c13e2acSDave Jiang 	return 0;
10833c13e2acSDave Jiang }
10843c13e2acSDave Jiang 
1085926f7480SDave Jiang static int nd_intel_test_cmd_overwrite(struct nfit_test *t,
1086926f7480SDave Jiang 		struct nd_intel_overwrite *nd_cmd,
1087926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1088926f7480SDave Jiang {
1089926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1090926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1091926f7480SDave Jiang 
1092926f7480SDave Jiang 	if ((sec->state & ND_INTEL_SEC_STATE_ENABLED) &&
1093926f7480SDave Jiang 			memcmp(nd_cmd->passphrase, sec->passphrase,
1094926f7480SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1095926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1096926f7480SDave Jiang 		dev_dbg(dev, "overwrite: wrong passphrase\n");
1097926f7480SDave Jiang 		return 0;
1098926f7480SDave Jiang 	}
1099926f7480SDave Jiang 
11002170a0d5SDave Jiang 	sec->old_state = sec->state;
1101926f7480SDave Jiang 	sec->state = ND_INTEL_SEC_STATE_OVERWRITE;
1102926f7480SDave Jiang 	dev_dbg(dev, "overwrite progressing.\n");
1103926f7480SDave Jiang 	sec->overwrite_end_time = get_jiffies_64() + 5 * HZ;
1104926f7480SDave Jiang 
1105926f7480SDave Jiang 	return 0;
1106926f7480SDave Jiang }
1107926f7480SDave Jiang 
1108926f7480SDave Jiang static int nd_intel_test_cmd_query_overwrite(struct nfit_test *t,
1109926f7480SDave Jiang 		struct nd_intel_query_overwrite *nd_cmd,
1110926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1111926f7480SDave Jiang {
1112926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1113926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1114926f7480SDave Jiang 
1115926f7480SDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_OVERWRITE)) {
1116926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_SEQUENCE_ERR;
1117926f7480SDave Jiang 		return 0;
1118926f7480SDave Jiang 	}
1119926f7480SDave Jiang 
1120926f7480SDave Jiang 	if (time_is_before_jiffies64(sec->overwrite_end_time)) {
1121926f7480SDave Jiang 		sec->overwrite_end_time = 0;
11222170a0d5SDave Jiang 		sec->state = sec->old_state;
11232170a0d5SDave Jiang 		sec->old_state = 0;
1124ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1125926f7480SDave Jiang 		dev_dbg(dev, "overwrite is complete\n");
1126926f7480SDave Jiang 	} else
1127926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_INPROGRESS;
1128926f7480SDave Jiang 	return 0;
1129926f7480SDave Jiang }
1130926f7480SDave Jiang 
1131ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_set_pass(struct nfit_test *t,
1132ecaa4a97SDave Jiang 		struct nd_intel_set_master_passphrase *nd_cmd,
1133ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1134ecaa4a97SDave Jiang {
1135ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1136ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1137ecaa4a97SDave Jiang 
1138ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1139ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1140ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong state\n");
1141ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1142ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1143ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong security state\n");
1144ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->master_passphrase,
1145ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1146ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1147ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: wrong passphrase\n");
1148ecaa4a97SDave Jiang 	} else {
1149ecaa4a97SDave Jiang 		memcpy(sec->master_passphrase, nd_cmd->new_pass,
1150ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
1151ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1152ecaa4a97SDave Jiang 		dev_dbg(dev, "master passphrase: updated\n");
1153ecaa4a97SDave Jiang 	}
1154ecaa4a97SDave Jiang 
1155ecaa4a97SDave Jiang 	return 0;
1156ecaa4a97SDave Jiang }
1157ecaa4a97SDave Jiang 
1158ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_secure_erase(struct nfit_test *t,
1159ecaa4a97SDave Jiang 		struct nd_intel_master_secure_erase *nd_cmd,
1160ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1161ecaa4a97SDave Jiang {
1162ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1163ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1164ecaa4a97SDave Jiang 
1165ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1166ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1167ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong state\n");
1168ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1169ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1170ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong security state\n");
1171ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->master_passphrase,
1172ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1173ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1174ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: wrong passphrase\n");
1175ecaa4a97SDave Jiang 	} else {
1176ecaa4a97SDave Jiang 		/* we do not erase master state passphrase ever */
1177ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1178ecaa4a97SDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1179ecaa4a97SDave Jiang 		sec->state = 0;
1180ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: done\n");
1181ecaa4a97SDave Jiang 	}
1182ecaa4a97SDave Jiang 
1183ecaa4a97SDave Jiang 	return 0;
1184ecaa4a97SDave Jiang }
1185ecaa4a97SDave Jiang 
1186916566aeSDan Williams static unsigned long last_activate;
1187916566aeSDan Williams 
1188916566aeSDan Williams static int nvdimm_bus_intel_fw_activate_businfo(struct nfit_test *t,
1189916566aeSDan Williams 		struct nd_intel_bus_fw_activate_businfo *nd_cmd,
1190916566aeSDan Williams 		unsigned int buf_len)
1191916566aeSDan Williams {
1192916566aeSDan Williams 	int i, armed = 0;
1193916566aeSDan Williams 	int state;
1194916566aeSDan Williams 	u64 tmo;
1195916566aeSDan Williams 
1196916566aeSDan Williams 	for (i = 0; i < NUM_DCR; i++) {
1197916566aeSDan Williams 		struct nfit_test_fw *fw = &t->fw[i];
1198916566aeSDan Williams 
1199916566aeSDan Williams 		if (fw->armed)
1200916566aeSDan Williams 			armed++;
1201916566aeSDan Williams 	}
1202916566aeSDan Williams 
1203916566aeSDan Williams 	/*
1204916566aeSDan Williams 	 * Emulate 3 second activation max, and 1 second incremental
1205916566aeSDan Williams 	 * quiesce time per dimm requiring multiple activates to get all
1206916566aeSDan Williams 	 * DIMMs updated.
1207916566aeSDan Williams 	 */
1208916566aeSDan Williams 	if (armed)
1209916566aeSDan Williams 		state = ND_INTEL_FWA_ARMED;
1210916566aeSDan Williams 	else if (!last_activate || time_after(jiffies, last_activate + 3 * HZ))
1211916566aeSDan Williams 		state = ND_INTEL_FWA_IDLE;
1212916566aeSDan Williams 	else
1213916566aeSDan Williams 		state = ND_INTEL_FWA_BUSY;
1214916566aeSDan Williams 
1215916566aeSDan Williams 	tmo = armed * USEC_PER_SEC;
1216916566aeSDan Williams 	*nd_cmd = (struct nd_intel_bus_fw_activate_businfo) {
1217916566aeSDan Williams 		.capability = ND_INTEL_BUS_FWA_CAP_FWQUIESCE
1218916566aeSDan Williams 			| ND_INTEL_BUS_FWA_CAP_OSQUIESCE
1219916566aeSDan Williams 			| ND_INTEL_BUS_FWA_CAP_RESET,
1220916566aeSDan Williams 		.state = state,
1221916566aeSDan Williams 		.activate_tmo = tmo,
1222916566aeSDan Williams 		.cpu_quiesce_tmo = tmo,
1223916566aeSDan Williams 		.io_quiesce_tmo = tmo,
1224916566aeSDan Williams 		.max_quiesce_tmo = 3 * USEC_PER_SEC,
1225916566aeSDan Williams 	};
1226916566aeSDan Williams 
1227916566aeSDan Williams 	return 0;
1228916566aeSDan Williams }
1229916566aeSDan Williams 
1230916566aeSDan Williams static int nvdimm_bus_intel_fw_activate(struct nfit_test *t,
1231916566aeSDan Williams 		struct nd_intel_bus_fw_activate *nd_cmd,
1232916566aeSDan Williams 		unsigned int buf_len)
1233916566aeSDan Williams {
1234916566aeSDan Williams 	struct nd_intel_bus_fw_activate_businfo info;
1235916566aeSDan Williams 	u32 status = 0;
1236916566aeSDan Williams 	int i;
1237916566aeSDan Williams 
1238916566aeSDan Williams 	nvdimm_bus_intel_fw_activate_businfo(t, &info, sizeof(info));
1239916566aeSDan Williams 	if (info.state == ND_INTEL_FWA_BUSY)
1240916566aeSDan Williams 		status = ND_INTEL_BUS_FWA_STATUS_BUSY;
1241916566aeSDan Williams 	else if (info.activate_tmo > info.max_quiesce_tmo)
1242916566aeSDan Williams 		status = ND_INTEL_BUS_FWA_STATUS_TMO;
1243916566aeSDan Williams 	else if (info.state == ND_INTEL_FWA_IDLE)
1244916566aeSDan Williams 		status = ND_INTEL_BUS_FWA_STATUS_NOARM;
1245916566aeSDan Williams 
1246916566aeSDan Williams 	dev_dbg(&t->pdev.dev, "status: %d\n", status);
1247916566aeSDan Williams 	nd_cmd->status = status;
1248916566aeSDan Williams 	if (status && status != ND_INTEL_BUS_FWA_STATUS_TMO)
1249916566aeSDan Williams 		return 0;
1250916566aeSDan Williams 
1251916566aeSDan Williams 	last_activate = jiffies;
1252916566aeSDan Williams 	for (i = 0; i < NUM_DCR; i++) {
1253916566aeSDan Williams 		struct nfit_test_fw *fw = &t->fw[i];
1254916566aeSDan Williams 
1255916566aeSDan Williams 		if (!fw->armed)
1256916566aeSDan Williams 			continue;
1257916566aeSDan Williams 		if (fw->state != FW_STATE_UPDATED)
1258916566aeSDan Williams 			fw->missed_activate = true;
1259916566aeSDan Williams 		else
1260916566aeSDan Williams 			fw->state = FW_STATE_NEW;
1261916566aeSDan Williams 		fw->armed = false;
1262916566aeSDan Williams 		fw->last_activate = last_activate;
1263916566aeSDan Williams 	}
1264916566aeSDan Williams 
1265916566aeSDan Williams 	return 0;
1266916566aeSDan Williams }
1267916566aeSDan Williams 
1268916566aeSDan Williams static int nd_intel_test_cmd_fw_activate_dimminfo(struct nfit_test *t,
1269916566aeSDan Williams 		struct nd_intel_fw_activate_dimminfo *nd_cmd,
1270916566aeSDan Williams 		unsigned int buf_len, int dimm)
1271916566aeSDan Williams {
1272916566aeSDan Williams 	struct nd_intel_bus_fw_activate_businfo info;
1273916566aeSDan Williams 	struct nfit_test_fw *fw = &t->fw[dimm];
1274916566aeSDan Williams 	u32 result, state;
1275916566aeSDan Williams 
1276916566aeSDan Williams 	nvdimm_bus_intel_fw_activate_businfo(t, &info, sizeof(info));
1277916566aeSDan Williams 
1278916566aeSDan Williams 	if (info.state == ND_INTEL_FWA_BUSY)
1279916566aeSDan Williams 		state = ND_INTEL_FWA_BUSY;
1280916566aeSDan Williams 	else if (info.state == ND_INTEL_FWA_IDLE)
1281916566aeSDan Williams 		state = ND_INTEL_FWA_IDLE;
1282916566aeSDan Williams 	else if (fw->armed)
1283916566aeSDan Williams 		state = ND_INTEL_FWA_ARMED;
1284916566aeSDan Williams 	else
1285916566aeSDan Williams 		state = ND_INTEL_FWA_IDLE;
1286916566aeSDan Williams 
1287916566aeSDan Williams 	result = ND_INTEL_DIMM_FWA_NONE;
1288916566aeSDan Williams 	if (last_activate && fw->last_activate == last_activate &&
1289916566aeSDan Williams 			state == ND_INTEL_FWA_IDLE) {
1290916566aeSDan Williams 		if (fw->missed_activate)
1291916566aeSDan Williams 			result = ND_INTEL_DIMM_FWA_NOTSTAGED;
1292916566aeSDan Williams 		else
1293916566aeSDan Williams 			result = ND_INTEL_DIMM_FWA_SUCCESS;
1294916566aeSDan Williams 	}
1295916566aeSDan Williams 
1296916566aeSDan Williams 	*nd_cmd = (struct nd_intel_fw_activate_dimminfo) {
1297916566aeSDan Williams 		.result = result,
1298916566aeSDan Williams 		.state = state,
1299916566aeSDan Williams 	};
1300916566aeSDan Williams 
1301916566aeSDan Williams 	return 0;
1302916566aeSDan Williams }
1303916566aeSDan Williams 
1304916566aeSDan Williams static int nd_intel_test_cmd_fw_activate_arm(struct nfit_test *t,
1305916566aeSDan Williams 		struct nd_intel_fw_activate_arm *nd_cmd,
1306916566aeSDan Williams 		unsigned int buf_len, int dimm)
1307916566aeSDan Williams {
1308916566aeSDan Williams 	struct nfit_test_fw *fw = &t->fw[dimm];
1309916566aeSDan Williams 
1310916566aeSDan Williams 	fw->armed = nd_cmd->activate_arm == ND_INTEL_DIMM_FWA_ARM;
1311916566aeSDan Williams 	nd_cmd->status = 0;
1312916566aeSDan Williams 	return 0;
1313916566aeSDan Williams }
1314ecaa4a97SDave Jiang 
1315bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
1316bfbaa952SDave Jiang {
1317bfbaa952SDave Jiang 	int i;
1318bfbaa952SDave Jiang 
1319bfbaa952SDave Jiang 	/* lookup per-dimm data */
1320bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1321bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1322bfbaa952SDave Jiang 			break;
1323bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
1324bfbaa952SDave Jiang 		return -ENXIO;
1325bfbaa952SDave Jiang 	return i;
1326bfbaa952SDave Jiang }
1327bfbaa952SDave Jiang 
13280d47c4dfSDan Williams static void nfit_ctl_dbg(struct acpi_nfit_desc *acpi_desc,
13290d47c4dfSDan Williams 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
13300d47c4dfSDan Williams 		unsigned int len)
13310d47c4dfSDan Williams {
13320d47c4dfSDan Williams 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
13330d47c4dfSDan Williams 	unsigned int func = cmd;
13340d47c4dfSDan Williams 	unsigned int family = 0;
13350d47c4dfSDan Williams 
13360d47c4dfSDan Williams 	if (cmd == ND_CMD_CALL) {
13370d47c4dfSDan Williams 		struct nd_cmd_pkg *pkg = buf;
13380d47c4dfSDan Williams 
13390d47c4dfSDan Williams 		len = pkg->nd_size_in;
13400d47c4dfSDan Williams 		family = pkg->nd_family;
13410d47c4dfSDan Williams 		buf = pkg->nd_payload;
13420d47c4dfSDan Williams 		func = pkg->nd_command;
13430d47c4dfSDan Williams 	}
13440d47c4dfSDan Williams 	dev_dbg(&t->pdev.dev, "%s family: %d cmd: %d: func: %d input length: %d\n",
13450d47c4dfSDan Williams 			nvdimm ? nvdimm_name(nvdimm) : "bus", family, cmd, func,
13460d47c4dfSDan Williams 			len);
13470d47c4dfSDan Williams 	print_hex_dump_debug("nvdimm in  ", DUMP_PREFIX_OFFSET, 16, 4,
13480d47c4dfSDan Williams 			buf, min(len, 256u), true);
13490d47c4dfSDan Williams }
13500d47c4dfSDan Williams 
135139c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
135239c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1353aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
135439c686b8SVishal Verma {
135539c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
135639c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
13576634fb06SDan Williams 	unsigned int func = cmd;
1358f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1359f471f1a7SDan Williams 
1360f471f1a7SDan Williams 	if (!cmd_rc)
1361f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1362f471f1a7SDan Williams 	*cmd_rc = 0;
136339c686b8SVishal Verma 
13640d47c4dfSDan Williams 	nfit_ctl_dbg(acpi_desc, nvdimm, cmd, buf, buf_len);
13650d47c4dfSDan Williams 
136639c686b8SVishal Verma 	if (nvdimm) {
136739c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1368e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
136939c686b8SVishal Verma 
13706634fb06SDan Williams 		if (!nfit_mem)
13716634fb06SDan Williams 			return -ENOTTY;
13726634fb06SDan Williams 
13736634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
13746634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
13756634fb06SDan Williams 
13766634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
13776634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
13786634fb06SDan Williams 			func = call_pkg->nd_command;
13796634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
13806634fb06SDan Williams 				return -ENOTTY;
1381bfbaa952SDave Jiang 
1382bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1383bfbaa952SDave Jiang 			if (i < 0)
1384bfbaa952SDave Jiang 				return i;
138524770658SDan Williams 			if (i >= NUM_DCR) {
138624770658SDan Williams 				dev_WARN_ONCE(&t->pdev.dev, 1,
138724770658SDan Williams 						"ND_CMD_CALL only valid for nfit_test0\n");
138824770658SDan Williams 				return -EINVAL;
138924770658SDan Williams 			}
1390bfbaa952SDave Jiang 
1391bfbaa952SDave Jiang 			switch (func) {
13923c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
13933c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
13943c13e2acSDave Jiang 						buf, buf_len, i);
13953c13e2acSDave Jiang 				break;
13963c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
13973c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
13983c13e2acSDave Jiang 						buf, buf_len, i);
13993c13e2acSDave Jiang 				break;
14003c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
14013c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
14023c13e2acSDave Jiang 						buf, buf_len, i);
14033c13e2acSDave Jiang 				break;
14043c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
14053c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
14063c13e2acSDave Jiang 						buf, buf_len, i);
14073c13e2acSDave Jiang 				break;
14083c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
14093c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
14103c13e2acSDave Jiang 						buf, buf_len, i);
14113c13e2acSDave Jiang 				break;
14123c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
14133c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
14143c13e2acSDave Jiang 						buf, buf_len, i);
14153c13e2acSDave Jiang 				break;
1416926f7480SDave Jiang 			case NVDIMM_INTEL_OVERWRITE:
1417926f7480SDave Jiang 				rc = nd_intel_test_cmd_overwrite(t,
141824770658SDan Williams 						buf, buf_len, i);
1419926f7480SDave Jiang 				break;
1420926f7480SDave Jiang 			case NVDIMM_INTEL_QUERY_OVERWRITE:
1421926f7480SDave Jiang 				rc = nd_intel_test_cmd_query_overwrite(t,
142224770658SDan Williams 						buf, buf_len, i);
1423926f7480SDave Jiang 				break;
1424ecaa4a97SDave Jiang 			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
1425ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_set_pass(t,
1426ecaa4a97SDave Jiang 						buf, buf_len, i);
1427ecaa4a97SDave Jiang 				break;
1428ecaa4a97SDave Jiang 			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
1429ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_secure_erase(t,
1430ecaa4a97SDave Jiang 						buf, buf_len, i);
1431ecaa4a97SDave Jiang 				break;
1432916566aeSDan Williams 			case NVDIMM_INTEL_FW_ACTIVATE_DIMMINFO:
1433916566aeSDan Williams 				rc = nd_intel_test_cmd_fw_activate_dimminfo(
1434916566aeSDan Williams 					t, buf, buf_len, i);
1435916566aeSDan Williams 				break;
1436916566aeSDan Williams 			case NVDIMM_INTEL_FW_ACTIVATE_ARM:
1437916566aeSDan Williams 				rc = nd_intel_test_cmd_fw_activate_arm(
1438916566aeSDan Williams 					t, buf, buf_len, i);
1439916566aeSDan Williams 				break;
1440674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
144139611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1442674d8bdeSDave Jiang 						buf, buf_len);
144339611e83SDan Williams 				break;
1444bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
144539611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
144624770658SDan Williams 						buf_len, i);
144739611e83SDan Williams 				break;
1448bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
144939611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
145024770658SDan Williams 						buf_len, i);
145139611e83SDan Williams 				break;
1452bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
145339611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
145424770658SDan Williams 						buf_len, i);
145539611e83SDan Williams 				break;
1456bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
145739611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
145824770658SDan Williams 						buf_len, i);
145939611e83SDan Williams 				break;
1460bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
146139611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
146224770658SDan Williams 						buf_len, i);
146339611e83SDan Williams 				break;
1464bfbaa952SDave Jiang 			case ND_INTEL_SMART:
146539611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
146624770658SDan Williams 						&t->smart[i]);
146739611e83SDan Williams 				break;
1468bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
146939611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1470bfbaa952SDave Jiang 						buf_len,
147124770658SDan Williams 						&t->smart_threshold[i]);
147239611e83SDan Williams 				break;
1473bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
147439611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1475bfbaa952SDave Jiang 						buf_len,
147624770658SDan Williams 						&t->smart_threshold[i],
147724770658SDan Williams 						&t->smart[i],
1478bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
147939611e83SDan Williams 				break;
14804cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
148139611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
14824cf260fcSVishal Verma 						buf_len,
148324770658SDan Williams 						&t->smart_threshold[i],
148424770658SDan Williams 						&t->smart[i],
14854cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
148639611e83SDan Williams 				break;
1487bfbaa952SDave Jiang 			default:
1488bfbaa952SDave Jiang 				return -ENOTTY;
1489bfbaa952SDave Jiang 			}
149039611e83SDan Williams 			return override_return_code(i, func, rc);
14916634fb06SDan Williams 		}
14926634fb06SDan Williams 
14936634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
14946634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
149539c686b8SVishal Verma 			return -ENOTTY;
149639c686b8SVishal Verma 
1497bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1498bfbaa952SDave Jiang 		if (i < 0)
1499bfbaa952SDave Jiang 			return i;
150073606afdSDan Williams 
15016634fb06SDan Williams 		switch (func) {
150239c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
150339c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
150439c686b8SVishal Verma 			break;
150539c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
150639c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1507dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
150839c686b8SVishal Verma 			break;
150939c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
151039c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1511dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
151239c686b8SVishal Verma 			break;
15136bc75619SDan Williams 		default:
15146bc75619SDan Williams 			return -ENOTTY;
15156bc75619SDan Williams 		}
151639611e83SDan Williams 		return override_return_code(i, func, rc);
151739c686b8SVishal Verma 	} else {
1518f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
151910246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
152010246dc8SYasunori Goto 
152110246dc8SYasunori Goto 		if (!nd_desc)
152210246dc8SYasunori Goto 			return -ENOTTY;
152310246dc8SYasunori Goto 
1524916566aeSDan Williams 		if (cmd == ND_CMD_CALL && call_pkg->nd_family
1525916566aeSDan Williams 				== NVDIMM_BUS_FAMILY_NFIT) {
152610246dc8SYasunori Goto 			func = call_pkg->nd_command;
152710246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
152810246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
152910246dc8SYasunori Goto 
153010246dc8SYasunori Goto 			switch (func) {
153110246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
153210246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
153310246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
153410246dc8SYasunori Goto 				return rc;
15359fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
15369fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
15379fb1a190SDave Jiang 					buf_len);
15389fb1a190SDave Jiang 				return rc;
15399fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
15409fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
15419fb1a190SDave Jiang 					buf_len);
15429fb1a190SDave Jiang 				return rc;
15439fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
15449fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
15459fb1a190SDave Jiang 					buf_len);
15469fb1a190SDave Jiang 				return rc;
154710246dc8SYasunori Goto 			default:
154810246dc8SYasunori Goto 				return -ENOTTY;
154910246dc8SYasunori Goto 			}
1550916566aeSDan Williams 		} else if (cmd == ND_CMD_CALL && call_pkg->nd_family
1551916566aeSDan Williams 				== NVDIMM_BUS_FAMILY_INTEL) {
1552916566aeSDan Williams 			func = call_pkg->nd_command;
1553916566aeSDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
1554916566aeSDan Williams 			buf = (void *) call_pkg->nd_payload;
1555916566aeSDan Williams 
1556916566aeSDan Williams 			switch (func) {
1557916566aeSDan Williams 			case NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO:
1558916566aeSDan Williams 				rc = nvdimm_bus_intel_fw_activate_businfo(t,
1559916566aeSDan Williams 						buf, buf_len);
1560916566aeSDan Williams 				return rc;
1561916566aeSDan Williams 			case NVDIMM_BUS_INTEL_FW_ACTIVATE:
1562916566aeSDan Williams 				rc = nvdimm_bus_intel_fw_activate(t, buf,
1563916566aeSDan Williams 						buf_len);
1564916566aeSDan Williams 				return rc;
1565916566aeSDan Williams 			default:
1566916566aeSDan Williams 				return -ENOTTY;
156710246dc8SYasunori Goto 			}
1568916566aeSDan Williams 		} else if (cmd == ND_CMD_CALL)
1569916566aeSDan Williams 			return -ENOTTY;
1570f471f1a7SDan Williams 
1571e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
157239c686b8SVishal Verma 			return -ENOTTY;
157339c686b8SVishal Verma 
15746634fb06SDan Williams 		switch (func) {
157539c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
157639c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
157739c686b8SVishal Verma 			break;
157839c686b8SVishal Verma 		case ND_CMD_ARS_START:
15799fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
15809fb1a190SDave Jiang 					buf_len, cmd_rc);
158139c686b8SVishal Verma 			break;
158239c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1583f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1584f471f1a7SDan Williams 					cmd_rc);
158539c686b8SVishal Verma 			break;
1586d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
15875e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1588d4f32367SDan Williams 			break;
158939c686b8SVishal Verma 		default:
159039c686b8SVishal Verma 			return -ENOTTY;
159139c686b8SVishal Verma 		}
159239c686b8SVishal Verma 	}
15936bc75619SDan Williams 
15946bc75619SDan Williams 	return rc;
15956bc75619SDan Williams }
15966bc75619SDan Williams 
15976bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
15986bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
15996bc75619SDan Williams 
16006bc75619SDan Williams static void release_nfit_res(void *data)
16016bc75619SDan Williams {
16026bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
16036bc75619SDan Williams 
16046bc75619SDan Williams 	spin_lock(&nfit_test_lock);
16056bc75619SDan Williams 	list_del(&nfit_res->list);
16066bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
16076bc75619SDan Williams 
1608e3f5df76SDan Williams 	if (resource_size(&nfit_res->res) >= DIMM_SIZE)
1609e3f5df76SDan Williams 		gen_pool_free(nfit_pool, nfit_res->res.start,
1610e3f5df76SDan Williams 				resource_size(&nfit_res->res));
16116bc75619SDan Williams 	vfree(nfit_res->buf);
16126bc75619SDan Williams 	kfree(nfit_res);
16136bc75619SDan Williams }
16146bc75619SDan Williams 
16156bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
16166bc75619SDan Williams 		void *buf)
16176bc75619SDan Williams {
16186bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
16196bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
16206bc75619SDan Williams 			GFP_KERNEL);
16216bc75619SDan Williams 	int rc;
16226bc75619SDan Williams 
1623e3f5df76SDan Williams 	if (!buf || !nfit_res || !*dma)
16246bc75619SDan Williams 		goto err;
16256bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
16266bc75619SDan Williams 	if (rc)
16276bc75619SDan Williams 		goto err;
16286bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
16296bc75619SDan Williams 	memset(buf, 0, size);
16306bc75619SDan Williams 	nfit_res->dev = dev;
16316bc75619SDan Williams 	nfit_res->buf = buf;
1632bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1633bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1634bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1635bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1636bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
16376bc75619SDan Williams 	spin_lock(&nfit_test_lock);
16386bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
16396bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
16406bc75619SDan Williams 
16416bc75619SDan Williams 	return nfit_res->buf;
16426bc75619SDan Williams  err:
1643e3f5df76SDan Williams 	if (*dma && size >= DIMM_SIZE)
1644e3f5df76SDan Williams 		gen_pool_free(nfit_pool, *dma, size);
1645ee8520feSDan Williams 	if (buf)
16466bc75619SDan Williams 		vfree(buf);
16476bc75619SDan Williams 	kfree(nfit_res);
16486bc75619SDan Williams 	return NULL;
16496bc75619SDan Williams }
16506bc75619SDan Williams 
16516bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
16526bc75619SDan Williams {
1653e3f5df76SDan Williams 	struct genpool_data_align data = {
1654e3f5df76SDan Williams 		.align = SZ_128M,
1655e3f5df76SDan Williams 	};
16566bc75619SDan Williams 	void *buf = vmalloc(size);
16576bc75619SDan Williams 
1658e3f5df76SDan Williams 	if (size >= DIMM_SIZE)
1659e3f5df76SDan Williams 		*dma = gen_pool_alloc_algo(nfit_pool, size,
1660e3f5df76SDan Williams 				gen_pool_first_fit_align, &data);
1661e3f5df76SDan Williams 	else
16626bc75619SDan Williams 		*dma = (unsigned long) buf;
16636bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
16646bc75619SDan Williams }
16656bc75619SDan Williams 
16666bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
16676bc75619SDan Williams {
16686bc75619SDan Williams 	int i;
16696bc75619SDan Williams 
16706bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
16716bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
16726bc75619SDan Williams 		struct nfit_test *t = instances[i];
16736bc75619SDan Williams 
16746bc75619SDan Williams 		if (!t)
16756bc75619SDan Williams 			continue;
16766bc75619SDan Williams 		spin_lock(&nfit_test_lock);
16776bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1678bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1679bd4cd745SDan Williams 						+ resource_size(&n->res))) {
16806bc75619SDan Williams 				nfit_res = n;
16816bc75619SDan Williams 				break;
16826bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
16836bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1684bd4cd745SDan Williams 						+ resource_size(&n->res))) {
16856bc75619SDan Williams 				nfit_res = n;
16866bc75619SDan Williams 				break;
16876bc75619SDan Williams 			}
16886bc75619SDan Williams 		}
16896bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
16906bc75619SDan Williams 		if (nfit_res)
16916bc75619SDan Williams 			return nfit_res;
16926bc75619SDan Williams 	}
16936bc75619SDan Williams 
16946bc75619SDan Williams 	return NULL;
16956bc75619SDan Williams }
16966bc75619SDan Williams 
1697f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1698f471f1a7SDan Williams {
16999fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1700f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
17019fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1702f471f1a7SDan Williams 	if (!ars_state->ars_status)
1703f471f1a7SDan Williams 		return -ENOMEM;
1704f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1705f471f1a7SDan Williams 	return 0;
1706f471f1a7SDan Williams }
1707f471f1a7SDan Williams 
1708231bf117SDan Williams static void put_dimms(void *data)
1709231bf117SDan Williams {
1710718fda67SDan Williams 	struct nfit_test *t = data;
1711231bf117SDan Williams 	int i;
1712231bf117SDan Williams 
1713718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1714718fda67SDan Williams 		if (t->dimm_dev[i])
1715718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1716231bf117SDan Williams }
1717231bf117SDan Williams 
1718231bf117SDan Williams static struct class *nfit_test_dimm;
1719231bf117SDan Williams 
172073606afdSDan Williams static int dimm_name_to_id(struct device *dev)
172173606afdSDan Williams {
172273606afdSDan Williams 	int dimm;
172373606afdSDan Williams 
1724718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
172573606afdSDan Williams 		return -ENXIO;
172673606afdSDan Williams 	return dimm;
172773606afdSDan Williams }
172873606afdSDan Williams 
172973606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
173073606afdSDan Williams 		char *buf)
173173606afdSDan Williams {
173273606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
173373606afdSDan Williams 
173473606afdSDan Williams 	if (dimm < 0)
173573606afdSDan Williams 		return dimm;
173673606afdSDan Williams 
173719357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
173873606afdSDan Williams }
173973606afdSDan Williams DEVICE_ATTR_RO(handle);
174073606afdSDan Williams 
174173606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
174273606afdSDan Williams 		char *buf)
174373606afdSDan Williams {
174473606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
174573606afdSDan Williams 
174673606afdSDan Williams 	if (dimm < 0)
174773606afdSDan Williams 		return dimm;
174873606afdSDan Williams 
174973606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
175073606afdSDan Williams }
175173606afdSDan Williams 
175273606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
175373606afdSDan Williams 		const char *buf, size_t size)
175473606afdSDan Williams {
175573606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
175673606afdSDan Williams 	unsigned long val;
175773606afdSDan Williams 	ssize_t rc;
175873606afdSDan Williams 
175973606afdSDan Williams 	if (dimm < 0)
176073606afdSDan Williams 		return dimm;
176173606afdSDan Williams 
176273606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
176373606afdSDan Williams 	if (rc)
176473606afdSDan Williams 		return rc;
176573606afdSDan Williams 
176673606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
176773606afdSDan Williams 	return size;
176873606afdSDan Williams }
176973606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
177073606afdSDan Williams 
177155c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
177255c72ab6SDan Williams 		char *buf)
177355c72ab6SDan Williams {
177455c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
177555c72ab6SDan Williams 
177655c72ab6SDan Williams 	if (dimm < 0)
177755c72ab6SDan Williams 		return dimm;
177855c72ab6SDan Williams 
177955c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
178055c72ab6SDan Williams }
178155c72ab6SDan Williams 
178255c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
178355c72ab6SDan Williams 		const char *buf, size_t size)
178455c72ab6SDan Williams {
178555c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
178655c72ab6SDan Williams 	unsigned long val;
178755c72ab6SDan Williams 	ssize_t rc;
178855c72ab6SDan Williams 
178955c72ab6SDan Williams 	if (dimm < 0)
179055c72ab6SDan Williams 		return dimm;
179155c72ab6SDan Williams 
179255c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
179355c72ab6SDan Williams 	if (rc)
179455c72ab6SDan Williams 		return rc;
179555c72ab6SDan Williams 
179655c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
179755c72ab6SDan Williams 	return size;
179855c72ab6SDan Williams }
179955c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
180055c72ab6SDan Williams 
18013c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
18023c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
18033c13e2acSDave Jiang {
18043c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
18053c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
18063c13e2acSDave Jiang 
18073c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
18083c13e2acSDave Jiang 	return size;
18093c13e2acSDave Jiang }
18103c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
18113c13e2acSDave Jiang 
181273606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
181373606afdSDan Williams 	&dev_attr_fail_cmd.attr,
181455c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
181573606afdSDan Williams 	&dev_attr_handle.attr,
18163c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
181773606afdSDan Williams 	NULL,
181873606afdSDan Williams };
181973606afdSDan Williams 
182073606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
182173606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
182273606afdSDan Williams };
182373606afdSDan Williams 
182473606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
182573606afdSDan Williams 	&nfit_test_dimm_attribute_group,
182673606afdSDan Williams 	NULL,
182773606afdSDan Williams };
182873606afdSDan Williams 
1829718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1830718fda67SDan Williams {
1831718fda67SDan Williams 	int i;
1832718fda67SDan Williams 
1833718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1834718fda67SDan Williams 		return -ENOMEM;
1835718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1836718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1837718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1838718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1839718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1840718fda67SDan Williams 		if (!t->dimm_dev[i])
1841718fda67SDan Williams 			return -ENOMEM;
1842718fda67SDan Williams 	}
1843718fda67SDan Williams 	return 0;
1844718fda67SDan Williams }
1845718fda67SDan Williams 
1846ecaa4a97SDave Jiang static void security_init(struct nfit_test *t)
1847ecaa4a97SDave Jiang {
1848ecaa4a97SDave Jiang 	int i;
1849ecaa4a97SDave Jiang 
1850ecaa4a97SDave Jiang 	for (i = 0; i < t->num_dcr; i++) {
1851ecaa4a97SDave Jiang 		struct nfit_test_sec *sec = &dimm_sec_info[i];
1852ecaa4a97SDave Jiang 
1853ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1854ecaa4a97SDave Jiang 	}
1855ecaa4a97SDave Jiang }
1856ecaa4a97SDave Jiang 
1857ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1858ed07c433SDan Williams {
1859ed07c433SDan Williams 	int i;
1860ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1861ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1862ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1863ed07c433SDan Williams 		.media_temperature = 40 * 16,
1864ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1865ed07c433SDan Williams 		.spares = 5,
1866ed07c433SDan Williams 	};
1867ed07c433SDan Williams 
1868ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1869b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1870ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1871ed07c433SDan Williams 				sizeof(smart_t_data));
1872ed07c433SDan Williams 	}
1873ed07c433SDan Williams }
1874ed07c433SDan Williams 
18756bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
18766bc75619SDan Williams {
18776b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
18786bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
18796bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
18803b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
18813b87356fSDan Williams 					window_size) * NUM_DCR
18829d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
188385d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1884f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1885f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
18866bc75619SDan Williams 	int i;
18876bc75619SDan Williams 
18886bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
18896bc75619SDan Williams 	if (!t->nfit_buf)
18906bc75619SDan Williams 		return -ENOMEM;
18916bc75619SDan Williams 	t->nfit_size = nfit_size;
18926bc75619SDan Williams 
1893ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
18946bc75619SDan Williams 	if (!t->spa_set[0])
18956bc75619SDan Williams 		return -ENOMEM;
18966bc75619SDan Williams 
1897ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
18986bc75619SDan Williams 	if (!t->spa_set[1])
18996bc75619SDan Williams 		return -ENOMEM;
19006bc75619SDan Williams 
1901ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
190220985164SVishal Verma 	if (!t->spa_set[2])
190320985164SVishal Verma 		return -ENOMEM;
190420985164SVishal Verma 
1905dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
19066bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
19076bc75619SDan Williams 		if (!t->dimm[i])
19086bc75619SDan Williams 			return -ENOMEM;
19096bc75619SDan Williams 
19106bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
19116bc75619SDan Williams 		if (!t->label[i])
19126bc75619SDan Williams 			return -ENOMEM;
19136bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
19149d27a87eSDan Williams 
19159d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
19169d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
191785d3fa02SDan Williams 				&t->flush_dma[i]);
19189d27a87eSDan Williams 		if (!t->flush[i])
19199d27a87eSDan Williams 			return -ENOMEM;
19206bc75619SDan Williams 	}
19216bc75619SDan Williams 
1922dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
19236bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
19246bc75619SDan Williams 		if (!t->dcr[i])
19256bc75619SDan Williams 			return -ENOMEM;
19266bc75619SDan Williams 	}
19276bc75619SDan Williams 
1928c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1929c14a868aSDan Williams 	if (!t->_fit)
1930c14a868aSDan Williams 		return -ENOMEM;
1931c14a868aSDan Williams 
1932718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1933231bf117SDan Williams 		return -ENOMEM;
1934ed07c433SDan Williams 	smart_init(t);
1935ecaa4a97SDave Jiang 	security_init(t);
1936f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
19376bc75619SDan Williams }
19386bc75619SDan Williams 
19396bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
19406bc75619SDan Williams {
19417bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1942ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1943ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1944dafb1048SDan Williams 	int i;
19456bc75619SDan Williams 
19466bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
19476bc75619SDan Williams 	if (!t->nfit_buf)
19486bc75619SDan Williams 		return -ENOMEM;
19496bc75619SDan Williams 	t->nfit_size = nfit_size;
19506bc75619SDan Williams 
1951ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
19526bc75619SDan Williams 	if (!t->spa_set[0])
19536bc75619SDan Williams 		return -ENOMEM;
19546bc75619SDan Williams 
1955dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1956dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1957dafb1048SDan Williams 		if (!t->label[i])
1958dafb1048SDan Williams 			return -ENOMEM;
1959dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1960dafb1048SDan Williams 	}
1961dafb1048SDan Williams 
19627bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
19637bfe97c7SDan Williams 	if (!t->spa_set[1])
19647bfe97c7SDan Williams 		return -ENOMEM;
19657bfe97c7SDan Williams 
1966718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1967718fda67SDan Williams 		return -ENOMEM;
1968ed07c433SDan Williams 	smart_init(t);
1969f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
19706bc75619SDan Williams }
19716bc75619SDan Williams 
19725dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
19735dc68e55SDan Williams {
19745dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
19755dc68e55SDan Williams 	dcr->device_id = 0;
19765dc68e55SDan Williams 	dcr->revision_id = 1;
19775dc68e55SDan Williams 	dcr->valid_fields = 1;
19785dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
19795dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
19805dc68e55SDan Williams }
19815dc68e55SDan Williams 
19826bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
19836bc75619SDan Williams {
198485d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
198585d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
19866bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
19876bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
19886bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
19896bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
19906bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
19916bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
19929d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1993f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1994d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
1995916566aeSDan Williams 	unsigned long *acpi_mask;
19966bc75619SDan Williams 
19976bc75619SDan Williams 	/*
19986bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
19996bc75619SDan Williams 	 * does not actually alias the related block-data-window
20006bc75619SDan Williams 	 * regions)
20016bc75619SDan Williams 	 */
20026b577c9dSLinda Knippers 	spa = nfit_buf;
20036bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20046bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20056bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
20066bc75619SDan Williams 	spa->range_index = 0+1;
20076bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
20086bc75619SDan Williams 	spa->length = SPA0_SIZE;
2009d7d8464dSRoss Zwisler 	offset += spa->header.length;
20106bc75619SDan Williams 
20116bc75619SDan Williams 	/*
20126bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
20136bc75619SDan Williams 	 * does not actually alias the related block-data-window
20146bc75619SDan Williams 	 * regions)
20156bc75619SDan Williams 	 */
2016d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20176bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20186bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20196bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
20206bc75619SDan Williams 	spa->range_index = 1+1;
20216bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
20226bc75619SDan Williams 	spa->length = SPA1_SIZE;
2023d7d8464dSRoss Zwisler 	offset += spa->header.length;
20246bc75619SDan Williams 
20256bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
2026d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20276bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20286bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20296bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20306bc75619SDan Williams 	spa->range_index = 2+1;
20316bc75619SDan Williams 	spa->address = t->dcr_dma[0];
20326bc75619SDan Williams 	spa->length = DCR_SIZE;
2033d7d8464dSRoss Zwisler 	offset += spa->header.length;
20346bc75619SDan Williams 
20356bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
2036d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20376bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20386bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20396bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20406bc75619SDan Williams 	spa->range_index = 3+1;
20416bc75619SDan Williams 	spa->address = t->dcr_dma[1];
20426bc75619SDan Williams 	spa->length = DCR_SIZE;
2043d7d8464dSRoss Zwisler 	offset += spa->header.length;
20446bc75619SDan Williams 
20456bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
2046d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20476bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20486bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20496bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20506bc75619SDan Williams 	spa->range_index = 4+1;
20516bc75619SDan Williams 	spa->address = t->dcr_dma[2];
20526bc75619SDan Williams 	spa->length = DCR_SIZE;
2053d7d8464dSRoss Zwisler 	offset += spa->header.length;
20546bc75619SDan Williams 
20556bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
2056d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20576bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20586bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20596bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20606bc75619SDan Williams 	spa->range_index = 5+1;
20616bc75619SDan Williams 	spa->address = t->dcr_dma[3];
20626bc75619SDan Williams 	spa->length = DCR_SIZE;
2063d7d8464dSRoss Zwisler 	offset += spa->header.length;
20646bc75619SDan Williams 
20656bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
2066d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20676bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20686bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20696bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
20706bc75619SDan Williams 	spa->range_index = 6+1;
20716bc75619SDan Williams 	spa->address = t->dimm_dma[0];
20726bc75619SDan Williams 	spa->length = DIMM_SIZE;
2073d7d8464dSRoss Zwisler 	offset += spa->header.length;
20746bc75619SDan Williams 
20756bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
2076d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20776bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20786bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20796bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
20806bc75619SDan Williams 	spa->range_index = 7+1;
20816bc75619SDan Williams 	spa->address = t->dimm_dma[1];
20826bc75619SDan Williams 	spa->length = DIMM_SIZE;
2083d7d8464dSRoss Zwisler 	offset += spa->header.length;
20846bc75619SDan Williams 
20856bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
2086d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20876bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20886bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20896bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
20906bc75619SDan Williams 	spa->range_index = 8+1;
20916bc75619SDan Williams 	spa->address = t->dimm_dma[2];
20926bc75619SDan Williams 	spa->length = DIMM_SIZE;
2093d7d8464dSRoss Zwisler 	offset += spa->header.length;
20946bc75619SDan Williams 
20956bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
2096d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20976bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20986bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20996bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
21006bc75619SDan Williams 	spa->range_index = 9+1;
21016bc75619SDan Williams 	spa->address = t->dimm_dma[3];
21026bc75619SDan Williams 	spa->length = DIMM_SIZE;
2103d7d8464dSRoss Zwisler 	offset += spa->header.length;
21046bc75619SDan Williams 
21056bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
21066bc75619SDan Williams 	memdev = nfit_buf + offset;
21076bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21086bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21096bc75619SDan Williams 	memdev->device_handle = handle[0];
21106bc75619SDan Williams 	memdev->physical_id = 0;
21116bc75619SDan Williams 	memdev->region_id = 0;
21126bc75619SDan Williams 	memdev->range_index = 0+1;
21133b87356fSDan Williams 	memdev->region_index = 4+1;
21146bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
2115df06a2d5SDan Williams 	memdev->region_offset = 1;
21166bc75619SDan Williams 	memdev->address = 0;
21176bc75619SDan Williams 	memdev->interleave_index = 0;
21186bc75619SDan Williams 	memdev->interleave_ways = 2;
2119d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21206bc75619SDan Williams 
21216bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
2122d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21236bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21246bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21256bc75619SDan Williams 	memdev->device_handle = handle[1];
21266bc75619SDan Williams 	memdev->physical_id = 1;
21276bc75619SDan Williams 	memdev->region_id = 0;
21286bc75619SDan Williams 	memdev->range_index = 0+1;
21293b87356fSDan Williams 	memdev->region_index = 5+1;
21306bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
2131df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
21326bc75619SDan Williams 	memdev->address = 0;
21336bc75619SDan Williams 	memdev->interleave_index = 0;
21346bc75619SDan Williams 	memdev->interleave_ways = 2;
2135ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2136d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21376bc75619SDan Williams 
21386bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
2139d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21406bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21416bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21426bc75619SDan Williams 	memdev->device_handle = handle[0];
21436bc75619SDan Williams 	memdev->physical_id = 0;
21446bc75619SDan Williams 	memdev->region_id = 1;
21456bc75619SDan Williams 	memdev->range_index = 1+1;
21463b87356fSDan Williams 	memdev->region_index = 4+1;
21476bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2148df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
21496bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
21506bc75619SDan Williams 	memdev->interleave_index = 0;
21516bc75619SDan Williams 	memdev->interleave_ways = 4;
2152ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2153d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21546bc75619SDan Williams 
21556bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
2156d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21576bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21586bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21596bc75619SDan Williams 	memdev->device_handle = handle[1];
21606bc75619SDan Williams 	memdev->physical_id = 1;
21616bc75619SDan Williams 	memdev->region_id = 1;
21626bc75619SDan Williams 	memdev->range_index = 1+1;
21633b87356fSDan Williams 	memdev->region_index = 5+1;
21646bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2165df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
21666bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
21676bc75619SDan Williams 	memdev->interleave_index = 0;
21686bc75619SDan Williams 	memdev->interleave_ways = 4;
2169d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21706bc75619SDan Williams 
21716bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
2172d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21736bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21746bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21756bc75619SDan Williams 	memdev->device_handle = handle[2];
21766bc75619SDan Williams 	memdev->physical_id = 2;
21776bc75619SDan Williams 	memdev->region_id = 0;
21786bc75619SDan Williams 	memdev->range_index = 1+1;
21793b87356fSDan Williams 	memdev->region_index = 6+1;
21806bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2181df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
21826bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
21836bc75619SDan Williams 	memdev->interleave_index = 0;
21846bc75619SDan Williams 	memdev->interleave_ways = 4;
2185ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2186d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21876bc75619SDan Williams 
21886bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
2189d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21906bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21916bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21926bc75619SDan Williams 	memdev->device_handle = handle[3];
21936bc75619SDan Williams 	memdev->physical_id = 3;
21946bc75619SDan Williams 	memdev->region_id = 0;
21956bc75619SDan Williams 	memdev->range_index = 1+1;
21963b87356fSDan Williams 	memdev->region_index = 7+1;
21976bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2198df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
21996bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
22006bc75619SDan Williams 	memdev->interleave_index = 0;
22016bc75619SDan Williams 	memdev->interleave_ways = 4;
2202d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22036bc75619SDan Williams 
22046bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
2205d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22066bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22076bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22086bc75619SDan Williams 	memdev->device_handle = handle[0];
22096bc75619SDan Williams 	memdev->physical_id = 0;
22106bc75619SDan Williams 	memdev->region_id = 0;
22116bc75619SDan Williams 	memdev->range_index = 2+1;
22126bc75619SDan Williams 	memdev->region_index = 0+1;
22136bc75619SDan Williams 	memdev->region_size = 0;
22146bc75619SDan Williams 	memdev->region_offset = 0;
22156bc75619SDan Williams 	memdev->address = 0;
22166bc75619SDan Williams 	memdev->interleave_index = 0;
22176bc75619SDan Williams 	memdev->interleave_ways = 1;
2218d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22196bc75619SDan Williams 
22206bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
2221d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22226bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22236bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22246bc75619SDan Williams 	memdev->device_handle = handle[1];
22256bc75619SDan Williams 	memdev->physical_id = 1;
22266bc75619SDan Williams 	memdev->region_id = 0;
22276bc75619SDan Williams 	memdev->range_index = 3+1;
22286bc75619SDan Williams 	memdev->region_index = 1+1;
22296bc75619SDan Williams 	memdev->region_size = 0;
22306bc75619SDan Williams 	memdev->region_offset = 0;
22316bc75619SDan Williams 	memdev->address = 0;
22326bc75619SDan Williams 	memdev->interleave_index = 0;
22336bc75619SDan Williams 	memdev->interleave_ways = 1;
2234d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22356bc75619SDan Williams 
22366bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
2237d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22386bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22396bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22406bc75619SDan Williams 	memdev->device_handle = handle[2];
22416bc75619SDan Williams 	memdev->physical_id = 2;
22426bc75619SDan Williams 	memdev->region_id = 0;
22436bc75619SDan Williams 	memdev->range_index = 4+1;
22446bc75619SDan Williams 	memdev->region_index = 2+1;
22456bc75619SDan Williams 	memdev->region_size = 0;
22466bc75619SDan Williams 	memdev->region_offset = 0;
22476bc75619SDan Williams 	memdev->address = 0;
22486bc75619SDan Williams 	memdev->interleave_index = 0;
22496bc75619SDan Williams 	memdev->interleave_ways = 1;
2250d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22516bc75619SDan Williams 
22526bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
2253d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22546bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22556bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22566bc75619SDan Williams 	memdev->device_handle = handle[3];
22576bc75619SDan Williams 	memdev->physical_id = 3;
22586bc75619SDan Williams 	memdev->region_id = 0;
22596bc75619SDan Williams 	memdev->range_index = 5+1;
22606bc75619SDan Williams 	memdev->region_index = 3+1;
22616bc75619SDan Williams 	memdev->region_size = 0;
22626bc75619SDan Williams 	memdev->region_offset = 0;
22636bc75619SDan Williams 	memdev->address = 0;
22646bc75619SDan Williams 	memdev->interleave_index = 0;
22656bc75619SDan Williams 	memdev->interleave_ways = 1;
2266d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22676bc75619SDan Williams 
22686bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
2269d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22706bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22716bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22726bc75619SDan Williams 	memdev->device_handle = handle[0];
22736bc75619SDan Williams 	memdev->physical_id = 0;
22746bc75619SDan Williams 	memdev->region_id = 0;
22756bc75619SDan Williams 	memdev->range_index = 6+1;
22766bc75619SDan Williams 	memdev->region_index = 0+1;
22776bc75619SDan Williams 	memdev->region_size = 0;
22786bc75619SDan Williams 	memdev->region_offset = 0;
22796bc75619SDan Williams 	memdev->address = 0;
22806bc75619SDan Williams 	memdev->interleave_index = 0;
22816bc75619SDan Williams 	memdev->interleave_ways = 1;
2282d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22836bc75619SDan Williams 
22846bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
2285d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22866bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22876bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22886bc75619SDan Williams 	memdev->device_handle = handle[1];
22896bc75619SDan Williams 	memdev->physical_id = 1;
22906bc75619SDan Williams 	memdev->region_id = 0;
22916bc75619SDan Williams 	memdev->range_index = 7+1;
22926bc75619SDan Williams 	memdev->region_index = 1+1;
22936bc75619SDan Williams 	memdev->region_size = 0;
22946bc75619SDan Williams 	memdev->region_offset = 0;
22956bc75619SDan Williams 	memdev->address = 0;
22966bc75619SDan Williams 	memdev->interleave_index = 0;
22976bc75619SDan Williams 	memdev->interleave_ways = 1;
2298d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22996bc75619SDan Williams 
23006bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
2301d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
23026bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
23036bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
23046bc75619SDan Williams 	memdev->device_handle = handle[2];
23056bc75619SDan Williams 	memdev->physical_id = 2;
23066bc75619SDan Williams 	memdev->region_id = 0;
23076bc75619SDan Williams 	memdev->range_index = 8+1;
23086bc75619SDan Williams 	memdev->region_index = 2+1;
23096bc75619SDan Williams 	memdev->region_size = 0;
23106bc75619SDan Williams 	memdev->region_offset = 0;
23116bc75619SDan Williams 	memdev->address = 0;
23126bc75619SDan Williams 	memdev->interleave_index = 0;
23136bc75619SDan Williams 	memdev->interleave_ways = 1;
2314d7d8464dSRoss Zwisler 	offset += memdev->header.length;
23156bc75619SDan Williams 
23166bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
2317d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
23186bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
23196bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
23206bc75619SDan Williams 	memdev->device_handle = handle[3];
23216bc75619SDan Williams 	memdev->physical_id = 3;
23226bc75619SDan Williams 	memdev->region_id = 0;
23236bc75619SDan Williams 	memdev->range_index = 9+1;
23246bc75619SDan Williams 	memdev->region_index = 3+1;
23256bc75619SDan Williams 	memdev->region_size = 0;
23266bc75619SDan Williams 	memdev->region_offset = 0;
23276bc75619SDan Williams 	memdev->address = 0;
23286bc75619SDan Williams 	memdev->interleave_index = 0;
23296bc75619SDan Williams 	memdev->interleave_ways = 1;
2330ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2331d7d8464dSRoss Zwisler 	offset += memdev->header.length;
23326bc75619SDan Williams 
23333b87356fSDan Williams 	/* dcr-descriptor0: blk */
23346bc75619SDan Williams 	dcr = nfit_buf + offset;
23356bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2336d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23376bc75619SDan Williams 	dcr->region_index = 0+1;
23385dc68e55SDan Williams 	dcr_common_init(dcr);
23396bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2340be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23416bc75619SDan Williams 	dcr->windows = 1;
23426bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23436bc75619SDan Williams 	dcr->command_offset = 0;
23446bc75619SDan Williams 	dcr->command_size = 8;
23456bc75619SDan Williams 	dcr->status_offset = 8;
23466bc75619SDan Williams 	dcr->status_size = 4;
2347d7d8464dSRoss Zwisler 	offset += dcr->header.length;
23486bc75619SDan Williams 
23493b87356fSDan Williams 	/* dcr-descriptor1: blk */
2350d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
23516bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2352d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23536bc75619SDan Williams 	dcr->region_index = 1+1;
23545dc68e55SDan Williams 	dcr_common_init(dcr);
23556bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2356be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23576bc75619SDan Williams 	dcr->windows = 1;
23586bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23596bc75619SDan Williams 	dcr->command_offset = 0;
23606bc75619SDan Williams 	dcr->command_size = 8;
23616bc75619SDan Williams 	dcr->status_offset = 8;
23626bc75619SDan Williams 	dcr->status_size = 4;
2363d7d8464dSRoss Zwisler 	offset += dcr->header.length;
23646bc75619SDan Williams 
23653b87356fSDan Williams 	/* dcr-descriptor2: blk */
2366d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
23676bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2368d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23696bc75619SDan Williams 	dcr->region_index = 2+1;
23705dc68e55SDan Williams 	dcr_common_init(dcr);
23716bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2372be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23736bc75619SDan Williams 	dcr->windows = 1;
23746bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23756bc75619SDan Williams 	dcr->command_offset = 0;
23766bc75619SDan Williams 	dcr->command_size = 8;
23776bc75619SDan Williams 	dcr->status_offset = 8;
23786bc75619SDan Williams 	dcr->status_size = 4;
2379d7d8464dSRoss Zwisler 	offset += dcr->header.length;
23806bc75619SDan Williams 
23813b87356fSDan Williams 	/* dcr-descriptor3: blk */
2382d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
23836bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2384d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23856bc75619SDan Williams 	dcr->region_index = 3+1;
23865dc68e55SDan Williams 	dcr_common_init(dcr);
23876bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2388be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23896bc75619SDan Williams 	dcr->windows = 1;
23906bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23916bc75619SDan Williams 	dcr->command_offset = 0;
23926bc75619SDan Williams 	dcr->command_size = 8;
23936bc75619SDan Williams 	dcr->status_offset = 8;
23946bc75619SDan Williams 	dcr->status_size = 4;
2395d7d8464dSRoss Zwisler 	offset += dcr->header.length;
23966bc75619SDan Williams 
23973b87356fSDan Williams 	/* dcr-descriptor0: pmem */
23983b87356fSDan Williams 	dcr = nfit_buf + offset;
23993b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24003b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24013b87356fSDan Williams 			window_size);
24023b87356fSDan Williams 	dcr->region_index = 4+1;
24035dc68e55SDan Williams 	dcr_common_init(dcr);
24043b87356fSDan Williams 	dcr->serial_number = ~handle[0];
24053b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24063b87356fSDan Williams 	dcr->windows = 0;
2407d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24083b87356fSDan Williams 
24093b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2410d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
24113b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24123b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24133b87356fSDan Williams 			window_size);
24143b87356fSDan Williams 	dcr->region_index = 5+1;
24155dc68e55SDan Williams 	dcr_common_init(dcr);
24163b87356fSDan Williams 	dcr->serial_number = ~handle[1];
24173b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24183b87356fSDan Williams 	dcr->windows = 0;
2419d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24203b87356fSDan Williams 
24213b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2422d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
24233b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24243b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24253b87356fSDan Williams 			window_size);
24263b87356fSDan Williams 	dcr->region_index = 6+1;
24275dc68e55SDan Williams 	dcr_common_init(dcr);
24283b87356fSDan Williams 	dcr->serial_number = ~handle[2];
24293b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24303b87356fSDan Williams 	dcr->windows = 0;
2431d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24323b87356fSDan Williams 
24333b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2434d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
24353b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24363b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24373b87356fSDan Williams 			window_size);
24383b87356fSDan Williams 	dcr->region_index = 7+1;
24395dc68e55SDan Williams 	dcr_common_init(dcr);
24403b87356fSDan Williams 	dcr->serial_number = ~handle[3];
24413b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24423b87356fSDan Williams 	dcr->windows = 0;
2443d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24443b87356fSDan Williams 
24456bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
24466bc75619SDan Williams 	bdw = nfit_buf + offset;
24476bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2448d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24496bc75619SDan Williams 	bdw->region_index = 0+1;
24506bc75619SDan Williams 	bdw->windows = 1;
24516bc75619SDan Williams 	bdw->offset = 0;
24526bc75619SDan Williams 	bdw->size = BDW_SIZE;
24536bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24546bc75619SDan Williams 	bdw->start_address = 0;
2455d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24566bc75619SDan Williams 
24576bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2458d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
24596bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2460d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24616bc75619SDan Williams 	bdw->region_index = 1+1;
24626bc75619SDan Williams 	bdw->windows = 1;
24636bc75619SDan Williams 	bdw->offset = 0;
24646bc75619SDan Williams 	bdw->size = BDW_SIZE;
24656bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24666bc75619SDan Williams 	bdw->start_address = 0;
2467d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24686bc75619SDan Williams 
24696bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2470d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
24716bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2472d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24736bc75619SDan Williams 	bdw->region_index = 2+1;
24746bc75619SDan Williams 	bdw->windows = 1;
24756bc75619SDan Williams 	bdw->offset = 0;
24766bc75619SDan Williams 	bdw->size = BDW_SIZE;
24776bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24786bc75619SDan Williams 	bdw->start_address = 0;
2479d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24806bc75619SDan Williams 
24816bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2482d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
24836bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2484d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24856bc75619SDan Williams 	bdw->region_index = 3+1;
24866bc75619SDan Williams 	bdw->windows = 1;
24876bc75619SDan Williams 	bdw->offset = 0;
24886bc75619SDan Williams 	bdw->size = BDW_SIZE;
24896bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24906bc75619SDan Williams 	bdw->start_address = 0;
2491d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24926bc75619SDan Williams 
24939d27a87eSDan Williams 	/* flush0 (dimm0) */
24949d27a87eSDan Williams 	flush = nfit_buf + offset;
24959d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
249685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
24979d27a87eSDan Williams 	flush->device_handle = handle[0];
249885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
249985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
250085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2501d7d8464dSRoss Zwisler 	offset += flush->header.length;
25029d27a87eSDan Williams 
25039d27a87eSDan Williams 	/* flush1 (dimm1) */
2504d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
25059d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
250685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
25079d27a87eSDan Williams 	flush->device_handle = handle[1];
250885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
250985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
251085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2511d7d8464dSRoss Zwisler 	offset += flush->header.length;
25129d27a87eSDan Williams 
25139d27a87eSDan Williams 	/* flush2 (dimm2) */
2514d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
25159d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
251685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
25179d27a87eSDan Williams 	flush->device_handle = handle[2];
251885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
251985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
252085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2521d7d8464dSRoss Zwisler 	offset += flush->header.length;
25229d27a87eSDan Williams 
25239d27a87eSDan Williams 	/* flush3 (dimm3) */
2524d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
25259d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
252685d3fa02SDan Williams 	flush->header.length = flush_hint_size;
25279d27a87eSDan Williams 	flush->device_handle = handle[3];
252885d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
252985d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
253085d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2531d7d8464dSRoss Zwisler 	offset += flush->header.length;
25329d27a87eSDan Williams 
2533f81e1d35SDave Jiang 	/* platform capabilities */
2534d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2535f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2536f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2537f81e1d35SDave Jiang 	pcap->highest_capability = 1;
25381273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2539d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2540f81e1d35SDave Jiang 
254120985164SVishal Verma 	if (t->setup_hotplug) {
25423b87356fSDan Williams 		/* dcr-descriptor4: blk */
254320985164SVishal Verma 		dcr = nfit_buf + offset;
254420985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2545d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
25463b87356fSDan Williams 		dcr->region_index = 8+1;
25475dc68e55SDan Williams 		dcr_common_init(dcr);
254820985164SVishal Verma 		dcr->serial_number = ~handle[4];
2549be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
255020985164SVishal Verma 		dcr->windows = 1;
255120985164SVishal Verma 		dcr->window_size = DCR_SIZE;
255220985164SVishal Verma 		dcr->command_offset = 0;
255320985164SVishal Verma 		dcr->command_size = 8;
255420985164SVishal Verma 		dcr->status_offset = 8;
255520985164SVishal Verma 		dcr->status_size = 4;
2556d7d8464dSRoss Zwisler 		offset += dcr->header.length;
255720985164SVishal Verma 
25583b87356fSDan Williams 		/* dcr-descriptor4: pmem */
25593b87356fSDan Williams 		dcr = nfit_buf + offset;
25603b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
25613b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
25623b87356fSDan Williams 				window_size);
25633b87356fSDan Williams 		dcr->region_index = 9+1;
25645dc68e55SDan Williams 		dcr_common_init(dcr);
25653b87356fSDan Williams 		dcr->serial_number = ~handle[4];
25663b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
25673b87356fSDan Williams 		dcr->windows = 0;
2568d7d8464dSRoss Zwisler 		offset += dcr->header.length;
25693b87356fSDan Williams 
257020985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
257120985164SVishal Verma 		bdw = nfit_buf + offset;
257220985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2573d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
25743b87356fSDan Williams 		bdw->region_index = 8+1;
257520985164SVishal Verma 		bdw->windows = 1;
257620985164SVishal Verma 		bdw->offset = 0;
257720985164SVishal Verma 		bdw->size = BDW_SIZE;
257820985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
257920985164SVishal Verma 		bdw->start_address = 0;
2580d7d8464dSRoss Zwisler 		offset += bdw->header.length;
258120985164SVishal Verma 
258220985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
258320985164SVishal Verma 		spa = nfit_buf + offset;
258420985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
258520985164SVishal Verma 		spa->header.length = sizeof(*spa);
258620985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
258720985164SVishal Verma 		spa->range_index = 10+1;
258820985164SVishal Verma 		spa->address = t->dcr_dma[4];
258920985164SVishal Verma 		spa->length = DCR_SIZE;
2590d7d8464dSRoss Zwisler 		offset += spa->header.length;
259120985164SVishal Verma 
259220985164SVishal Verma 		/*
259320985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
259420985164SVishal Verma 		 * does not actually alias the related block-data-window
259520985164SVishal Verma 		 * regions)
259620985164SVishal Verma 		 */
2597d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
259820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
259920985164SVishal Verma 		spa->header.length = sizeof(*spa);
260020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
260120985164SVishal Verma 		spa->range_index = 11+1;
260220985164SVishal Verma 		spa->address = t->spa_set_dma[2];
260320985164SVishal Verma 		spa->length = SPA0_SIZE;
2604d7d8464dSRoss Zwisler 		offset += spa->header.length;
260520985164SVishal Verma 
260620985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2607d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
260820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
260920985164SVishal Verma 		spa->header.length = sizeof(*spa);
261020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
261120985164SVishal Verma 		spa->range_index = 12+1;
261220985164SVishal Verma 		spa->address = t->dimm_dma[4];
261320985164SVishal Verma 		spa->length = DIMM_SIZE;
2614d7d8464dSRoss Zwisler 		offset += spa->header.length;
261520985164SVishal Verma 
261620985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
261720985164SVishal Verma 		memdev = nfit_buf + offset;
261820985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
261920985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
262020985164SVishal Verma 		memdev->device_handle = handle[4];
262120985164SVishal Verma 		memdev->physical_id = 4;
262220985164SVishal Verma 		memdev->region_id = 0;
262320985164SVishal Verma 		memdev->range_index = 10+1;
26243b87356fSDan Williams 		memdev->region_index = 8+1;
262520985164SVishal Verma 		memdev->region_size = 0;
262620985164SVishal Verma 		memdev->region_offset = 0;
262720985164SVishal Verma 		memdev->address = 0;
262820985164SVishal Verma 		memdev->interleave_index = 0;
262920985164SVishal Verma 		memdev->interleave_ways = 1;
2630d7d8464dSRoss Zwisler 		offset += memdev->header.length;
263120985164SVishal Verma 
2632d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2633d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
263420985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
263520985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
263620985164SVishal Verma 		memdev->device_handle = handle[4];
263720985164SVishal Verma 		memdev->physical_id = 4;
263820985164SVishal Verma 		memdev->region_id = 0;
263920985164SVishal Verma 		memdev->range_index = 11+1;
26403b87356fSDan Williams 		memdev->region_index = 9+1;
264120985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2642df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
264320985164SVishal Verma 		memdev->address = 0;
264420985164SVishal Verma 		memdev->interleave_index = 0;
264520985164SVishal Verma 		memdev->interleave_ways = 1;
2646ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2647d7d8464dSRoss Zwisler 		offset += memdev->header.length;
264820985164SVishal Verma 
26493b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2650d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
265120985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
265220985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
265320985164SVishal Verma 		memdev->device_handle = handle[4];
265420985164SVishal Verma 		memdev->physical_id = 4;
265520985164SVishal Verma 		memdev->region_id = 0;
265620985164SVishal Verma 		memdev->range_index = 12+1;
26573b87356fSDan Williams 		memdev->region_index = 8+1;
265820985164SVishal Verma 		memdev->region_size = 0;
265920985164SVishal Verma 		memdev->region_offset = 0;
266020985164SVishal Verma 		memdev->address = 0;
266120985164SVishal Verma 		memdev->interleave_index = 0;
266220985164SVishal Verma 		memdev->interleave_ways = 1;
2663d7d8464dSRoss Zwisler 		offset += memdev->header.length;
266420985164SVishal Verma 
266520985164SVishal Verma 		/* flush3 (dimm4) */
266620985164SVishal Verma 		flush = nfit_buf + offset;
266720985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
266885d3fa02SDan Williams 		flush->header.length = flush_hint_size;
266920985164SVishal Verma 		flush->device_handle = handle[4];
267085d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
267185d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
267285d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
267385d3fa02SDan Williams 				+ i * sizeof(u64);
2674d7d8464dSRoss Zwisler 		offset += flush->header.length;
26759741a559SRoss Zwisler 
26769741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
26779741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
267820985164SVishal Verma 	}
267920985164SVishal Verma 
26801526f9e2SRoss Zwisler 	t->nfit_filled = offset;
26811526f9e2SRoss Zwisler 
26829fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
26839fb1a190SDave Jiang 			SPA0_SIZE);
2684f471f1a7SDan Williams 
26856bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2686e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2687e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2688e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2689ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2690ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2691ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
26924cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2693e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2694e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2695e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2696e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
269710246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
2698d46e6a21SDan Williams 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_dsm_mask);
2699d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_dsm_mask);
2700d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_dsm_mask);
2701d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_dsm_mask);
2702bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2703bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2704bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2705bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2706bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2707674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
27083c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
27093c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
27103c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
27113c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
27123c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
27133c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
27143c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
27153c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2716926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2717926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2718ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
2719ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2720ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
2721ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2722916566aeSDan Williams 	set_bit(NVDIMM_INTEL_FW_ACTIVATE_DIMMINFO, &acpi_desc->dimm_cmd_force_en);
2723916566aeSDan Williams 	set_bit(NVDIMM_INTEL_FW_ACTIVATE_ARM, &acpi_desc->dimm_cmd_force_en);
2724916566aeSDan Williams 
2725916566aeSDan Williams 	acpi_mask = &acpi_desc->family_dsm_mask[NVDIMM_BUS_FAMILY_INTEL];
2726916566aeSDan Williams 	set_bit(NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO, acpi_mask);
2727916566aeSDan Williams 	set_bit(NVDIMM_BUS_INTEL_FW_ACTIVATE, acpi_mask);
27286bc75619SDan Williams }
27296bc75619SDan Williams 
27306bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
27316bc75619SDan Williams {
27326b577c9dSLinda Knippers 	size_t offset;
27336bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
27346bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
27356bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
27366bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2737d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
27386bc75619SDan Williams 
27396b577c9dSLinda Knippers 	offset = 0;
27406bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
27416bc75619SDan Williams 	spa = nfit_buf + offset;
27426bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
27436bc75619SDan Williams 	spa->header.length = sizeof(*spa);
27446bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
27456bc75619SDan Williams 	spa->range_index = 0+1;
27466bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
27476bc75619SDan Williams 	spa->length = SPA2_SIZE;
2748d7d8464dSRoss Zwisler 	offset += spa->header.length;
27496bc75619SDan Williams 
27507bfe97c7SDan Williams 	/* virtual cd region */
2751d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
27527bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
27537bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
27547bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
27557bfe97c7SDan Williams 	spa->range_index = 0;
27567bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
27577bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2758d7d8464dSRoss Zwisler 	offset += spa->header.length;
27597bfe97c7SDan Williams 
27606bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
27616bc75619SDan Williams 	memdev = nfit_buf + offset;
27626bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
27636bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2764dafb1048SDan Williams 	memdev->device_handle = handle[5];
27656bc75619SDan Williams 	memdev->physical_id = 0;
27666bc75619SDan Williams 	memdev->region_id = 0;
27676bc75619SDan Williams 	memdev->range_index = 0+1;
27686bc75619SDan Williams 	memdev->region_index = 0+1;
27696bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
27706bc75619SDan Williams 	memdev->region_offset = 0;
27716bc75619SDan Williams 	memdev->address = 0;
27726bc75619SDan Williams 	memdev->interleave_index = 0;
27736bc75619SDan Williams 	memdev->interleave_ways = 1;
277458138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
277558138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2776f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2777d7d8464dSRoss Zwisler 	offset += memdev->header.length;
27786bc75619SDan Williams 
27796bc75619SDan Williams 	/* dcr-descriptor0 */
27806bc75619SDan Williams 	dcr = nfit_buf + offset;
27816bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
27823b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
27833b87356fSDan Williams 			window_size);
27846bc75619SDan Williams 	dcr->region_index = 0+1;
27855dc68e55SDan Williams 	dcr_common_init(dcr);
2786dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2787be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
27886bc75619SDan Williams 	dcr->windows = 0;
2789ac40b675SDan Williams 	offset += dcr->header.length;
2790d7d8464dSRoss Zwisler 
2791ac40b675SDan Williams 	memdev = nfit_buf + offset;
2792ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2793ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2794ac40b675SDan Williams 	memdev->device_handle = handle[6];
2795ac40b675SDan Williams 	memdev->physical_id = 0;
2796ac40b675SDan Williams 	memdev->region_id = 0;
2797ac40b675SDan Williams 	memdev->range_index = 0;
2798ac40b675SDan Williams 	memdev->region_index = 0+2;
2799ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2800ac40b675SDan Williams 	memdev->region_offset = 0;
2801ac40b675SDan Williams 	memdev->address = 0;
2802ac40b675SDan Williams 	memdev->interleave_index = 0;
2803ac40b675SDan Williams 	memdev->interleave_ways = 1;
2804ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2805d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2806ac40b675SDan Williams 
2807ac40b675SDan Williams 	/* dcr-descriptor1 */
2808ac40b675SDan Williams 	dcr = nfit_buf + offset;
2809ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2810ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2811ac40b675SDan Williams 			window_size);
2812ac40b675SDan Williams 	dcr->region_index = 0+2;
2813ac40b675SDan Williams 	dcr_common_init(dcr);
2814ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2815ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2816ac40b675SDan Williams 	dcr->windows = 0;
2817d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2818ac40b675SDan Williams 
28199741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
28209741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
28219741a559SRoss Zwisler 
28221526f9e2SRoss Zwisler 	t->nfit_filled = offset;
28231526f9e2SRoss Zwisler 
28249fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
28259fb1a190SDave Jiang 			SPA2_SIZE);
2826f471f1a7SDan Williams 
2827d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2828e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2829e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2830e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2831e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2832674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
28339484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
28349484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
28359484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
28366bc75619SDan Williams }
28376bc75619SDan Williams 
28386bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
28396bc75619SDan Williams 		void *iobuf, u64 len, int rw)
28406bc75619SDan Williams {
28416bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
28426bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
28436bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
28446bc75619SDan Williams 	unsigned int lane;
28456bc75619SDan Williams 
28466bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
28476bc75619SDan Williams 	if (rw)
284867a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
284967a3e8feSRoss Zwisler 	else {
285067a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
285167a3e8feSRoss Zwisler 
28525deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
28535deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
285467a3e8feSRoss Zwisler 	}
28556bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
28566bc75619SDan Williams 
28576bc75619SDan Williams 	return 0;
28586bc75619SDan Williams }
28596bc75619SDan Williams 
2860a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2861a7de92daSDan Williams 
2862a7de92daSDan Williams union acpi_object *result;
2863a7de92daSDan Williams 
2864a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
286594116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2866a7de92daSDan Williams {
2867a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2868a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2869a7de92daSDan Williams 
2870a7de92daSDan Williams 	return result;
2871a7de92daSDan Williams }
2872a7de92daSDan Williams 
2873a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2874a7de92daSDan Williams {
2875a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2876a7de92daSDan Williams 	if (!result)
2877a7de92daSDan Williams 		return -ENOMEM;
2878a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2879a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2880a7de92daSDan Williams 	result->buffer.length = size;
2881a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2882a7de92daSDan Williams 	memset(buf, 0, size);
2883a7de92daSDan Williams 	return 0;
2884a7de92daSDan Williams }
2885a7de92daSDan Williams 
2886a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2887a7de92daSDan Williams {
2888a7de92daSDan Williams 	int rc, cmd_rc;
2889a7de92daSDan Williams 	struct nvdimm *nvdimm;
2890a7de92daSDan Williams 	struct acpi_device *adev;
2891a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2892a7de92daSDan Williams 	struct nd_ars_record *record;
2893a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2894a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2895a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2896abfd4d9cSDan Williams 	struct nfit_ctl_test_cmd {
2897abfd4d9cSDan Williams 		struct nd_cmd_pkg pkg;
2898a7de92daSDan Williams 		union {
2899a7de92daSDan Williams 			struct nd_cmd_get_config_size cfg_size;
2900fb2a1748SDan Williams 			struct nd_cmd_clear_error clear_err;
2901a7de92daSDan Williams 			struct nd_cmd_ars_status ars_stat;
2902a7de92daSDan Williams 			struct nd_cmd_ars_cap ars_cap;
2903916566aeSDan Williams 			struct nd_intel_bus_fw_activate_businfo fwa_info;
2904a7de92daSDan Williams 			char buf[sizeof(struct nd_cmd_ars_status)
2905a7de92daSDan Williams 				+ sizeof(struct nd_ars_record)];
2906abfd4d9cSDan Williams 		};
2907abfd4d9cSDan Williams 	} cmd;
2908a7de92daSDan Williams 
2909a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2910a7de92daSDan Williams 	if (!adev)
2911a7de92daSDan Williams 		return -ENOMEM;
2912a7de92daSDan Williams 	*adev = (struct acpi_device) {
2913a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2914a7de92daSDan Williams 		.dev = {
2915a7de92daSDan Williams 			.init_name = "test-adev",
2916a7de92daSDan Williams 		},
2917a7de92daSDan Williams 	};
2918a7de92daSDan Williams 
2919a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2920a7de92daSDan Williams 	if (!acpi_desc)
2921a7de92daSDan Williams 		return -ENOMEM;
2922a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2923a7de92daSDan Williams 		.nd_desc = {
2924a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2925a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2926a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
292710246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
292810246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2929a7de92daSDan Williams 			.module = THIS_MODULE,
2930a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2931a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
2932916566aeSDan Williams 			.bus_family_mask = 1UL << NVDIMM_BUS_FAMILY_NFIT
2933916566aeSDan Williams 				| 1UL << NVDIMM_BUS_FAMILY_INTEL,
2934d46e6a21SDan Williams 		},
29359fb1a190SDave Jiang 		.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
29369fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_SET
29379fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
29389fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_GET,
2939916566aeSDan Williams 		.family_dsm_mask[NVDIMM_BUS_FAMILY_INTEL] =
2940916566aeSDan Williams 			NVDIMM_BUS_INTEL_FW_ACTIVATE_CMDMASK,
2941a7de92daSDan Williams 		.dev = &adev->dev,
2942a7de92daSDan Williams 	};
2943a7de92daSDan Williams 
2944a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2945a7de92daSDan Williams 	if (!nfit_mem)
2946a7de92daSDan Williams 		return -ENOMEM;
2947a7de92daSDan Williams 
2948a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2949a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2950a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2951a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2952a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2953a7de92daSDan Williams 		.adev = adev,
2954a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2955a7de92daSDan Williams 		.dsm_mask = mask,
2956a7de92daSDan Williams 	};
2957a7de92daSDan Williams 
2958a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2959a7de92daSDan Williams 	if (!nvdimm)
2960a7de92daSDan Williams 		return -ENOMEM;
2961a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2962a7de92daSDan Williams 		.provider_data = nfit_mem,
2963a7de92daSDan Williams 		.cmd_mask = mask,
2964a7de92daSDan Williams 		.dev = {
2965a7de92daSDan Williams 			.init_name = "test-dimm",
2966a7de92daSDan Williams 		},
2967a7de92daSDan Williams 	};
2968a7de92daSDan Williams 
2969a7de92daSDan Williams 
2970a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2971abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.cfg_size);
2972abfd4d9cSDan Williams 	cmd.cfg_size = (struct nd_cmd_get_config_size) {
2973a7de92daSDan Williams 		.status = 0,
2974a7de92daSDan Williams 		.config_size = SZ_128K,
2975a7de92daSDan Williams 		.max_xfer = SZ_4K,
2976a7de92daSDan Williams 	};
2977abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
2978a7de92daSDan Williams 	if (rc)
2979a7de92daSDan Williams 		return rc;
2980a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2981abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
2982a7de92daSDan Williams 
2983abfd4d9cSDan Williams 	if (rc < 0 || cmd_rc || cmd.cfg_size.status != 0
2984abfd4d9cSDan Williams 			|| cmd.cfg_size.config_size != SZ_128K
2985abfd4d9cSDan Williams 			|| cmd.cfg_size.max_xfer != SZ_4K) {
2986a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2987a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2988a7de92daSDan Williams 		return -EIO;
2989a7de92daSDan Williams 	}
2990a7de92daSDan Williams 
2991a7de92daSDan Williams 
2992a7de92daSDan Williams 	/* test ars_status with zero output */
2993a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2994abfd4d9cSDan Williams 	cmd.ars_stat = (struct nd_cmd_ars_status) {
2995a7de92daSDan Williams 		.out_length = 0,
2996a7de92daSDan Williams 	};
2997abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
2998a7de92daSDan Williams 	if (rc)
2999a7de92daSDan Williams 		return rc;
3000a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
3001abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3002a7de92daSDan Williams 
3003a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
3004a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3005a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3006a7de92daSDan Williams 		return -EIO;
3007a7de92daSDan Williams 	}
3008a7de92daSDan Williams 
3009a7de92daSDan Williams 
3010a7de92daSDan Williams 	/* test ars_cap with benign extended status */
3011abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.ars_cap);
3012abfd4d9cSDan Williams 	cmd.ars_cap = (struct nd_cmd_ars_cap) {
3013a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
3014a7de92daSDan Williams 	};
3015a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
3016abfd4d9cSDan Williams 	rc = setup_result(cmd.buf + offset, cmd_size - offset);
3017a7de92daSDan Williams 	if (rc)
3018a7de92daSDan Williams 		return rc;
3019a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
3020abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3021a7de92daSDan Williams 
3022a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
3023a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3024a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3025a7de92daSDan Williams 		return -EIO;
3026a7de92daSDan Williams 	}
3027a7de92daSDan Williams 
3028a7de92daSDan Williams 
3029a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
3030abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.ars_stat) + sizeof(struct nd_ars_record);
3031abfd4d9cSDan Williams 	cmd.ars_stat = (struct nd_cmd_ars_status) {
3032a7de92daSDan Williams 		.out_length = cmd_size - 4,
3033a7de92daSDan Williams 	};
3034abfd4d9cSDan Williams 	record = &cmd.ars_stat.records[0];
3035a7de92daSDan Williams 	*record = (struct nd_ars_record) {
3036a7de92daSDan Williams 		.length = test_val,
3037a7de92daSDan Williams 	};
3038abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3039a7de92daSDan Williams 	if (rc)
3040a7de92daSDan Williams 		return rc;
3041a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
3042abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3043a7de92daSDan Williams 
3044a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
3045a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3046a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3047a7de92daSDan Williams 		return -EIO;
3048a7de92daSDan Williams 	}
3049a7de92daSDan Williams 
3050a7de92daSDan Williams 
3051a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
3052abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.ars_stat) + sizeof(struct nd_ars_record);
3053abfd4d9cSDan Williams 	cmd.ars_stat = (struct nd_cmd_ars_status) {
3054a7de92daSDan Williams 		.out_length = cmd_size,
3055a7de92daSDan Williams 	};
3056abfd4d9cSDan Williams 	record = &cmd.ars_stat.records[0];
3057a7de92daSDan Williams 	*record = (struct nd_ars_record) {
3058a7de92daSDan Williams 		.length = test_val,
3059a7de92daSDan Williams 	};
3060abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3061a7de92daSDan Williams 	if (rc)
3062a7de92daSDan Williams 		return rc;
3063a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
3064abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3065a7de92daSDan Williams 
3066a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
3067a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3068a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3069a7de92daSDan Williams 		return -EIO;
3070a7de92daSDan Williams 	}
3071a7de92daSDan Williams 
3072a7de92daSDan Williams 
3073a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
3074abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.cfg_size);
3075abfd4d9cSDan Williams 	cmd.cfg_size = (struct nd_cmd_get_config_size) {
3076a7de92daSDan Williams 		.status = 1 << 16,
3077a7de92daSDan Williams 	};
3078abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3079a7de92daSDan Williams 	if (rc)
3080a7de92daSDan Williams 		return rc;
3081a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
3082abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3083a7de92daSDan Williams 
3084a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
3085a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3086a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3087a7de92daSDan Williams 		return -EIO;
3088a7de92daSDan Williams 	}
3089a7de92daSDan Williams 
3090fb2a1748SDan Williams 	/* test clear error */
3091abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.clear_err);
3092abfd4d9cSDan Williams 	cmd.clear_err = (struct nd_cmd_clear_error) {
3093fb2a1748SDan Williams 		.length = 512,
3094fb2a1748SDan Williams 		.cleared = 512,
3095fb2a1748SDan Williams 	};
3096abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3097fb2a1748SDan Williams 	if (rc)
3098fb2a1748SDan Williams 		return rc;
3099fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
3100abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3101fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
3102fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3103fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
3104fb2a1748SDan Williams 		return -EIO;
3105fb2a1748SDan Williams 	}
3106fb2a1748SDan Williams 
3107916566aeSDan Williams 	/* test firmware activate bus info */
3108916566aeSDan Williams 	cmd_size = sizeof(cmd.fwa_info);
3109916566aeSDan Williams 	cmd = (struct nfit_ctl_test_cmd) {
3110916566aeSDan Williams 		.pkg = {
3111916566aeSDan Williams 			.nd_command = NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO,
3112916566aeSDan Williams 			.nd_family = NVDIMM_BUS_FAMILY_INTEL,
3113916566aeSDan Williams 			.nd_size_out = cmd_size,
3114916566aeSDan Williams 			.nd_fw_size = cmd_size,
3115916566aeSDan Williams 		},
3116916566aeSDan Williams 		.fwa_info = {
3117916566aeSDan Williams 			.state = ND_INTEL_FWA_IDLE,
3118916566aeSDan Williams 			.capability = ND_INTEL_BUS_FWA_CAP_FWQUIESCE
3119916566aeSDan Williams 				| ND_INTEL_BUS_FWA_CAP_OSQUIESCE,
3120916566aeSDan Williams 			.activate_tmo = 1,
3121916566aeSDan Williams 			.cpu_quiesce_tmo = 1,
3122916566aeSDan Williams 			.io_quiesce_tmo = 1,
3123916566aeSDan Williams 			.max_quiesce_tmo = 1,
3124916566aeSDan Williams 		},
3125916566aeSDan Williams 	};
3126916566aeSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3127916566aeSDan Williams 	if (rc)
3128916566aeSDan Williams 		return rc;
3129916566aeSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CALL,
3130916566aeSDan Williams 			&cmd, sizeof(cmd.pkg) + cmd_size, &cmd_rc);
3131916566aeSDan Williams 	if (rc < 0 || cmd_rc) {
3132916566aeSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3133916566aeSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3134916566aeSDan Williams 		return -EIO;
3135916566aeSDan Williams 	}
3136916566aeSDan Williams 
3137a7de92daSDan Williams 	return 0;
3138a7de92daSDan Williams }
3139a7de92daSDan Williams 
31406bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
31416bc75619SDan Williams {
31426bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
31436bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
31446bc75619SDan Williams 	struct device *dev = &pdev->dev;
31456bc75619SDan Williams 	struct nfit_test *nfit_test;
3146231bf117SDan Williams 	struct nfit_mem *nfit_mem;
3147c14a868aSDan Williams 	union acpi_object *obj;
31486bc75619SDan Williams 	int rc;
31496bc75619SDan Williams 
3150a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
3151a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
3152a7de92daSDan Williams 		if (rc)
3153a7de92daSDan Williams 			return rc;
3154a7de92daSDan Williams 	}
3155a7de92daSDan Williams 
31566bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
31576bc75619SDan Williams 
31586bc75619SDan Williams 	/* common alloc */
31596bc75619SDan Williams 	if (nfit_test->num_dcr) {
31606bc75619SDan Williams 		int num = nfit_test->num_dcr;
31616bc75619SDan Williams 
31626bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
31636bc75619SDan Williams 				GFP_KERNEL);
31646bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
31656bc75619SDan Williams 				GFP_KERNEL);
31669d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
31679d27a87eSDan Williams 				GFP_KERNEL);
31689d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
31699d27a87eSDan Williams 				GFP_KERNEL);
31706bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
31716bc75619SDan Williams 				GFP_KERNEL);
31726bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
31736bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
31746bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
31756bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
31766bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
31776bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
3178ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
3179ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
3180ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
3181ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
3182ed07c433SDan Williams 				GFP_KERNEL);
3183bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
3184bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
31856bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
31866bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
31879d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
3188bfbaa952SDave Jiang 				&& nfit_test->flush_dma
3189bfbaa952SDave Jiang 				&& nfit_test->fw)
31906bc75619SDan Williams 			/* pass */;
31916bc75619SDan Williams 		else
31926bc75619SDan Williams 			return -ENOMEM;
31936bc75619SDan Williams 	}
31946bc75619SDan Williams 
31956bc75619SDan Williams 	if (nfit_test->num_pm) {
31966bc75619SDan Williams 		int num = nfit_test->num_pm;
31976bc75619SDan Williams 
31986bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
31996bc75619SDan Williams 				GFP_KERNEL);
32006bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
32016bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
32026bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
32036bc75619SDan Williams 			/* pass */;
32046bc75619SDan Williams 		else
32056bc75619SDan Williams 			return -ENOMEM;
32066bc75619SDan Williams 	}
32076bc75619SDan Williams 
32086bc75619SDan Williams 	/* per-nfit specific alloc */
32096bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
32106bc75619SDan Williams 		return -ENOMEM;
32116bc75619SDan Williams 
32126bc75619SDan Williams 	nfit_test->setup(nfit_test);
32136bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
3214a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
32156bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
32166bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
3217a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
3218bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
3219a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
32206bc75619SDan Williams 
3221e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
32221526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
322358cd71b4SDan Williams 	if (rc)
322420985164SVishal Verma 		return rc;
322520985164SVishal Verma 
3226fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
3227fbabd829SDan Williams 	if (rc)
3228fbabd829SDan Williams 		return rc;
3229fbabd829SDan Williams 
323020985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
323120985164SVishal Verma 		return 0;
323220985164SVishal Verma 
323320985164SVishal Verma 	nfit_test->setup_hotplug = 1;
323420985164SVishal Verma 	nfit_test->setup(nfit_test);
323520985164SVishal Verma 
3236c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3237c14a868aSDan Williams 	if (!obj)
3238c14a868aSDan Williams 		return -ENOMEM;
3239c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
3240c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
3241c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
3242c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
3243c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
3244231bf117SDan Williams 
3245231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
3246231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
3247231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
3248231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
3249231bf117SDan Williams 		int i;
3250231bf117SDan Williams 
3251af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
3252231bf117SDan Williams 			if (nfit_handle == handle[i])
3253231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
3254231bf117SDan Williams 						nfit_mem);
3255231bf117SDan Williams 	}
3256231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
32576bc75619SDan Williams 
32586bc75619SDan Williams 	return 0;
32596bc75619SDan Williams }
32606bc75619SDan Williams 
32616bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
32626bc75619SDan Williams {
32636bc75619SDan Williams 	return 0;
32646bc75619SDan Williams }
32656bc75619SDan Williams 
32666bc75619SDan Williams static void nfit_test_release(struct device *dev)
32676bc75619SDan Williams {
32686bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
32696bc75619SDan Williams 
32706bc75619SDan Williams 	kfree(nfit_test);
32716bc75619SDan Williams }
32726bc75619SDan Williams 
32736bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
32746bc75619SDan Williams 	{ KBUILD_MODNAME },
32756bc75619SDan Williams 	{ },
32766bc75619SDan Williams };
32776bc75619SDan Williams 
32786bc75619SDan Williams static struct platform_driver nfit_test_driver = {
32796bc75619SDan Williams 	.probe = nfit_test_probe,
32806bc75619SDan Williams 	.remove = nfit_test_remove,
32816bc75619SDan Williams 	.driver = {
32826bc75619SDan Williams 		.name = KBUILD_MODNAME,
32836bc75619SDan Williams 	},
32846bc75619SDan Williams 	.id_table = nfit_test_id,
32856bc75619SDan Williams };
32866bc75619SDan Williams 
3287*ec6347bbSDan Williams static char copy_mc_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
32885d8beee2SDan Williams 
32895d8beee2SDan Williams enum INJECT {
32905d8beee2SDan Williams 	INJECT_NONE,
32915d8beee2SDan Williams 	INJECT_SRC,
32925d8beee2SDan Williams 	INJECT_DST,
32935d8beee2SDan Williams };
32945d8beee2SDan Williams 
3295*ec6347bbSDan Williams static void copy_mc_test_init(char *dst, char *src, size_t size)
32965d8beee2SDan Williams {
32975d8beee2SDan Williams 	size_t i;
32985d8beee2SDan Williams 
32995d8beee2SDan Williams 	memset(dst, 0xff, size);
33005d8beee2SDan Williams 	for (i = 0; i < size; i++)
33015d8beee2SDan Williams 		src[i] = (char) i;
33025d8beee2SDan Williams }
33035d8beee2SDan Williams 
3304*ec6347bbSDan Williams static bool copy_mc_test_validate(unsigned char *dst, unsigned char *src,
33055d8beee2SDan Williams 		size_t size, unsigned long rem)
33065d8beee2SDan Williams {
33075d8beee2SDan Williams 	size_t i;
33085d8beee2SDan Williams 
33095d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
33105d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
33115d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
33125d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
33135d8beee2SDan Williams 					(unsigned char) i);
33145d8beee2SDan Williams 			return false;
33155d8beee2SDan Williams 		}
33165d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
33175d8beee2SDan Williams 		if (dst[i] != 0xffU) {
33185d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
33195d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
33205d8beee2SDan Williams 			return false;
33215d8beee2SDan Williams 		}
33225d8beee2SDan Williams 	return true;
33235d8beee2SDan Williams }
33245d8beee2SDan Williams 
3325*ec6347bbSDan Williams void copy_mc_test(void)
33265d8beee2SDan Williams {
33275d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
33285d8beee2SDan Williams 	enum INJECT inj;
33295d8beee2SDan Williams 
3330*ec6347bbSDan Williams 	if (IS_ENABLED(CONFIG_COPY_MC_TEST)) {
33315d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
33325d8beee2SDan Williams 	} else {
33335d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
33345d8beee2SDan Williams 		return;
33355d8beee2SDan Williams 	}
33365d8beee2SDan Williams 
33375d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
33385d8beee2SDan Williams 		int i;
33395d8beee2SDan Williams 
33405d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
33415d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
33425d8beee2SDan Williams 			unsigned long expect, rem;
33435d8beee2SDan Williams 			void *src, *dst;
33445d8beee2SDan Williams 			bool valid;
33455d8beee2SDan Williams 
33465d8beee2SDan Williams 			switch (inj) {
33475d8beee2SDan Williams 			case INJECT_NONE:
3348*ec6347bbSDan Williams 				copy_mc_inject_src(NULL);
3349*ec6347bbSDan Williams 				copy_mc_inject_dst(NULL);
3350*ec6347bbSDan Williams 				dst = &copy_mc_buf[2048];
3351*ec6347bbSDan Williams 				src = &copy_mc_buf[1024 - i];
33525d8beee2SDan Williams 				expect = 0;
33535d8beee2SDan Williams 				break;
33545d8beee2SDan Williams 			case INJECT_SRC:
3355*ec6347bbSDan Williams 				copy_mc_inject_src(&copy_mc_buf[1024]);
3356*ec6347bbSDan Williams 				copy_mc_inject_dst(NULL);
3357*ec6347bbSDan Williams 				dst = &copy_mc_buf[2048];
3358*ec6347bbSDan Williams 				src = &copy_mc_buf[1024 - i];
33595d8beee2SDan Williams 				expect = 512 - i;
33605d8beee2SDan Williams 				break;
33615d8beee2SDan Williams 			case INJECT_DST:
3362*ec6347bbSDan Williams 				copy_mc_inject_src(NULL);
3363*ec6347bbSDan Williams 				copy_mc_inject_dst(&copy_mc_buf[2048]);
3364*ec6347bbSDan Williams 				dst = &copy_mc_buf[2048 - i];
3365*ec6347bbSDan Williams 				src = &copy_mc_buf[1024];
33665d8beee2SDan Williams 				expect = 512 - i;
33675d8beee2SDan Williams 				break;
33685d8beee2SDan Williams 			}
33695d8beee2SDan Williams 
3370*ec6347bbSDan Williams 			copy_mc_test_init(dst, src, 512);
3371*ec6347bbSDan Williams 			rem = copy_mc_fragile(dst, src, 512);
3372*ec6347bbSDan Williams 			valid = copy_mc_test_validate(dst, src, 512, expect);
33735d8beee2SDan Williams 			if (rem == expect && valid)
33745d8beee2SDan Williams 				continue;
33755d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
33765d8beee2SDan Williams 					__func__,
33775d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
33785d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
33795d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
33805d8beee2SDan Williams 					expect);
33815d8beee2SDan Williams 		}
33825d8beee2SDan Williams 	}
33835d8beee2SDan Williams 
3384*ec6347bbSDan Williams 	copy_mc_inject_src(NULL);
3385*ec6347bbSDan Williams 	copy_mc_inject_dst(NULL);
33865d8beee2SDan Williams }
33875d8beee2SDan Williams 
33886bc75619SDan Williams static __init int nfit_test_init(void)
33896bc75619SDan Williams {
33906bc75619SDan Williams 	int rc, i;
33916bc75619SDan Williams 
33920fb5c8dfSDan Williams 	pmem_test();
33930fb5c8dfSDan Williams 	libnvdimm_test();
33940fb5c8dfSDan Williams 	acpi_nfit_test();
33950fb5c8dfSDan Williams 	device_dax_test();
3396*ec6347bbSDan Williams 	copy_mc_test();
339792f6f2d7SVishal Verma 	dax_pmem_test();
339892f6f2d7SVishal Verma 	dax_pmem_core_test();
3399c0e71d60SJan Kara #ifdef CONFIG_DEV_DAX_PMEM_COMPAT
340092f6f2d7SVishal Verma 	dax_pmem_compat_test();
3401c0e71d60SJan Kara #endif
34020fb5c8dfSDan Williams 
3403a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3404231bf117SDan Williams 
34059fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
34069fb1a190SDave Jiang 	if (!nfit_wq)
34079fb1a190SDave Jiang 		return -ENOMEM;
34089fb1a190SDave Jiang 
3409a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
3410a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
3411a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
3412a7de92daSDan Williams 		goto err_register;
3413a7de92daSDan Williams 	}
34146bc75619SDan Williams 
3415e3f5df76SDan Williams 	nfit_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
3416e3f5df76SDan Williams 	if (!nfit_pool) {
3417e3f5df76SDan Williams 		rc = -ENOMEM;
3418e3f5df76SDan Williams 		goto err_register;
3419e3f5df76SDan Williams 	}
3420e3f5df76SDan Williams 
3421e3f5df76SDan Williams 	if (gen_pool_add(nfit_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
3422e3f5df76SDan Williams 		rc = -ENOMEM;
3423e3f5df76SDan Williams 		goto err_register;
3424e3f5df76SDan Williams 	}
3425e3f5df76SDan Williams 
34266bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
34276bc75619SDan Williams 		struct nfit_test *nfit_test;
34286bc75619SDan Williams 		struct platform_device *pdev;
34296bc75619SDan Williams 
34306bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
34316bc75619SDan Williams 		if (!nfit_test) {
34326bc75619SDan Williams 			rc = -ENOMEM;
34336bc75619SDan Williams 			goto err_register;
34346bc75619SDan Williams 		}
34356bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
34369fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
34376bc75619SDan Williams 		switch (i) {
34386bc75619SDan Williams 		case 0:
34396bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3440dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
34416bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
34426bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
34436bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
34446bc75619SDan Williams 			break;
34456bc75619SDan Williams 		case 1:
3446a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3447dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3448ac40b675SDan Williams 			nfit_test->num_dcr = 2;
34496bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
34506bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
34516bc75619SDan Williams 			break;
34526bc75619SDan Williams 		default:
34536bc75619SDan Williams 			rc = -EINVAL;
34546bc75619SDan Williams 			goto err_register;
34556bc75619SDan Williams 		}
34566bc75619SDan Williams 		pdev = &nfit_test->pdev;
34576bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
34586bc75619SDan Williams 		pdev->id = i;
34596bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
34606bc75619SDan Williams 		rc = platform_device_register(pdev);
34616bc75619SDan Williams 		if (rc) {
34626bc75619SDan Williams 			put_device(&pdev->dev);
34636bc75619SDan Williams 			goto err_register;
34646bc75619SDan Williams 		}
34658b06b884SDan Williams 		get_device(&pdev->dev);
34666bc75619SDan Williams 
34676bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
34686bc75619SDan Williams 		if (rc)
34696bc75619SDan Williams 			goto err_register;
34706bc75619SDan Williams 
34716bc75619SDan Williams 		instances[i] = nfit_test;
34729fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
34736bc75619SDan Williams 	}
34746bc75619SDan Williams 
34756bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
34766bc75619SDan Williams 	if (rc)
34776bc75619SDan Williams 		goto err_register;
34786bc75619SDan Williams 	return 0;
34796bc75619SDan Williams 
34806bc75619SDan Williams  err_register:
3481e3f5df76SDan Williams 	if (nfit_pool)
3482e3f5df76SDan Williams 		gen_pool_destroy(nfit_pool);
3483e3f5df76SDan Williams 
34849fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
34856bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
34866bc75619SDan Williams 		if (instances[i])
34876bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
34886bc75619SDan Williams 	nfit_test_teardown();
34898b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
34908b06b884SDan Williams 		if (instances[i])
34918b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
34928b06b884SDan Williams 
34936bc75619SDan Williams 	return rc;
34946bc75619SDan Williams }
34956bc75619SDan Williams 
34966bc75619SDan Williams static __exit void nfit_test_exit(void)
34976bc75619SDan Williams {
34986bc75619SDan Williams 	int i;
34996bc75619SDan Williams 
35009fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
35019fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
35026bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
35036bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
35048b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
35056bc75619SDan Williams 	nfit_test_teardown();
35068b06b884SDan Williams 
3507e3f5df76SDan Williams 	gen_pool_destroy(nfit_pool);
3508e3f5df76SDan Williams 
35098b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
35108b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3511231bf117SDan Williams 	class_destroy(nfit_test_dimm);
35126bc75619SDan Williams }
35136bc75619SDan Williams 
35146bc75619SDan Williams module_init(nfit_test_init);
35156bc75619SDan Williams module_exit(nfit_test_exit);
35166bc75619SDan Williams MODULE_LICENSE("GPL v2");
35176bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3518